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