<?php
namespace App\EventListener;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Security;
class CrmAccessListener
{
public function __construct(private readonly UrlGeneratorInterface $urlGenerator, private readonly Security $security)
{
}
public function onKernelRequest(RequestEvent $event): void
{
$request = $event->getRequest();
$pathInfo = $request->getPathInfo();
if (str_starts_with($pathInfo, '/crm')) {
$user = $this->security->getUser();
if ($user && (!$user->getCrm() || !$user->getCrm()["access"])) {
$redirectUrl = $this->urlGenerator->generate('home');
$event->setResponse(new RedirectResponse($redirectUrl));
}
}
}
}