src/Mm/Escmid/EaaBundle/EventListener/AuthListener.php line 29

Open in your IDE?
  1. <?php
  2. namespace Mm\Escmid\EaaBundle\EventListener;
  3. use Mm\Escmid\EaaBundle\Controller\ApiControllerInterface;
  4. use Mm\Escmid\EaaBundle\Controller\AuthenticatedControllerInterface;
  5. use Mm\Escmid\EaaBundle\Controller\LegacyController;
  6. use Mm\Escmid\EaaBundle\Platform\Security\AccessDeniedHttpException;
  7. use Mm\Escmid\EaaBundle\Platform\Security\AuthenticationException;
  8. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  9. use Symfony\Bundle\FrameworkBundle\Controller\RedirectController;
  10. use Symfony\Bundle\WebProfilerBundle\Controller\ExceptionPanelController;
  11. use Symfony\Bundle\WebProfilerBundle\Controller\ProfilerController;
  12. use Symfony\Bundle\WebProfilerBundle\Controller\RouterController;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  15. class AuthListener implements EventSubscriberInterface
  16. {
  17. private $controllersWhitelist = [];
  18. /**
  19. * AuthListener constructor.
  20. */
  21. public function __construct()
  22. {
  23. }
  24. public function onKernelController(ControllerEvent $event): void
  25. {
  26. /** @var Controller[] */
  27. $controllers = $event->getController();
  28. /*
  29. * $controller passed can be either a class or a Closure. This is not usual in Symfony but it may happen.
  30. * If it is a class, it comes in array format.
  31. */
  32. if (!is_array($controllers)) {
  33. return;
  34. }
  35. if ($controllers[0] instanceof AuthenticatedControllerInterface) {
  36. $controllers[0]->setRequest($event->getRequest());
  37. $this->checkController($controllers[0]);
  38. } elseif (($controllers[0] instanceof ProfilerController)
  39. || ($controllers[0] instanceof RouterController)
  40. ) {
  41. /*
  42. * allow access for Symfony WebProfilerBundle controllers
  43. */
  44. } elseif ($controllers[0] instanceof ApiControllerInterface) {
  45. // allow access to API
  46. } elseif ($controllers[0] instanceof RedirectController) {
  47. // allow redirects
  48. } elseif ($controllers[0] instanceof ExceptionPanelController) {
  49. // allow handling exceptions
  50. } else {
  51. $unknown = true;
  52. foreach ($this->controllersWhitelist as $className) {
  53. if ($controllers[0] instanceof $className) {
  54. $unknown = false;
  55. break;
  56. }
  57. }
  58. if ($unknown) {
  59. throw new \Exception("Invalid controller '".$controllers[0]::class."', must be instance of either one of these:
  60. \n* Mm\\Escmid\\EaaBundle\\Controller\\AuthenticatedControllerInterface\n
  61. * Symfony\\Bundle\\WebProfilerBundle\\Controller\\ProfilerController\n
  62. * Symfony\\Bundle\\WebProfilerBundle\\Controller\\RouterController\n
  63. * Mm\\Escmid\\EaaBundle\\Controller\\ApiControllerInterface\n
  64. * Symfony\\Bundle\\FrameworkBundle\\Controller\\RedirectController\n
  65. * Symfony\\Bundle\\WebProfilerBundle\\Controller\\ExceptionPanelController\n".'* '.implode("\n* ", $this->controllersWhitelist));
  66. }
  67. }
  68. }
  69. public function checkController(AuthenticatedControllerInterface $controller): void
  70. {
  71. $principal = $controller->getPrincipal();
  72. $controller->refreshLastSessionActivity();
  73. if ($controller->requiresLogin() || LegacyController::class === $controller::class) {
  74. if (null == $principal || !$principal->isLoggedIn()) {
  75. throw new AuthenticationException('Not logged in.');
  76. }
  77. }
  78. if (!$controller->checkAreaPermissions()) {
  79. $path = $controller->getCurrentPath();
  80. throw new AccessDeniedHttpException(sprintf('Area Access denied for path %s.', $path));
  81. }
  82. if (!$controller->checkPermissions()) {
  83. throw new AccessDeniedHttpException('Access denied.');
  84. }
  85. }
  86. /**
  87. * @return array<string, mixed>
  88. */
  89. public static function getSubscribedEvents(): array
  90. {
  91. return [\Symfony\Component\HttpKernel\KernelEvents::CONTROLLER => 'onKernelController'];
  92. }
  93. }