rewrite PersonVoter for sf3

This commit is contained in:
Julien Fastré 2018-04-04 21:51:16 +02:00
parent 19de34b4db
commit b042a7ae55

View File

@ -23,6 +23,9 @@ use Chill\MainBundle\Security\Authorization\AbstractChillVoter;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
use Chill\MainBundle\Security\ProvideRoleHierarchyInterface;
use Chill\PersonBundle\Entity\Person;
use Chill\MainBundle\Entity\Center;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
/**
*
@ -48,36 +51,28 @@ class PersonVoter extends AbstractChillVoter implements ProvideRoleHierarchyInte
$this->helper = $helper;
}
protected function getSupportedAttributes()
protected function supports($attribute, $subject)
{
return array(self::CREATE, self::UPDATE, self::SEE, self::STATS, self::LISTS);
if ($subject instanceof Person) {
return \in_array($attribute, [
self::CREATE, self::UPDATE, self::SEE
]);
} elseif ($subject instanceof Center) {
return \in_array($attribute, [
self::STATS, self::LISTS
]);
} else {
return false;
}
}
protected function getSupportedClasses()
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
return array('Chill\PersonBundle\Entity\Person', 'Chill\MainBundle\Entity\Center');
}
protected function isGranted($attribute, $object, $user = null)
{
if (!$user instanceof User) {
if (!$token->getUser() instanceof User) {
return false;
}
if ($object instanceof \Chill\MainBundle\Entity\Center) {
if (! \in_array($attribute, [ self::STATS, self::LISTS ])) {
throw new \LogicException("the expected type is \Chill\MainBundle\Entity\Center for "
. "role, '".$attribute." ".get_class($object)."' given.");
}
} elseif ($object instanceof \Chill\PersonBundle\Entity\Person) {
if (\in_array($attribute, [ self::STATS, self::LISTS ])){
throw new \LogicException("the expected type is \Chill\PersonBundle\Entity\Person for "
. "role, '".$attribute." ".get_class($object)."' given.");
}
}
return $this->helper->userHasAccess($user, $object, $attribute);
return $this->helper->userHasAccess($token->getUser(), $subject, $attribute);
}
public function getRoles()