@@ -927,13 +927,14 @@ public function createDocument(string $collection, Document $document): Document
927927 * @param string $collection
928928 * @param string $id
929929 * @param Document $document
930+ * @param bool $skipPermissions
930931 * @return Document
931932 * @throws Exception
932933 * @throws PDOException
933934 * @throws DuplicateException
934935 * @throws \Throwable
935936 */
936- public function updateDocument (string $ collection , string $ id , Document $ document ): Document
937+ public function updateDocument (string $ collection , string $ id , Document $ document, bool $ skipPermissions ): Document
937938 {
938939 try {
939940 $ attributes = $ document ->getAttributes ();
@@ -944,149 +945,151 @@ public function updateDocument(string $collection, string $id, Document $documen
944945 $ name = $ this ->filter ($ collection );
945946 $ columns = '' ;
946947
947- $ sql = "
948+ if (!$ skipPermissions ){
949+ $ sql = "
948950 SELECT _type, _permission
949951 FROM {$ this ->getSQLTable ($ name . '_perms ' )}
950952 WHERE _document = :_uid
951953 {$ this ->getTenantQuery ($ collection )}
952954 " ;
955+ var_dump ($ sql );
956+ $ sql = $ this ->trigger (Database::EVENT_PERMISSIONS_READ , $ sql );
953957
954- $ sql = $ this ->trigger (Database::EVENT_PERMISSIONS_READ , $ sql );
958+ /**
959+ * Get current permissions from the database
960+ */
961+ $ sqlPermissions = $ this ->getPDO ()->prepare ($ sql );
962+ $ sqlPermissions ->bindValue (':_uid ' , $ document ->getId ());
955963
956- /**
957- * Get current permissions from the database
958- */
959- $ sqlPermissions = $ this ->getPDO ()->prepare ($ sql );
960- $ sqlPermissions ->bindValue (':_uid ' , $ document ->getId ());
961-
962- if ($ this ->sharedTables ) {
963- $ sqlPermissions ->bindValue (':_tenant ' , $ this ->tenant );
964- }
964+ if ($ this ->sharedTables ) {
965+ $ sqlPermissions ->bindValue (':_tenant ' , $ this ->tenant );
966+ }
965967
966- $ sqlPermissions ->execute ();
967- $ permissions = $ sqlPermissions ->fetchAll ();
968- $ sqlPermissions ->closeCursor ();
968+ $ sqlPermissions ->execute ();
969+ $ permissions = $ sqlPermissions ->fetchAll ();
970+ $ sqlPermissions ->closeCursor ();
969971
970- $ initial = [];
971- foreach (Database::PERMISSIONS as $ type ) {
972- $ initial [$ type ] = [];
973- }
972+ $ initial = [];
973+ foreach (Database::PERMISSIONS as $ type ) {
974+ $ initial [$ type ] = [];
975+ }
974976
975- $ permissions = array_reduce ($ permissions , function (array $ carry , array $ item ) {
976- $ carry [$ item ['_type ' ]][] = $ item ['_permission ' ];
977+ $ permissions = array_reduce ($ permissions , function (array $ carry , array $ item ) {
978+ $ carry [$ item ['_type ' ]][] = $ item ['_permission ' ];
977979
978- return $ carry ;
979- }, $ initial );
980+ return $ carry ;
981+ }, $ initial );
980982
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 ;
983+ /**
984+ * Get removed Permissions
985+ */
986+ $ removals = [];
987+ foreach (Database::PERMISSIONS as $ type ) {
988+ $ diff = \array_diff ($ permissions [$ type ], $ document ->getPermissionsByType ($ type ));
989+ if (!empty ($ diff )) {
990+ $ removals [$ type ] = $ diff ;
991+ }
989992 }
990- }
991993
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 ;
994+ /**
995+ * Get added Permissions
996+ */
997+ $ additions = [];
998+ foreach (Database::PERMISSIONS as $ type ) {
999+ $ diff = \array_diff ($ document ->getPermissionsByType ($ type ), $ permissions [$ type ]);
1000+ if (!empty ($ diff )) {
1001+ $ additions [$ type ] = $ diff ;
1002+ }
10001003 }
1001- }
10021004
1003- /**
1004- * Query to remove permissions
1005- */
1006- $ removeQuery = '' ;
1007- if (!empty ($ removals )) {
1008- $ removeQuery = ' AND ( ' ;
1009- foreach ($ removals as $ type => $ permissions ) {
1010- $ removeQuery .= "(
1005+ /**
1006+ * Query to remove permissions
1007+ */
1008+ $ removeQuery = '' ;
1009+ if (!empty ($ removals )) {
1010+ $ removeQuery = ' AND ( ' ;
1011+ foreach ($ removals as $ type => $ permissions ) {
1012+ $ removeQuery .= "(
10111013 _type = ' {$ type }'
10121014 AND _permission IN ( " . implode (', ' , \array_map (fn (string $ i ) => ":_remove_ {$ type }_ {$ i }" , \array_keys ($ permissions ))) . ")
10131015 ) " ;
1014- if ($ type !== \array_key_last ($ removals )) {
1015- $ removeQuery .= ' OR ' ;
1016+ if ($ type !== \array_key_last ($ removals )) {
1017+ $ removeQuery .= ' OR ' ;
1018+ }
10161019 }
10171020 }
1018- }
1019- if (!empty ($ removeQuery )) {
1020- $ removeQuery .= ') ' ;
1021- $ sql = "
1021+ if (!empty ($ removeQuery )) {
1022+ $ removeQuery .= ') ' ;
1023+ $ sql = "
10221024 DELETE
10231025 FROM {$ this ->getSQLTable ($ name . '_perms ' )}
10241026 WHERE _document = :_uid
10251027 {$ this ->getTenantQuery ($ collection )}
10261028 " ;
10271029
1028- $ removeQuery = $ sql . $ removeQuery ;
1030+ $ removeQuery = $ sql . $ removeQuery ;
10291031
1030- $ removeQuery = $ this ->trigger (Database::EVENT_PERMISSIONS_DELETE , $ removeQuery );
1032+ $ removeQuery = $ this ->trigger (Database::EVENT_PERMISSIONS_DELETE , $ removeQuery );
10311033
1032- $ stmtRemovePermissions = $ this ->getPDO ()->prepare ($ removeQuery );
1033- $ stmtRemovePermissions ->bindValue (':_uid ' , $ document ->getId ());
1034+ $ stmtRemovePermissions = $ this ->getPDO ()->prepare ($ removeQuery );
1035+ $ stmtRemovePermissions ->bindValue (':_uid ' , $ document ->getId ());
10341036
1035- if ($ this ->sharedTables ) {
1036- $ stmtRemovePermissions ->bindValue (':_tenant ' , $ this ->tenant );
1037- }
1037+ if ($ this ->sharedTables ) {
1038+ $ stmtRemovePermissions ->bindValue (':_tenant ' , $ this ->tenant );
1039+ }
10381040
1039- foreach ($ removals as $ type => $ permissions ) {
1040- foreach ($ permissions as $ i => $ permission ) {
1041- $ stmtRemovePermissions ->bindValue (":_remove_ {$ type }_ {$ i }" , $ permission );
1041+ foreach ($ removals as $ type => $ permissions ) {
1042+ foreach ($ permissions as $ i => $ permission ) {
1043+ $ stmtRemovePermissions ->bindValue (":_remove_ {$ type }_ {$ i }" , $ permission );
1044+ }
10421045 }
10431046 }
1044- }
1045-
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 }" ;
10541047
1055- if ($ this ->sharedTables ) {
1056- $ value .= ", :_tenant) " ;
1057- } else {
1058- $ value .= ") " ;
1048+ /**
1049+ * Query to add permissions
1050+ */
1051+ if (!empty ($ additions )) {
1052+ $ values = [];
1053+ foreach ($ additions as $ type => $ permissions ) {
1054+ foreach ($ permissions as $ i => $ _ ) {
1055+ $ value = "( :_uid, ' {$ type }', :_add_ {$ type }_ {$ i }" ;
1056+
1057+ if ($ this ->sharedTables ) {
1058+ $ value .= ", :_tenant) " ;
1059+ } else {
1060+ $ value .= ") " ;
1061+ }
1062+
1063+ $ values [] = $ value ;
10591064 }
1060-
1061- $ values [] = $ value ;
10621065 }
1063- }
10641066
1065- $ sql = "
1067+ $ sql = "
10661068 INSERT INTO {$ this ->getSQLTable ($ name . '_perms ' )} (_document, _type, _permission
10671069 " ;
10681070
1069- if ($ this ->sharedTables ) {
1070- $ sql .= ', _tenant) ' ;
1071- } else {
1072- $ sql .= ') ' ;
1073- }
1071+ if ($ this ->sharedTables ) {
1072+ $ sql .= ', _tenant) ' ;
1073+ } else {
1074+ $ sql .= ') ' ;
1075+ }
10741076
1075- $ sql .= " VALUES " . \implode (', ' , $ values );
1077+ $ sql .= " VALUES " . \implode (', ' , $ values );
10761078
1077- $ sql = $ this ->trigger (Database::EVENT_PERMISSIONS_CREATE , $ sql );
1079+ $ sql = $ this ->trigger (Database::EVENT_PERMISSIONS_CREATE , $ sql );
10781080
1079- $ stmtAddPermissions = $ this ->getPDO ()->prepare ($ sql );
1081+ $ stmtAddPermissions = $ this ->getPDO ()->prepare ($ sql );
10801082
1081- $ stmtAddPermissions ->bindValue (":_uid " , $ document ->getId ());
1083+ $ stmtAddPermissions ->bindValue (":_uid " , $ document ->getId ());
10821084
1083- if ($ this ->sharedTables ) {
1084- $ stmtAddPermissions ->bindValue (":_tenant " , $ this ->tenant );
1085- }
1085+ if ($ this ->sharedTables ) {
1086+ $ stmtAddPermissions ->bindValue (":_tenant " , $ this ->tenant );
1087+ }
10861088
1087- foreach ($ additions as $ type => $ permissions ) {
1088- foreach ($ permissions as $ i => $ permission ) {
1089- $ stmtAddPermissions ->bindValue (":_add_ {$ type }_ {$ i }" , $ permission );
1089+ foreach ($ additions as $ type => $ permissions ) {
1090+ foreach ($ permissions as $ i => $ permission ) {
1091+ $ stmtAddPermissions ->bindValue (":_add_ {$ type }_ {$ i }" , $ permission );
1092+ }
10901093 }
10911094 }
10921095 }
0 commit comments