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
|
25 |
|
public function __construct($attributes) |
18
|
|
|
{ |
19
|
25 |
|
$this->attributes = $attributes; |
20
|
25 |
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param string $attributeId |
24
|
|
|
* @return \PhpAbac\Model\AbstractAttribute |
25
|
|
|
*/ |
26
|
8 |
|
public function getAttribute($attributeId) { |
27
|
8 |
|
$attributeKeys = explode('.', $attributeId); |
28
|
|
|
// The first element will be the attribute ID, then the field ID |
29
|
8 |
|
$attributeId = array_shift($attributeKeys); |
30
|
8 |
|
$attributeName = implode('.', $attributeKeys); |
31
|
|
|
// The field ID is also the attribute object property |
32
|
8 |
|
$attributeData = $this->attributes[$attributeId]; |
33
|
|
|
return |
34
|
8 |
|
($attributeId === 'environment') |
35
|
4 |
|
? $this->getEnvironmentAttribute($attributeData, $attributeName) |
36
|
8 |
|
: $this->getClassicAttribute($attributeData, $attributeName) |
37
|
|
|
; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param array $attributeData |
42
|
|
|
* @param string $property |
43
|
|
|
* @return \PhpAbac\Model\Attribute |
44
|
|
|
*/ |
45
|
6 |
|
private function getClassicAttribute($attributeData, $property) { |
46
|
|
|
return |
47
|
6 |
|
(new Attribute()) |
|
|
|
|
48
|
6 |
|
->setName($attributeData['fields'][$property]['name']) |
49
|
6 |
|
->setType($attributeData['type']) |
50
|
6 |
|
->setProperty($property) |
51
|
6 |
|
->setSlug($this->slugify($attributeData['fields'][$property]['name'])) |
52
|
|
|
; |
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
|
|
|
; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param AbstractAttribute $attribute |
72
|
|
|
* @param string $attributeType |
|
|
|
|
73
|
|
|
* @param object $user |
74
|
|
|
* @param object $object |
75
|
|
|
* @return mixed |
76
|
|
|
*/ |
77
|
6 |
|
public function retrieveAttribute(AbstractAttribute $attribute, $user = null, $object = null) |
78
|
|
|
{ |
79
|
6 |
|
switch($attribute->getType()) { |
|
|
|
|
80
|
6 |
|
case 'user': |
81
|
3 |
|
return $this->retrieveClassicAttribute($attribute, $user); |
|
|
|
|
82
|
4 |
|
case 'resource': |
83
|
3 |
|
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
|
5 |
|
private function retrieveClassicAttribute(Attribute $attribute, $object) |
95
|
|
|
{ |
96
|
5 |
|
$propertyPath = explode('.', $attribute->getProperty()); |
97
|
5 |
|
$propertyValue = $object; |
98
|
5 |
|
foreach($propertyPath as $property) { |
|
|
|
|
99
|
5 |
|
$getter = 'get'.ucfirst($property); |
100
|
5 |
|
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
|
5 |
|
if (($propertyValue = $propertyValue->{$getter}()) === null) { |
104
|
5 |
|
return null; |
105
|
|
|
} |
106
|
|
|
} |
107
|
5 |
|
return $propertyValue; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* |
112
|
|
|
* @param \PhpAbac\Model\EnvironmentAttribute $attribute |
113
|
|
|
* @return mixed |
114
|
|
|
*/ |
115
|
2 |
|
private function retrieveEnvironmentAttribute(EnvironmentAttribute $attribute) { |
116
|
2 |
|
return getenv($attribute->getVariableName()); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/* |
120
|
|
|
* @param string $name |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
8 |
|
public function slugify($name) |
124
|
|
|
{ |
125
|
|
|
// replace non letter or digits by - |
126
|
8 |
|
$name = trim(preg_replace('~[^\\pL\d]+~u', '-', $name), '-'); |
127
|
|
|
// transliterate |
128
|
8 |
|
if (function_exists('iconv')) { |
129
|
8 |
|
$name = iconv('utf-8', 'us-ascii//TRANSLIT', $name); |
130
|
|
|
} |
131
|
|
|
// remove unwanted characters |
132
|
8 |
|
$name = preg_replace('~[^-\w]+~', '', strtolower($name)); |
133
|
8 |
|
if (empty($name)) { |
134
|
|
|
return 'n-a'; |
135
|
|
|
} |
136
|
8 |
|
return $name; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
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: