|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Sonata Project package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Sonata\AdminBundle\Security\Handler; |
|
15
|
|
|
|
|
16
|
|
|
use Sonata\AdminBundle\Admin\AdminInterface; |
|
17
|
|
|
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
|
18
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @final since sonata-project/admin-bundle 3.52 |
|
22
|
|
|
* |
|
23
|
|
|
* @author Thomas Rabaix <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class RoleSecurityHandler implements SecurityHandlerInterface |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var AuthorizationCheckerInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $authorizationChecker; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $superAdminRoles; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param AuthorizationCheckerInterface $authorizationChecker |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct($authorizationChecker, array $superAdminRoles) |
|
41
|
|
|
{ |
|
42
|
|
|
// NEXT_MAJOR: Move AuthorizationCheckerInterface check to method signature |
|
43
|
|
|
if (!$authorizationChecker instanceof AuthorizationCheckerInterface) { |
|
44
|
|
|
throw new \InvalidArgumentException('Argument 1 should be an instance of Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface'); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$this->authorizationChecker = $authorizationChecker; |
|
48
|
|
|
$this->superAdminRoles = $superAdminRoles; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function isGranted(AdminInterface $admin, $attributes, $object = null) |
|
52
|
|
|
{ |
|
53
|
|
|
if (!\is_array($attributes)) { |
|
54
|
|
|
$attributes = [$attributes]; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
foreach ($attributes as $pos => $attribute) { |
|
58
|
|
|
$attributes[$pos] = sprintf($this->getBaseRole($admin), $attribute); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$allRole = sprintf($this->getBaseRole($admin), 'ALL'); |
|
62
|
|
|
|
|
63
|
|
|
try { |
|
64
|
|
|
return $this->isAnyGranted($this->superAdminRoles) |
|
65
|
|
|
|| $this->isAnyGranted($attributes, $object) |
|
66
|
|
|
|| $this->isAnyGranted([$allRole], $object); |
|
67
|
|
|
} catch (AuthenticationCredentialsNotFoundException $e) { |
|
68
|
|
|
return false; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function getBaseRole(AdminInterface $admin) |
|
73
|
|
|
{ |
|
74
|
|
|
return 'ROLE_'.str_replace('.', '_', strtoupper($admin->getCode())).'_%s'; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function buildSecurityInformation(AdminInterface $admin) |
|
78
|
|
|
{ |
|
79
|
|
|
return []; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function createObjectSecurity(AdminInterface $admin, $object) |
|
83
|
|
|
{ |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function deleteObjectSecurity(AdminInterface $admin, $object) |
|
87
|
|
|
{ |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
private function isAnyGranted(array $attributes, $subject = null): bool |
|
91
|
|
|
{ |
|
92
|
|
|
foreach ($attributes as $attribute) { |
|
93
|
|
|
if ($this->authorizationChecker->isGranted($attribute, $subject)) { |
|
94
|
|
|
return true; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return false; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|