Skip to content

Instantly share code, notes, and snippets.

@yoye
Created February 1, 2012 09:48
Show Gist options
  • Save yoye/1716201 to your computer and use it in GitHub Desktop.
Save yoye/1716201 to your computer and use it in GitHub Desktop.
Utilisation de Voter dans Symfony2
<?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