src/EventSubscriber/Chat/ChatPreSerializeSubscriber.php line 57

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: SUSAN MEDINA
  5.  * Date: 21/05/2019
  6.  * Time: 02:15 PM
  7.  */
  8. namespace App\EventSubscriber\Chat;
  9. use App\Services\UtilsService;
  10. use Symfony\Component\HttpFoundation\RequestStack;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpKernel\Event\ViewEvent;
  15. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  16. use ApiPlatform\Core\EventListener\EventPriorities;
  17. use Symfony\Contracts\Translation\TranslatorInterface;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\HttpKernel\KernelEvents;
  20. use App\Exception\NotFoundException;
  21. use App\Services\ChatService;
  22. use App\Entity\Chat\Chat;
  23. use App\Entity\App\User;
  24. class ChatPreSerializeSubscriber implements EventSubscriberInterface
  25. {
  26.     private $tokenStorage;
  27.     private $authorizationChecker;
  28.     private $chatService;
  29.     private $utilsService;
  30.     private $translator;
  31.     private $request;
  32.     public function __construct(
  33.         TokenStorageInterface $tokenStorage,
  34.         AuthorizationCheckerInterface $checker,
  35.         ChatService $chatService,
  36.         UtilsService $utilsService,
  37.         TranslatorInterface $translator,
  38.         RequestStack $requestStack
  39. ) {
  40.         $this->tokenStorage $tokenStorage;
  41.         $this->authorizationChecker $checker;
  42.         $this->chatService $chatService;
  43.         $this->utilsService $utilsService;
  44.         $this->translator $translator;
  45.         $this->request $requestStack->getCurrentRequest();
  46.     }
  47.     /**
  48.      * @param ViewEvent $event
  49.      * @return Response|void
  50.      * @throws NotFoundException
  51.      */
  52.     public function onKernelView(ViewEvent $event)
  53.     {
  54.         if ($this->utilsService->isAPublicRequest($event)) {
  55.             return;
  56.         }
  57.         $chat $event->getControllerResult();
  58.         $request $event->getRequest();
  59.         $method $request->getMethod();
  60.         if (!($chat instanceof Chat) || (Request::METHOD_GET !== $method)) {
  61.             return;
  62.         }
  63.         $userCurrent $this->tokenStorage->getToken()->getUser();
  64.         if (!($userCurrent instanceof User)) {
  65.             $response = new Response();
  66.             $response->setContent(json_encode([
  67.                 'detail' => $this->translator->trans(
  68.                     'User current not found',
  69.                     [],
  70.                     null,
  71.                     $this->request->getLocale())
  72.             ]));
  73.             $response->setStatusCode(Response::HTTP_NOT_ACCEPTABLE);
  74.             return $response;
  75.         }
  76.         if ($this->authorizationChecker->isGranted('ROLE_SUPERADMIN')) {
  77.             return;
  78.         }
  79.         $authorization false;
  80.         $userChat $this->chatService->getUsersOfChat($chat);
  81.         $search array_search($userCurrent->getId(), array_column($userChat'id'));
  82.         if ($search !== false){
  83.             $authorization true;
  84.         }
  85.         if (!$authorization) {
  86.             $response = new Response();
  87.             $response->setContent(json_encode([
  88.                 'detail' => $this->translator->trans(
  89.                     'You have not permission for write on this chat',
  90.                     [],
  91.                     null,
  92.                     $this->request->getLocale())
  93.             ]));
  94.             $response->setStatusCode(Response::HTTP_NOT_ACCEPTABLE);
  95.             return $response;
  96.         }
  97.     }
  98.     public static function getSubscribedEvents()
  99.     {
  100.         return [
  101.             KernelEvents::VIEW => ['onKernelView'EventPriorities::PRE_SERIALIZE]
  102.         ];
  103.     }
  104. }