-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathPermission.php
More file actions
199 lines (172 loc) · 4.89 KB
/
Permission.php
File metadata and controls
199 lines (172 loc) · 4.89 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
<?php
namespace Utopia\Database\Helpers;
use Exception;
use Utopia\Database\Database;
use Utopia\Database\Exception as DatabaseException;
class Permission
{
private Role $role;
public function __construct(
private string $permission,
string $role,
string $identifier = '',
string $dimension = '',
) {
$this->role = new Role($role, $identifier, $dimension);
}
/**
* Create a permission string from this Permission instance
*
* @return string
*/
public function toString(): string
{
return $this->permission . '("' . $this->role->toString() . '")';
}
/**
*
* @return string
*/
public function getPermission(): string
{
return $this->permission;
}
/**
* @return string
*/
public function getRole(): string
{
return $this->role->getRole();
}
/**
* @return string
*/
public function getIdentifier(): string
{
return $this->role->getIdentifier();
}
/**
* @return string
*/
public function getDimension(): string
{
return $this->role->getDimension();
}
/**
* Parse a permission string into a Permission object
*
* @param string $permission
* @return self
* @throws Exception
*/
public static function parse(string $permission): self
{
$permissionParts = \explode('("', $permission);
if (\count($permissionParts) !== 2) {
throw new DatabaseException('Invalid permission string format: "' . $permission . '".');
}
$permission = $permissionParts[0];
if (!\in_array($permission, Database::PERMISSIONS)) {
throw new DatabaseException('Invalid permission type: "' . $permission . '".');
}
$fullRole = \str_replace('")', '', $permissionParts[1]);
$roleParts = \explode(':', $fullRole);
$role = $roleParts[0];
$hasIdentifier = \count($roleParts) > 1;
$hasDimension = \str_contains($fullRole, '/');
if (!$hasIdentifier && !$hasDimension) {
return new self($permission, $role);
}
if ($hasIdentifier && !$hasDimension) {
$identifier = $roleParts[1];
return new self($permission, $role, $identifier);
}
if (!$hasIdentifier) {
$dimensionParts = \explode('/', $fullRole);
if (\count($dimensionParts) !== 2) {
throw new DatabaseException('Only one dimension can be provided');
}
$role = $dimensionParts[0];
$dimension = $dimensionParts[1];
if (empty($dimension)) {
throw new DatabaseException('Dimension must not be empty');
}
return new self($permission, $role, '', $dimension);
}
// Has both identifier and dimension
$dimensionParts = \explode('/', $roleParts[1]);
if (\count($dimensionParts) !== 2) {
throw new DatabaseException('Only one dimension can be provided');
}
$identifier = $dimensionParts[0];
$dimension = $dimensionParts[1];
if (empty($dimension)) {
throw new DatabaseException('Dimension must not be empty');
}
return new self($permission, $role, $identifier, $dimension);
}
/**
* Create a read permission string from the given Role
*
* @param Role $role
* @return string
*/
public static function read(Role $role): string
{
$permission = new self(
'read',
$role->getRole(),
$role->getIdentifier(),
$role->getDimension()
);
return $permission->toString();
}
/**
* Create a create permission string from the given Role
*
* @param Role $role
* @return string
*/
public static function create(Role $role): string
{
$permission = new self(
'create',
$role->getRole(),
$role->getIdentifier(),
$role->getDimension()
);
return $permission->toString();
}
/**
* Create an update permission string from the given Role
*
* @param Role $role
* @return string
*/
public static function update(Role $role): string
{
$permission = new self(
'update',
$role->getRole(),
$role->getIdentifier(),
$role->getDimension()
);
return $permission->toString();
}
/**
* Create a delete permission string from the given Role
*
* @param Role $role
* @return string
*/
public static function delete(Role $role): string
{
$permission = new self(
'delete',
$role->getRole(),
$role->getIdentifier(),
$role->getDimension()
);
return $permission->toString();
}
}