RequestListener   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 9
c 1
b 0
f 1
dl 0
loc 20
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 13 6
A __construct() 0 3 1
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