refactor voter to take into account two differents object type

This commit is contained in:
Julien Fastré 2016-01-26 11:23:38 +01:00
parent 9cb37a6115
commit 0ab2e85aef

View File

@ -57,13 +57,23 @@ class PersonVoter extends AbstractChillVoter implements ProvideRoleInterface
return array('Chill\PersonBundle\Entity\Person', 'Chill\MainBundle\Entity\Center');
}
protected function isGranted($attribute, $person, $user = null)
protected function isGranted($attribute, $object, $user = null)
{
if (!$user instanceof User) {
return false;
}
return $this->helper->userHasAccess($user, $person, $attribute);
if ($attribute === self::STATS and !$object instanceof \Chill\MainBundle\Entity\Center) {
throw new \LogicException("the expected type is \Chill\MainBundle\Entity\Center for "
. "role ".self::STATS." ".get_class($object)." given.");
}
if ($attribute !== self::STATS and !$object instanceof \Chill\PersonBundle\Entity\Person) {
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);
}