src/Component/Core/Common/Security/Voter/ConversationOwningVoter.php line 20

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file has been created by developers from BitBag.
  4.  * Feel free to contact us once you face any issues or want to start
  5.  * You can find more information about us on https://bitbag.io and write us
  6.  * an email on hello@bitbag.io.
  7.  */
  8. declare(strict_types=1);
  9. namespace BitBag\OpenMarketplace\Component\Core\Common\Security\Voter;
  10. use BitBag\OpenMarketplace\Component\Messaging\Entity\ConversationInterface;
  11. use Sylius\Component\Core\Model\AdminUserInterface;
  12. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  13. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  14. use Symfony\Component\Security\Core\User\UserInterface;
  15. final class ConversationOwningVoter extends Voter
  16. {
  17.     public const UPDATE 'UPDATE-CONVERSATION';
  18.     protected function supports($attribute$subject)
  19.     {
  20.         if (!in_array($attribute, [self::UPDATE])) {
  21.             return false;
  22.         }
  23.         return true;
  24.     }
  25.     /*
  26.      * This method call is ignored because phpstan force us to type hint arguments but this method in symfony 4.4
  27.      * is declared without so type hinted arguments cause trouble
  28.      */
  29.     /** @phpstan-ignore-next-line */
  30.     protected function voteOnAttribute(
  31.         $attribute,
  32.         $subject,
  33.         TokenInterface $token
  34.     ) {
  35.         $user $token->getUser();
  36.         if (null === $user || null === $subject) {
  37.             return false;
  38.         }
  39.         /** @var ConversationInterface $conversation */
  40.         $conversation $subject;
  41.         switch ($attribute) {
  42.             case self::UPDATE:
  43.                 return $this->doesUserOwnConversation($conversation$user);
  44.             default:
  45.                 return false;
  46.         }
  47.     }
  48.     private function doesUserOwnConversation(ConversationInterface $conversationUserInterface $user): bool
  49.     {
  50.         $conversationUser $conversation->getApplicant();
  51.         if ($user === $conversationUser ||
  52.             $user instanceof AdminUserInterface) {
  53.             return true;
  54.         }
  55.         return false;
  56.     }
  57. }