|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LAG\AdminBundle\Event\Subscriber; |
|
4
|
|
|
|
|
5
|
|
|
use LAG\AdminBundle\Configuration\ApplicationConfiguration; |
|
6
|
|
|
use LAG\AdminBundle\Configuration\ApplicationConfigurationStorage; |
|
7
|
|
|
use LAG\AdminBundle\Event\Events; |
|
8
|
|
|
use LAG\AdminBundle\Event\Events\AdminEvent; |
|
9
|
|
|
use LAG\AdminBundle\Exception\Exception; |
|
10
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
11
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
|
12
|
|
|
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
|
13
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
|
14
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
|
15
|
|
|
|
|
16
|
|
|
class SecuritySubscriber implements EventSubscriberInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var TokenStorageInterface |
|
20
|
|
|
*/ |
|
21
|
|
|
private $tokenStorage; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var AuthorizationCheckerInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
private $authorizationChecker; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var ApplicationConfiguration |
|
30
|
|
|
*/ |
|
31
|
|
|
private $applicationConfiguration; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* SecuritySubscriber constructor. |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct( |
|
37
|
4 |
|
ApplicationConfigurationStorage $applicationConfigurationStorage, |
|
38
|
|
|
TokenStorageInterface $tokenStorage, |
|
39
|
|
|
AuthorizationCheckerInterface $authorizationChecker |
|
40
|
|
|
) { |
|
41
|
|
|
$this->tokenStorage = $tokenStorage; |
|
42
|
4 |
|
$this->authorizationChecker = $authorizationChecker; |
|
43
|
4 |
|
$this->applicationConfiguration = $applicationConfigurationStorage->getConfiguration(); |
|
44
|
4 |
|
} |
|
45
|
4 |
|
|
|
46
|
|
|
public static function getSubscribedEvents(): array |
|
47
|
4 |
|
{ |
|
48
|
|
|
return [ |
|
49
|
|
|
Events::ADMIN_HANDLE_REQUEST => 'handleRequest', |
|
50
|
4 |
|
]; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @throws AccessDeniedException |
|
55
|
|
|
*/ |
|
56
|
|
|
public function handleRequest(AdminEvent $event): void |
|
57
|
4 |
|
{ |
|
58
|
|
|
if (!$this->applicationConfiguration->getParameter('enable_security')) { |
|
59
|
4 |
|
return; |
|
60
|
2 |
|
} |
|
61
|
|
|
$user = $this->getUser(); |
|
62
|
2 |
|
$expectedRoles = $event->getAdmin()->getConfiguration()->getPermissions(); |
|
63
|
|
|
|
|
64
|
2 |
|
foreach ($expectedRoles as $role) { |
|
65
|
|
|
if (!$this->authorizationChecker->isGranted($role, $user)) { |
|
66
|
|
|
throw new AccessDeniedException(sprintf('The user with roles "%s" is not granted. Allowed roles are "%s"', implode('", "', $user->getRoles()), implode('", "', $expectedRoles))); |
|
67
|
2 |
|
} |
|
68
|
2 |
|
} |
|
69
|
|
|
} |
|
70
|
2 |
|
|
|
71
|
2 |
|
/** |
|
72
|
|
|
* @throws Exception |
|
73
|
|
|
*/ |
|
74
|
|
|
private function getUser(): UserInterface |
|
75
|
2 |
|
{ |
|
76
|
2 |
|
$token = $this->tokenStorage->getToken(); |
|
77
|
|
|
|
|
78
|
|
|
if (null === $token) { |
|
79
|
2 |
|
throw new Exception('The security token is not defined'); |
|
80
|
|
|
} |
|
81
|
|
|
$user = $token->getUser(); |
|
82
|
2 |
|
|
|
83
|
|
|
if (!$user instanceof UserInterface) { |
|
84
|
|
|
throw new Exception('The security user is not defined'); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $user; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|