Created
March 21, 2016 11:10
-
-
Save jenkoian/3d7810e8fba57006994b to your computer and use it in GitHub Desktop.
PermissionUrlVoter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//... | |
class PermissionUrlVoter implements VoterInterface | |
{ | |
//... | |
/** | |
* {@inheritdoc} | |
*/ | |
public function vote(TokenInterface $token, $object, array $attributes) | |
{ | |
$result = VoterInterface::ACCESS_ABSTAIN; | |
// only vote on Request objects inside this voter | |
if (!$object instance Request) { | |
return $result; | |
} | |
/** @var Request $request */ | |
$request = $object; | |
$permissionKey = false; | |
foreach ($this->permissionMap as $permission => $urlRegex) { | |
if (preg_match(‘{‘.$urlRegex.’}’, rawurldecode($request->getPathInfo()))) { | |
$permissionKey = $permission; | |
break; | |
} | |
} | |
if ($permissionKey === false) { | |
return $result; | |
} | |
$configuredRoles = $this->getConfigurationRoleMap(); | |
$userRoles = $this->getUserRoles($token); | |
foreach ($configuredRoles as $configuredRole => $permissions) { | |
if (!in_array(‘ROLE_’ . strtoupper($configuredRole), $userRoles)) { | |
continue; | |
} | |
$result = VoterInterface::ACCESS_DENIED; | |
if (is_array($permissions) && in_array($permissionKey, $permissions)) { | |
return VoterInterface::ACCESS_GRANTED; | |
} | |
} | |
return $result; | |
} | |
//... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment