src/Security/Voter/ExpenseVoter.php line 14

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