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

Open in your IDE?
  1. <?php
  2. namespace AppBundle\Security\Authorization\Voter;
  3. use AppBundle\Entity\Claim;
  4. use AppBundle\Entity\RecurringClaim;
  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 ClaimVoter extends Voter
  9. {
  10.     const ATTEST 'ATTEST';
  11.     const CANCEL 'CANCEL';
  12.     const VIEW 'VIEW';
  13.     const EDIT 'EDIT';
  14.     const PROCESS 'PROCESS';
  15.     /**
  16.      * @inheritdoc
  17.      */
  18.     protected function supports($attribute$subject): bool
  19.     {
  20.         if ($subject instanceof Claim || $subject instanceof RecurringClaim) {
  21.             return in_array($attribute, array(self::ATTESTself::CANCELself::VIEWself::EDITself::PROCESS), true);
  22.         }
  23.         return false;
  24.     }
  25.     /**
  26.      * @inheritdoc
  27.      */
  28.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  29.     {
  30.         // make sure there is a user object (i.e. that the user is logged in)
  31.         $user $token->getUser();
  32.         if (!$user instanceof UserInterface) {
  33.             return false;
  34.         }
  35.         switch($attribute) {
  36.             case self::CANCEL// Supervisors should be rejecting via the processing side, so only participants have permission to cancel
  37.                 return $user === $subject->getParticipant();
  38.             case SELF::ATTEST// Participants are allowed to attest to the validity of their claims and supervisors can attest on any participant's behalf
  39.             case self::VIEW// Participants can view their own claims or supervisors can load claims for any participant
  40.             case self::EDIT// Participants can edit their own claims or supervisors can edit claims for any participant
  41.                 return $user === $subject->getParticipant() || in_array('ROLE_SUPERVISOR'$user->getRoles(), true);
  42.             case SELF::PROCESS:
  43.                 // Supervisors can process any claims
  44.                 return in_array('ROLE_SUPERVISOR'$user->getRoles(), true);
  45.         }
  46.         return false;
  47.     }
  48. }