diff --git a/src/Appwrite/Permissions/PermissionsProcessor.php b/src/Appwrite/Permissions/PermissionsProcessor.php index 075ec7037b..82aed1b61a 100644 --- a/src/Appwrite/Permissions/PermissionsProcessor.php +++ b/src/Appwrite/Permissions/PermissionsProcessor.php @@ -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; } -} \ No newline at end of file + + 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; + } +}