1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpAbac\Manager; |
4
|
|
|
|
5
|
|
|
use PhpAbac\Model\AbstractAttribute; |
6
|
|
|
use PhpAbac\Model\Attribute; |
7
|
|
|
use PhpAbac\Model\EnvironmentAttribute; |
8
|
|
|
|
9
|
|
|
class AttributeManager |
10
|
|
|
{ |
11
|
|
|
/** @var array **/ |
12
|
|
|
private $attributes; |
13
|
|
|
|
14
|
|
|
/** @var string Prefix to add before getter name (default)'get' */ |
15
|
|
|
private $getter_prefix = 'get'; |
16
|
|
|
/** @var string Function to apply on the getter name ( before adding prefix ) (default)'ucfirst' */ |
17
|
|
|
private $getter_name_transformation_function = 'ucfirst'; |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param array $attributes |
22
|
|
|
* @param array $options A List of option to configure This Abac Instance |
23
|
|
|
* Options list : |
24
|
|
|
* 'getter_prefix' => Prefix to add before getter name (default)'get' |
25
|
|
|
* 'getter_name_transformation_function' => Function to apply on the getter name ( before adding prefix ) (default)'ucfirst' |
26
|
|
|
*/ |
27
|
28 |
|
public function __construct($attributes, $options = []) |
28
|
|
|
{ |
29
|
28 |
|
$this->attributes = $attributes; |
30
|
|
|
|
31
|
28 |
|
$options = array_intersect_key( $options, array_flip( [ |
32
|
28 |
|
'getter_prefix', |
33
|
28 |
|
'getter_name_transformation_function', |
34
|
28 |
|
] ) ); |
35
|
|
|
|
36
|
28 |
|
foreach($options as $name => $value) { |
|
|
|
|
37
|
|
|
$this->$name = $value; |
38
|
28 |
|
} |
39
|
28 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $attributeId |
43
|
|
|
* @return \PhpAbac\Model\AbstractAttribute |
44
|
|
|
*/ |
45
|
11 |
|
public function getAttribute($attributeId) { |
46
|
11 |
|
$attributeKeys = explode('.', $attributeId); |
47
|
|
|
// The first element will be the attribute ID, then the field ID |
48
|
11 |
|
$attributeId = array_shift($attributeKeys); |
49
|
11 |
|
$attributeName = implode('.', $attributeKeys); |
50
|
|
|
// The field ID is also the attribute object property |
51
|
11 |
|
$attributeData = $this->attributes[$attributeId]; |
52
|
|
|
return |
53
|
11 |
|
($attributeId === 'environment') |
54
|
11 |
|
? $this->getEnvironmentAttribute($attributeData, $attributeName) |
55
|
11 |
|
: $this->getClassicAttribute($attributeData, $attributeName) |
56
|
11 |
|
; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param array $attributeData |
61
|
|
|
* @param string $property |
62
|
|
|
* @return \PhpAbac\Model\Attribute |
63
|
|
|
*/ |
64
|
9 |
|
private function getClassicAttribute($attributeData, $property) { |
65
|
|
|
return |
66
|
9 |
|
(new Attribute()) |
67
|
9 |
|
->setName($attributeData['fields'][$property]['name']) |
68
|
9 |
|
->setType($attributeData['type']) |
69
|
9 |
|
->setProperty($property) |
70
|
9 |
|
->setSlug($this->slugify($attributeData['fields'][$property]['name'])) |
71
|
9 |
|
; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param array $attributeData |
76
|
|
|
* @param string $key |
77
|
|
|
* @return \PhpAbac\Model\EnvironmentAttribute |
78
|
|
|
*/ |
79
|
4 |
|
private function getEnvironmentAttribute($attributeData, $key) { |
80
|
|
|
return |
81
|
4 |
|
(new EnvironmentAttribute()) |
82
|
4 |
|
->setName($attributeData[$key]['name']) |
83
|
4 |
|
->setType('environment') |
84
|
4 |
|
->setVariableName($attributeData[$key]['variable_name']) |
85
|
4 |
|
->setSlug($this->slugify($attributeData[$key]['name'])) |
86
|
4 |
|
; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param AbstractAttribute $attribute |
91
|
|
|
* @param string $attributeType |
|
|
|
|
92
|
|
|
* @param object $user |
93
|
|
|
* @param object $object |
94
|
|
|
* @return mixed |
95
|
|
|
*/ |
96
|
9 |
|
public function retrieveAttribute(AbstractAttribute $attribute, $user = null, $object = null, $getter_params = []) |
97
|
|
|
{ |
98
|
9 |
|
switch($attribute->getType()) { |
|
|
|
|
99
|
9 |
|
case 'user': |
100
|
6 |
|
return $this->retrieveClassicAttribute($attribute, $user, $getter_params); |
|
|
|
|
101
|
6 |
|
case 'resource': |
102
|
5 |
|
return $this->retrieveClassicAttribute($attribute, $object); |
|
|
|
|
103
|
2 |
|
case 'environment': |
104
|
2 |
|
return $this->retrieveEnvironmentAttribute($attribute); |
|
|
|
|
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param Attribute $attribute |
110
|
|
|
* @param object $object |
111
|
|
|
* @return mixed |
112
|
|
|
*/ |
113
|
8 |
|
private function retrieveClassicAttribute(Attribute $attribute, $object, $getter_params = []) |
114
|
|
|
{ |
115
|
8 |
|
$propertyPath = explode('.', $attribute->getProperty()); |
116
|
8 |
|
$propertyValue = $object; |
117
|
8 |
|
foreach($propertyPath as $property) { |
|
|
|
|
118
|
|
|
|
119
|
|
|
|
120
|
8 |
|
$getter = $this->getter_prefix.call_user_func($this->getter_name_transformation_function,$property); |
121
|
|
|
// Use is_callable, instead of method_exists, to deal with __call magic method |
122
|
8 |
|
if(!is_callable([$propertyValue,$getter])) { |
|
|
|
|
123
|
|
|
throw new \InvalidArgumentException('There is no getter for the "'.$attribute->getProperty().'" attribute for object "'.get_class($propertyValue).'" with getter "'.$getter.'"'); |
124
|
|
|
} |
125
|
8 |
|
if ( ( $propertyValue = call_user_func_array( [ |
126
|
8 |
|
$propertyValue, |
127
|
8 |
|
$getter, |
128
|
8 |
|
], isset( $getter_params[ $property ] ) ? $getter_params[ $property ] : [] ) ) === null |
129
|
8 |
|
) { |
130
|
1 |
|
return null; |
131
|
|
|
} |
132
|
8 |
|
} |
133
|
8 |
|
return $propertyValue; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* |
138
|
|
|
* @param \PhpAbac\Model\EnvironmentAttribute $attribute |
139
|
|
|
* @return mixed |
140
|
|
|
*/ |
141
|
2 |
|
private function retrieveEnvironmentAttribute(EnvironmentAttribute $attribute) { |
142
|
2 |
|
return getenv($attribute->getVariableName()); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/* |
146
|
|
|
* @param string $name |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
11 |
|
public function slugify($name) |
150
|
|
|
{ |
151
|
|
|
// replace non letter or digits by - |
152
|
11 |
|
$name = trim(preg_replace('~[^\\pL\d]+~u', '-', $name), '-'); |
153
|
|
|
// transliterate |
154
|
11 |
|
if (function_exists('iconv')) { |
155
|
11 |
|
$name = iconv('utf-8', 'us-ascii//TRANSLIT', $name); |
156
|
11 |
|
} |
157
|
|
|
// remove unwanted characters |
158
|
11 |
|
$name = preg_replace('~[^-\w]+~', '', strtolower($name)); |
159
|
11 |
|
if (empty($name)) { |
160
|
|
|
return 'n-a'; |
161
|
|
|
} |
162
|
11 |
|
return $name; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|