<?php
namespace App\EventListener\Restrictions;
use Exception;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\Routing\Router;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class MaintenanceListener
{
protected Router $router;
private Security $security;
private TokenStorageInterface $token;
public function __construct(Router $router, Security $security, TokenStorageInterface $token)
{
$this->router = $router;
$this->security = $security;
$this->token = $token;
}
/**
* @throws Exception
*/
public function onKernelRequest(RequestEvent $event)
{
$request = $event->getRequest();
$clientIp = $request->getClientIp();
$allowedIp = [];
//This part just consists of checking if a user is logged in in order to log them out
//By default, the redirection is on the login page because of the firewall configuration
if ($this->security->getUser()) {
if ($_ENV['RESTRICTION_ACCESS'] && $_ENV['CHECK_MAINTENANCE_PAGE_ON']) {
if (isset($_ENV['ALLOWED_IP']) && $_ENV['ALLOWED_IP']) {
$allowedIp = explode(',', $_ENV['ALLOWED_IP']);
}
if (!in_array($clientIp, $allowedIp)) {
$this->token->setToken(null);
}
}
}
}
}