src/EventListener/RouteRequestListener.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\PrivateUser;
  4. use App\Entity\ProUser;
  5. use App\Entity\User;
  6. use Symfony\Component\HttpFoundation\RedirectResponse;
  7. use Symfony\Component\HttpKernel\Event\RequestEvent;
  8. use Symfony\Component\Routing\RouterInterface;
  9. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  10. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  11. class RouteRequestListener
  12. {
  13.     public function __construct(private readonly TokenStorageInterface $tokenStorage, private readonly AuthorizationCheckerInterface $authorizationChecker, private readonly RouterInterface $router)
  14.     {
  15.     }
  16.     public function onKernelRequest(RequestEvent $event)
  17.     {
  18.         $request $event->getRequest();
  19.         $user $this->getUser();
  20.         if (
  21.             $event->isMainRequest()
  22.             && 'frontend_cgv_validation' !== $request->get('_route')
  23.             && 'fos_user_profile_edit' !== $request->get('_route')
  24.             && $user instanceof ProUser
  25.             && !$this->isGranted('ROLE_ACCESS'$user)
  26.             && null === $user->getCgvValidatedAt()
  27.             && !$this->authorizationChecker->isGranted('ROLE_API_RMS')
  28.         ) {
  29.             $response = new RedirectResponse($this->router->generate('frontend_cgv_validation'));
  30.             $event->setResponse($response);
  31.             return $response;
  32.         }
  33.         if (
  34.             $user instanceof User
  35.             && $user->isGranted('ROLE_STRANGER_SALESPERSON')
  36.             && 'backend_index' === $request->get('_route')
  37.         ) {
  38.             $response = new RedirectResponse($this->router->generate('backend_sales_list'));
  39.             $event->setResponse($response);
  40.         }
  41.         if (!in_array($request->get('_route'), ['homepage''homepage_pro'])) {
  42.             return;
  43.         }
  44.         if (
  45.             $user instanceof ProUser
  46.             && 'homepage' === $request->get('_route')
  47.         ) {
  48.             $response = new RedirectResponse($this->router->generate('homepage_pro'));
  49.             $event->setResponse($response);
  50.         }
  51.         if (
  52.             $user instanceof PrivateUser
  53.             && 'homepage_pro' === $request->get('_route')
  54.         ) {
  55.             $response = new RedirectResponse($this->router->generate('homepage'));
  56.             $event->setResponse($response);
  57.         }
  58.     }
  59.     protected function isGranted($attributes$object)
  60.     {
  61.         return $this->authorizationChecker->isGranted($attributes$object);
  62.     }
  63.     protected function getUser()
  64.     {
  65.         if (null !== $this->tokenStorage->getToken()) {
  66.             return $this->tokenStorage->getToken()->getUser();
  67.         }
  68.         return null;
  69.     }
  70. }