RequestListener::__invoke()   A
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 6
eloc 8
c 1
b 0
f 1
nc 5
nop 1
dl 0
loc 13
rs 9.2222
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LAG\AdminBundle\EventListener\Security;
6
7
use LAG\AdminBundle\Metadata\AdminResource;
8
use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
9
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
10
use Symfony\Component\Security\Core\Security;
11
12
class RequestListener
13
{
14
    public function __construct(
15
        private Security $security,
16
    ) {
17
    }
18
19
    public function __invoke(ControllerArgumentsEvent $event): void
20
    {
21
        $user = $this->security->getUser();
22
23
        foreach ($event->getArguments() as $argument) {
24
            if (!$argument instanceof AdminResource) {
25
                continue;
26
            }
27
            $permissions = $argument->getCurrentOperation()->getPermissions();
28
29
            foreach ($permissions as $permission) {
30
                if ($user === null || !$this->security->isGranted($permission, $user)) {
31
                    throw new AccessDeniedException();
32
                }
33
            }
34
        }
35
    }
36
}
37