src/EventListener/Restrictions/MaintenanceListener.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\EventListener\Restrictions;
  3. use Exception;
  4. use Symfony\Component\HttpKernel\Event\RequestEvent;
  5. use Symfony\Component\Routing\Router;
  6. use Symfony\Component\Security\Core\Security;
  7. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  8. class MaintenanceListener
  9. {
  10.     protected Router $router;
  11.     private Security $security;
  12.     private TokenStorageInterface $token;
  13.     public function __construct(Router $routerSecurity $securityTokenStorageInterface $token)
  14.     {
  15.         $this->router $router;
  16.         $this->security $security;
  17.         $this->token $token;
  18.     }
  19.     /**
  20.      * @throws Exception
  21.      */
  22.     public function onKernelRequest(RequestEvent $event)
  23.     {
  24.         $request $event->getRequest();
  25.         $clientIp $request->getClientIp();
  26.         $allowedIp = [];
  27.         //This part just consists of checking if a user is logged in in order to log them out
  28.         //By default, the redirection is on the login page because of the firewall configuration
  29.         if ($this->security->getUser()) {
  30.             if ($_ENV['RESTRICTION_ACCESS'] && $_ENV['CHECK_MAINTENANCE_PAGE_ON']) {
  31.                 if (isset($_ENV['ALLOWED_IP']) && $_ENV['ALLOWED_IP']) {
  32.                     $allowedIp explode(','$_ENV['ALLOWED_IP']);
  33.                 }
  34.                 if (!in_array($clientIp$allowedIp)) {
  35.                     $this->token->setToken(null);
  36.                 }
  37.             }
  38.         }
  39.     }
  40. }