1
0
Fork 0
mirror of synced 2024-09-28 07:21:35 +12:00

Add permissions process function to add default

This commit is contained in:
Jake Barnby 2022-08-05 17:24:11 +12:00
parent bed6eb70af
commit 08609af053

View file

@ -2,15 +2,17 @@
namespace Appwrite\Permissions;
use Utopia\Database\Database;
class PermissionsProcessor
{
public static function processAggregatePermissions(array $permissions): array
public static function handleAggregates(array $permissions): array
{
$aggregates = [
'admin' => ['create', 'update', 'delete', 'read',],
'write' => ['create', 'update', 'delete',],
];
foreach($permissions as $i => $permission) {
foreach ($permissions as $i => $permission) {
foreach ($aggregates as $type => $subTypes) {
if (!\str_starts_with($permission, $type)) {
continue;
@ -22,5 +24,28 @@ class PermissionsProcessor
unset($permissions[$i]);
}
}
return $permissions;
}
}
public static function addDefaultsIfNeeded(?array $permissions, string $userId): array
{
if (\is_null($permissions)) {
$permissions = [];
if (!empty($userId)) {
$permissions = [
'read(user:' . $userId . ') ',
'create(user:' . $userId . ') ',
'update(user:' . $userId . ') ',
'delete(user:' . $userId . ') ',
];
}
return $permissions;
}
foreach (Database::PERMISSIONS as $permission) {
if (empty(\preg_grep("#^{$permission}\(.+\)$#", $permissions)) && !empty($userId)) {
$permissions[] = $permission . '(user:' . $userId . ')';
}
}
return $permissions;
}
}