1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpAbac; |
4
|
|
|
|
5
|
|
|
use PhpAbac\Manager\AttributeManager; |
6
|
|
|
use PhpAbac\Manager\PolicyRuleManager; |
7
|
|
|
use PhpAbac\Manager\ConfigurationManager; |
8
|
|
|
use PhpAbac\Manager\CacheManager; |
9
|
|
|
|
10
|
|
|
use Symfony\Component\Config\FileLocator; |
11
|
|
|
|
12
|
|
|
class Abac |
13
|
|
|
{ |
14
|
|
|
/** @var \PhpAbac\Manager\ConfigurationManager **/ |
15
|
|
|
private $configuration; |
16
|
|
|
/** @var \PhpAbac\Manager\PolicyRuleManager **/ |
17
|
|
|
private $policyRuleManager; |
18
|
|
|
/** @var \PhpAbac\Manager\AttributeManager **/ |
19
|
|
|
private $attributeManager; |
20
|
|
|
/** @var \PhpAbac\Manager\CacheManager **/ |
21
|
|
|
private $cacheManager; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param array $configPaths |
25
|
|
|
*/ |
26
|
1 |
|
public function __construct($configPaths) |
27
|
|
|
{ |
28
|
1 |
|
$this->configure($configPaths); |
29
|
1 |
|
$this->attributeManager = new AttributeManager($this->configuration->getAttributes()); |
30
|
1 |
|
$this->policyRuleManager = new PolicyRuleManager($this->attributeManager, $this->configuration->getRules()); |
31
|
1 |
|
$this->cacheManager = new CacheManager(); |
32
|
1 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param array $configPaths |
36
|
|
|
*/ |
37
|
1 |
|
public function configure($configPaths) { |
38
|
1 |
|
$locator = new FileLocator($configPaths); |
39
|
1 |
|
$this->configuration = new ConfigurationManager($locator); |
40
|
1 |
|
$this->configuration->parseConfigurationFile($configPaths); |
41
|
1 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Return true if both user and object respects all the rules conditions |
45
|
|
|
* If the objectId is null, policy rules about its attributes will be ignored |
46
|
|
|
* In case of mismatch between attributes and expected values, |
47
|
|
|
* an array with the concerned attributes slugs will be returned. |
48
|
|
|
* |
49
|
|
|
* Available options are : |
50
|
|
|
* * dynamic_attributes: array |
51
|
|
|
* * cache_result: boolean |
52
|
|
|
* * cache_ttl: integer |
53
|
|
|
* * cache_driver: string |
54
|
|
|
* |
55
|
|
|
* Available cache drivers are : |
56
|
|
|
* * memory |
57
|
|
|
* |
58
|
|
|
* @param string $ruleName |
59
|
|
|
* @param object $user |
60
|
|
|
* @param object $object |
61
|
|
|
* @param array $options |
62
|
|
|
* @return boolean|array |
63
|
|
|
*/ |
64
|
1 |
|
public function enforce($ruleName, $user, $object = null, $options = []) { |
65
|
|
|
// Retrieve cache value for the current rule and values if cache item is valid |
66
|
1 |
|
if(($cacheResult = isset($options['cache_result']) && $options['cache_result'] === true) === true) { |
|
|
|
|
67
|
|
|
$cacheItem = $this->cacheManager->getItem( |
68
|
|
|
"$ruleName-{$user->getId()}-" . (($object !== null) ? $object->getId() : ''), |
69
|
|
|
(isset($options['cache_driver'])) ? $options['cache_driver'] : null, |
70
|
|
|
(isset($options['cache_ttl'])) ? $options['cache_ttl'] : null |
71
|
|
|
); |
72
|
|
|
// We check if the cache value s valid before returning it |
73
|
|
|
if(($cacheValue = $cacheItem->get()) !== null) { |
|
|
|
|
74
|
|
|
return $cacheValue; |
75
|
|
|
} |
76
|
|
|
} |
77
|
1 |
|
$policyRule = $this->policyRuleManager->getRule($ruleName); |
78
|
1 |
|
$rejectedAttributes = []; |
79
|
|
|
|
80
|
1 |
|
foreach ($policyRule->getPolicyRuleAttributes() as $pra) { |
81
|
1 |
|
$attribute = $pra->getAttribute(); |
82
|
1 |
|
$attribute->setValue($this->attributeManager->retrieveAttribute($attribute, $user, $object)); |
83
|
1 |
|
$comparisonClass = 'PhpAbac\\Comparison\\'.ucfirst($pra->getComparisonType()).'Comparison'; |
84
|
1 |
|
$comparison = new $comparisonClass(); |
85
|
1 |
|
$dynamicAttributes = (isset($options['dynamic_attributes'])) ? $options['dynamic_attributes'] : []; |
86
|
|
|
$value = |
87
|
1 |
|
($pra->getValue() === 'dynamic') |
88
|
1 |
|
? $this->attributeManager->getDynamicAttribute($attribute->getSlug(), $dynamicAttributes) |
89
|
1 |
|
: $pra->getValue() |
90
|
1 |
|
; |
91
|
1 |
|
if ($comparison->{$pra->getComparison()}($value, $attribute->getValue()) !== true) { |
92
|
1 |
|
$rejectedAttributes[] = $attribute->getSlug(); |
93
|
1 |
|
} |
94
|
1 |
|
} |
95
|
1 |
|
$result = (count($rejectedAttributes) === 0) ? : $rejectedAttributes; |
96
|
1 |
|
if($cacheResult) { |
|
|
|
|
97
|
|
|
$cacheItem->set($result); |
|
|
|
|
98
|
|
|
$this->cacheManager->save($cacheItem); |
99
|
|
|
} |
100
|
1 |
|
return $result; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|