Add permissions process function to add default
This commit is contained in:
parent
bed6eb70af
commit
08609af053
1 changed files with 28 additions and 3 deletions
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue