Created
January 31, 2012 05:55
-
-
Save fivestar/1709098 to your computer and use it in GitHub Desktop.
AllRoleVoter
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
// src/Acme/DemoBundle/Security/AllRoleVoter.php | |
<?php | |
namespace Acme\DemoBundle\Security; | |
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | |
use Symfony\Component\Security\Core\Authorization\Voter\RoleVoter as AnyRoleVoter; | |
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface | |
/** | |
* AllRoleVoter | |
* | |
* @author Katsuhiro Ogawa <[email protected]> | |
*/ | |
class AllRoleVoter extends AnyRoleVoter | |
{ | |
/** | |
* {@inheritdoc} | |
*/ | |
public function vote(TokenInterface $token, $object, array $attributes) | |
{ | |
$result = VoterInterface::ACCESS_ABSTAIN; | |
$roles = $this->extractRoles($token); | |
foreach ($attributes as $attribute) { | |
if (!$this->supportsAttribute($attribute)) { | |
continue; | |
} | |
$result = VoterInterface::ACCESS_GRANTED; | |
foreach ($roles as $role) { | |
if ($attribute !== $role->getRole()) { | |
return VoterInterface::ACCESS_DENIED; | |
} | |
} | |
} | |
return $result; | |
} | |
} |
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
# src/Acme/DemoBundle/Resources/config/services.yml | |
parameters: | |
security.access.simple_role_voter.class: Acme\DemoBundle\Security\AllRoleVoter | |
# ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment