Skip to content

Commit db1a0f9

Browse files
authored
Merge pull request #1236 from appwrite/feat-update-permissions-docs
2 parents efda558 + c1aaf05 commit db1a0f9

File tree

25 files changed

+289
-17
lines changed

25 files changed

+289
-17
lines changed

src/SDK/Language.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,60 @@ protected function toUpperSnakeCase($str): string
137137
{
138138
return \strtoupper($this->toSnakeCase($str));
139139
}
140+
141+
public function isPermissionString(string $string): bool
142+
{
143+
$pattern = '/^\["(read|update|delete|write)\(\\"[^\\"]+\\"\)"(,\s*"(read|update|delete|write)\(\\"[^\\"]+\\"\)")*\]$/';
144+
return preg_match($pattern, $string) === 1;
145+
}
146+
147+
public function extractPermissionParts(string $string): array
148+
{
149+
$inner = substr($string, 1, -1);
150+
preg_match_all('/"(read|update|delete|write)\(\\"([^\\"]+)\\"\)"/', $inner, $matches, PREG_SET_ORDER);
151+
152+
$result = [];
153+
foreach ($matches as $match) {
154+
$action = $match[1];
155+
$roleString = $match[2];
156+
157+
$role = null;
158+
$id = null;
159+
$innerRole = null;
160+
161+
if (strpos($roleString, ':') !== false) {
162+
$role = explode(':', $roleString, 2)[0];
163+
$idString = explode(':', $roleString, 2)[1];
164+
165+
if (strpos($idString, '/') !== false) {
166+
$id = explode('/', $idString, 2)[0];
167+
$innerRole = explode('/', $idString, 2)[1];
168+
} else {
169+
$id = $idString;
170+
}
171+
} else {
172+
$role = $roleString;
173+
}
174+
175+
$result[] = [
176+
'action' => $action,
177+
'role' => $role,
178+
'id' => $id ?? null,
179+
'innerRole' => $innerRole
180+
];
181+
}
182+
183+
return $result;
184+
}
185+
186+
public function hasPermissionParam(array $parameters): bool
187+
{
188+
foreach ($parameters as $param) {
189+
$example = $param['example'] ?? '';
190+
if (!empty($example) && is_string($example) && $this->isPermissionString($example)) {
191+
return true;
192+
}
193+
}
194+
return false;
195+
}
140196
}

src/SDK/Language/Dart.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,8 @@ public function getParamExample(array $param): string
240240
}
241241

242242
return match ($type) {
243-
self::TYPE_ARRAY, self::TYPE_FILE, self::TYPE_INTEGER, self::TYPE_NUMBER => $example,
243+
self::TYPE_ARRAY => $this->isPermissionString($example) ? $this->getPermissionExample($example) : $example,
244+
self::TYPE_FILE, self::TYPE_INTEGER, self::TYPE_NUMBER => $example,
244245
self::TYPE_BOOLEAN => ($example) ? 'true' : 'false',
245246
self::TYPE_OBJECT => ($decoded = json_decode($example, true)) !== null
246247
? (empty($decoded) && $example === '{}'
@@ -251,6 +252,23 @@ public function getParamExample(array $param): string
251252
};
252253
}
253254

255+
public function getPermissionExample(string $example): string
256+
{
257+
$permissions = [];
258+
foreach ($this->extractPermissionParts($example) as $permission) {
259+
$args = [];
260+
if ($permission['id'] !== null) {
261+
$args[] = "'" . $permission['id'] . "'";
262+
}
263+
if ($permission['innerRole'] !== null) {
264+
$args[] = "'" . $permission['innerRole'] . "'";
265+
}
266+
$argsString = implode(', ', $args);
267+
$permissions[] = 'Permission.' . $permission['action'] . '(Role.' . $permission['role'] . '(' . $argsString . '))';
268+
}
269+
return '[' . implode(', ', $permissions) . ']';
270+
}
271+
254272
/**
255273
* @return array
256274
*/

src/SDK/Language/Deno.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ public function getParamExample(array $param): string
176176
}
177177

178178
return match ($type) {
179-
self::TYPE_ARRAY, self::TYPE_INTEGER, self::TYPE_NUMBER => $example,
179+
self::TYPE_ARRAY => $this->isPermissionString($example) ? $this->getPermissionExample($example) : $example,
180+
self::TYPE_INTEGER, self::TYPE_NUMBER => $example,
180181
self::TYPE_FILE => 'InputFile.fromPath(\'/path/to/file.png\', \'file.png\')',
181182
self::TYPE_BOOLEAN => ($example) ? 'true' : 'false',
182183
self::TYPE_OBJECT => ($example === '{}')
@@ -187,4 +188,21 @@ public function getParamExample(array $param): string
187188
self::TYPE_STRING => "'{$example}'",
188189
};
189190
}
191+
192+
public function getPermissionExample(string $example): string
193+
{
194+
$permissions = [];
195+
foreach ($this->extractPermissionParts($example) as $permission) {
196+
$args = [];
197+
if ($permission['id'] !== null) {
198+
$args[] = "'" . $permission['id'] . "'";
199+
}
200+
if ($permission['innerRole'] !== null) {
201+
$args[] = "'" . $permission['innerRole'] . "'";
202+
}
203+
$argsString = implode(', ', $args);
204+
$permissions[] = 'Permission.' . $permission['action'] . '(Role.' . $permission['role'] . '(' . $argsString . '))';
205+
}
206+
return '[' . implode(', ', $permissions) . ']';
207+
}
190208
}

src/SDK/Language/DotNet.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,11 @@ public function getParamExample(array $param): string
282282
case self::TYPE_FILE:
283283
case self::TYPE_NUMBER:
284284
case self::TYPE_INTEGER:
285-
case self::TYPE_ARRAY:
286285
$output .= $example;
287286
break;
287+
case self::TYPE_ARRAY:
288+
$output .= $this->isPermissionString($example) ? $this->getPermissionExample($example) : $example;
289+
break;
288290
case self::TYPE_OBJECT:
289291
if ($example === '{}') {
290292
$output .= '[object]';
@@ -310,6 +312,23 @@ public function getParamExample(array $param): string
310312
return $output;
311313
}
312314

315+
public function getPermissionExample(string $example): string
316+
{
317+
$permissions = [];
318+
foreach ($this->extractPermissionParts($example) as $permission) {
319+
$args = [];
320+
if ($permission['id'] !== null) {
321+
$args[] = '"' . $permission['id'] . '"';
322+
}
323+
if ($permission['innerRole'] !== null) {
324+
$args[] = '"' . $permission['innerRole'] . '"';
325+
}
326+
$argsString = implode(', ', $args);
327+
$permissions[] = 'Permission.' . ucfirst($permission['action']) . '(Role.' . ucfirst($permission['role']) . '(' . $argsString . '))';
328+
}
329+
return 'new List<string> { ' . implode(', ', $permissions) . ' }';
330+
}
331+
313332
/**
314333
* @return array
315334
*/

src/SDK/Language/Kotlin.php

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -257,13 +257,17 @@ public function getParamExample(array $param): string
257257
$output .= $example;
258258
break;
259259
case self::TYPE_ARRAY:
260-
if (\str_starts_with($example, '[')) {
261-
$example = \substr($example, 1);
262-
}
263-
if (\str_ends_with($example, ']')) {
264-
$example = \substr($example, 0, -1);
260+
if ($this->isPermissionString($example)) {
261+
$output .= $this->getPermissionExample($example);
262+
} else {
263+
if (\str_starts_with($example, '[')) {
264+
$example = \substr($example, 1);
265+
}
266+
if (\str_ends_with($example, ']')) {
267+
$example = \substr($example, 0, -1);
268+
}
269+
$output .= 'listOf(' . $example . ')';
265270
}
266-
$output .= 'listOf(' . $example . ')';
267271
break;
268272
case self::TYPE_BOOLEAN:
269273
$output .= ($example) ? 'true' : 'false';
@@ -277,6 +281,23 @@ public function getParamExample(array $param): string
277281
return $output;
278282
}
279283

284+
public function getPermissionExample(string $example): string
285+
{
286+
$permissions = [];
287+
foreach ($this->extractPermissionParts($example) as $permission) {
288+
$args = [];
289+
if ($permission['id'] !== null) {
290+
$args[] = '"' . $permission['id'] . '"';
291+
}
292+
if ($permission['innerRole'] !== null) {
293+
$args[] = '"' . $permission['innerRole'] . '"';
294+
}
295+
$argsString = implode(', ', $args);
296+
$permissions[] = 'Permission.' . $permission['action'] . '(Role.' . $permission['role'] . '(' . $argsString . '))';
297+
}
298+
return 'listOf(' . implode(', ', $permissions) . ')';
299+
}
300+
280301
/**
281302
* @return array
282303
*/

src/SDK/Language/PHP.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,9 +356,11 @@ public function getParamExample(array $param): string
356356
switch ($type) {
357357
case self::TYPE_NUMBER:
358358
case self::TYPE_INTEGER:
359-
case self::TYPE_ARRAY:
360359
$output .= $example;
361360
break;
361+
case self::TYPE_ARRAY:
362+
$output .= $this->isPermissionString($example) ? $this->getPermissionExample($example) : $example;
363+
break;
362364
case self::TYPE_OBJECT:
363365
$output .= $this->jsonToAssoc(json_decode($example, true));
364366
break;
@@ -377,6 +379,23 @@ public function getParamExample(array $param): string
377379
return $output;
378380
}
379381

382+
public function getPermissionExample(string $example): string
383+
{
384+
$permissions = [];
385+
foreach ($this->extractPermissionParts($example) as $permission) {
386+
$args = [];
387+
if ($permission['id'] !== null) {
388+
$args[] = '"' . $permission['id'] . '"';
389+
}
390+
if ($permission['innerRole'] !== null) {
391+
$args[] = '"' . $permission['innerRole'] . '"';
392+
}
393+
$argsString = implode(', ', $args);
394+
$permissions[] = 'Permission::' . $permission['action'] . '(Role::' . $permission['role'] . '(' . $argsString . '))';
395+
}
396+
return '[' . implode(', ', $permissions) . ']';
397+
}
398+
380399
/**
381400
* Converts JSON Object To PHP Native Assoc Array
382401
*

src/SDK/Language/Python.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,8 @@ public function getParamExample(array $param): string
342342
}
343343

344344
return match ($type) {
345-
self::TYPE_ARRAY, self::TYPE_FILE, self::TYPE_INTEGER, self::TYPE_NUMBER => $example,
345+
self::TYPE_ARRAY => $this->isPermissionString($example) ? $this->getPermissionExample($example) : $example,
346+
self::TYPE_FILE, self::TYPE_INTEGER, self::TYPE_NUMBER => $example,
346347
self::TYPE_BOOLEAN => ($example) ? 'True' : 'False',
347348
self::TYPE_OBJECT => ($example === '{}')
348349
? '{}'
@@ -353,6 +354,23 @@ public function getParamExample(array $param): string
353354
};
354355
}
355356

357+
public function getPermissionExample(string $example): string
358+
{
359+
$permissions = [];
360+
foreach ($this->extractPermissionParts($example) as $permission) {
361+
$args = [];
362+
if ($permission['id'] !== null) {
363+
$args[] = '"' . $permission['id'] . '"';
364+
}
365+
if ($permission['innerRole'] !== null) {
366+
$args[] = '"' . $permission['innerRole'] . '"';
367+
}
368+
$argsString = implode(', ', $args);
369+
$permissions[] = 'Permission.' . $permission['action'] . '(Role.' . $permission['role'] . '(' . $argsString . '))';
370+
}
371+
return '[' . implode(', ', $permissions) . ']';
372+
}
373+
356374
public function getFilters(): array
357375
{
358376
return [

src/SDK/Language/Ruby.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,9 +302,11 @@ public function getParamExample(array $param): string
302302
switch ($type) {
303303
case self::TYPE_NUMBER:
304304
case self::TYPE_INTEGER:
305-
case self::TYPE_ARRAY:
306305
$output .= $example;
307306
break;
307+
case self::TYPE_ARRAY:
308+
$output .= $this->isPermissionString($example) ? $this->getPermissionExample($example) : $example;
309+
break;
308310
case self::TYPE_OBJECT:
309311
$output .= $this->jsonToHash(json_decode($example, true));
310312
break;
@@ -323,6 +325,23 @@ public function getParamExample(array $param): string
323325
return $output;
324326
}
325327

328+
public function getPermissionExample(string $example): string
329+
{
330+
$permissions = [];
331+
foreach ($this->extractPermissionParts($example) as $permission) {
332+
$args = [];
333+
if ($permission['id'] !== null) {
334+
$args[] = "'" . $permission['id'] . "'";
335+
}
336+
if ($permission['innerRole'] !== null) {
337+
$args[] = "'" . $permission['innerRole'] . "'";
338+
}
339+
$argsString = implode(', ', $args);
340+
$permissions[] = 'Permission.' . $permission['action'] . '(Role.' . $permission['role'] . '(' . $argsString . '))';
341+
}
342+
return '[' . implode(', ', $permissions) . ']';
343+
}
344+
326345
/**
327346
* Converts JSON Object To Ruby Native Hash
328347
*

src/SDK/Language/Swift.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,9 +425,11 @@ public function getParamExample(array $param): string
425425
case self::TYPE_FILE:
426426
case self::TYPE_NUMBER:
427427
case self::TYPE_INTEGER:
428-
case self::TYPE_ARRAY:
429428
$output .= $example;
430429
break;
430+
case self::TYPE_ARRAY:
431+
$output .= $this->isPermissionString($example) ? $this->getPermissionExample($example) : $example;
432+
break;
431433
case self::TYPE_BOOLEAN:
432434
$output .= ($example) ? 'true' : 'false';
433435
break;
@@ -448,6 +450,23 @@ public function getParamExample(array $param): string
448450
return $output;
449451
}
450452

453+
public function getPermissionExample(string $example): string
454+
{
455+
$permissions = [];
456+
foreach ($this->extractPermissionParts($example) as $permission) {
457+
$args = [];
458+
if ($permission['id'] !== null) {
459+
$args[] = '"' . $permission['id'] . '"';
460+
}
461+
if ($permission['innerRole'] !== null) {
462+
$args[] = '"' . $permission['innerRole'] . '"';
463+
}
464+
$argsString = implode(', ', $args);
465+
$permissions[] = 'Permission.' . $permission['action'] . '(Role.' . $permission['role'] . '(' . $argsString . '))';
466+
}
467+
return '[' . implode(', ', $permissions) . ']';
468+
}
469+
451470
/**
452471
* Converts JSON Object To Swift Native Dictionary
453472
*

src/SDK/Language/Web.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ public function getParamExample(array $param): string
150150
}
151151

152152
return match ($type) {
153-
self::TYPE_ARRAY, self::TYPE_INTEGER, self::TYPE_NUMBER => $example,
153+
self::TYPE_ARRAY => $this->isPermissionString($example) ? $this->getPermissionExample($example) : $example,
154+
self::TYPE_INTEGER, self::TYPE_NUMBER => $example,
154155
self::TYPE_FILE => 'document.getElementById(\'uploader\').files[0]',
155156
self::TYPE_BOOLEAN => ($example) ? 'true' : 'false',
156157
self::TYPE_OBJECT => ($example === '{}')
@@ -162,6 +163,23 @@ public function getParamExample(array $param): string
162163
};
163164
}
164165

166+
public function getPermissionExample(string $example): string
167+
{
168+
$permissions = [];
169+
foreach ($this->extractPermissionParts($example) as $permission) {
170+
$args = [];
171+
if ($permission['id'] !== null) {
172+
$args[] = "'" . $permission['id'] . "'";
173+
}
174+
if ($permission['innerRole'] !== null) {
175+
$args[] = "'" . $permission['innerRole'] . "'";
176+
}
177+
$argsString = implode(', ', $args);
178+
$permissions[] = 'Permission.' . $permission['action'] . '(Role.' . $permission['role'] . '(' . $argsString . '))';
179+
}
180+
return '[' . implode(', ', $permissions) . ']';
181+
}
182+
165183
public function getReadOnlyProperties(array $parameter, string $responseModel, array $spec = []): array
166184
{
167185
$properties = [];

0 commit comments

Comments
 (0)