@@ -1139,140 +1139,55 @@ public function updateDocument(Document $collection, string $id, Document $docum
11391139 $ attributes ['_createdAt ' ] = $ document ->getCreatedAt ();
11401140 $ attributes ['_updatedAt ' ] = $ document ->getUpdatedAt ();
11411141 $ attributes ['_permissions ' ] = json_encode ($ document ->getPermissions ());
1142+ $ attributes ['_uid ' ] = $ document ->getId ();
11421143
11431144 $ name = $ this ->filter ($ collection );
11441145 $ columns = '' ;
11451146
11461147 if (!$ skipPermissions ) {
1148+ $ newUid = $ document ->offsetExists ('$id ' ) ? $ document ->getId () : $ id ;
1149+
11471150 $ sql = "
1148- SELECT _type, _permission
1149- FROM {$ this ->getSQLTable ($ name . '_perms ' )}
1151+ DELETE FROM {$ this ->getSQLTable ($ name . '_perms ' )}
11501152 WHERE _document = :_uid
11511153 {$ this ->getTenantQuery ($ collection )}
11521154 " ;
11531155
1154- $ sql = $ this ->trigger (Database::EVENT_PERMISSIONS_READ , $ sql );
1155-
1156- /**
1157- * Get current permissions from the database
1158- */
1159- $ permissionsStmt = $ this ->getPDO ()->prepare ($ sql );
1160- $ permissionsStmt ->bindValue (':_uid ' , $ document ->getId ());
1156+ $ sql = $ this ->trigger (Database::EVENT_PERMISSIONS_DELETE , $ sql );
11611157
1158+ $ stmtRemovePermissions = $ this ->getPDO ()->prepare ($ sql );
1159+ $ stmtRemovePermissions ->bindValue (':_uid ' , $ id );
11621160 if ($ this ->sharedTables ) {
1163- $ permissionsStmt ->bindValue (':_tenant ' , $ this ->tenant );
1161+ $ stmtRemovePermissions ->bindValue (':_tenant ' , $ this ->tenant );
11641162 }
11651163
1166- $ this ->execute ($ permissionsStmt );
1167- $ permissions = $ permissionsStmt ->fetchAll ();
1168- $ permissionsStmt ->closeCursor ();
1169-
1170- $ initial = [];
1164+ $ values = [];
1165+ $ binds = [];
11711166 foreach (Database::PERMISSIONS as $ type ) {
1172- $ initial [$ type ] = [];
1173- }
1174-
1175- $ permissions = array_reduce ($ permissions , function (array $ carry , array $ item ) {
1176- $ carry [$ item ['_type ' ]][] = $ item ['_permission ' ];
1177-
1178- return $ carry ;
1179- }, $ initial );
1180-
1181- /**
1182- * Get removed Permissions
1183- */
1184- $ removals = [];
1185- foreach (Database::PERMISSIONS as $ type ) {
1186- $ diff = \array_diff ($ permissions [$ type ], $ document ->getPermissionsByType ($ type ));
1187- if (!empty ($ diff )) {
1188- $ removals [$ type ] = $ diff ;
1189- }
1190- }
1191-
1192- /**
1193- * Get added Permissions
1194- */
1195- $ additions = [];
1196- foreach (Database::PERMISSIONS as $ type ) {
1197- $ diff = \array_diff ($ document ->getPermissionsByType ($ type ), $ permissions [$ type ]);
1198- if (!empty ($ diff )) {
1199- $ additions [$ type ] = $ diff ;
1200- }
1201- }
1202-
1203- /**
1204- * Query to remove permissions
1205- */
1206- $ removeQuery = '' ;
1207- if (!empty ($ removals )) {
1208- $ removeQuery = ' AND ( ' ;
1209- foreach ($ removals as $ type => $ permissions ) {
1210- $ removeQuery .= "(
1211- _type = ' {$ type }'
1212- AND _permission IN ( " . implode (', ' , \array_map (fn (string $ i ) => ":_remove_ {$ type }_ {$ i }" , \array_keys ($ permissions ))) . ")
1213- ) " ;
1214- if ($ type !== \array_key_last ($ removals )) {
1215- $ removeQuery .= ' OR ' ;
1216- }
1167+ foreach ($ document ->getPermissionsByType ($ type ) as $ i => $ permission ) {
1168+ $ sqlTenant = $ this ->sharedTables ? ', :_tenant ' : '' ;
1169+ $ values [] = "( :_uid, ' {$ type }', :_add_ {$ type }_ {$ i } {$ sqlTenant }) " ;
1170+ $ binds [":_add_ {$ type }_ {$ i }" ] = $ permission ;
12171171 }
12181172 }
1219- if (!empty ($ removeQuery )) {
1220- $ removeQuery .= ') ' ;
1221-
1222- $ sql = "
1223- DELETE
1224- FROM {$ this ->getSQLTable ($ name . '_perms ' )}
1225- WHERE _document = :_uid
1226- {$ this ->getTenantQuery ($ collection )}
1227- " ;
1228-
1229- $ removeQuery = $ sql . $ removeQuery ;
1230-
1231- $ removeQuery = $ this ->trigger (Database::EVENT_PERMISSIONS_DELETE , $ removeQuery );
1232- $ stmtRemovePermissions = $ this ->getPDO ()->prepare ($ removeQuery );
1233- $ stmtRemovePermissions ->bindValue (':_uid ' , $ document ->getId ());
1234-
1235- if ($ this ->sharedTables ) {
1236- $ stmtRemovePermissions ->bindValue (':_tenant ' , $ this ->tenant );
1237- }
1238-
1239- foreach ($ removals as $ type => $ permissions ) {
1240- foreach ($ permissions as $ i => $ permission ) {
1241- $ stmtRemovePermissions ->bindValue (":_remove_ {$ type }_ {$ i }" , $ permission );
1242- }
1243- }
1244- }
1245-
1246- /**
1247- * Query to add permissions
1248- */
1249- if (!empty ($ additions )) {
1250- $ values = [];
1251- foreach ($ additions as $ type => $ permissions ) {
1252- foreach ($ permissions as $ i => $ _ ) {
1253- $ sqlTenant = $ this ->sharedTables ? ', :_tenant ' : '' ;
1254- $ values [] = "( :_uid, ' {$ type }', :_add_ {$ type }_ {$ i } {$ sqlTenant }) " ;
1255- }
1256- }
12571173
1174+ if (!empty ($ values )) {
12581175 $ sqlTenant = $ this ->sharedTables ? ', _tenant ' : '' ;
12591176
12601177 $ sql = "
12611178 INSERT INTO {$ this ->getSQLTable ($ name . '_perms ' )} (_document, _type, _permission {$ sqlTenant })
1262- VALUES " . \implode (', ' , $ values );
1179+ VALUES " . \implode (', ' , $ values );
12631180
12641181 $ sql = $ this ->trigger (Database::EVENT_PERMISSIONS_CREATE , $ sql );
12651182
12661183 $ stmtAddPermissions = $ this ->getPDO ()->prepare ($ sql );
1267- $ stmtAddPermissions ->bindValue (":_uid " , $ document -> getId () );
1184+ $ stmtAddPermissions ->bindValue (":_uid " , $ newUid );
12681185 if ($ this ->sharedTables ) {
12691186 $ stmtAddPermissions ->bindValue (':_tenant ' , $ this ->tenant );
12701187 }
12711188
1272- foreach ($ additions as $ type => $ permissions ) {
1273- foreach ($ permissions as $ i => $ permission ) {
1274- $ stmtAddPermissions ->bindValue (":_add_ {$ type }_ {$ i }" , $ permission );
1275- }
1189+ foreach ($ binds as $ key => $ permission ) {
1190+ $ stmtAddPermissions ->bindValue ($ key , $ permission );
12761191 }
12771192 }
12781193 }
@@ -1312,7 +1227,7 @@ public function updateDocument(Document $collection, string $id, Document $docum
13121227
13131228 $ sql = "
13141229 UPDATE {$ this ->getSQLTable ($ name )}
1315- SET { $ columns} _uid = :_newUid
1230+ SET " . \rtrim ( $ columns, ' , ' ) . "
13161231 WHERE _id=:_sequence
13171232 {$ this ->getTenantQuery ($ collection )}
13181233 " ;
@@ -1322,7 +1237,6 @@ public function updateDocument(Document $collection, string $id, Document $docum
13221237 $ stmt = $ this ->getPDO ()->prepare ($ sql );
13231238
13241239 $ stmt ->bindValue (':_sequence ' , $ document ->getSequence ());
1325- $ stmt ->bindValue (':_newUid ' , $ document ->getId ());
13261240
13271241 if ($ this ->sharedTables ) {
13281242 $ stmt ->bindValue (':_tenant ' , $ this ->tenant );
0 commit comments