Skip to content

Commit ad7613a

Browse files
authored
Merge pull request #631 from utopia-php/revert-611-optimize-update-document
Revert "Skip permissions update"
2 parents 239b19b + b96fa84 commit ad7613a

File tree

6 files changed

+279
-294
lines changed

6 files changed

+279
-294
lines changed

src/Database/Adapter.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -697,11 +697,10 @@ abstract public function createDocuments(string $collection, array $documents):
697697
* @param string $collection
698698
* @param string $id
699699
* @param Document $document
700-
* @param bool $skipPermissions
701700
*
702701
* @return Document
703702
*/
704-
abstract public function updateDocument(string $collection, string $id, Document $document, bool $skipPermissions): Document;
703+
abstract public function updateDocument(string $collection, string $id, Document $document): Document;
705704

706705
/**
707706
* Update documents

src/Database/Adapter/MariaDB.php

Lines changed: 97 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -926,14 +926,13 @@ public function createDocument(string $collection, Document $document): Document
926926
* @param string $collection
927927
* @param string $id
928928
* @param Document $document
929-
* @param bool $skipPermissions
930929
* @return Document
931930
* @throws Exception
932931
* @throws PDOException
933932
* @throws DuplicateException
934933
* @throws \Throwable
935934
*/
936-
public function updateDocument(string $collection, string $id, Document $document, bool $skipPermissions): Document
935+
public function updateDocument(string $collection, string $id, Document $document): Document
937936
{
938937
try {
939938
$attributes = $document->getAttributes();
@@ -944,151 +943,149 @@ public function updateDocument(string $collection, string $id, Document $documen
944943
$name = $this->filter($collection);
945944
$columns = '';
946945

947-
if (!$skipPermissions) {
948-
$sql = "
946+
$sql = "
949947
SELECT _type, _permission
950948
FROM {$this->getSQLTable($name . '_perms')}
951949
WHERE _document = :_uid
952950
{$this->getTenantQuery($collection)}
953951
";
954952

955-
$sql = $this->trigger(Database::EVENT_PERMISSIONS_READ, $sql);
953+
$sql = $this->trigger(Database::EVENT_PERMISSIONS_READ, $sql);
956954

957-
/**
958-
* Get current permissions from the database
959-
*/
960-
$sqlPermissions = $this->getPDO()->prepare($sql);
961-
$sqlPermissions->bindValue(':_uid', $document->getId());
955+
/**
956+
* Get current permissions from the database
957+
*/
958+
$sqlPermissions = $this->getPDO()->prepare($sql);
959+
$sqlPermissions->bindValue(':_uid', $document->getId());
962960

963-
if ($this->sharedTables) {
964-
$sqlPermissions->bindValue(':_tenant', $this->tenant);
965-
}
961+
if ($this->sharedTables) {
962+
$sqlPermissions->bindValue(':_tenant', $this->tenant);
963+
}
966964

967-
$sqlPermissions->execute();
968-
$permissions = $sqlPermissions->fetchAll();
969-
$sqlPermissions->closeCursor();
965+
$sqlPermissions->execute();
966+
$permissions = $sqlPermissions->fetchAll();
967+
$sqlPermissions->closeCursor();
970968

971-
$initial = [];
972-
foreach (Database::PERMISSIONS as $type) {
973-
$initial[$type] = [];
974-
}
969+
$initial = [];
970+
foreach (Database::PERMISSIONS as $type) {
971+
$initial[$type] = [];
972+
}
975973

976-
$permissions = array_reduce($permissions, function (array $carry, array $item) {
977-
$carry[$item['_type']][] = $item['_permission'];
974+
$permissions = array_reduce($permissions, function (array $carry, array $item) {
975+
$carry[$item['_type']][] = $item['_permission'];
978976

979-
return $carry;
980-
}, $initial);
977+
return $carry;
978+
}, $initial);
981979

982-
/**
983-
* Get removed Permissions
984-
*/
985-
$removals = [];
986-
foreach (Database::PERMISSIONS as $type) {
987-
$diff = \array_diff($permissions[$type], $document->getPermissionsByType($type));
988-
if (!empty($diff)) {
989-
$removals[$type] = $diff;
990-
}
980+
/**
981+
* Get removed Permissions
982+
*/
983+
$removals = [];
984+
foreach (Database::PERMISSIONS as $type) {
985+
$diff = \array_diff($permissions[$type], $document->getPermissionsByType($type));
986+
if (!empty($diff)) {
987+
$removals[$type] = $diff;
991988
}
989+
}
992990

993-
/**
994-
* Get added Permissions
995-
*/
996-
$additions = [];
997-
foreach (Database::PERMISSIONS as $type) {
998-
$diff = \array_diff($document->getPermissionsByType($type), $permissions[$type]);
999-
if (!empty($diff)) {
1000-
$additions[$type] = $diff;
1001-
}
991+
/**
992+
* Get added Permissions
993+
*/
994+
$additions = [];
995+
foreach (Database::PERMISSIONS as $type) {
996+
$diff = \array_diff($document->getPermissionsByType($type), $permissions[$type]);
997+
if (!empty($diff)) {
998+
$additions[$type] = $diff;
1002999
}
1000+
}
10031001

1004-
/**
1005-
* Query to remove permissions
1006-
*/
1007-
$removeQuery = '';
1008-
if (!empty($removals)) {
1009-
$removeQuery = ' AND (';
1010-
foreach ($removals as $type => $permissions) {
1011-
$removeQuery .= "(
1002+
/**
1003+
* Query to remove permissions
1004+
*/
1005+
$removeQuery = '';
1006+
if (!empty($removals)) {
1007+
$removeQuery = ' AND (';
1008+
foreach ($removals as $type => $permissions) {
1009+
$removeQuery .= "(
10121010
_type = '{$type}'
10131011
AND _permission IN (" . implode(', ', \array_map(fn (string $i) => ":_remove_{$type}_{$i}", \array_keys($permissions))) . ")
10141012
)";
1015-
if ($type !== \array_key_last($removals)) {
1016-
$removeQuery .= ' OR ';
1017-
}
1013+
if ($type !== \array_key_last($removals)) {
1014+
$removeQuery .= ' OR ';
10181015
}
10191016
}
1020-
if (!empty($removeQuery)) {
1021-
$removeQuery .= ')';
1022-
$sql = "
1017+
}
1018+
if (!empty($removeQuery)) {
1019+
$removeQuery .= ')';
1020+
$sql = "
10231021
DELETE
10241022
FROM {$this->getSQLTable($name . '_perms')}
10251023
WHERE _document = :_uid
10261024
{$this->getTenantQuery($collection)}
10271025
";
10281026

1029-
$removeQuery = $sql . $removeQuery;
1027+
$removeQuery = $sql . $removeQuery;
10301028

1031-
$removeQuery = $this->trigger(Database::EVENT_PERMISSIONS_DELETE, $removeQuery);
1029+
$removeQuery = $this->trigger(Database::EVENT_PERMISSIONS_DELETE, $removeQuery);
10321030

1033-
$stmtRemovePermissions = $this->getPDO()->prepare($removeQuery);
1034-
$stmtRemovePermissions->bindValue(':_uid', $document->getId());
1031+
$stmtRemovePermissions = $this->getPDO()->prepare($removeQuery);
1032+
$stmtRemovePermissions->bindValue(':_uid', $document->getId());
10351033

1036-
if ($this->sharedTables) {
1037-
$stmtRemovePermissions->bindValue(':_tenant', $this->tenant);
1038-
}
1034+
if ($this->sharedTables) {
1035+
$stmtRemovePermissions->bindValue(':_tenant', $this->tenant);
1036+
}
10391037

1040-
foreach ($removals as $type => $permissions) {
1041-
foreach ($permissions as $i => $permission) {
1042-
$stmtRemovePermissions->bindValue(":_remove_{$type}_{$i}", $permission);
1043-
}
1038+
foreach ($removals as $type => $permissions) {
1039+
foreach ($permissions as $i => $permission) {
1040+
$stmtRemovePermissions->bindValue(":_remove_{$type}_{$i}", $permission);
10441041
}
10451042
}
1043+
}
1044+
1045+
/**
1046+
* Query to add permissions
1047+
*/
1048+
if (!empty($additions)) {
1049+
$values = [];
1050+
foreach ($additions as $type => $permissions) {
1051+
foreach ($permissions as $i => $_) {
1052+
$value = "( :_uid, '{$type}', :_add_{$type}_{$i}";
10461053

1047-
/**
1048-
* Query to add permissions
1049-
*/
1050-
if (!empty($additions)) {
1051-
$values = [];
1052-
foreach ($additions as $type => $permissions) {
1053-
foreach ($permissions as $i => $_) {
1054-
$value = "( :_uid, '{$type}', :_add_{$type}_{$i}";
1055-
1056-
if ($this->sharedTables) {
1057-
$value .= ", :_tenant)";
1058-
} else {
1059-
$value .= ")";
1060-
}
1061-
1062-
$values[] = $value;
1054+
if ($this->sharedTables) {
1055+
$value .= ", :_tenant)";
1056+
} else {
1057+
$value .= ")";
10631058
}
1059+
1060+
$values[] = $value;
10641061
}
1062+
}
10651063

1066-
$sql = "
1064+
$sql = "
10671065
INSERT INTO {$this->getSQLTable($name . '_perms')} (_document, _type, _permission
10681066
";
10691067

1070-
if ($this->sharedTables) {
1071-
$sql .= ', _tenant)';
1072-
} else {
1073-
$sql .= ')';
1074-
}
1068+
if ($this->sharedTables) {
1069+
$sql .= ', _tenant)';
1070+
} else {
1071+
$sql .= ')';
1072+
}
10751073

1076-
$sql .= " VALUES " . \implode(', ', $values);
1074+
$sql .= " VALUES " . \implode(', ', $values);
10771075

1078-
$sql = $this->trigger(Database::EVENT_PERMISSIONS_CREATE, $sql);
1076+
$sql = $this->trigger(Database::EVENT_PERMISSIONS_CREATE, $sql);
10791077

1080-
$stmtAddPermissions = $this->getPDO()->prepare($sql);
1078+
$stmtAddPermissions = $this->getPDO()->prepare($sql);
10811079

1082-
$stmtAddPermissions->bindValue(":_uid", $document->getId());
1080+
$stmtAddPermissions->bindValue(":_uid", $document->getId());
10831081

1084-
if ($this->sharedTables) {
1085-
$stmtAddPermissions->bindValue(":_tenant", $this->tenant);
1086-
}
1082+
if ($this->sharedTables) {
1083+
$stmtAddPermissions->bindValue(":_tenant", $this->tenant);
1084+
}
10871085

1088-
foreach ($additions as $type => $permissions) {
1089-
foreach ($permissions as $i => $permission) {
1090-
$stmtAddPermissions->bindValue(":_add_{$type}_{$i}", $permission);
1091-
}
1086+
foreach ($additions as $type => $permissions) {
1087+
foreach ($permissions as $i => $permission) {
1088+
$stmtAddPermissions->bindValue(":_add_{$type}_{$i}", $permission);
10921089
}
10931090
}
10941091
}

src/Database/Adapter/Pool.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ public function createDocuments(string $collection, array $documents): array
235235
return $this->delegate(__FUNCTION__, \func_get_args());
236236
}
237237

238-
public function updateDocument(string $collection, string $id, Document $document, bool $skipPermissions): Document
238+
public function updateDocument(string $collection, string $id, Document $document): Document
239239
{
240240
return $this->delegate(__FUNCTION__, \func_get_args());
241241
}

0 commit comments

Comments
 (0)