src/AppBundle/Security/Authorization/Voter/PlanVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace AppBundle\Security\Authorization\Voter;
  3. use AppBundle\Entity\Participant;
  4. use AppBundle\Entity\Plan;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. use Symfony\Component\Security\Core\User\UserInterface;
  8. class PlanVoter extends Voter
  9. {
  10.     const VIEW 'VIEW';
  11.     /**
  12.      * @inheritdoc
  13.      */
  14.     protected function supports($attribute$subject): bool
  15.     {
  16.         if ($subject instanceof Plan) {
  17.             return in_array($attribute, array(self::VIEW), true);
  18.         }
  19.         return false;
  20.     }
  21.     /**
  22.      * @inheritdoc
  23.      */
  24.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  25.     {
  26.         // make sure there is a user object (i.e. that the user is logged in)
  27.         $user $token->getUser();
  28.         if (!$user instanceof UserInterface) {
  29.             return false;
  30.         }
  31.         $employer $subject->getEmployer();
  32.         switch($attribute) {
  33.             case self::VIEW:
  34.                 // Participants and employers can view a plan that belongs to the employer. Supervisors can view all plans.
  35.                 return $user === $employer || ($user instanceof Participant && $user->getEmployer() === $employer) || in_array('ROLE_SUPERVISOR'$user->getRoles(), true);
  36.         }
  37.         return false;
  38.     }
  39. }