1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpAbac\Manager; |
4
|
|
|
|
5
|
|
|
use PhpAbac\Abac; |
6
|
|
|
use PhpAbac\Model\PolicyRule; |
7
|
|
|
use PhpAbac\Model\PolicyRuleAttribute; |
8
|
|
|
|
9
|
|
|
class PolicyRuleManager |
10
|
|
|
{ |
11
|
|
|
/** @var \PhpAbac\Manager\AttributeManager **/ |
12
|
|
|
private $attributeManager; |
13
|
|
|
/** @var array **/ |
14
|
|
|
private $rules; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param \PhpAbac\Manager\AttributeManager $attributeManager |
18
|
|
|
* @param array $rules |
19
|
|
|
*/ |
20
|
2 |
|
public function __construct(AttributeManager $attributeManager, $rules) |
21
|
|
|
{ |
22
|
2 |
|
$this->attributeManager = $attributeManager; |
23
|
2 |
|
$this->rules = $rules; |
24
|
2 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param string $ruleName |
28
|
|
|
* |
29
|
|
|
* @return PolicyRule |
30
|
|
|
* |
31
|
|
|
* @throws \InvalidArgumentException |
32
|
|
|
*/ |
33
|
2 |
|
public function getRule($ruleName) |
34
|
|
|
{ |
35
|
2 |
|
if(!isset($this->rules[$ruleName])) { |
|
|
|
|
36
|
|
|
throw new \InvalidArgumentException('The given rule "' . $ruleName . '" is not configured'); |
37
|
|
|
} |
38
|
|
|
$rule = |
39
|
2 |
|
(new PolicyRule()) |
40
|
2 |
|
->setName($ruleName) |
41
|
2 |
|
; |
42
|
2 |
|
foreach($this->processRuleAttributes($this->rules[$ruleName]['attributes']) as $pra) { |
|
|
|
|
43
|
2 |
|
$rule->addPolicyRuleAttribute($pra); |
44
|
2 |
|
} |
45
|
2 |
|
return $rule; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param array $attributes |
50
|
|
|
*/ |
51
|
2 |
|
public function processRuleAttributes($attributes) { |
52
|
2 |
|
foreach($attributes as $attributeName => $attribute) { |
|
|
|
|
53
|
2 |
|
$pra = (new PolicyRuleAttribute()) |
54
|
2 |
|
->setAttribute($this->attributeManager->getAttribute($attributeName)) |
55
|
2 |
|
->setComparison($attribute['comparison']) |
56
|
2 |
|
->setComparisonType($attribute['comparison_type']) |
57
|
2 |
|
->setValue((isset($attribute['value'])) ? $attribute['value'] : null) |
58
|
2 |
|
; |
59
|
2 |
|
foreach($attribute as $key => $value) { |
|
|
|
|
60
|
2 |
|
if(!in_array($key, ['comparison', 'comparison_type', 'value'])) { |
|
|
|
|
61
|
1 |
|
$pra->addExtraData($key, $value); |
62
|
1 |
|
} |
63
|
2 |
|
} |
64
|
2 |
|
yield $pra; |
65
|
2 |
|
} |
66
|
2 |
|
} |
67
|
|
|
} |
68
|
|
|
|