Created
March 21, 2016 11:13
-
-
Save jenkoian/8f251a75ee55549a4b6f to your computer and use it in GitHub Desktop.
PermissionVoter
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 PermissionVoter implements VoterInterface | |
{ | |
//... | |
/** | |
* {@inheritdoc} | |
*/ | |
public function vote(TokenInterface $token, $object, array $attributes) | |
{ | |
$result = VoterInterface::ACCESS_ABSTAIN; | |
$configuredRoles = $this->getConfigurationRoleMap(); | |
$userRoles = $this->getUserRoles($token); | |
foreach ($attributes as $attribute) { | |
foreach ($configuredRoles as $configuredRole => $permissions) { | |
if (!in_array(‘ROLE_’ . strtoupper($configuredRole), $userRoles)) { | |
continue; | |
} | |
$result = VoterInterface::ACCESS_DENIED; | |
if (is_array($permissions) && in_array($attribute, $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