Created
February 1, 2012 09:48
-
-
Save yoye/1716201 to your computer and use it in GitHub Desktop.
Utilisation de Voter dans Symfony2
This file contains hidden or 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 | |
namespace Acme\Bundle\Security\Authorization\Voter; | |
use Acme\Bundle\Entity\Foobar; | |
use Symfony\Component\DependencyInjection\ContainerInterface; | |
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface; | |
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | |
class OwnerVoter implements VoterInterface | |
{ | |
public function supportsAttribute($attribute) | |
{ | |
return 'OWNER' === $attribute; | |
} | |
public function supportsClass($class) | |
{ | |
return $class instanceof Foobar; | |
} | |
function vote(TokenInterface $token, $object, array $attributes) | |
{ | |
foreach ($attributes as $attribute) { | |
if ($this->supportsAttribute($attribute) && $this->supportsClass($object)) { | |
$user = $token->getUser(); | |
// Tournament Owner | |
if ($object instanceof Foobar && $user->equals($object->getUser())) { | |
return VoterInterface::ACCESS_GRANTED; | |
} | |
} | |
} | |
return VoterInterface::ACCESS_DENIED; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment