src/Security/Voter/CallBotConversationVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Repository\CorrespondanceBotCentreRepository;
  4. use App\Services\Metier\CallBotConversationHistoriqueSM;
  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 CallBotConversationVoter extends Voter
  9. {
  10.     private CallBotConversationHistoriqueSM $callBotSM;
  11.     private CorrespondanceBotCentreRepository $correspondanceBotCentreRepository;
  12.     const CAN_SEE_CONVERSATION "can_see_conversation";
  13.     const ACTIONS = [
  14.         self::CAN_SEE_CONVERSATION
  15.     ];
  16.     public function __construct(
  17.         CallBotConversationHistoriqueSM $callBotSM
  18.         CorrespondanceBotCentreRepository $correspondanceBotCentreRepository
  19.     )
  20.     {
  21.         $this->callBotSM $callBotSM;
  22.         $this->correspondanceBotCentreRepository $correspondanceBotCentreRepository;
  23.     }
  24.     /**
  25.      * Elle vérifie si l’attribut demandé (can_see_conversation) est géré par ce voter
  26.      *
  27.      * @param string $attribute An attribute
  28.      * @param mixed $subject The subject to vote on
  29.      *
  30.      * @return bool true if this Voter supports the given attribute, false otherwise
  31.      */
  32.     protected function supports(string $attribute$subject): bool
  33.     {
  34.         return in_array($attributeself::ACTIONS);
  35.     }
  36.     
  37.     
  38.     /**
  39.      * Il récupère l'utilisateur à partir du TokenInterface.
  40.      * Il vérifie l'action demandée et appelle la méthode correspondante (canSeeConversation).
  41.      * En cas d'erreur, il lève une exception logique.
  42.      *
  43.      * @param string $attribute The attribute to be checked.
  44.      * @param mixed $subject The subject to vote on.
  45.      * @param TokenInterface $token The security token containing the user credentials.
  46.      *
  47.      * @return bool True if the user is granted permission for the attribute, false otherwise.
  48.      *
  49.      * @throws \LogicException If the attribute is not supported by this voter.
  50.      */
  51.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token)
  52.     {
  53.         $user $token->getUser();
  54.         switch ($attribute) {
  55.             case self::CAN_SEE_CONVERSATION:
  56.                 return $this->canSeeConversation($user$subject);
  57.             default:
  58.                 throw new \LogicException('This code should not be reached!');
  59.         }
  60.     }
  61.     /**
  62.      * Vérifie si l'utilisateur peut acc der  une conversation
  63.      *
  64.      * @param UserInterface|null $user
  65.      *
  66.      * @return bool
  67.      */
  68.     private function canSeeConversation(?UserInterface $user): bool
  69.     {
  70.         if (!$user instanceof UserInterface) {
  71.             return false;
  72.         }
  73.         $dossiers $this->callBotSM->getConnectedUserDossiers($user);
  74.         $correspondence $this->correspondanceBotCentreRepository->findOneBy(['dossier' => $dossiers[0]['cle']]);
  75.         return !empty($correspondence);
  76.     }
  77. }