src/Security/Voter/ScoreVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\User;
  4. use App\Manager\VoterManager;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. use Symfony\Component\Security\Core\Security;
  8. use Symfony\Component\Security\Core\User\UserInterface;
  9. class ScoreVoter extends Voter
  10. {
  11.     const ACCESS 'SCORE_ACCESS';
  12.     private  $security;
  13.     private  $voterManager;
  14.     public function __construct(Security $security,VoterManager $voterManager)
  15.     {
  16.         $this->security $security;
  17.         $this->voterManager $voterManager;
  18.     }
  19.     /**
  20.      * @param string $attribute
  21.      * @param $subject
  22.      * @return bool
  23.      */
  24.     protected function supports(string $attribute$subject): bool
  25.     {
  26.         if(!in_array($attribute, [self::ACCESS])){
  27.             return false;
  28.         }
  29. /*        if(!$user instanceof User){
  30.             return false;
  31.         }*/
  32.         return true;
  33.     }
  34.     /**
  35.      * @param $attribute
  36.      * @param $subject
  37.      * @param TokenInterface $token
  38.      * @return bool
  39.      */
  40.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  41.     {
  42.         // On récupère l'utilisateur à partir du token
  43.         $currentUser $token->getUser();
  44.         if(!$currentUser instanceof UserInterface) return false;
  45.         // On vérifie si l'utilisateur est admin
  46.         if($this->security->isGranted('ROLE_SUPER_ADMIN')) return true;
  47.         return $this->voterManager->hasRight($currentUser,$attribute);
  48.     }
  49. }