src/Security/ActionVoter.php line 11

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