@@ -980,155 +980,55 @@ public function updateDocument(Document $collection, string $id, Document $docum
980980 $ attributes ['_createdAt ' ] = $ document ->getCreatedAt ();
981981 $ attributes ['_updatedAt ' ] = $ document ->getUpdatedAt ();
982982 $ attributes ['_permissions ' ] = json_encode ($ document ->getPermissions ());
983+ $ attributes ['_uid ' ] = $ document ->getId ();
983984
984985 $ name = $ this ->filter ($ collection );
985986 $ columns = '' ;
986987
987988 if (!$ skipPermissions ) {
989+ $ newUid = $ document ->offsetExists ('$id ' ) ? $ document ->getId () : $ id ;
990+
988991 $ sql = "
989- SELECT _type, _permission
990- FROM {$ this ->getSQLTable ($ name . '_perms ' )}
992+ DELETE FROM {$ this ->getSQLTable ($ name . '_perms ' )}
991993 WHERE _document = :_uid
992994 {$ this ->getTenantQuery ($ collection )}
993- " ;
995+ " ;
994996
995- $ sql = $ this ->trigger (Database::EVENT_PERMISSIONS_READ , $ sql );
996-
997- /**
998- * Get current permissions from the database
999- */
1000- $ sqlPermissions = $ this ->getPDO ()->prepare ($ sql );
1001- $ sqlPermissions ->bindValue (':_uid ' , $ document ->getId ());
997+ $ sql = $ this ->trigger (Database::EVENT_PERMISSIONS_DELETE , $ sql );
1002998
999+ $ stmtRemovePermissions = $ this ->getPDO ()->prepare ($ sql );
1000+ $ stmtRemovePermissions ->bindValue (':_uid ' , $ id );
10031001 if ($ this ->sharedTables ) {
1004- $ sqlPermissions ->bindValue (':_tenant ' , $ this ->tenant );
1002+ $ stmtRemovePermissions ->bindValue (':_tenant ' , $ this ->tenant );
10051003 }
10061004
1007- $ sqlPermissions ->execute ();
1008- $ permissions = $ sqlPermissions ->fetchAll ();
1009- $ sqlPermissions ->closeCursor ();
1010-
1011- $ initial = [];
1012- foreach (Database::PERMISSIONS as $ type ) {
1013- $ initial [$ type ] = [];
1014- }
1015-
1016- $ permissions = array_reduce ($ permissions , function (array $ carry , array $ item ) {
1017- $ carry [$ item ['_type ' ]][] = $ item ['_permission ' ];
1018-
1019- return $ carry ;
1020- }, $ initial );
1021-
1022- /**
1023- * Get removed Permissions
1024- */
1025- $ removals = [];
1005+ $ values = [];
1006+ $ binds = [];
10261007 foreach (Database::PERMISSIONS as $ type ) {
1027- $ diff = \array_diff ($ permissions [$ type ], $ document ->getPermissionsByType ($ type ));
1028- if (!empty ($ diff )) {
1029- $ removals [$ type ] = $ diff ;
1030- }
1031- }
1032-
1033- /**
1034- * Get added Permissions
1035- */
1036- $ additions = [];
1037- foreach (Database::PERMISSIONS as $ type ) {
1038- $ diff = \array_diff ($ document ->getPermissionsByType ($ type ), $ permissions [$ type ]);
1039- if (!empty ($ diff )) {
1040- $ additions [$ type ] = $ diff ;
1041- }
1042- }
1043-
1044- /**
1045- * Query to remove permissions
1046- */
1047- $ removeQuery = '' ;
1048- if (!empty ($ removals )) {
1049- $ removeQuery = ' AND ( ' ;
1050- foreach ($ removals as $ type => $ permissions ) {
1051- $ removeQuery .= "(
1052- _type = ' {$ type }'
1053- AND _permission IN ( " . implode (', ' , \array_map (fn (string $ i ) => ":_remove_ {$ type }_ {$ i }" , \array_keys ($ permissions ))) . ")
1054- ) " ;
1055- if ($ type !== \array_key_last ($ removals )) {
1056- $ removeQuery .= ' OR ' ;
1057- }
1008+ foreach ($ document ->getPermissionsByType ($ type ) as $ i => $ permission ) {
1009+ $ tenantPlaceholder = $ this ->sharedTables ? ', :_tenant ' : '' ;
1010+ $ values [] = "( :_uid, ' {$ type }', :_add_ {$ type }_ {$ i } {$ tenantPlaceholder }) " ;
1011+ $ binds [":_add_ {$ type }_ {$ i }" ] = $ permission ;
10581012 }
10591013 }
1060- if (!empty ($ removeQuery )) {
1061- $ removeQuery .= ') ' ;
1062- $ sql = "
1063- DELETE
1064- FROM {$ this ->getSQLTable ($ name . '_perms ' )}
1065- WHERE _document = :_uid
1066- {$ this ->getTenantQuery ($ collection )}
1067- " ;
1068-
1069- $ removeQuery = $ sql . $ removeQuery ;
10701014
1071- $ removeQuery = $ this ->trigger (Database::EVENT_PERMISSIONS_DELETE , $ removeQuery );
1072-
1073- $ stmtRemovePermissions = $ this ->getPDO ()->prepare ($ removeQuery );
1074- $ stmtRemovePermissions ->bindValue (':_uid ' , $ document ->getId ());
1075-
1076- if ($ this ->sharedTables ) {
1077- $ stmtRemovePermissions ->bindValue (':_tenant ' , $ this ->tenant );
1078- }
1079-
1080- foreach ($ removals as $ type => $ permissions ) {
1081- foreach ($ permissions as $ i => $ permission ) {
1082- $ stmtRemovePermissions ->bindValue (":_remove_ {$ type }_ {$ i }" , $ permission );
1083- }
1084- }
1085- }
1086-
1087- /**
1088- * Query to add permissions
1089- */
1090- if (!empty ($ additions )) {
1091- $ values = [];
1092- foreach ($ additions as $ type => $ permissions ) {
1093- foreach ($ permissions as $ i => $ _ ) {
1094- $ value = "( :_uid, ' {$ type }', :_add_ {$ type }_ {$ i }" ;
1095-
1096- if ($ this ->sharedTables ) {
1097- $ value .= ", :_tenant) " ;
1098- } else {
1099- $ value .= ") " ;
1100- }
1101-
1102- $ values [] = $ value ;
1103- }
1104- }
1015+ if (!empty ($ values )) {
1016+ $ tenantColumn = $ this ->sharedTables ? ', _tenant ' : '' ;
11051017
11061018 $ sql = "
1107- INSERT INTO {$ this ->getSQLTable ($ name . '_perms ' )} (_document, _type, _permission
1108- " ;
1109-
1110- if ($ this ->sharedTables ) {
1111- $ sql .= ', _tenant) ' ;
1112- } else {
1113- $ sql .= ') ' ;
1114- }
1115-
1116- $ sql .= " VALUES " . \implode (', ' , $ values );
1019+ INSERT INTO {$ this ->getSQLTable ($ name . '_perms ' )} (_document, _type, _permission {$ tenantColumn })
1020+ VALUES " . \implode (', ' , $ values );
11171021
11181022 $ sql = $ this ->trigger (Database::EVENT_PERMISSIONS_CREATE , $ sql );
11191023
11201024 $ stmtAddPermissions = $ this ->getPDO ()->prepare ($ sql );
1121-
1122- $ stmtAddPermissions ->bindValue (":_uid " , $ document ->getId ());
1123-
1025+ $ stmtAddPermissions ->bindValue (":_uid " , $ newUid );
11241026 if ($ this ->sharedTables ) {
11251027 $ stmtAddPermissions ->bindValue (":_tenant " , $ this ->tenant );
11261028 }
11271029
1128- foreach ($ additions as $ type => $ permissions ) {
1129- foreach ($ permissions as $ i => $ permission ) {
1130- $ stmtAddPermissions ->bindValue (":_add_ {$ type }_ {$ i }" , $ permission );
1131- }
1030+ foreach ($ binds as $ key => $ permission ) {
1031+ $ stmtAddPermissions ->bindValue ($ key , $ permission );
11321032 }
11331033 }
11341034 }
@@ -1168,7 +1068,7 @@ public function updateDocument(Document $collection, string $id, Document $docum
11681068
11691069 $ sql = "
11701070 UPDATE {$ this ->getSQLTable ($ name )}
1171- SET { $ columns} _uid = :_newUid
1071+ SET " . \rtrim ( $ columns, ' , ' ) . "
11721072 WHERE _id=:_sequence
11731073 {$ this ->getTenantQuery ($ collection )}
11741074 " ;
@@ -1178,7 +1078,6 @@ public function updateDocument(Document $collection, string $id, Document $docum
11781078 $ stmt = $ this ->getPDO ()->prepare ($ sql );
11791079
11801080 $ stmt ->bindValue (':_sequence ' , $ document ->getSequence ());
1181- $ stmt ->bindValue (':_newUid ' , $ document ->getId ());
11821081
11831082 if ($ this ->sharedTables ) {
11841083 $ stmt ->bindValue (':_tenant ' , $ this ->tenant );
0 commit comments