src/EventSubscriber/ChatMessage/ChatMessagePostValidateSubscriber.php line 56

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: SUSAN MEDINA
  5.  * Date: 21/05/2019
  6.  * Time: 02:43 PM
  7.  */
  8. namespace App\EventSubscriber\ChatMessage;
  9. use App\Entity\App\MediaObject;
  10. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\HttpKernel\Event\ViewEvent;
  13. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  14. use ApiPlatform\Core\EventListener\EventPriorities;
  15. use Symfony\Contracts\Translation\TranslatorInterface;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpKernel\KernelEvents;
  18. use App\Exception\InvalidArgumentException;
  19. use App\Exception\NotFoundException;
  20. use App\Exception\AccessDeniedException;
  21. use App\Services\ChatService;
  22. use App\Services\UtilsService;
  23. use App\Entity\Chat\ChatMessage;
  24. use App\Entity\App\User;
  25. class ChatMessagePostValidateSubscriber implements EventSubscriberInterface
  26. {
  27.     private $tokenStorage;
  28.     private $authorizationChecker;
  29.     private $chatService;
  30.     private $utilsService;
  31.     private $translator;
  32.     public function __construct(
  33.         TokenStorageInterface $tokenStorage,
  34.         AuthorizationCheckerInterface $checker,
  35.         ChatService $chatService,
  36.         UtilsService $utilsService,
  37.         TranslatorInterface $translator
  38.     ){
  39.         $this->tokenStorage $tokenStorage;
  40.         $this->authorizationChecker $checker;
  41.         $this->chatService $chatService;
  42.         $this->utilsService $utilsService;
  43.         $this->translator $translator;
  44.     }
  45.     /**
  46.      * @param ViewEvent $event
  47.      * @throws AccessDeniedException
  48.      * @throws InvalidArgumentException
  49.      * @throws NotFoundException
  50.      */
  51.     public function onKernelView(ViewEvent $event)
  52.     {
  53.         if ($this->utilsService->isAPublicRequest($event)) {
  54.             return;
  55.         }
  56.         $chatMessage $event->getControllerResult();
  57.         $request $event->getRequest();
  58.         $method $request->getMethod();
  59.         $userCurrent $this->tokenStorage->getToken()->getUser();
  60.         if (!($chatMessage instanceof ChatMessage) || (Request::METHOD_POST !== $method)
  61.         ) {
  62.             return;
  63.         }
  64.         if (!($userCurrent instanceof User)) {
  65.             throw new NotFoundException($this->translator->trans('User current not found'));
  66.         }
  67.         $content $request->getContent();
  68.         $params json_decode($contenttrue);
  69.         if (isset($params['mediaObjects']) && !empty($params['mediaObjects']) && count($params['mediaObjects']) > 0) {
  70.             foreach ($chatMessage->getMediaObjects() as $mediaObject) {
  71.                 if ($mediaObject->getType() !== MediaObject::TYPE_MESSAGE) {
  72.                     throw new InvalidArgumentException(
  73.                         $this->translator->trans(
  74.                             'mediaObject.validate.type_not_allowed',
  75.                             [
  76.                                 '%mediaId%' => $mediaObject->getId(),
  77.                                 '%mediaType%' => $mediaObject->getType(),
  78.                                 '%mediaTypeAvailable%' => MediaObject::TYPE_MESSAGE
  79.                             ]
  80.                         )
  81.                     );
  82.                 }
  83.             }
  84.             $chatMessage->setType('mediaObjects');
  85.         } else if(!isset($params['body']) || empty(trim($params['body']))) {
  86.             throw new InvalidArgumentException('body: ' $this->translator->trans('This value should not be blank'));
  87.         }
  88.         if ($this->authorizationChecker->isGranted('ROLE_SUPERADMIN')) {
  89.             return;
  90.         }
  91.         $authorization false;
  92.         $userChat $this->chatService->getUsersOfChat($chatMessage->getChat());
  93.         $search array_search($userCurrent->getId(), array_column($userChat'id'));
  94.         if ($search !== false) {
  95.             $authorization true;
  96.         }
  97.         if (!$authorization) {
  98.             throw new AccessDeniedException(
  99.                 $this->translator->trans('You have not permission for write on this chat')
  100.             );
  101.         }
  102.     }
  103.     public static function getSubscribedEvents()
  104.     {
  105.         return [
  106.             KernelEvents::VIEW => ['onKernelView'EventPriorities::POST_VALIDATE]
  107.         ];
  108.     }
  109. }