src/Security/Voter/ChatVoter.php line 23

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Security\Voter;
  4. use App\Entity\Chat;
  5. use App\Entity\Team;
  6. use App\Entity\User;
  7. use App\Repository\TeamUserRepository;
  8. use App\Service\Moderation\BlockedUsers;
  9. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  10. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  11. use Symfony\Component\Security\Core\Security;
  12. /**
  13.  * CHAT_VIEW : lire un chat et ses messages.
  14.  * Autorisé à l'admin, aux participants du chat (chat privé), aux membres de la team (chat d'équipe, type global)
  15.  * et aux clients de l'entreprise (chat Général, type main).
  16.  * Un chat privé avec un utilisateur qu'on a bloqué est refusé à celui qui a bloqué.
  17.  * Même règle que App\Doctrine\ChatAccessExtension, qui l'applique aux listes.
  18.  */
  19. class ChatVoter extends Voter
  20. {
  21.     public const VIEW = 'CHAT_VIEW';
  22.     private Security $security;
  23.     private TeamUserRepository $teamUserRepository;
  24.     private BlockedUsers $blockedUsers;
  25.     public function __construct(Security $security, TeamUserRepository $teamUserRepository, BlockedUsers $blockedUsers)
  26.     {
  27.         $this->security = $security;
  28.         $this->teamUserRepository = $teamUserRepository;
  29.         $this->blockedUsers = $blockedUsers;
  30.     }
  31.     protected function supports(string $attribute, $subject): bool
  32.     {
  33.         return self::VIEW === $attribute && $subject instanceof Chat;
  34.     }
  35.     /**
  36.      * @param Chat $subject
  37.      */
  38.     protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
  39.     {
  40.         if ($this->security->isGranted(User::ROLE_ADMIN)) {
  41.             return true;
  42.         }
  43.         $user = $token->getUser();
  44.         if (!$user instanceof User) {
  45.             return false;
  46.         }
  47.         // Chat privé : participant, sauf si l'autre participant est un utilisateur qu'il a bloqué
  48.         if (!$subject->getUsers()->isEmpty()) {
  49.             $memberIds = array_map(static fn (User $member): ?int => $member->getId(), $subject->getUsers()->toArray());
  50.             if (!in_array($user->getId(), $memberIds, true)) {
  51.                 return false;
  52.             }
  53.             return [] === array_intersect($memberIds, $this->blockedUsers->blockedBy($user));
  54.         }
  55.         if (Chat::TYPE_MAIN === $subject->getType()) {
  56.             $company = $user->getClient() ? $user->getClient()->getCompany() : null;
  57.             return null !== $company && null !== $subject->getCompany() && $subject->getCompany()->getId() === $company->getId();
  58.         }
  59.         if (Chat::TYPE_GLOBAL === $subject->getType() && $subject->getTeam() instanceof Team) {
  60.             return null !== $this->teamUserRepository->findOneBy(['team' => $subject->getTeam(), 'user' => $user]);
  61.         }
  62.         return false;
  63.     }
  64. }