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