|
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
|
|
|
/** |
|
15
|
|
|
* @param array $attributes |
|
16
|
|
|
*/ |
|
17
|
6 |
|
public function __construct($attributes) |
|
18
|
|
|
{ |
|
19
|
6 |
|
$this->attributes = $attributes; |
|
20
|
6 |
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param string $attributeId |
|
24
|
|
|
* @return \PhpAbac\Model\AbstractAttribute |
|
25
|
|
|
*/ |
|
26
|
6 |
|
public function getAttribute($attributeId) { |
|
27
|
6 |
|
$attributeKeys = explode('.', $attributeId); |
|
28
|
|
|
// The first element will be the attribute ID, then the field ID |
|
29
|
6 |
|
$attributeId = array_shift($attributeKeys); |
|
30
|
6 |
|
$attributeName = implode('.', $attributeKeys); |
|
31
|
|
|
// The field ID is also the attribute object property |
|
32
|
6 |
|
$attributeData = $this->attributes[$attributeId]; |
|
33
|
|
|
return |
|
34
|
6 |
|
($attributeId === 'environment') |
|
35
|
6 |
|
? $this->getEnvironmentAttribute($attributeData, $attributeName) |
|
36
|
6 |
|
: $this->getClassicAttribute($attributeData, $attributeName) |
|
37
|
6 |
|
; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param array $attributeData |
|
42
|
|
|
* @param string $property |
|
43
|
|
|
* @return \PhpAbac\Model\Attribute |
|
44
|
|
|
*/ |
|
45
|
4 |
|
private function getClassicAttribute($attributeData, $property) { |
|
46
|
|
|
return |
|
47
|
4 |
|
(new Attribute()) |
|
|
|
|
|
|
48
|
4 |
|
->setName($attributeData['fields'][$property]['name']) |
|
49
|
4 |
|
->setType($attributeData['type']) |
|
50
|
4 |
|
->setProperty($property) |
|
51
|
4 |
|
->setSlug($this->slugify($attributeData['fields'][$property]['name'])) |
|
52
|
4 |
|
; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param array $attributeData |
|
57
|
|
|
* @param string $key |
|
58
|
|
|
* @return \PhpAbac\Model\EnvironmentAttribute |
|
59
|
|
|
*/ |
|
60
|
4 |
|
private function getEnvironmentAttribute($attributeData, $key) { |
|
61
|
|
|
return |
|
62
|
4 |
|
(new EnvironmentAttribute()) |
|
|
|
|
|
|
63
|
4 |
|
->setName($attributeData[$key]['name']) |
|
64
|
4 |
|
->setType('environment') |
|
65
|
4 |
|
->setVariableName($attributeData[$key]['variable_name']) |
|
66
|
4 |
|
->setSlug($this->slugify($attributeData[$key]['name'])) |
|
67
|
4 |
|
; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param AbstractAttribute $attribute |
|
72
|
|
|
* @param string $attributeType |
|
|
|
|
|
|
73
|
|
|
* @param object $user |
|
74
|
|
|
* @param object $object |
|
75
|
|
|
* @return mixed |
|
76
|
|
|
*/ |
|
77
|
3 |
|
public function retrieveAttribute(AbstractAttribute $attribute, $user, $object = null) |
|
78
|
|
|
{ |
|
79
|
3 |
|
switch($attribute->getType()) { |
|
|
|
|
|
|
80
|
3 |
|
case 'user': |
|
81
|
2 |
|
return $this->retrieveClassicAttribute($attribute, $user); |
|
|
|
|
|
|
82
|
2 |
|
case 'resource': |
|
83
|
1 |
|
return $this->retrieveClassicAttribute($attribute, $object); |
|
|
|
|
|
|
84
|
2 |
|
case 'environment': |
|
85
|
2 |
|
return $this->retrieveEnvironmentAttribute($attribute); |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param Attribute $attribute |
|
91
|
|
|
* @param object $object |
|
92
|
|
|
* @return mixed |
|
93
|
|
|
*/ |
|
94
|
2 |
|
private function retrieveClassicAttribute(Attribute $attribute, $object) |
|
95
|
|
|
{ |
|
96
|
2 |
|
$propertyPath = explode('.', $attribute->getProperty()); |
|
97
|
2 |
|
$propertyValue = $object; |
|
98
|
2 |
|
foreach($propertyPath as $property) { |
|
|
|
|
|
|
99
|
2 |
|
$getter = 'get'.ucfirst($property); |
|
100
|
2 |
|
if(!method_exists($propertyValue, $getter)) { |
|
|
|
|
|
|
101
|
|
|
throw new \InvalidArgumentException('There is no getter for the "'.$attribute->getProperty().'" attribute for object "'.get_class($propertyValue).'"'); |
|
102
|
|
|
} |
|
103
|
2 |
|
$propertyValue = $propertyValue->{$getter}(); |
|
104
|
2 |
|
} |
|
105
|
2 |
|
return $propertyValue; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* |
|
110
|
|
|
* @param \PhpAbac\Model\EnvironmentAttribute $attribute |
|
111
|
|
|
* @return mixed |
|
112
|
|
|
*/ |
|
113
|
2 |
|
private function retrieveEnvironmentAttribute(EnvironmentAttribute $attribute) { |
|
114
|
2 |
|
return getenv($attribute->getVariableName()); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param string $slug |
|
119
|
|
|
* @param array $dynamicAttributes |
|
120
|
|
|
* |
|
121
|
|
|
* @return mixed |
|
122
|
|
|
* |
|
123
|
|
|
* @throws \InvalidArgumentException |
|
124
|
|
|
*/ |
|
125
|
1 |
|
public function getDynamicAttribute($slug, $dynamicAttributes = []) |
|
126
|
|
|
{ |
|
127
|
1 |
|
if (!isset($dynamicAttributes[$slug])) { |
|
128
|
|
|
throw new \InvalidArgumentException('The "'.$slug.'" attribute is dynamic and its value must be given'); |
|
129
|
|
|
} |
|
130
|
1 |
|
return $dynamicAttributes[$slug]; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/* |
|
134
|
|
|
* @param string $name |
|
135
|
|
|
* @return string |
|
136
|
|
|
*/ |
|
137
|
6 |
|
public function slugify($name) |
|
138
|
|
|
{ |
|
139
|
|
|
// replace non letter or digits by - |
|
140
|
6 |
|
$name = trim(preg_replace('~[^\\pL\d]+~u', '-', $name), '-'); |
|
141
|
|
|
// transliterate |
|
142
|
6 |
|
if (function_exists('iconv')) { |
|
143
|
6 |
|
$name = iconv('utf-8', 'us-ascii//TRANSLIT', $name); |
|
144
|
6 |
|
} |
|
145
|
|
|
// remove unwanted characters |
|
146
|
6 |
|
$name = preg_replace('~[^-\w]+~', '', strtolower($name)); |
|
147
|
6 |
|
if (empty($name)) { |
|
148
|
|
|
return 'n-a'; |
|
149
|
|
|
} |
|
150
|
6 |
|
return $name; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: