src/Core/EventSubscriber/ContactContextSubscriber.php line 65

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * Copyright (c) 2022 TECLA Consulting Group oü.
  5.  * All rights reserved.
  6.  *
  7.  * This unpublished material is proprietary to TECLA Consulting Group oü.
  8.  * All rights reserved. The methods and
  9.  * techniques described herein are considered trade secrets
  10.  * and/or confidential. Reproduction or distribution, in whole
  11.  * or in part, is forbidden except by express written permission
  12.  * of TECLA Consulting Group oü.
  13.  *
  14.  * @author    Matúš Sýkorjak <matus@tecla.no>
  15.  * @copyright 2022 TECLA Consulting Group oü
  16.  */
  17. namespace App\Core\EventSubscriber;
  18. use App\Contact\Model\Identity\ContactId;
  19. use App\Contact\Repository\ContactRepositoryInterface;
  20. use App\Contact\Security\ContactContextInterface;
  21. use App\Contact\Service\ContactAccessTrackingServiceInterface;
  22. use JetBrains\PhpStorm\ArrayShape;
  23. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  24. use Symfony\Component\HttpFoundation\RedirectResponse;
  25. use Symfony\Component\HttpKernel\Event\RequestEvent;
  26. use Symfony\Component\HttpKernel\KernelEvents;
  27. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  28. use Throwable;
  29. final class ContactContextSubscriber implements EventSubscriberInterface
  30. {
  31.     private UrlGeneratorInterface $urlGenerator;
  32.     private ContactContextInterface $contactContext;
  33.     private ContactRepositoryInterface $contactRepository;
  34.     private ContactAccessTrackingServiceInterface $accessTrackingService;
  35.     public function __construct(
  36.         UrlGeneratorInterface $urlGenerator,
  37.         ContactContextInterface $contactContext,
  38.         ContactRepositoryInterface $contactRepository,
  39.         ContactAccessTrackingServiceInterface $accessTrackingService
  40.     ) {
  41.         $this->urlGenerator $urlGenerator;
  42.         $this->contactContext $contactContext;
  43.         $this->contactRepository $contactRepository;
  44.         $this->accessTrackingService $accessTrackingService;
  45.     }
  46.     #[ArrayShape([KernelEvents::REQUEST => 'string'])]
  47.     public static function getSubscribedEvents(): array
  48.     {
  49.         return [
  50.             KernelEvents::REQUEST => [
  51.                 ['onKernelRequest'5],
  52.             ],
  53.         ];
  54.     }
  55.     public function onKernelRequest(RequestEvent $event): void
  56.     {
  57.         if (false === $event->isMainRequest()) {
  58.             return;
  59.         }
  60.         $request $event->getRequest();
  61.         $routeName $request->get('_route');
  62.         if ('_' === $routeName[0]) {
  63.             return;
  64.         }
  65.         $cid $request->query->get('cid');
  66.         if (null === $cid) {
  67.             return;
  68.         }
  69.         try {
  70.             $contactId ContactId::createFromString($request->query->get('cid'));
  71.             $currentContext $this->contactRepository->find($contactId);
  72.         } catch (Throwable) {
  73.             $currentContext null;
  74.         }
  75.         if (null === $currentContext) {
  76.             return;
  77.         }
  78.         if (false === $this->contactContext->isAccessible($currentContext)) {
  79.             $request->getSession()->getFlashBag()->add('error''Act as selected Contact is forbidden!');
  80.             $response = new RedirectResponse($this->urlGenerator->generate('app_homepage', ['cid' => $this->contactContext->getBase()?->getId()]));
  81.             $event->setResponse($response);
  82.         }
  83.         $this->contactContext->setCurrent($currentContext);
  84.         if ($this->contactContext->getBase() !== $this->contactContext->getCurrent() && false === $request->isXmlHttpRequest()) {
  85.             $this->accessTrackingService->track(
  86.                 $this->contactContext->getCurrent(),
  87.                 $this->contactContext->getBase()
  88.             );
  89.         }
  90.     }
  91. }