src/EventSubscriber/AccessControlUser.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Controller\Back\CustomerController;
  4. use App\Controller\Back\MaintenanceController;
  5. use App\Service\ParameterService;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpFoundation\RedirectResponse;
  8. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. use Symfony\Component\Routing\RouterInterface;
  11. class AccessControlUser implements EventSubscriberInterface
  12. {
  13. private ParameterService $parameterService;
  14. private RouterInterface $routerInterface;
  15. public function __construct(
  16. ParameterService $parameterService,
  17. RouterInterface $routerInterface
  18. ) {
  19. $this->parameterService = $parameterService;
  20. $this->routerInterface = $routerInterface;
  21. }
  22. public function onKernelController(ControllerEvent $event)
  23. {
  24. $controller = $event->getController();
  25. if (is_array($controller)) {
  26. $controller = $controller[0];
  27. }
  28. $this->restrictIfSensorModeOnlyOn($controller, $event);
  29. }
  30. public static function getSubscribedEvents()
  31. {
  32. return [
  33. KernelEvents::CONTROLLER => 'onKernelController',
  34. ];
  35. }
  36. private function restrictIfSensorModeOnlyOn(
  37. $controller,
  38. ControllerEvent $event
  39. ) {
  40. if (
  41. (bool) $this->parameterService->get('sensorModeOnly')
  42. && (
  43. $controller instanceof MaintenanceController
  44. || $controller instanceof CustomerController
  45. )
  46. ) {
  47. $event->setController(function () {
  48. return new RedirectResponse($this->routerInterface->generate('home'));
  49. });
  50. }
  51. }
  52. }