src/Security/Voter/PrelevementVoter.php line 11

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