Skip to content

Commit 4dd6f63

Browse files
committed
Use Auth method to simplify code
1 parent 85c2078 commit 4dd6f63

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

app/controllers/api/database.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1577,14 +1577,15 @@ function createAttribute($collectionId, $attribute, $response, $dbForInternal, $
15771577
$data['$write'] = (is_null($write) && !$user->isEmpty()) ? ['user:'.$user->getId()] : $write ?? []; // By default set write permissions for user
15781578

15791579
// Users can only add their roles to documents, API keys can add any
1580+
$roles = Authorization::getRoles();
15801581
foreach ($data['$read'] as $read) {
1581-
if (!Authorization::isRole('role:'.Auth::USER_ROLE_APP) && !Authorization::isRole($read)) {
1582-
throw new Exception('Read permissions must be one of: ('.\implode(', ', Authorization::getRoles()).')', 400);
1582+
if (!Auth::isAppUser($roles) && !Authorization::isRole($read)) {
1583+
throw new Exception('Read permissions must be one of: ('.\implode(', ', $roles).')', 400);
15831584
}
15841585
}
15851586
foreach ($data['$write'] as $write) {
1586-
if (!Authorization::isRole('role:'.Auth::USER_ROLE_APP) && !Authorization::isRole($write)) {
1587-
throw new Exception('Write permissions must be one of: ('.\implode(', ', Authorization::getRoles()).')', 400);
1587+
if (!Auth::isAppUser($roles) && !Authorization::isRole($write)) {
1588+
throw new Exception('Write permissions must be one of: ('.\implode(', ', $roles).')', 400);
15881589
}
15891590
}
15901591

@@ -1827,14 +1828,15 @@ function createAttribute($collectionId, $attribute, $response, $dbForInternal, $
18271828
$data['$write'] = (is_null($write)) ? ($document->getWrite() ?? []) : $write; // By default inherit write permissions
18281829

18291830
// Users can only add their roles to documents, API keys can add any
1831+
$roles = Authorization::getRoles();
18301832
foreach ($data['$read'] as $read) {
1831-
if (!Authorization::isRole('role:'.Auth::USER_ROLE_APP) && !Authorization::isRole($read)) {
1832-
throw new Exception('Read permissions must be one of: ('.\implode(', ', Authorization::getRoles()).')', 400);
1833+
if (!Auth::isAppUser($roles) && !Authorization::isRole($read)) {
1834+
throw new Exception('Read permissions must be one of: ('.\implode(', ', $roles).')', 400);
18331835
}
18341836
}
18351837
foreach ($data['$write'] as $write) {
1836-
if (!Authorization::isRole('role:'.Auth::USER_ROLE_APP) && !Authorization::isRole($write)) {
1837-
throw new Exception('Write permissions must be one of: ('.\implode(', ', Authorization::getRoles()).')', 400);
1838+
if (!Auth::isAppUser($roles) && !Authorization::isRole($write)) {
1839+
throw new Exception('Write permissions must be one of: ('.\implode(', ', $roles).')', 400);
18381840
}
18391841
}
18401842

0 commit comments

Comments
 (0)