src/Security/Voter/FormationVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Formation;
  4. use App\Repository\FormationRegistrationRepository;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. use Symfony\Component\Security\Core\Security;
  8. use Symfony\Component\Security\Core\User\UserInterface;
  9. class FormationVoter extends Voter
  10. {
  11.     public const FORMATION_SHOW 'FORMATION_SHOW';
  12.     private $security;
  13.     private FormationRegistrationRepository $formationRepository;
  14.     public function __construct(Security $securityFormationRegistrationRepository $formationRepository)
  15.     {
  16.         $this->security $security;
  17.         $this->formationRepository $formationRepository;
  18.     }
  19.     protected function supports(string $attribute$subject): bool
  20.     {
  21.         return in_array($attribute, [self::FORMATION_SHOW])
  22.             && ( $subject instanceof Formation);
  23.     }
  24.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  25.     {
  26.         //utilisateur connecté
  27.         $user $token->getUser();
  28.         // if the user is anonymous, do not grant access
  29.         if (!$user instanceof UserInterface) {
  30.             return false;
  31.         }
  32.         switch ($attribute) {
  33.             case self::FORMATION_SHOW:
  34.                 return $this->canUserShowFormation($subject$user);
  35.         }
  36.         return false;
  37.     }
  38.     /**
  39.      * @param Formation $formation
  40.      * @param UserInterface $user - user connecté
  41.      * @return bool
  42.      */
  43.     private function canUserShowFormation(Formation $formationUserInterface $user): bool
  44.     {
  45.         //si admin ok
  46.         if($this->security->isGranted('ROLE_ADMIN') && $formation->getOwner() === $user){
  47.             return true;
  48.         }
  49.         //si rh
  50.         if ($this->security->isGranted('ROLE_RH')){
  51. //            $f = $this->formationRepository->belongsToUser($user,$formation->getId());
  52.             //soit je suis propriétaire
  53.             if($formation->getOwner() === $user){
  54.                 return true ;
  55.             }
  56.             $f $this->formationRepository->userIsRegistered($user,$formation->getId());
  57.             //soit je suis inscrit et la formation est public
  58.             if( ($f && !empty($f)) &&  $formation->isPublished()){
  59.                 return true;
  60.             }
  61.         }
  62.         //si user
  63.         if ($this->security->isGranted('ROLE_USER_ACTIF') || $this->security->isGranted('ROLE_USER_INACTIF')) {
  64.             $f $this->formationRepository->userIsRegistered($user,$formation->getId());
  65.             if( ($f && !empty($f)) &&  $formation->isPublished()){
  66.                 return true;
  67.             }
  68.         }
  69.         return false;
  70.     }
  71. }