src/Permission/Voter/PermissionsVoter.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Permission\Voter;
  3. use App\Entity\User;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
  7. /**
  8.  * Doc: https://symfony.com/doc/5.4/security/voters.html
  9.  *
  10.  * @package App\Permission\Security\Voter
  11.  */
  12. class PermissionsVoter implements VoterInterface
  13. {
  14.     public const SHOW 'SHOW';
  15.     public const EDIT 'EDIT';
  16.     public const DELETE 'DELETE';
  17.     public function __construct(protected AccessDecisionManagerInterface $decisionManager)
  18.     {
  19.     }
  20.     protected function getSupportedAttributes()
  21.     {
  22.         return [self::SHOWself::EDITself::DELETE];
  23.     }
  24.     /**
  25.      * Desc:
  26.      * $attribute => role's permission text. for example: EDIT,SHOW,DELETE
  27.      * $subject => the role that want to check. for example: ROLE_ADMIN,ROLE_SUPER_ADMIN
  28.      *
  29.      *
  30.      * @param $attribute
  31.      * @param $subject
  32.      * @return bool
  33.      */
  34.     protected function supports($attribute$subject)
  35.     {
  36.         return in_array($attribute$this->getSupportedAttributes());
  37.     }
  38.     /**
  39.      * Desc:
  40.      * $attribute => role's permission text. for example: EDIT,SHOW,DELETE
  41.      * $subject => the role that want to check. for example: ROLE_ADMIN,ROLE_SUPER_ADMIN
  42.      *
  43.      * @param $attribute
  44.      * @param $subject
  45.      * @param TokenInterface $token
  46.      * @return bool
  47.      */
  48.     protected function voteOnAttribute($permission$roleTokenInterface $token)
  49.     {
  50.         // Super Admin Pass
  51.         if ($this->decisionManager->decide($token, [User::ROLE_SUPER_ADMIN])) {
  52.             return true;
  53.         }
  54.         /**
  55.          * @var $user User
  56.          */
  57.         $user $token->getUser();
  58.         if (!$user instanceof User) {
  59.             return false;
  60.         }
  61.         $hasPermission false;
  62.         if (is_array($role)) {
  63.             foreach ($role as $roleValue) {
  64.                 if (in_array($roleValue$user->getRoles())) {
  65.                     $hasPermission true;
  66.                 }
  67.             }
  68.         } else {
  69.             if (in_array($role$user->getRoles())) {
  70.                 $hasPermission true;
  71.             }
  72.         }
  73.         return $hasPermission;
  74.     }
  75.     public function vote(TokenInterface $token$subject, array $attributes)
  76.     {
  77.         $vote self::ACCESS_ABSTAIN;
  78.         foreach ($attributes as $attribute) {
  79.             if (!$this->supports($attribute$subject)) {
  80.                 continue;
  81.             }
  82.             $vote self::ACCESS_DENIED;
  83.             if ($this->voteOnAttribute($attribute$subject$token)) {
  84.                 return self::ACCESS_GRANTED;
  85.             }
  86.         }
  87.         return $vote;
  88.     }
  89. }