src/Security/Mission/OrdreMissionRegionalVoter.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Security\Mission;
  3. use App\Entity\Mission\OrdreMission;
  4. use App\Entity\User;
  5. use Symfony\Component\Security\Core\Security;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. /**
  8. * Vérifie qu'un utilisateur peut valider/refuser un ordre de mission au nom
  9. * du responsable régional. Remplace les boucles hasRole('ROLE_REG_SEG') etc.
  10. * dispersées dans l'ancien contrôleur.
  11. */
  12. class OrdreMissionRegionalVoter extends Voter
  13. {
  14. public const VALIDER_REGIONAL = 'MISSION_VALIDER_REGIONAL';
  15. public function __construct(
  16. private readonly Security $security,
  17. ) {
  18. }
  19. protected function supports(string $attribute, mixed $subject): bool
  20. {
  21. return $attribute === self::VALIDER_REGIONAL && $subject instanceof OrdreMission;
  22. }
  23. protected function voteOnAttribute(string $attribute, mixed $subject, \Symfony\Component\Security\Core\Authentication\Token\TokenInterface $token): bool
  24. {
  25. /** @var OrdreMission $ordre */
  26. $ordre = $subject;
  27. $role = $ordre->getRegion()->roleResponsableRegional();
  28. // Bamako : pas d'étape régionale, personne ne "valide" à ce niveau.
  29. if ($role === null) {
  30. return false;
  31. }
  32. return $this->security->isGranted($role) || $this->security->isGranted('ROLE_ADMIN');
  33. }
  34. }