|
1
|
|
|
<?php |
|
2
|
|
|
namespace App\Security; |
|
3
|
|
|
|
|
4
|
|
|
use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents; |
|
5
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
6
|
|
|
use Symfony\Component\EventDispatcher\GenericEvent; |
|
7
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
|
8
|
|
|
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface; |
|
9
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
|
10
|
|
|
|
|
11
|
|
|
class EasyAdminSecurityEventSubscriber implements EventSubscriberInterface |
|
12
|
|
|
{ |
|
13
|
|
|
private $decisionManager; |
|
14
|
|
|
private $token; |
|
15
|
|
|
|
|
16
|
|
|
public function __construct(AccessDecisionManagerInterface $decisionManager, TokenStorageInterface $token) |
|
17
|
|
|
{ |
|
18
|
|
|
$this->decisionManager = $decisionManager; |
|
19
|
|
|
$this->token = $token; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public static function getSubscribedEvents() |
|
23
|
|
|
{ |
|
24
|
|
|
return [ |
|
25
|
|
|
EasyAdminEvents::PRE_LIST => ['isAuthorized'], |
|
26
|
|
|
EasyAdminEvents::PRE_EDIT => ['isAuthorized'], |
|
27
|
|
|
EasyAdminEvents::PRE_DELETE => ['isAuthorized'], |
|
28
|
|
|
EasyAdminEvents::PRE_NEW => ['isAuthorized'], |
|
29
|
|
|
EasyAdminEvents::PRE_SHOW => ['isAuthorized'], |
|
30
|
|
|
]; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function isAuthorized(GenericEvent $event) |
|
34
|
|
|
{ |
|
35
|
|
|
$entityConfig = $event['entity']; |
|
36
|
|
|
|
|
37
|
|
|
$action = $event->getArgument('request')->query->get('action'); |
|
38
|
|
|
|
|
39
|
|
|
if (!array_key_exists('permissions', $entityConfig) || |
|
40
|
|
|
!array_key_exists($action, $entityConfig['permissions']) |
|
41
|
|
|
) { |
|
42
|
|
|
return; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$authorizedRoles = $entityConfig['permissions']['action']; |
|
46
|
|
|
|
|
47
|
|
|
if (!$this->decisionManager->decide($this->token->getToken(), $authorizedRoles)) { |
|
|
|
|
|
|
48
|
|
|
throw new AccessDeniedException(); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|