|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpAbac\Comparison; |
|
4
|
|
|
|
|
5
|
|
|
class ArrayComparison extends AbstractComparison |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* @param array $haystack |
|
9
|
|
|
* @param mixed $needle |
|
10
|
|
|
* |
|
11
|
|
|
* @return bool |
|
12
|
|
|
*/ |
|
13
|
3 |
|
public function isIn($haystack, $needle) |
|
14
|
|
|
{ |
|
15
|
3 |
|
return in_array($needle, $haystack); |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @param array $haystack |
|
20
|
|
|
* @param mixed $needle |
|
21
|
|
|
* |
|
22
|
|
|
* @return bool |
|
23
|
|
|
*/ |
|
24
|
1 |
|
public function isNotIn($haystack, $needle) |
|
25
|
|
|
{ |
|
26
|
1 |
|
return !$this->isIn($haystack, $needle); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param array $array1 |
|
31
|
|
|
* @param array $array2 |
|
32
|
|
|
* |
|
33
|
|
|
* @return bool |
|
34
|
|
|
*/ |
|
35
|
2 |
|
public function intersect($array1, $array2) |
|
36
|
|
|
{ |
|
37
|
2 |
|
return count(array_intersect($array1, $array2)) > 0; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param array $array1 |
|
42
|
|
|
* @param array $array2 |
|
43
|
|
|
* |
|
44
|
|
|
* @return bool |
|
45
|
|
|
*/ |
|
46
|
1 |
|
public function doNotIntersect($array1, $array2) |
|
47
|
|
|
{ |
|
48
|
1 |
|
return !$this->intersect($array1, $array2); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param array $policyRuleAttributes |
|
53
|
|
|
* @param array $attributes |
|
54
|
|
|
* @param array $extraData |
|
55
|
|
|
* @return boolean |
|
56
|
|
|
*/ |
|
57
|
4 |
|
public function contains($policyRuleAttributes, $attributes, $extraData = []) { |
|
|
|
|
|
|
58
|
4 |
|
foreach($extraData['attribute']->getValue() as $attribute) { |
|
|
|
|
|
|
59
|
4 |
|
$result = true; |
|
60
|
|
|
// For each attribute, we check the whole rules set |
|
61
|
4 |
|
foreach($policyRuleAttributes as $pra) { |
|
|
|
|
|
|
62
|
4 |
|
$attributeData = $pra->getAttribute(); |
|
63
|
4 |
|
$attributeData->setValue( |
|
64
|
4 |
|
$this->comparisonManager->getAttributeManager()->retrieveAttribute($attributeData, $extraData['user'], $attribute) |
|
65
|
4 |
|
); |
|
66
|
|
|
// If one field is not matched, the whole attribute is rejected |
|
67
|
4 |
|
if(!$this->comparisonManager->compare($pra, true)) { |
|
|
|
|
|
|
68
|
|
|
//var_dump($attributeData->getName(), $attributeData->getValue(), $pra->getValue()); |
|
69
|
2 |
|
$result = false; |
|
70
|
2 |
|
break; |
|
71
|
|
|
} |
|
72
|
4 |
|
} |
|
73
|
|
|
// If the result is still true at the end of the attribute check, the rule is enforced |
|
74
|
4 |
|
if($result === true) { |
|
|
|
|
|
|
75
|
4 |
|
return true; |
|
76
|
|
|
} |
|
77
|
4 |
|
} |
|
78
|
4 |
|
return false; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.