|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ae\FeatureBundle\Security; |
|
4
|
|
|
|
|
5
|
|
|
use Ae\FeatureBundle\Entity\Feature; |
|
6
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
|
7
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; |
|
8
|
|
|
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
|
9
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Controls access to a Feature. |
|
13
|
|
|
* |
|
14
|
|
|
* @author Carlo Forghieri <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
class FeatureSecurity |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @param AuthorizationCheckerInterface |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $context; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param TokenStorageInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
private $storage; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param string |
|
30
|
|
|
*/ |
|
31
|
|
|
private $providerKey; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param AuthorizationCheckerInterface $context |
|
35
|
|
|
* @param TokenStorageInterface $storage |
|
36
|
|
|
* @param string $providerKey |
|
37
|
|
|
*/ |
|
38
|
8 |
|
public function __construct( |
|
39
|
|
|
AuthorizationCheckerInterface $context, |
|
40
|
|
|
TokenStorageInterface $storage, |
|
41
|
|
|
string $providerKey |
|
42
|
|
|
) { |
|
43
|
8 |
|
$this->context = $context; |
|
44
|
8 |
|
$this->storage = $storage; |
|
45
|
8 |
|
$this->providerKey = $providerKey; |
|
46
|
8 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param Feature $feature |
|
50
|
|
|
* |
|
51
|
|
|
* @return bool |
|
52
|
|
|
*/ |
|
53
|
6 |
|
public function isGranted(Feature $feature) |
|
54
|
|
|
{ |
|
55
|
|
|
// feature is enabled without required roles |
|
56
|
|
|
// there's no need to check on user roles |
|
57
|
6 |
|
if (!$feature->requiresRoleCheck()) { |
|
58
|
2 |
|
return $feature->isEnabled(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
4 |
|
if (!$feature->isEnabled()) { |
|
62
|
1 |
|
return false; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
3 |
|
if ($feature->getRole()) { |
|
66
|
2 |
|
if (!$this->context->isGranted($feature->getRole())) { |
|
67
|
|
|
return false; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
3 |
|
if ('' !== trim($feature->getParentRole())) { |
|
72
|
1 |
|
if (!$this->context->isGranted($feature->getParentRole())) { |
|
73
|
1 |
|
return false; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
2 |
|
return true; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
2 |
|
public function isGrantedForUser(Feature $feature, UserInterface $user): bool |
|
81
|
|
|
{ |
|
82
|
2 |
|
$oldToken = $this->storage->getToken(); |
|
83
|
|
|
|
|
84
|
2 |
|
$this->storage->setToken(new UsernamePasswordToken( |
|
85
|
2 |
|
$user, |
|
|
|
|
|
|
86
|
2 |
|
null, |
|
87
|
2 |
|
$this->providerKey, |
|
88
|
2 |
|
$user->getRoles() |
|
89
|
|
|
)); |
|
90
|
|
|
|
|
91
|
2 |
|
$granted = $this->isGranted($feature); |
|
92
|
|
|
|
|
93
|
2 |
|
$this->storage->setToken($oldToken); |
|
94
|
|
|
|
|
95
|
2 |
|
return $granted; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|