src/EventListener/CrmAccessListener.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use Symfony\Component\HttpFoundation\RedirectResponse;
  4. use Symfony\Component\HttpKernel\Event\RequestEvent;
  5. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  6. use Symfony\Component\Security\Core\Security;
  7. class CrmAccessListener
  8. {
  9.     public function __construct(private readonly UrlGeneratorInterface $urlGenerator, private readonly Security $security)
  10.     {
  11.     }
  12.     public function onKernelRequest(RequestEvent $event): void
  13.     {
  14.         $request $event->getRequest();
  15.         $pathInfo $request->getPathInfo();
  16.         if (str_starts_with($pathInfo'/crm')) {
  17.             $user $this->security->getUser();
  18.             if ($user && (!$user->getCrm() || !$user->getCrm()["access"])) {
  19.                 $redirectUrl $this->urlGenerator->generate('home');
  20.                 $event->setResponse(new RedirectResponse($redirectUrl));
  21.             }
  22.         }
  23.     }
  24. }