src/Security/ReunionVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use App\Entity\Reunion;
  4. use App\Entity\User;
  5. use App\Repository\Configuration\AutorisationRepository;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. class ReunionVoter extends Voter
  9. {
  10.     const REUNION_CRUD 'REUNION_CRUD';
  11.     private $autorisationRepository;
  12.     public function __construct(AutorisationRepository $autorisationRepository)
  13.     {
  14.         
  15.         $this->autorisationRepository $autorisationRepository;
  16.     }
  17.     protected function supports(string $attribute$subject): bool
  18.     {
  19.         if (!in_array($attribute, [self::REUNION_CRUD])) {
  20.             return false;
  21.         }
  22.         // if (!$subject instanceof Reunion) {
  23.         //     return false;
  24.         // }
  25.         return true;
  26.     }
  27.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  28.     {
  29.         $user $token->getUser();
  30.         if (!$user instanceof User) {
  31.             return false;
  32.         }
  33.         $autorisation $this->autorisationRepository->findOneBy(['code'=>$attribute]);
  34.         if (!$autorisation) {
  35.             return false;
  36.         }
  37.         
  38.         $roles array_intersect($autorisation->getRoles(),$user->getRoles());
  39.         if(count($roles)) {
  40.             return true;
  41.         }
  42.         return false;
  43.         
  44.         throw new \LogicException('This code should not be reached!');
  45.     }
  46.     
  47. }