src/Security/Voter/AllowNotifyGestionVoter.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Roles;
  4. use App\Entity\User;
  5. use App\Entity\Dossier;
  6. use App\Entity\StatutDossier;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
  9. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  10. use Symfony\Component\Security\Core\Security;
  11. use Symfony\Component\Security\Core\User\UserInterface;
  12. class AllowNotifyGestionVoter extends Voter
  13. {
  14.     /** @var Security */
  15.     protected $security;
  16.     public function __construct(Security $security)
  17.     {
  18.         $this->security $security;
  19.     }
  20.     protected function supports($attribute$subject): bool
  21.     {
  22.         // replace with your own logic
  23.         // https://symfony.com/doc/current/security/voters.html
  24.         return in_array($attribute, ['CAN_NOTIFY_GESTION'])
  25.             && $subject instanceof Dossier;
  26.     }
  27.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  28.     {
  29.         // On vérifie que l'utilisateur est un des nôtres
  30.        $user $token->getUser();
  31.        if (!$user instanceof User) {
  32.            return false;
  33.        }
  34.        // On vérifie qu'il a les droits 
  35.        if (!($this->security->isGranted(Roles::ROLE_USER) || $this->security->isGranted(Roles::ROLE_OPERATOR)) ) {
  36.            return false;
  37.        }
  38.        
  39.         // On revérifie que l'on a bien affaire à un dossier
  40.         if (!($subject instanceof Dossier)) {
  41.             return false;
  42.         }
  43.         $workflow StatutDossier::STATUTS_WORKFLOW;
  44.         $statutActuel $subject->getStatut();
  45.         
  46.         if (!in_array(StatutDossier::STATUT_GESTION$workflow[$statutActuel])) {
  47.             return false;
  48.         }
  49.         return true;
  50.     }
  51. }