-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathAttributeReleasePolicy.php
More file actions
248 lines (216 loc) · 6.61 KB
/
AttributeReleasePolicy.php
File metadata and controls
248 lines (216 loc) · 6.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
/**
* Copyright 2010 SURFnet B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace OpenConext\EngineBlock\Metadata;
use InvalidArgumentException;
/**
* Class AttributeReleasePolicy
* @package OpenConext\EngineBlock\Metadata
*/
class AttributeReleasePolicy
{
const WILDCARD_CHARACTER = '*';
/**
* Holds attribute rule values with optional 'source'.
*
* Non-aggregated attributes are stored in the format:
*
* attribute name => [value, value, value]
*
* Attributes aggregated from different sources specify a source along with the value:
*
* attribute name => [
* [ 'soure' => '...', 'value' => '...' ]
* ]
*
* @var array
*/
private $attributeRules;
/**
* @param array $attributeRules
*/
public function __construct(array $attributeRules)
{
foreach ($attributeRules as $key => $rules) {
if (!is_string($key)) {
throw new InvalidArgumentException(sprintf('Invalid key: "%s"', var_export($key, true)));
}
if (!is_array($rules)) {
throw new InvalidArgumentException(
sprintf('Invalid values for attribute "%s", not an array: "%s"', $key, var_export($rules, true))
);
}
foreach ($rules as $rule) {
$this->validateRule($key, $rule);
}
}
$this->attributeRules = $attributeRules;
}
/**
* @param string $key
* @param mixed $rule
* @throws InvalidArgumentException
*/
private function validateRule($key, $rule)
{
if (is_array($rule)) {
if (!isset($rule['value'])) {
throw new InvalidArgumentException(
sprintf(
'Invalid value for attribute "%s", rule must contain a value key, got: "%s"',
$key,
var_export($rule, true)
)
);
}
$value = $rule['value'];
} else {
$value = $rule;
}
if (!is_string($value)) {
throw new InvalidArgumentException(
sprintf('Invalid value for attribute "%s", not a string: "%s"', $key, var_export($value, true))
);
}
}
/**
* Return all attribute rules eligible for attribute aggregation.
*
* A rule is eligible for attribute aggregation if it contains a source.
*
* @return array
*/
public function getRulesWithSourceSpecification()
{
$rulesWithSource = [];
foreach ($this->attributeRules as $name => $rules) {
$rulesWithSource[$name] = array_filter(
$rules,
function ($rule) {
return isset($rule['source']) && $rule['source'] !== 'idp';
}
);
}
return array_filter($rulesWithSource);
}
/**
* @return array
*/
public function getAttributeNames()
{
return array_keys($this->attributeRules);
}
/**
* @param $attributeName
* @return bool
*/
public function hasAttribute($attributeName)
{
return isset($this->attributeRules[$attributeName]);
}
/**
* @param $attributeName
* @param $attributeValue
* @return bool
*/
public function isAllowed($attributeName, $attributeValue)
{
if (!$this->hasAttribute($attributeName)) {
return false;
}
foreach ($this->attributeRules[$attributeName] as $rule) {
$allowedValue = $this->getRuleValue($rule);
if ($attributeValue === $allowedValue) {
// Literal match.
return true;
}
if ($allowedValue === self::WILDCARD_CHARACTER) {
// Only a single wildcard character, all values are permitted.
return true;
}
// We support wildcard matching at the end only, like 'some*' would match 'someValue' or 'somethingElse'
if (substr($allowedValue, -1) !== self::WILDCARD_CHARACTER) {
// Not a supported pattern
continue;
}
// Would contain 'some'
$patternStart = substr($allowedValue, 0, -1);
// Does $attributeValue start with 'some'?
if (strpos($attributeValue, $patternStart) === 0) {
return true;
}
}
return false;
}
/**
* Read the value of an ARP rule, ignoring the source.
*
* @param $rule
* @return string
*/
private function getRuleValue($rule)
{
if (isset($rule['value'])) {
return (string) $rule['value'];
}
return (string) $rule;
}
/**
* Loads the motivation text for an attribute.
*
* @param $attributeName
* @return string
*/
public function getMotivation($attributeName)
{
if (!$this->hasAttribute($attributeName)) {
return;
}
if (empty($this->attributeRules[$attributeName][0]['motivation'])) {
return;
}
return $this->attributeRules[$attributeName][0]['motivation'];
}
/**
* Loads the first source it finds in the list of attribute rules for the given attributeName.
*
* @param $attributeName
* @return string
*/
public function getSource($attributeName)
{
if ($this->hasAttribute($attributeName) && isset($this->attributeRules[$attributeName][0]['source'])) {
return $this->attributeRules[$attributeName][0]['source'];
}
return 'idp';
}
/**
* @return array
*/
public function getAttributeRules()
{
return $this->attributeRules;
}
/**
* A convenience static constructor for the AttributeReleasePolicy.
* @param array $attributeReleasePolicy
* @return AttributeReleasePolicy
*/
public static function fromArray(array $attributeReleasePolicy): AttributeReleasePolicy
{
return new self($attributeReleasePolicy);
}
}