@@ -4333,9 +4333,8 @@ public function createDocuments(
43334333 $ batchSize = \min (Database::INSERT_BATCH_SIZE , \max (1 , $ batchSize ));
43344334 $ collection = $ this ->silent (fn () => $ this ->getCollection ($ collection ));
43354335 if ($ collection ->getId () !== self ::METADATA ) {
4336- $ authorization = new Authorization (self ::PERMISSION_CREATE );
4337- if (!$ authorization ->isValid ($ collection ->getCreate ())) {
4338- throw new AuthorizationException ($ authorization ->getDescription ());
4336+ if (!$ this ->authorization ->isValid (new Input (self ::PERMISSION_CREATE , $ collection ->getCreate ()))) {
4337+ throw new AuthorizationException ($ this ->authorization ->getDescription ());
43394338 }
43404339 }
43414340
@@ -4886,26 +4885,31 @@ public function updateDocument(string $collection, string $id, Document $documen
48864885 break ;
48874886 }
48884887 }
4889- }
4890-
4891- if ($ shouldUpdate ) {
48924888
4893- $ isUpdateValid = $ this -> authorization -> isValid ( new Input ( self :: PERMISSION_UPDATE , [
4894- ... $ collection ->getUpdate (),
4895- ...( $ documentSecurity ? $ old ->getUpdate () : [])
4896- ]) );
4889+ $ updatePermissions = array_merge (
4890+ $ collection ->getUpdate (),
4891+ $ documentSecurity ? $ old ->getUpdate () : []
4892+ );
48974893
4898- $ isReadValid = $ this -> authorization -> isValid ( new Input ( self :: PERMISSION_READ , [
4899- ... $ collection ->getRead (),
4900- ...( $ documentSecurity ? $ old ->getRead () : [])
4901- ]) );
4894+ $ readPermissions = array_merge (
4895+ $ collection ->getRead (),
4896+ $ documentSecurity ? $ old ->getRead () : []
4897+ );
49024898
4903- if (!$ isReadValid || !$ isUpdateValid ) {
4904- throw new AuthorizationException ($ this ->authorization ->getDescription ());
4899+ if ($ shouldUpdate ) {
4900+ if (!$ this ->authorization ->isValid (new Input (self ::PERMISSION_UPDATE , $ updatePermissions ))) {
4901+ throw new AuthorizationException ($ this ->authorization ->getDescription ());
4902+ }
4903+ } else {
4904+ if (!$ this ->authorization ->isValid (new Input (self ::PERMISSION_READ , $ readPermissions ))){
4905+ throw new AuthorizationException ($ this ->authorization ->getDescription ());
4906+ }
49054907 }
4906-
4907- $ document ->setAttribute ('$updatedAt ' , ($ newUpdatedAt === null || !$ this ->preserveDates ) ? $ time : $ newUpdatedAt );
49084908 }
4909+
4910+ if ($ shouldUpdate ) {
4911+ $ document ->setAttribute ('$updatedAt ' , ($ newUpdatedAt === null || !$ this ->preserveDates ) ? $ time : $ newUpdatedAt );
4912+ }
49094913
49104914 // Check if document was updated after the request timestamp
49114915 $ oldUpdatedAt = new \DateTime ($ old ->getUpdatedAt ());
@@ -4991,11 +4995,10 @@ public function updateDocuments(
49914995 }
49924996
49934997 $ documentSecurity = $ collection ->getAttribute ('documentSecurity ' , false );
4994- $ authorization = new Authorization (self ::PERMISSION_UPDATE );
4995- $ skipAuth = $ authorization ->isValid ($ collection ->getUpdate ());
4998+ $ skipAuth = $ this ->authorization ->isValid (new Input (self ::PERMISSION_UPDATE , $ collection ->getUpdate ()));
49964999
49975000 if (!$ skipAuth && !$ documentSecurity && $ collection ->getId () !== self ::METADATA ) {
4998- throw new AuthorizationException ($ authorization ->getDescription ());
5001+ throw new AuthorizationException ($ this -> authorization ->getDescription ());
49995002 }
50005003
50015004 $ attributes = $ collection ->getAttribute ('attributes ' , []);
@@ -6070,7 +6073,7 @@ public function deleteDocument(string $collection, string $id): bool
60706073 if ($ collection ->getId () !== self ::METADATA ) {
60716074 $ documentSecurity = $ collection ->getAttribute ('documentSecurity ' , false );
60726075
6073- if (!$ this ->authorization ->isValid (new Input ([
6076+ if (!$ this ->authorization ->isValid (new Input (self :: PERMISSION_DELETE , [
60746077 ...$ collection ->getDelete (),
60756078 ...($ documentSecurity ? $ document ->getDelete () : [])
60766079 ]))) {
@@ -6522,11 +6525,10 @@ public function deleteDocuments(
65226525 }
65236526
65246527 $ documentSecurity = $ collection ->getAttribute ('documentSecurity ' , false );
6525- $ authorization = new Authorization (self ::PERMISSION_DELETE );
6526- $ skipAuth = $ authorization ->isValid ($ collection ->getDelete ());
6528+ $ skipAuth = $ this ->authorization ->isValid (new Input (self ::PERMISSION_DELETE , $ collection ->getDelete ()));
65276529
65286530 if (!$ skipAuth && !$ documentSecurity && $ collection ->getId () !== self ::METADATA ) {
6529- throw new AuthorizationException ($ authorization ->getDescription ());
6531+ throw new AuthorizationException ($ this -> authorization ->getDescription ());
65306532 }
65316533
65326534 $ attributes = $ collection ->getAttribute ('attributes ' , []);
@@ -6745,12 +6747,12 @@ public function find(string $collection, array $queries = [], string $forPermiss
67456747 }
67466748 }
67476749
6748- $ authorization = new Authorization ($ forPermission );
67496750 $ documentSecurity = $ collection ->getAttribute ('documentSecurity ' , false );
6750- $ skipAuth = $ authorization ->isValid ($ collection ->getPermissionsByType ($ forPermission ));
6751+ $ skipAuth = $ this ->authorization ->isValid (new Input ($ forPermission , $ collection ->getPermissionsByType ($ forPermission )));
6752+
67516753
67526754 if (!$ skipAuth && !$ documentSecurity && $ collection ->getId () !== self ::METADATA ) {
6753- throw new AuthorizationException ($ authorization ->getDescription ());
6755+ throw new AuthorizationException ($ this -> authorization ->getDescription ());
67546756 }
67556757
67566758 $ relationships = \array_filter (
@@ -6825,7 +6827,7 @@ public function find(string $collection, array $queries = [], string $forPermiss
68256827 $ cursorDirection ,
68266828 $ forPermission
68276829 );
6828-
6830+
68296831 $ results = $ skipAuth ? $ this ->authorization ->skip ($ getResults ) : $ getResults ();
68306832 }
68316833
@@ -6968,11 +6970,7 @@ public function count(string $collection, array $queries = [], ?int $max = null)
69686970 }
69696971 }
69706972
6971- $ authorization = new Authorization (self ::PERMISSION_READ );
6972- if ($ authorization ->isValid ($ collection ->getRead ())) {
6973- $ skipAuth = true ;
6974- }
6975-
6973+ $ skipAuth = $ this ->authorization ->isValid (new Input (self ::PERMISSION_READ , $ collection ->getRead ()));
69766974 $ relationships = \array_filter (
69776975 $ collection ->getAttribute ('attributes ' , []),
69786976 fn (Document $ attribute ) => $ attribute ->getAttribute ('type ' ) === self ::VAR_RELATIONSHIP
0 commit comments