vendor/damienharper/auditor-bundle/src/Event/ViewerEventSubscriber.php line 26

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace DH\AuditorBundle\Event;
  4. use DH\Auditor\Auditor;
  5. use DH\Auditor\Configuration as AuditorConfiguration;
  6. use DH\Auditor\Provider\Doctrine\Configuration as DoctrineProviderConfiguration;
  7. use DH\Auditor\Provider\Doctrine\DoctrineProvider;
  8. use DH\AuditorBundle\Controller\ViewerController;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpKernel\Event\KernelEvent;
  11. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  12. use Symfony\Component\HttpKernel\KernelEvents;
  13. class ViewerEventSubscriber implements EventSubscriberInterface
  14. {
  15. private Auditor $auditor;
  16. public function __construct(Auditor $auditor)
  17. {
  18. $this->auditor = $auditor;
  19. }
  20. public function onKernelController(KernelEvent $event): void
  21. {
  22. // Symfony 3.4+ compatibility (no ControllerEvent typehint)
  23. if (!method_exists($event, 'getController')) {
  24. throw new NotFoundHttpException();
  25. }
  26. $controller = $event->getController();
  27. // when a controller class defines multiple action methods, the controller
  28. // is returned as [$controllerInstance, 'methodName']
  29. if (\is_array($controller)) {
  30. $controller = $controller[0];
  31. }
  32. /** @var AuditorConfiguration $auditorConfiguration */
  33. $auditorConfiguration = $this->auditor->getConfiguration();
  34. /** @var DoctrineProviderConfiguration $providerConfiguration */
  35. $providerConfiguration = $this->auditor->getProvider(DoctrineProvider::class)->getConfiguration();
  36. $isAuditorEnabled = $auditorConfiguration->isEnabled();
  37. $isViewerEnabled = $providerConfiguration->isViewerEnabled();
  38. if ($controller instanceof ViewerController && (!$isAuditorEnabled || !$isViewerEnabled)) {
  39. throw new NotFoundHttpException();
  40. }
  41. }
  42. public static function getSubscribedEvents(): array
  43. {
  44. return [
  45. KernelEvents::CONTROLLER => 'onKernelController',
  46. ];
  47. }
  48. }