src/Security/Voter/TranscriptOfRecordsVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\TranscriptOfRecords;
  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 TranscriptOfRecordsVoter extends Voter
  11. {
  12.     public const VIEW 'TOR_VIEW';
  13.     public const CREATE 'TOR_CREATE';
  14.     public const MODIFY 'TOR_MODIFY';
  15.     private Security $security;
  16.     private StaffDirectory $staffDirectory;
  17.     public function __construct(
  18.         Security $security,
  19.         StaffDirectory $staffDirectory,
  20.     ) {
  21.         $this->security $security;
  22.         $this->staffDirectory $staffDirectory;
  23.     }
  24.     protected function supports(string $attributemixed $subject): bool
  25.     {
  26.         return in_array($attribute, [
  27.                 self::VIEW,
  28.                 self::CREATE,
  29.                 self::MODIFY,
  30.             ])
  31.             && $subject instanceof TranscriptOfRecords
  32.         ;
  33.     }
  34.     protected function voteOnAttribute(string $attributemixed $subjectTokenInterface $token): bool
  35.     {
  36.         /**
  37.          * @var User $user
  38.          */
  39.         $user $token->getUser();
  40.         // if the user is anonymous, do not grant access
  41.         if (!$user instanceof UserInterface) {
  42.             return false;
  43.         }
  44.         if ($this->security->isGranted('ROLE_SUPER_ADMIN')) {
  45.             return true;
  46.         }
  47.         $student $user->getStudent();
  48.         $isCorrectStudent null !== $student && $subject->getStudent() === $student;
  49.         switch ($attribute) {
  50.             case self::VIEW:
  51.                 return
  52.                     $this->staffDirectory->isStaffAtSendingHei($user$subject)
  53.                     || $this->staffDirectory->isStaffAtReceivingHei($user$subject)
  54.                     || (
  55.                         $isCorrectStudent
  56.                     );
  57.             case self::CREATE:
  58.             case self::MODIFY:
  59.                 return $this->staffDirectory->isStaffAtReceivingHei($user$subject);
  60.         }
  61.         return false;
  62.     }
  63. }