Skip to content

Instantly share code, notes, and snippets.

@jenkoian
Created March 21, 2016 11:13
Show Gist options
  • Save jenkoian/8f251a75ee55549a4b6f to your computer and use it in GitHub Desktop.
Save jenkoian/8f251a75ee55549a4b6f to your computer and use it in GitHub Desktop.
PermissionVoter
<?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