src/Security/Voter/Messenger/ConversationVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter\Messenger;
  3. use App\Entity\Conversation;
  4. use App\Security\ApiUser;
  5. use App\Service\User\MemberOperatorService;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. class ConversationVoter extends Voter
  9. {
  10.     public const PERMISSION_READ 'read';
  11.     public const PERMISSION_WRITE 'write';
  12.     public const PERMISSION_DELETE 'delete';
  13.     protected function supports(string $attribute$subject): bool
  14.     {
  15.         if (!$subject instanceof Conversation) {
  16.             return false;
  17.         }
  18.         return in_array($attribute, [
  19.             self::PERMISSION_READ,
  20.             self::PERMISSION_WRITE,
  21.             self::PERMISSION_DELETE,
  22.         ], true);
  23.     }
  24.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  25.     {
  26.         $user $token->getUser();
  27.         if (!$user instanceof ApiUser) {
  28.             return false;
  29.         }
  30.         /*
  31.          * @var $subject Conversation
  32.          */
  33.         if (self::PERMISSION_DELETE == $attribute) {
  34.             return $this->canDelete($subject$user);
  35.         }
  36.         if (self::PERMISSION_READ === $attribute) {
  37.             return $this->canRead($subject$user);
  38.         }
  39.         if (self::PERMISSION_WRITE === $attribute) {
  40.             return $this->canWrite($subject$user);
  41.         }
  42.         return false;
  43.     }
  44.     protected function canRead(Conversation $subjectApiUser $user): bool
  45.     {
  46.         if ($user->getIsOperator()) {
  47.             foreach ($subject->getRoutes() as $route) {
  48.                 if (MemberOperatorService::isMemberOperatedByUser($route->getFromMember(), $user)) {
  49.                     return true;
  50.                 }
  51.             }
  52.         }
  53.         if ($user->getIsAdmin()) {
  54.             return true;
  55.         }
  56.         return $this->isParticipant($subject$user);
  57.     }
  58.     protected function canWrite(Conversation $subjectApiUser $user): bool
  59.     {
  60.         if ($user->getIsOperator()) {
  61.             return true;
  62.         }
  63.         // admins may not write
  64.         return $this->isParticipant($subject$user);
  65.     }
  66.     protected function canDelete(Conversation $subjectApiUser $user): bool
  67.     {
  68.         if ($user->getIsAdmin()) {
  69.             return true;
  70.         }
  71.         return $this->isParticipant($subject$user);
  72.     }
  73.     protected function isParticipant(Conversation $subjectApiUser $user): bool
  74.     {
  75.         foreach ($subject->getRoutes() as $route) {
  76.             if ($route->getFromMember()->getId() == $user->getMember()->getId()) {
  77.                 return true;
  78.             }
  79.         }
  80.         return false;
  81.     }
  82. }