@@ -925,13 +925,14 @@ public function createDocument(string $collection, Document $document): Document
925925 * @param string $collection
926926 * @param string $id
927927 * @param Document $document
928+ * @param bool $skipPermissions
928929 * @return Document
929930 * @throws Exception
930931 * @throws PDOException
931932 * @throws DuplicateException
932933 * @throws \Throwable
933934 */
934- public function updateDocument (string $ collection , string $ id , Document $ document ): Document
935+ public function updateDocument (string $ collection , string $ id , Document $ document, bool $ skipPermissions ): Document
935936 {
936937 try {
937938 $ attributes = $ document ->getAttributes ();
@@ -942,149 +943,151 @@ public function updateDocument(string $collection, string $id, Document $documen
942943 $ name = $ this ->filter ($ collection );
943944 $ columns = '' ;
944945
945- $ sql = "
946+ if (!$ skipPermissions ) {
947+ $ sql = "
946948 SELECT _type, _permission
947949 FROM {$ this ->getSQLTable ($ name . '_perms ' )}
948950 WHERE _document = :_uid
949951 {$ this ->getTenantQuery ($ collection )}
950952 " ;
951953
952- $ sql = $ this ->trigger (Database::EVENT_PERMISSIONS_READ , $ sql );
954+ $ sql = $ this ->trigger (Database::EVENT_PERMISSIONS_READ , $ sql );
953955
954- /**
955- * Get current permissions from the database
956- */
957- $ sqlPermissions = $ this ->getPDO ()->prepare ($ sql );
958- $ sqlPermissions ->bindValue (':_uid ' , $ document ->getId ());
956+ /**
957+ * Get current permissions from the database
958+ */
959+ $ sqlPermissions = $ this ->getPDO ()->prepare ($ sql );
960+ $ sqlPermissions ->bindValue (':_uid ' , $ document ->getId ());
959961
960- if ($ this ->sharedTables ) {
961- $ sqlPermissions ->bindValue (':_tenant ' , $ this ->tenant );
962- }
962+ if ($ this ->sharedTables ) {
963+ $ sqlPermissions ->bindValue (':_tenant ' , $ this ->tenant );
964+ }
963965
964- $ sqlPermissions ->execute ();
965- $ permissions = $ sqlPermissions ->fetchAll ();
966- $ sqlPermissions ->closeCursor ();
966+ $ sqlPermissions ->execute ();
967+ $ permissions = $ sqlPermissions ->fetchAll ();
968+ $ sqlPermissions ->closeCursor ();
967969
968- $ initial = [];
969- foreach (Database::PERMISSIONS as $ type ) {
970- $ initial [$ type ] = [];
971- }
970+ $ initial = [];
971+ foreach (Database::PERMISSIONS as $ type ) {
972+ $ initial [$ type ] = [];
973+ }
972974
973- $ permissions = array_reduce ($ permissions , function (array $ carry , array $ item ) {
974- $ carry [$ item ['_type ' ]][] = $ item ['_permission ' ];
975+ $ permissions = array_reduce ($ permissions , function (array $ carry , array $ item ) {
976+ $ carry [$ item ['_type ' ]][] = $ item ['_permission ' ];
975977
976- return $ carry ;
977- }, $ initial );
978+ return $ carry ;
979+ }, $ initial );
978980
979- /**
980- * Get removed Permissions
981- */
982- $ removals = [];
983- foreach (Database::PERMISSIONS as $ type ) {
984- $ diff = \array_diff ($ permissions [$ type ], $ document ->getPermissionsByType ($ type ));
985- if (!empty ($ diff )) {
986- $ removals [$ type ] = $ diff ;
981+ /**
982+ * Get removed Permissions
983+ */
984+ $ removals = [];
985+ foreach (Database::PERMISSIONS as $ type ) {
986+ $ diff = \array_diff ($ permissions [$ type ], $ document ->getPermissionsByType ($ type ));
987+ if (!empty ($ diff )) {
988+ $ removals [$ type ] = $ diff ;
989+ }
987990 }
988- }
989991
990- /**
991- * Get added Permissions
992- */
993- $ additions = [];
994- foreach (Database::PERMISSIONS as $ type ) {
995- $ diff = \array_diff ($ document ->getPermissionsByType ($ type ), $ permissions [$ type ]);
996- if (!empty ($ diff )) {
997- $ additions [$ type ] = $ diff ;
992+ /**
993+ * Get added Permissions
994+ */
995+ $ additions = [];
996+ foreach (Database::PERMISSIONS as $ type ) {
997+ $ diff = \array_diff ($ document ->getPermissionsByType ($ type ), $ permissions [$ type ]);
998+ if (!empty ($ diff )) {
999+ $ additions [$ type ] = $ diff ;
1000+ }
9981001 }
999- }
10001002
1001- /**
1002- * Query to remove permissions
1003- */
1004- $ removeQuery = '' ;
1005- if (!empty ($ removals )) {
1006- $ removeQuery = ' AND ( ' ;
1007- foreach ($ removals as $ type => $ permissions ) {
1008- $ removeQuery .= "(
1003+ /**
1004+ * Query to remove permissions
1005+ */
1006+ $ removeQuery = '' ;
1007+ if (!empty ($ removals )) {
1008+ $ removeQuery = ' AND ( ' ;
1009+ foreach ($ removals as $ type => $ permissions ) {
1010+ $ removeQuery .= "(
10091011 _type = ' {$ type }'
10101012 AND _permission IN ( " . implode (', ' , \array_map (fn (string $ i ) => ":_remove_ {$ type }_ {$ i }" , \array_keys ($ permissions ))) . ")
10111013 ) " ;
1012- if ($ type !== \array_key_last ($ removals )) {
1013- $ removeQuery .= ' OR ' ;
1014+ if ($ type !== \array_key_last ($ removals )) {
1015+ $ removeQuery .= ' OR ' ;
1016+ }
10141017 }
10151018 }
1016- }
1017- if (!empty ($ removeQuery )) {
1018- $ removeQuery .= ') ' ;
1019- $ sql = "
1019+ if (!empty ($ removeQuery )) {
1020+ $ removeQuery .= ') ' ;
1021+ $ sql = "
10201022 DELETE
10211023 FROM {$ this ->getSQLTable ($ name . '_perms ' )}
10221024 WHERE _document = :_uid
10231025 {$ this ->getTenantQuery ($ collection )}
10241026 " ;
10251027
1026- $ removeQuery = $ sql . $ removeQuery ;
1028+ $ removeQuery = $ sql . $ removeQuery ;
10271029
1028- $ removeQuery = $ this ->trigger (Database::EVENT_PERMISSIONS_DELETE , $ removeQuery );
1030+ $ removeQuery = $ this ->trigger (Database::EVENT_PERMISSIONS_DELETE , $ removeQuery );
10291031
1030- $ stmtRemovePermissions = $ this ->getPDO ()->prepare ($ removeQuery );
1031- $ stmtRemovePermissions ->bindValue (':_uid ' , $ document ->getId ());
1032+ $ stmtRemovePermissions = $ this ->getPDO ()->prepare ($ removeQuery );
1033+ $ stmtRemovePermissions ->bindValue (':_uid ' , $ document ->getId ());
10321034
1033- if ($ this ->sharedTables ) {
1034- $ stmtRemovePermissions ->bindValue (':_tenant ' , $ this ->tenant );
1035- }
1035+ if ($ this ->sharedTables ) {
1036+ $ stmtRemovePermissions ->bindValue (':_tenant ' , $ this ->tenant );
1037+ }
10361038
1037- foreach ($ removals as $ type => $ permissions ) {
1038- foreach ($ permissions as $ i => $ permission ) {
1039- $ stmtRemovePermissions ->bindValue (":_remove_ {$ type }_ {$ i }" , $ permission );
1039+ foreach ($ removals as $ type => $ permissions ) {
1040+ foreach ($ permissions as $ i => $ permission ) {
1041+ $ stmtRemovePermissions ->bindValue (":_remove_ {$ type }_ {$ i }" , $ permission );
1042+ }
10401043 }
10411044 }
1042- }
10431045
1044- /**
1045- * Query to add permissions
1046- */
1047- if (!empty ($ additions )) {
1048- $ values = [];
1049- foreach ($ additions as $ type => $ permissions ) {
1050- foreach ($ permissions as $ i => $ _ ) {
1051- $ value = "( :_uid, ' {$ type }', :_add_ {$ type }_ {$ i }" ;
1052-
1053- if ($ this ->sharedTables ) {
1054- $ value .= ", :_tenant) " ;
1055- } else {
1056- $ value .= ") " ;
1046+ /**
1047+ * Query to add permissions
1048+ */
1049+ if (!empty ($ additions )) {
1050+ $ values = [];
1051+ foreach ($ additions as $ type => $ permissions ) {
1052+ foreach ($ permissions as $ i => $ _ ) {
1053+ $ value = "( :_uid, ' {$ type }', :_add_ {$ type }_ {$ i }" ;
1054+
1055+ if ($ this ->sharedTables ) {
1056+ $ value .= ", :_tenant) " ;
1057+ } else {
1058+ $ value .= ") " ;
1059+ }
1060+
1061+ $ values [] = $ value ;
10571062 }
1058-
1059- $ values [] = $ value ;
10601063 }
1061- }
10621064
1063- $ sql = "
1065+ $ sql = "
10641066 INSERT INTO {$ this ->getSQLTable ($ name . '_perms ' )} (_document, _type, _permission
10651067 " ;
10661068
1067- if ($ this ->sharedTables ) {
1068- $ sql .= ', _tenant) ' ;
1069- } else {
1070- $ sql .= ') ' ;
1071- }
1069+ if ($ this ->sharedTables ) {
1070+ $ sql .= ', _tenant) ' ;
1071+ } else {
1072+ $ sql .= ') ' ;
1073+ }
10721074
1073- $ sql .= " VALUES " . \implode (', ' , $ values );
1075+ $ sql .= " VALUES " . \implode (', ' , $ values );
10741076
1075- $ sql = $ this ->trigger (Database::EVENT_PERMISSIONS_CREATE , $ sql );
1077+ $ sql = $ this ->trigger (Database::EVENT_PERMISSIONS_CREATE , $ sql );
10761078
1077- $ stmtAddPermissions = $ this ->getPDO ()->prepare ($ sql );
1079+ $ stmtAddPermissions = $ this ->getPDO ()->prepare ($ sql );
10781080
1079- $ stmtAddPermissions ->bindValue (":_uid " , $ document ->getId ());
1081+ $ stmtAddPermissions ->bindValue (":_uid " , $ document ->getId ());
10801082
1081- if ($ this ->sharedTables ) {
1082- $ stmtAddPermissions ->bindValue (":_tenant " , $ this ->tenant );
1083- }
1083+ if ($ this ->sharedTables ) {
1084+ $ stmtAddPermissions ->bindValue (":_tenant " , $ this ->tenant );
1085+ }
10841086
1085- foreach ($ additions as $ type => $ permissions ) {
1086- foreach ($ permissions as $ i => $ permission ) {
1087- $ stmtAddPermissions ->bindValue (":_add_ {$ type }_ {$ i }" , $ permission );
1087+ foreach ($ additions as $ type => $ permissions ) {
1088+ foreach ($ permissions as $ i => $ permission ) {
1089+ $ stmtAddPermissions ->bindValue (":_add_ {$ type }_ {$ i }" , $ permission );
1090+ }
10881091 }
10891092 }
10901093 }
@@ -1486,7 +1489,7 @@ public function find(string $collection, array $queries = [], ?int $limit = 25,
14861489 unset($ results [$ index ]['_id ' ]);
14871490 }
14881491 if (\array_key_exists ('_tenant ' , $ document )) {
1489- $ results [$ index ]['$tenant ' ] = $ document ['_tenant ' ] === null ? null : ( int ) $ document [ ' _tenant ' ] ;
1492+ $ results [$ index ]['$tenant ' ] = $ document ['_tenant ' ];
14901493 unset($ results [$ index ]['_tenant ' ]);
14911494 }
14921495 if (\array_key_exists ('_createdAt ' , $ document )) {
@@ -1962,4 +1965,12 @@ public function getSupportForNumericCasting(): bool
19621965 {
19631966 return true ;
19641967 }
1968+
1969+ public function getSupportForIndexArray (): bool
1970+ {
1971+ /**
1972+ * Disabled to be compatible with Mysql adapter
1973+ */
1974+ return false ;
1975+ }
19651976}
0 commit comments