-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathPermissions.php
More file actions
133 lines (115 loc) · 3.36 KB
/
Permissions.php
File metadata and controls
133 lines (115 loc) · 3.36 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
<?php
namespace Utopia\Database\Validator;
use Utopia\Database\Database;
use Utopia\Database\Helpers\Permission;
class Permissions extends Roles
{
protected string $message = 'Permissions Error';
/**
* @var array<string>
*/
protected array $allowed;
protected int $length;
/**
* Permissions constructor.
*
* @param int $length maximum amount of permissions. 0 means unlimited.
* @param array<string> $allowed allowed permissions. Defaults to all available.
*/
public function __construct(int $length = 0, array $allowed = Database::PERMISSIONS)
{
$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 $permissions
*
* @return bool
*/
public function isValid($permissions): bool
{
if (!\is_array($permissions)) {
$this->message = 'Permissions must be an array of strings.';
return false;
}
if ($this->length && \count($permissions) > $this->length) {
$this->message = 'You can only provide up to ' . $this->length . ' permissions.';
return false;
}
foreach ($permissions as $permission) {
if (!\is_string($permission)) {
$this->message = 'Every permission must be of type string.';
return false;
}
if ($permission === '*') {
$this->message = 'Wildcard permission "*" has been replaced. Use "any" instead.';
return false;
}
if (\str_contains($permission, 'role:')) {
$this->message = 'Permissions using the "role:" prefix have been replaced. Use "users", "guests", or "any" instead.';
return false;
}
$isAllowed = false;
foreach ($this->allowed as $allowed) {
if (\str_starts_with($permission, $allowed)) {
$isAllowed = true;
break;
}
}
if (!$isAllowed) {
$this->message = 'Permission "' . $permission . '" is not allowed. Must be one of: ' . \implode(', ', $this->allowed) . '.';
return false;
}
try {
$permission = Permission::parse($permission);
} catch (\Exception $e) {
$this->message = $e->getMessage();
return false;
}
$role = $permission->getRole();
$identifier = $permission->getIdentifier();
$dimension = $permission->getDimension();
if (!$this->isValidRole($role, $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;
}
}