-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathRoles.php
More file actions
313 lines (280 loc) · 9.01 KB
/
Roles.php
File metadata and controls
313 lines (280 loc) · 9.01 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<?php
namespace Utopia\Database\Validator;
use Utopia\Database\Helpers\Role;
use Utopia\Validator;
class Roles extends Validator
{
// Roles
public const ROLE_ANY = 'any';
public const ROLE_GUESTS = 'guests';
public const ROLE_USERS = 'users';
public const ROLE_USER = 'user';
public const ROLE_TEAM = 'team';
public const ROLE_MEMBER = 'member';
public const ROLE_LABEL = 'label';
public const ROLES = [
self::ROLE_ANY,
self::ROLE_GUESTS,
self::ROLE_USERS,
self::ROLE_USER,
self::ROLE_TEAM,
self::ROLE_MEMBER,
self::ROLE_LABEL,
];
protected string $message = 'Roles Error';
/**
* @var array<string>
*/
protected array $allowed;
protected int $length;
public const CONFIG = [
self::ROLE_ANY => [
'identifier' => [
'allowed' => false,
'required' => false,
],
'dimension' => [
'allowed' => false,
'required' => false,
],
],
self::ROLE_GUESTS => [
'identifier' => [
'allowed' => false,
'required' => false,
],
'dimension' => [
'allowed' => false,
'required' => false,
],
],
self::ROLE_USERS => [
'identifier' => [
'allowed' => false,
'required' => false,
],
'dimension' => [
'allowed' => true,
'required' => false,
'options' => self::USER_DIMENSIONS
],
],
self::ROLE_USER => [
'identifier' => [
'allowed' => true,
'required' => true,
],
'dimension' => [
'allowed' => true,
'required' => false,
'options' => self::USER_DIMENSIONS
],
],
self::ROLE_TEAM => [
'identifier' => [
'allowed' => true,
'required' => true,
],
'dimension' => [
'allowed' => true,
'required' => false,
],
],
self::ROLE_MEMBER => [
'identifier' => [
'allowed' => true,
'required' => true,
],
'dimension' => [
'allowed' => false,
'required' => false,
],
],
self::ROLE_LABEL => [
'identifier' => [
'allowed' => true,
'required' => true,
],
'dimension' => [
'allowed' => false,
'required' => false,
],
],
];
// Dimensions
public const DIMENSION_VERIFIED = 'verified';
public const DIMENSION_UNVERIFIED = 'unverified';
public const USER_DIMENSIONS = [
self::DIMENSION_VERIFIED,
self::DIMENSION_UNVERIFIED,
];
/**
* Roles constructor.
*
* @param int $length maximum amount of role. 0 means unlimited.
* @param array<string> $allowed allowed roles. Defaults to all available.
*/
public function __construct(int $length = 0, array $allowed = self::ROLES)
{
$this->length = $length;
$this->allowed = $allowed;
}
/**
* Get Description.
*
* Returns validator description
*
* @return string
*/
public function getDescription(): string
{
return $this->message;
}
/**
* Is valid.
*
* Returns true if valid or false if not.
*
* @param mixed $roles
*
* @return bool
*/
public function isValid($roles): bool
{
if (!\is_array($roles)) {
$this->message = 'Roles must be an array of strings.';
return false;
}
if ($this->length && \count($roles) > $this->length) {
$this->message = 'You can only provide up to ' . $this->length . ' roles.';
return false;
}
foreach ($roles as $role) {
if (!\is_string($role)) {
$this->message = 'Every role must be of type string.';
return false;
}
if ($role === '*') {
$this->message = 'Wildcard role "*" has been replaced. Use "any" instead.';
return false;
}
if (\str_contains($role, 'role:')) {
$this->message = 'Roles using the "role:" prefix have been removed. Use "users", "guests", or "any" instead.';
return false;
}
$isAllowed = false;
foreach ($this->allowed as $allowed) {
if (\str_starts_with($role, $allowed)) {
$isAllowed = true;
break;
}
}
if (!$isAllowed) {
$this->message = 'Role "' . $role . '" is not allowed. Must be one of: ' . \implode(', ', $this->allowed) . '.';
return false;
}
try {
$role = Role::parse($role);
} catch (\Exception $e) {
$this->message = $e->getMessage();
return false;
}
$roleName = $role->getRole();
$identifier = $role->getIdentifier();
$dimension = $role->getDimension();
if (!$this->isValidRole($roleName, $identifier, $dimension)) {
return false;
}
}
return true;
}
/**
* Is array
*
* Function will return true if object is array.
*
* @return bool
*/
public function isArray(): bool
{
return false;
}
/**
* Get Type
*
* Returns validator type.
*
* @return string
*/
public function getType(): string
{
return self::TYPE_ARRAY;
}
protected function isValidRole(
string $role,
string $identifier,
string $dimension
): bool {
$identifierValidator = match ($role) {
self::ROLE_LABEL => new Label(),
default => new Key(),
};
/**
* For project-specific permissions, roles will be in the format `project-<projectId>-<role>`.
* Template takes 9 characters, `projectId` and `role` can be upto 36 characters. In total, 81 characters.
*/
$dimensionValidator = new Key(maxLength: 81);
$config = self::CONFIG[$role] ?? null;
if (empty($config)) {
$this->message = 'Role "' . $role . '" is not allowed. Must be one of: ' . \implode(', ', self::ROLES) . '.';
return false;
}
// Process identifier configuration
$allowed = $config['identifier']['allowed'];
$required = $config['identifier']['required'];
// Not allowed and has an identifier
if (!$allowed && !empty($identifier)) {
$this->message = 'Role "' . $role . '"' . ' can not have an ID value.';
return false;
}
// Required and has no identifier
if ($allowed && $required && empty($identifier)) {
$this->message = 'Role "' . $role . '"' . ' must have an ID value.';
return false;
}
// Allowed and has an invalid identifier
if ($allowed && !empty($identifier) && !$identifierValidator->isValid($identifier)) {
$this->message = 'Role "' . $role . '"' . ' identifier value is invalid: ' . $identifierValidator->getDescription();
return false;
}
// Process dimension configuration
$allowed = $config['dimension']['allowed'];
$required = $config['dimension']['required'];
$options = $config['dimension']['options'] ?? [$dimension];
// Not allowed and has a dimension
if (!$allowed && !empty($dimension)) {
$this->message = 'Role "' . $role . '"' . ' can not have a dimension value.';
return false;
}
// Required and has no dimension
// PHPStan complains because there are currently no dimensions that are required, but there might be in future
// @phpstan-ignore-next-line
if ($allowed && $required && empty($dimension)) {
$this->message = 'Role "' . $role . '"' . ' must have a dimension value.';
return false;
}
if ($allowed && !empty($dimension)) {
// Allowed and dimension is not an allowed option
if (!\in_array($dimension, $options)) {
$this->message = 'Role "' . $role . '"' . ' dimension value is invalid. Must be one of: ' . \implode(', ', $options) . '.';
return false;
}
// Allowed and dimension is not a valid key
if (!$dimensionValidator->isValid($dimension)) {
$this->message = 'Role "' . $role . '"' . ' dimension value is invalid: ' . $dimensionValidator->getDescription();
return false;
}
}
return true;
}
}