vendor/crea/security-bundle/src/Voter/RightVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace Crea\SecurityBundle\Voter;
  3. use Crea\SecurityBundle\Entity\Right;
  4. use Crea\SecurityBundle\Entity\User;
  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 RightVoter extends Voter
  9. {
  10.     const RIGHT_LIST "SECURITY_RIGHT_LIST";
  11.     /**
  12.      * @inheritDoc
  13.      */
  14.     protected function supports($attribute$subject): bool
  15.     {
  16.         return $attribute == self::RIGHT_LIST && ($subject === null || $subject instanceof Right);
  17.     }
  18.     /**
  19.      * @inheritDoc
  20.      */
  21.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  22.     {
  23.         /** @var User $loggedUser */
  24.         $loggedUser $token->getUser();
  25.         if (!$loggedUser instanceof UserInterface)
  26.             return false;
  27.         if ($attribute == self::RIGHT_LIST) {
  28.             return $this->voteOnList($loggedUser);
  29.         }
  30.         return false;
  31.     }
  32.     private function voteOnList(UserInterface $loggedUser): bool
  33.     {
  34.         if (in_array(self::RIGHT_LIST$loggedUser->getRoles())) {
  35.             return true;
  36.         }
  37.         return false;
  38.     }
  39. }