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
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Controls access to a Feature. |
12
|
|
|
* |
13
|
|
|
* @author Carlo Forghieri <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class FeatureSecurity |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @param AuthorizationCheckerInterface |
19
|
|
|
*/ |
20
|
|
|
protected $context; |
21
|
|
|
|
22
|
|
|
/** |
23
|
8 |
|
* @param TokenStorageInterface |
24
|
|
|
*/ |
25
|
8 |
|
private $storage; |
26
|
8 |
|
|
27
|
|
|
/** |
28
|
|
|
* @param string |
29
|
|
|
*/ |
30
|
|
|
private $providerKey; |
31
|
|
|
|
32
|
|
|
/** |
33
|
8 |
|
* @param AuthorizationCheckerInterface $context |
34
|
|
|
* @param TokenStorageInterface $storage |
35
|
8 |
|
* @param string $providerKey |
36
|
|
|
*/ |
37
|
|
|
public function __construct( |
38
|
|
|
AuthorizationCheckerInterface $context, |
39
|
8 |
|
TokenStorageInterface $storage, |
40
|
2 |
|
string $providerKey |
41
|
|
|
) { |
42
|
|
|
$this->context = $context; |
43
|
6 |
|
$this->storage = $storage; |
44
|
4 |
|
$this->providerKey = $providerKey; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param Feature $feature |
49
|
6 |
|
* |
50
|
2 |
|
* @return bool |
51
|
2 |
|
*/ |
52
|
|
|
public function isGranted(Feature $feature) |
53
|
|
|
{ |
54
|
|
|
if (!$feature->isEnabled()) { |
55
|
4 |
|
return false; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if ($feature->getRole()) { |
59
|
|
|
if (!$this->context->isGranted($feature->getRole())) { |
60
|
|
|
return false; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if ('' !== trim($feature->getParentRole())) { |
65
|
|
|
if (!$this->context->isGranted($feature->getParentRole())) { |
66
|
|
|
return false; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return true; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function isGrantedForUser(Feature $feature, UserInterface $user): bool |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
$oldToken = $this->storage->getToken(); |
76
|
|
|
|
77
|
|
|
$this->storage->setToken(new UsernamePasswordToken( |
78
|
|
|
$user, |
79
|
|
|
null, |
80
|
|
|
$this->providerKey, |
81
|
|
|
$user->getRoles() |
82
|
|
|
)); |
83
|
|
|
|
84
|
|
|
$granted = $this->isGranted($feature); |
85
|
|
|
|
86
|
|
|
$this->storage->setToken($oldToken); |
87
|
|
|
|
88
|
|
|
return $granted; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths