src/Security/ProcessusVoter.php line 11

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