src/Security/Voter/FrequentationVoter.php line 12

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