<?php
namespace App\EventSubscriber;
use App\Controller\Back\CustomerController;
use App\Controller\Back\MaintenanceController;
use App\Service\ParameterService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Routing\RouterInterface;
class AccessControlUser implements EventSubscriberInterface
{
private ParameterService $parameterService;
private RouterInterface $routerInterface;
public function __construct(
ParameterService $parameterService,
RouterInterface $routerInterface
) {
$this->parameterService = $parameterService;
$this->routerInterface = $routerInterface;
}
public function onKernelController(ControllerEvent $event)
{
$controller = $event->getController();
if (is_array($controller)) {
$controller = $controller[0];
}
$this->restrictIfSensorModeOnlyOn($controller, $event);
}
public static function getSubscribedEvents()
{
return [
KernelEvents::CONTROLLER => 'onKernelController',
];
}
private function restrictIfSensorModeOnlyOn(
$controller,
ControllerEvent $event
) {
if (
(bool) $this->parameterService->get('sensorModeOnly')
&& (
$controller instanceof MaintenanceController
|| $controller instanceof CustomerController
)
) {
$event->setController(function () {
return new RedirectResponse($this->routerInterface->generate('home'));
});
}
}
}