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($attribute, self::ACTIONS);
  35. }
  36. /**
  37. * Il récupère l'utilisateur à partir du TokenInterface.
  38. * Il vérifie l'action demandée et appelle la méthode correspondante (canSeeConversation).
  39. * En cas d'erreur, il lève une exception logique.
  40. *
  41. * @param string $attribute The attribute to be checked.
  42. * @param mixed $subject The subject to vote on.
  43. * @param TokenInterface $token The security token containing the user credentials.
  44. *
  45. * @return bool True if the user is granted permission for the attribute, false otherwise.
  46. *
  47. * @throws \LogicException If the attribute is not supported by this voter.
  48. */
  49. protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token)
  50. {
  51. $user = $token->getUser();
  52. switch ($attribute) {
  53. case self::CAN_SEE_CONVERSATION:
  54. return $this->canSeeConversation($user, $subject);
  55. default:
  56. throw new \LogicException('This code should not be reached!');
  57. }
  58. }
  59. /**
  60. * Vérifie si l'utilisateur peut acc der une conversation
  61. *
  62. * @param UserInterface|null $user
  63. *
  64. * @return bool
  65. */
  66. private function canSeeConversation(?UserInterface $user): bool
  67. {
  68. if (!$user instanceof UserInterface) {
  69. return false;
  70. }
  71. $dossiers = $this->callBotSM->getConnectedUserDossiers($user);
  72. $correspondence = $this->correspondanceBotCentreRepository->findOneBy(['dossier' => $dossiers[0]['cle']]);
  73. return !empty($correspondence);
  74. }
  75. }