|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpAbac\Manager; |
|
4
|
|
|
|
|
5
|
|
|
use PhpAbac\Model\PolicyRuleAttribute; |
|
6
|
|
|
|
|
7
|
|
|
use PhpAbac\Comparison\{ |
|
8
|
|
|
ArrayComparison, |
|
9
|
|
|
BooleanComparison, |
|
10
|
|
|
DatetimeComparison, |
|
11
|
|
|
NumericComparison, |
|
12
|
|
|
ObjectComparison, |
|
13
|
|
|
UserComparison, |
|
14
|
|
|
StringComparison |
|
15
|
|
|
}; |
|
16
|
|
|
|
|
17
|
|
|
class ComparisonManager implements ComparisonManagerInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var AttributeManager **/ |
|
20
|
|
|
protected $attributeManager; |
|
21
|
|
|
/** @var array **/ |
|
22
|
|
|
protected $comparisons = [ |
|
23
|
|
|
'array' => ArrayComparison::class, |
|
24
|
|
|
'boolean' => BooleanComparison::class, |
|
25
|
|
|
'datetime' => DatetimeComparison::class, |
|
26
|
|
|
'numeric' => NumericComparison::class, |
|
27
|
|
|
'object' => ObjectComparison::class, |
|
28
|
|
|
'user' => UserComparison::class, |
|
29
|
|
|
'string' => StringComparison::class, |
|
30
|
|
|
]; |
|
31
|
|
|
/** @var array **/ |
|
32
|
|
|
protected $rejectedAttributes = []; |
|
33
|
|
|
|
|
34
|
10 |
|
public function __construct(AttributeManager $manager) |
|
35
|
|
|
{ |
|
36
|
10 |
|
$this->attributeManager = $manager; |
|
37
|
10 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* This method retrieve the comparison class, instanciate it, |
|
41
|
|
|
* and then perform the configured comparison |
|
42
|
|
|
* It does return a control value for special operations, |
|
43
|
|
|
* but the real check is at the end of the enforce() method, |
|
44
|
|
|
* when the rejected attributes are counted. |
|
45
|
|
|
* |
|
46
|
|
|
* If the second parameter is set to true, compare will not report errors. |
|
47
|
|
|
* This is used to test a bunch of comparisons expecting not all of them true to return a granted access. |
|
48
|
|
|
* In fact, this parameter is used in comparisons which need to perform comparisons on their own. |
|
49
|
|
|
*/ |
|
50
|
8 |
|
public function compare(PolicyRuleAttribute $pra, bool $subComparing = false): bool |
|
51
|
|
|
{ |
|
52
|
8 |
|
$attribute = $pra->getAttribute(); |
|
53
|
|
|
// The expected value can be set in the configuration as dynamic |
|
54
|
|
|
// In this case, we retrieve the expected value in the passed options |
|
55
|
|
|
$praValue = |
|
56
|
8 |
|
($pra->getValue() === 'dynamic') |
|
57
|
1 |
|
? $this->getDynamicAttribute($attribute->getSlug()) |
|
58
|
8 |
|
: $pra->getValue() |
|
59
|
|
|
; |
|
60
|
|
|
// Checking that the configured comparison type is available |
|
61
|
8 |
|
if (!isset($this->comparisons[$pra->getComparisonType()])) { |
|
62
|
1 |
|
throw new \InvalidArgumentException('The requested comparison class does not exist'); |
|
63
|
|
|
} |
|
64
|
|
|
// The comparison class will perform the attribute check with the configured method |
|
65
|
|
|
// For more complex comparisons, the comparison manager is injected |
|
66
|
7 |
|
$comparison = new $this->comparisons[$pra->getComparisonType()]($this); |
|
67
|
7 |
|
if (!method_exists($comparison, $pra->getComparison())) { |
|
68
|
1 |
|
throw new \InvalidArgumentException('The requested comparison method does not exist'); |
|
69
|
|
|
} |
|
70
|
|
|
// Then the comparison is performed with needed |
|
71
|
6 |
|
$result = $comparison->{$pra->getComparison()}($praValue, $attribute->getValue(), $pra->getExtraData()); |
|
72
|
|
|
// If the checked attribute is not valid, the attribute slug is marked as rejected |
|
73
|
|
|
// The rejected attributes will be returned instead of the expected true boolean |
|
74
|
6 |
|
if ($result !== true) { |
|
75
|
|
|
// In case of sub comparing, the error reporting is disabled |
|
76
|
5 |
|
if (!in_array($attribute->getSlug(), $this->rejectedAttributes) && $subComparing === false) { |
|
77
|
5 |
|
$this->rejectedAttributes[] = $attribute->getSlug(); |
|
78
|
|
|
} |
|
79
|
5 |
|
return false; |
|
80
|
|
|
} |
|
81
|
5 |
|
return true; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
2 |
|
public function setDynamicAttributes(array $dynamicAttributes) |
|
85
|
|
|
{ |
|
86
|
2 |
|
$this->dynamicAttributes = $dynamicAttributes; |
|
|
|
|
|
|
87
|
2 |
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* A dynamic attribute is a value given by the user code as an option |
|
91
|
|
|
* If a policy rule attribute is dynamic, |
|
92
|
|
|
* we check that the developer has given a dynamic value in the options |
|
93
|
|
|
* |
|
94
|
|
|
* Dynamic attributes are given with slugs as key |
|
95
|
|
|
* |
|
96
|
|
|
* @param string $attributeSlug |
|
97
|
|
|
* @return mixed |
|
98
|
|
|
* @throws \InvalidArgumentException |
|
99
|
|
|
*/ |
|
100
|
3 |
|
public function getDynamicAttribute(string $attributeSlug) |
|
101
|
|
|
{ |
|
102
|
3 |
|
if (!isset($this->dynamicAttributes[$attributeSlug])) { |
|
103
|
1 |
|
throw new \InvalidArgumentException("The dynamic value for attribute $attributeSlug was not given"); |
|
104
|
|
|
} |
|
105
|
2 |
|
return $this->dynamicAttributes[$attributeSlug]; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function addComparison(string $type, string $class) |
|
109
|
|
|
{ |
|
110
|
|
|
$this->comparisons[$type] = $class; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
3 |
|
public function getAttributeManager(): AttributeManager |
|
114
|
|
|
{ |
|
115
|
3 |
|
return $this->attributeManager; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* This method is called when all the policy rule attributes are checked |
|
120
|
|
|
* All along the comparisons, the failing attributes slugs are stored |
|
121
|
|
|
* If the rejected attributes array is not empty, it means that the rule is not enforced |
|
122
|
|
|
*/ |
|
123
|
6 |
|
public function getResult(): array |
|
124
|
|
|
{ |
|
125
|
|
|
$result = |
|
126
|
6 |
|
(count($this->rejectedAttributes) > 0) |
|
127
|
5 |
|
? $this->rejectedAttributes |
|
128
|
6 |
|
: [] |
|
129
|
|
|
; |
|
130
|
6 |
|
$this->rejectedAttributes = []; |
|
131
|
6 |
|
return $result; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: