Commit b042a7ae authored by Julien Fastré's avatar Julien Fastré

rewrite PersonVoter for sf3

parent 19de34b4
Pipeline #45973 passed with stages
in 6 minutes and 2 seconds
......@@ -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);
}
protected function getSupportedClasses()
{
return array('Chill\PersonBundle\Entity\Person', 'Chill\MainBundle\Entity\Center');
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 isGranted($attribute, $object, $user = null)
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
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()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment