src/Core/EventSubscriber/ContactContextSubscriber.php line 65
<?phpdeclare(strict_types=1);/*** Copyright (c) 2022 TECLA Consulting Group oü.* All rights reserved.** This unpublished material is proprietary to TECLA Consulting Group oü.* All rights reserved. The methods and* techniques described herein are considered trade secrets* and/or confidential. Reproduction or distribution, in whole* or in part, is forbidden except by express written permission* of TECLA Consulting Group oü.** @author Matúš Sýkorjak <matus@tecla.no>* @copyright 2022 TECLA Consulting Group oü*/namespace App\Core\EventSubscriber;use App\Contact\Model\Identity\ContactId;use App\Contact\Repository\ContactRepositoryInterface;use App\Contact\Security\ContactContextInterface;use App\Contact\Service\ContactAccessTrackingServiceInterface;use JetBrains\PhpStorm\ArrayShape;use Symfony\Component\EventDispatcher\EventSubscriberInterface;use Symfony\Component\HttpFoundation\RedirectResponse;use Symfony\Component\HttpKernel\Event\RequestEvent;use Symfony\Component\HttpKernel\KernelEvents;use Symfony\Component\Routing\Generator\UrlGeneratorInterface;use Throwable;final class ContactContextSubscriber implements EventSubscriberInterface{private UrlGeneratorInterface $urlGenerator;private ContactContextInterface $contactContext;private ContactRepositoryInterface $contactRepository;private ContactAccessTrackingServiceInterface $accessTrackingService;public function __construct(UrlGeneratorInterface $urlGenerator,ContactContextInterface $contactContext,ContactRepositoryInterface $contactRepository,ContactAccessTrackingServiceInterface $accessTrackingService) {$this->urlGenerator = $urlGenerator;$this->contactContext = $contactContext;$this->contactRepository = $contactRepository;$this->accessTrackingService = $accessTrackingService;}#[ArrayShape([KernelEvents::REQUEST => 'string'])]public static function getSubscribedEvents(): array{return [KernelEvents::REQUEST => [['onKernelRequest', 5],],];}public function onKernelRequest(RequestEvent $event): void{if (false === $event->isMainRequest()) {return;}$request = $event->getRequest();$routeName = $request->get('_route');if ('_' === $routeName[0]) {return;}$cid = $request->query->get('cid');if (null === $cid) {return;}try {$contactId = ContactId::createFromString($request->query->get('cid'));$currentContext = $this->contactRepository->find($contactId);} catch (Throwable) {$currentContext = null;}if (null === $currentContext) {return;}if (false === $this->contactContext->isAccessible($currentContext)) {$request->getSession()->getFlashBag()->add('error', 'Act as selected Contact is forbidden!');$response = new RedirectResponse($this->urlGenerator->generate('app_homepage', ['cid' => $this->contactContext->getBase()?->getId()]));$event->setResponse($response);}$this->contactContext->setCurrent($currentContext);if ($this->contactContext->getBase() !== $this->contactContext->getCurrent() && false === $request->isXmlHttpRequest()) {$this->accessTrackingService->track($this->contactContext->getCurrent(),$this->contactContext->getBase());}}}