Skip to content

Instantly share code, notes, and snippets.

@fivestar
Created January 31, 2012 05:55
Show Gist options
  • Save fivestar/1709098 to your computer and use it in GitHub Desktop.
Save fivestar/1709098 to your computer and use it in GitHub Desktop.
AllRoleVoter
// 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;
}
}
# 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