Deprecated: Constant E_STRICT is deprecated in /var/www/PixelForce/vendor/symfony/error-handler/ErrorHandler.php on line 58

Deprecated: Constant E_STRICT is deprecated in /var/www/PixelForce/vendor/symfony/error-handler/ErrorHandler.php on line 76
Symfony Profiler

src/Security/Voters/AgentFormationFicheVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voters;
  3. use App\Entity\Formation;
  4. use App\Entity\User;
  5. use App\Repository\FormationAgentRepository;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. class AgentFormationFicheVoter extends Voter
  9. {
  10.     const AGENT_FICHE 'agent_fiche';
  11.     /**
  12.      * @var FormationAgentRepository
  13.      */
  14.     private $formationAgentRepository;
  15.     public function __construct(FormationAgentRepository $formationAgentRepository)
  16.     {
  17.         $this->formationAgentRepository $formationAgentRepository;
  18.     }
  19.     /**
  20.      * Determines if the attribute and subject are supported by this voter.
  21.      *
  22.      * @param string $attribute An attribute
  23.      * @param mixed $subject The subject to secure, e.g. an object the user wants to access or any other PHP type
  24.      *
  25.      * @return bool
  26.      */
  27.     protected function supports(string $attribute$subject)
  28.     {
  29.         // if the attribute isn't one we support, return false
  30.         if (!in_array($attribute, [self::AGENT_FICHE])) {
  31.             return false;
  32.         }
  33.         // only vote on `Post` objects
  34.         if (!$subject instanceof Formation) {
  35.             return false;
  36.         }
  37.         return true;
  38.     }
  39.     /**
  40.      * Perform a single access check operation on a given attribute, subject and token.
  41.      * It is safe to assume that $attribute and $subject already passed the "supports()" method check.
  42.      *
  43.      * @param mixed $subject
  44.      *
  45.      * @return bool
  46.      */
  47.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token)
  48.     {
  49.         $user $token->getUser();
  50.         if (!$user instanceof User) {
  51.             // the user must be logged in; if not, deny access
  52.             return false;
  53.         }
  54.         /** @var Formation $formation */
  55.         $formation $subject;
  56.         $formationAgentRelation $this->formationAgentRepository->findOneBy(['formation' => $formation'agent' => $user]);
  57.         $statut Formation::STATUT_BLOQUEE;
  58.         if($formationAgentRelation) {
  59.             $statut $formationAgentRelation->getStatut();
  60.         }
  61.         switch ($attribute) {
  62.             case self::AGENT_FICHE:
  63.                 return $statut === Formation::STATUT_DISPONIBLE || $statut === Formation::STATUT_TERMINER;
  64.         }
  65.         throw new \LogicException('Non autorisé la formation n\'est pas disponible');
  66.     }
  67. }