src/Security/Voter/ConversationVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Messaging\Conversation;
  4. use App\Entity\User;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. class ConversationVoter extends Voter
  8. {
  9.     public const VIEW 'VIEW';
  10.     /**
  11.      * @param string $attribute
  12.      * @param mixed  $subject
  13.      *
  14.      * @return bool
  15.      */
  16.     protected function supports($attribute$subject)
  17.     {
  18.         if ($attribute !== static::VIEW) {
  19.             return false;
  20.         }
  21.         if (!$subject instanceof Conversation) {
  22.             return false;
  23.         }
  24.         return true;
  25.     }
  26.     /**
  27.      * @param string $attribute
  28.      * @param mixed  $subject
  29.      *
  30.      * @return bool
  31.      */
  32.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  33.     {
  34.         $user $token->getUser();
  35.         if (!$user instanceof User) {
  36.             return false;
  37.         }
  38.         if (static::VIEW === $attribute) {
  39.             return $this->canView($subject$user);
  40.         }
  41.         return false;
  42.     }
  43.     protected function canView(Conversation $conversationUser $user): bool
  44.     {
  45.         $mainUser $user;
  46.         if ($user->isGuest()) {
  47.             $mainUser $user->getMainAccount();
  48.         }
  49.         return $mainUser === $conversation->getUser();
  50.     }
  51. }