src/Security/Voter/LearningAgreementVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\LearningAgreement;
  4. use App\Entity\User;
  5. use App\HeiAdministration\StaffDirectory;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. use Symfony\Component\Security\Core\Security;
  9. use Symfony\Component\Security\Core\User\UserInterface;
  10. class LearningAgreementVoter extends Voter
  11. {
  12.     public const VIEW 'LA_VIEW';
  13.     public const ADMIN 'LA_ADMIN';
  14.     public const SENDING_ACCEPT 'LA_SENDING_ACCEPT';
  15.     public const SENDING_REJECT 'LA_SENDING_REJECT';
  16.     public const RECEIVING_ACCEPT 'LA_RECEIVING_ACCEPT';
  17.     public const RECEIVING_REJECT 'LA_RECEIVING_REJECT';
  18.     public const STUDENT_SUBMIT 'LA_STUDENT_SUBMIT';
  19.     private Security $security;
  20.     private StaffDirectory $staffDirectory;
  21.     public function __construct(
  22.         Security $security,
  23.         StaffDirectory $staffDirectory,
  24.     ) {
  25.         $this->security $security;
  26.         $this->staffDirectory $staffDirectory;
  27.     }
  28.     protected function supports(string $attributemixed $subject): bool
  29.     {
  30.         return in_array($attribute, [
  31.                 self::VIEW,
  32.                 self::ADMIN,
  33.                 self::SENDING_ACCEPT,
  34.                 self::SENDING_REJECT,
  35.                 self::RECEIVING_ACCEPT,
  36.                 self::RECEIVING_REJECT,
  37.                 self::STUDENT_SUBMIT,
  38.             ])
  39.             && $subject instanceof LearningAgreement
  40.         ;
  41.     }
  42.     protected function voteOnAttribute(string $attributemixed $subjectTokenInterface $token): bool
  43.     {
  44.         /**
  45.          * @var User $user
  46.          */
  47.         $user $token->getUser();
  48.         // if the user is anonymous, do not grant access
  49.         if (!$user instanceof UserInterface) {
  50.             return false;
  51.         }
  52.         if ($this->security->isGranted('ROLE_SUPER_ADMIN')) {
  53.             return true;
  54.         }
  55.         $student $user->getStudent();
  56.         $isCorrectStudent null !== $student && $subject->getStudent() === $student;
  57.         switch ($attribute) {
  58.             case self::VIEW:
  59.                 return
  60.                     $this->staffDirectory->isStaffAtSendingHei($user$subject)
  61.                     || $this->staffDirectory->isStaffAtReceivingHei($user$subject)
  62.                     || (
  63.                         $isCorrectStudent
  64.                     );
  65.             case self::ADMIN:
  66.             case self::STUDENT_SUBMIT:
  67.                 return $isCorrectStudent;
  68.             case self::SENDING_ACCEPT:
  69.             case self::SENDING_REJECT:
  70.                 return $this->staffDirectory->isStaffAtSendingHei($user$subject);
  71.             case self::RECEIVING_ACCEPT:
  72.             case self::RECEIVING_REJECT:
  73.                 return $this->staffDirectory->isStaffAtReceivingHei($user$subject);
  74.         }
  75.         return false;
  76.     }
  77. }