src/Security/Voter/StudentVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use Symfony\Component\Security\Core\Security;
  4. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  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 StudentVoter extends Voter
  9. {
  10.     private $security;
  11.     public function __constructSecurity $security)
  12.     {
  13.         $this->security $security;
  14.       
  15.     }
  16.     protected function supports(string $attribute$subject): bool
  17.     {
  18.         if(!$this->security->getUser())
  19.             return false;
  20.         if(in_array("ROLE_STUDENT"$this->security->getUser()->getRoles()))
  21.             return true;
  22.         return false;
  23.     }
  24.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  25.     {
  26.      
  27.      
  28.         $user $token->getUser();
  29.   
  30.         if (!$user instanceof UserInterface) {
  31.             return false;
  32.         }
  33.         if (!(in_array("ROLE_STUDENT"$user->getRoles()))) {
  34.             return false;
  35.         }
  36.     
  37.         return true;
  38.     }
  39. }