|
26 | 26 | use Utopia\Database\Exception\Duplicate as DuplicateException;
|
27 | 27 | use Utopia\Database\Exception\Limit as LimitException;
|
28 | 28 | use Utopia\Database\Exception\Structure as StructureException;
|
| 29 | +use Appwrite\Auth\Auth; |
29 | 30 | use Appwrite\Database\Validator\CustomId;
|
30 | 31 | use Appwrite\Network\Validator\Email;
|
31 | 32 | use Appwrite\Network\Validator\IP;
|
@@ -1575,6 +1576,18 @@ function createAttribute($collectionId, $attribute, $response, $dbForInternal, $
|
1575 | 1576 | $data['$read'] = (is_null($read) && !$user->isEmpty()) ? ['user:'.$user->getId()] : $read ?? []; // By default set read permissions for user
|
1576 | 1577 | $data['$write'] = (is_null($write) && !$user->isEmpty()) ? ['user:'.$user->getId()] : $write ?? []; // By default set write permissions for user
|
1577 | 1578 |
|
| 1579 | + // Users can only add their roles to documents, API keys can add any |
| 1580 | + 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); |
| 1583 | + } |
| 1584 | + } |
| 1585 | + 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); |
| 1588 | + } |
| 1589 | + } |
| 1590 | + |
1578 | 1591 | try {
|
1579 | 1592 | if ($collection->getAttribute('permission') === 'collection') {
|
1580 | 1593 | /** @var Document $document */
|
@@ -1813,6 +1826,18 @@ function createAttribute($collectionId, $attribute, $response, $dbForInternal, $
|
1813 | 1826 | $data['$read'] = (is_null($read)) ? ($document->getRead() ?? []) : $read; // By default inherit read permissions
|
1814 | 1827 | $data['$write'] = (is_null($write)) ? ($document->getWrite() ?? []) : $write; // By default inherit write permissions
|
1815 | 1828 |
|
| 1829 | + // Users can only add their roles to documents, API keys can add any |
| 1830 | + 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 | + } |
| 1834 | + } |
| 1835 | + 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 | + } |
| 1839 | + } |
| 1840 | + |
1816 | 1841 | try {
|
1817 | 1842 | if ($collection->getAttribute('permission') === 'collection') {
|
1818 | 1843 | /** @var Document $document */
|
|
0 commit comments