src/Security/Voter/UtilisateurVoter.php line 12

  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Utilisateur;
  4. use App\Service\UtilisateurService;
  5. use Symfony\Bundle\SecurityBundle\Security;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. use Symfony\Component\Security\Core\User\UserInterface;
  9. class UtilisateurVoter extends Voter
  10. {
  11.     private $security;
  12.     private $utilisateurService;
  13.     public const EDIT 'UTILISATEUR_EDIT';
  14.     public const VIEW 'UTILISATEUR_VIEW';
  15.     public function __construct(Security $securityUtilisateurService $utilisateurService)
  16.     {
  17.         $this->security $security;
  18.         $this->utilisateurService $utilisateurService;
  19.     }
  20.     protected function supports(string $attributemixed $subject): bool
  21.     {
  22.         // replace with your own logic
  23.         // https://symfony.com/doc/current/security/voters.html
  24.         return in_array($attribute, [self::EDITself::VIEW])
  25.             && $subject instanceof \App\Entity\Utilisateur;
  26.     }
  27.     protected function voteOnAttribute(string $attributemixed $utilisateurTokenInterface $token): bool
  28.     {
  29.         /** @var Utilisateur $user */
  30.         $user $token->getUser();
  31.         // if the user is anonymous, do not grant access
  32.         if (!$user instanceof UserInterface) {
  33.             return false;
  34.         }
  35.         // ROLE_SUPERADMIN > ROLE_ADMIN
  36.         if ($this->security->isGranted('ROLE_ADMIN')) {
  37.             return true;
  38.         }
  39.         $employes $this->utilisateurService->getEmployes($user);
  40.         /** @var Utilisateur $utilisateur */
  41.         // ... (check conditions and return true to grant permission) ...
  42.         switch ($attribute) {
  43.             case self::EDIT:
  44.             case self::VIEW:
  45.                 // logic to determine if the user can EDIT or VIEW
  46.                 // return true or false
  47.                 if ($this->security->isGranted('ROLE_GROUPE')) {
  48.                     if ($employes[$utilisateur->getRole()->value]->contains($utilisateur)) {
  49.                         return true;
  50.                     }
  51.                     return false;
  52.                 }
  53.                 if ($this->security->isGranted('ROLE_ORGANISME')) {
  54.                     if ($employes[$utilisateur->getRole()->value]->contains($utilisateur)) {
  55.                         return true;
  56.                     }
  57.                     return false;
  58.                 }
  59.                 if ($this->security->isGranted('ROLE_ETABLISSEMENT')) {
  60.                     if ($employes[$utilisateur->getRole()->value]->contains($utilisateur)) {
  61.                         return true;
  62.                     }
  63.                     return false;
  64.                 }
  65.                 
  66.                 break;
  67.         }
  68.         return false;
  69.     }
  70. }