|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpAbac\Manager; |
|
4
|
|
|
|
|
5
|
|
|
use PhpAbac\Model\PolicyRule; |
|
6
|
|
|
use PhpAbac\Model\PolicyRuleAttribute; |
|
7
|
|
|
|
|
8
|
|
|
class PolicyRuleManager |
|
9
|
|
|
{ |
|
10
|
|
|
/** @var \PhpAbac\Manager\AttributeManager * */ |
|
11
|
|
|
private $attributeManager; |
|
12
|
|
|
/** @var array * */ |
|
13
|
|
|
private $rules; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @param \PhpAbac\Manager\AttributeManager $attributeManager |
|
17
|
|
|
* @param array $rules |
|
18
|
|
|
*/ |
|
19
|
5 |
|
public function __construct(AttributeManager $attributeManager, $rules) |
|
20
|
|
|
{ |
|
21
|
5 |
|
$this->attributeManager = $attributeManager; |
|
22
|
5 |
|
$this->rules = $rules; |
|
23
|
5 |
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param string $ruleName |
|
27
|
|
|
* @param object $user |
|
28
|
|
|
* @param object $resource |
|
29
|
|
|
* @return PolicyRule[] |
|
30
|
|
|
* @throws \InvalidArgumentException |
|
31
|
|
|
*/ |
|
32
|
5 |
|
public function getRule($ruleName, $user, $resource) |
|
33
|
|
|
{ |
|
34
|
5 |
|
if (!isset($this->rules[$ruleName])) { |
|
35
|
|
|
throw new \InvalidArgumentException('The given rule "' . $ruleName . '" is not configured'); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
// force to treat always arrays |
|
39
|
5 |
|
if (array_key_exists('attributes', $this->rules[$ruleName])) { |
|
40
|
4 |
|
$this->rules[$ruleName] = [$this->rules[$ruleName]]; |
|
41
|
4 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
5 |
|
$rule_a = []; |
|
45
|
5 |
|
foreach ($this->rules[$ruleName] as $rule) { |
|
46
|
|
|
$Policy = |
|
47
|
5 |
|
(new PolicyRule()) |
|
48
|
5 |
|
->setName($ruleName); |
|
49
|
|
|
// For each policy rule attribute, the data is formatted |
|
50
|
5 |
|
foreach ($this->processRuleAttributes($rule['attributes'], $user, $resource) as $pra) { |
|
51
|
5 |
|
$Policy->addPolicyRuleAttribute($pra); |
|
52
|
5 |
|
} |
|
53
|
5 |
|
$rule_a[] = $Policy; |
|
54
|
5 |
|
} |
|
55
|
5 |
|
return $rule_a; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* This method is meant to convert attribute data from array to formatted policy rule attribute |
|
60
|
|
|
* |
|
61
|
|
|
* @param array $attributes |
|
62
|
|
|
* @param object $user |
|
63
|
|
|
* @param object $resource |
|
64
|
|
|
*/ |
|
65
|
5 |
|
public function processRuleAttributes($attributes, $user, $resource) |
|
66
|
|
|
{ |
|
67
|
5 |
|
foreach ($attributes as $attributeName => $attribute) { |
|
68
|
5 |
|
$pra = (new PolicyRuleAttribute()) |
|
69
|
5 |
|
->setAttribute($this->attributeManager->getAttribute($attributeName)) |
|
70
|
5 |
|
->setComparison($attribute['comparison']) |
|
71
|
5 |
|
->setComparisonType($attribute['comparison_type']) |
|
72
|
5 |
|
->setValue((isset($attribute['value'])) ? $attribute['value'] : null) |
|
73
|
5 |
|
->setGetterParams( isset( $attribute[ 'getter_params' ] ) ? $attribute[ 'getter_params' ] : [] ); |
|
74
|
5 |
|
$this->processRuleAttributeComparisonType($pra, $user, $resource); |
|
75
|
|
|
// In the case the user configured more keys than the basic ones |
|
76
|
|
|
// it will be stored as extra data |
|
77
|
5 |
|
foreach ($attribute as $key => $value) { |
|
78
|
5 |
|
if (!in_array($key, ['comparison', 'comparison_type', 'value','getter_params'])) { |
|
79
|
3 |
|
$pra->addExtraData($key, $value); |
|
80
|
3 |
|
} |
|
81
|
5 |
|
} |
|
82
|
|
|
// This generator avoid useless memory consumption instead of returning a whole array |
|
83
|
5 |
|
yield $pra; |
|
84
|
5 |
|
} |
|
85
|
5 |
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* This method is meant to set appropriated extra data to $pra depending on comparison type |
|
89
|
|
|
* |
|
90
|
|
|
* @param PolicyRuleAttribute $pra |
|
91
|
|
|
* @param object $user |
|
92
|
|
|
* @param object $resource |
|
93
|
|
|
*/ |
|
94
|
5 |
|
public function processRuleAttributeComparisonType(PolicyRuleAttribute $pra, $user, $resource) |
|
95
|
|
|
{ |
|
96
|
5 |
|
switch ($pra->getComparisonType()) { |
|
97
|
5 |
|
case 'user': |
|
98
|
2 |
|
$pra->setExtraData(['user' => $user]); |
|
99
|
2 |
|
break; |
|
100
|
5 |
|
case 'object': |
|
101
|
|
|
$pra->setExtraData(['resource' => $resource]); |
|
102
|
|
|
break; |
|
103
|
5 |
|
} |
|
104
|
5 |
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|