src/EventSubscriber/MaintenanceSubscriber.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Form\LoginType;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpFoundation\RedirectResponse;
  6. use Symfony\Component\HttpKernel\Event\RequestEvent;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Security\Core\Security;
  9. use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
  10. use Twig\Environment;
  11. use App\Service\SiteConfig;
  12. use Symfony\Component\DependencyInjection\ContainerInterface;
  13. class MaintenanceSubscriber implements EventSubscriberInterface
  14. {
  15.     private bool $isDev;
  16.     public function __construct(
  17.         private SiteConfig $config,
  18.         private Security $security,
  19.         private ContainerBagInterface $params,
  20.         private ContainerInterface $container,
  21.     ) {
  22.         $this->isDev = ($params->get('kernel.environment') == 'dev');
  23.     }
  24.     public function onKernelRequest(RequestEvent $event)
  25.     {
  26.         if (!$event->isMainRequest()) {
  27.             return;
  28.         }
  29.         $request $event->getRequest();
  30.         $isMaintenance $this->config->getMaintenanceMode();
  31.         $ActivedItmConnect $this->config->getActivedItmConnect();
  32.         $ignoredRoutes = [
  33.             '_wdt',
  34.             '_profiler',
  35.             // 'login',
  36.             'logout',
  37.             'itmconnect',
  38.             'callback',
  39.             'admin',
  40.             // 'contact',
  41.             'maintenance',
  42.             'token',
  43.             'authorize',
  44.             'authorize2',
  45.             '.well-known/jwks.json',
  46.             'fr/.well-known/jwks.json',
  47.             'api/oauth2/',
  48.             'before-authorize',
  49.             'api/users/preferences',
  50.             'api/users/categories'
  51.         ];
  52.         function contains(string $haystack, array $needle): bool
  53.         {
  54.             foreach ($needle as $item) {
  55.                 if (str_contains($haystack$item)) {
  56.                     return true;
  57.                 }
  58.             }
  59.             return false;
  60.         }
  61.         if ($this->security->getUser() !== null) {
  62.             return;
  63.         }
  64.         if (contains($request->getPathInfo(), $ignoredRoutes)) {
  65.             return;
  66.         }
  67.         if ($request->cookies->get('maintenance') !== null && !$ActivedItmConnect) {
  68.             return;
  69.         }
  70.         if (isset($_GET['tokenAccess']) && $_GET['tokenAccess'] == 'aconseil') {
  71.             return;
  72.         }
  73.         if ($ActivedItmConnect) {
  74.             $route '/itmconnect';
  75.         } elseif ($isMaintenance) {
  76.             $route '/maintenance';
  77.         }
  78.         //get actual route to redirect after have access maintenance or after login via itmConnect
  79.         $session $request->getSession();
  80.         
  81.         if ($session->get('customReferrer') === null
  82.             $session->set('customReferrer'$request->getUri());
  83.         
  84.         if ($isMaintenance || $ActivedItmConnect) {
  85.             $event->setResponse(new RedirectResponse($route));
  86.         }
  87.     }
  88.     public static function getSubscribedEvents()
  89.     {
  90.         return [
  91.             'kernel.request' => 'onKernelRequest',
  92.         ];
  93.     }
  94. }