src/Security/FacultyVoter.php line 10

Open in your IDE?
  1. <?php
  2.     namespace App\Security;
  3.     use App\Entity\FacultyInterface;
  4.     use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5.     use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  6.     use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7.     class FacultyVoter extends Voter
  8.     {
  9.         private const VIEW 'VIEW';
  10.         private AuthorizationCheckerInterface $authorizationChecker;
  11.         public function __construct(AuthorizationCheckerInterface $authorizationChecker)
  12.         {
  13.             $this->authorizationChecker $authorizationChecker;
  14.         }
  15.         protected function supports(string $attributemixed $subject): bool
  16.         {
  17.             return $subject instanceof FacultyInterface &&
  18.                 $attribute === self::VIEW;
  19.         }
  20.         /**
  21.          * @param string           $attribute
  22.          * @param FacultyInterface $subject
  23.          * @param TokenInterface   $token
  24.          *
  25.          * @return bool
  26.          */
  27.         protected function voteOnAttribute(string $attributemixed $subjectTokenInterface $token): bool
  28.         {
  29.             $subjectsFaculty $subject?->getFacultyIdentifier();
  30.             return $this->authorizationChecker->isGranted("ROLE_FACULTY_".$subjectsFaculty);
  31.         }
  32.     }