src/AppBundle/Security/Authorization/Voter/ParticipantVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace AppBundle\Security\Authorization\Voter;
  3. use AppBundle\Entity\Participant;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. use Symfony\Component\Security\Core\User\UserInterface;
  7. class ParticipantVoter extends Voter
  8. {
  9.     const SUBMIT_CLAIM 'SUBMIT_CLAIM';
  10.     const VIEW 'VIEW';
  11.     const VIEW_CLAIM 'VIEW_CLAIM';
  12.     /**
  13.      * @inheritdoc
  14.      */
  15.     protected function supports($attribute$subject): bool 
  16.     {
  17.         if ($subject instanceof Participant) {
  18.             return in_array($attribute, array(self::SUBMIT_CLAIMself::VIEWself::VIEW_CLAIM), true);
  19.         }
  20.         return false;
  21.     }
  22.     /**
  23.      * @inheritdoc
  24.      */
  25.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  26.     {
  27.         // make sure there is a user object (i.e. that the user is logged in)
  28.         $user $token->getUser();
  29.         if (!$user instanceof UserInterface) {
  30.             return false;
  31.         }
  32.         switch($attribute) {
  33.             case self::SUBMIT_CLAIM:
  34.                 // Participants can submit claims for themselves or supervisors can submit claims for any participant
  35.                 return $user === $subject || in_array('ROLE_SUPERVISOR'$user->getRoles(), true);
  36.             case self::VIEW:
  37.                 // Participants can view themselves; employers can view their participants; supervisors can view anyone
  38.                 // TODO: address assistant accounts
  39.                 return $user === $subject || $user === $subject->getEmployer() || in_array('ROLE_SUPERVISOR'$user->getRoles(), true);
  40.             case self::VIEW_CLAIM:
  41.                 // Participants can view their own claims or supervisors can load claims for any participant
  42.                 return $user === $subject || in_array('ROLE_SUPERVISOR'$user->getRoles(), true);
  43.         }
  44.         return false;
  45.     }
  46. }