Skip to content

Commit 130beb0

Browse files
committed
SQLite
1 parent cadce6c commit 130beb0

1 file changed

Lines changed: 87 additions & 85 deletions

File tree

src/Database/Adapter/SQLite.php

Lines changed: 87 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -648,134 +648,136 @@ public function updateDocument(string $collection, string $id, Document $documen
648648
$name = $this->filter($collection);
649649
$columns = '';
650650

651-
$sql = "
651+
if (!$skipPermissions) {
652+
$sql = "
652653
SELECT _type, _permission
653654
FROM `{$this->getNamespace()}_{$name}_perms`
654655
WHERE _document = :_uid
655656
{$this->getTenantQuery($collection)}
656657
";
657658

658-
$sql = $this->trigger(Database::EVENT_PERMISSIONS_READ, $sql);
659+
$sql = $this->trigger(Database::EVENT_PERMISSIONS_READ, $sql);
659660

660-
/**
661-
* Get current permissions from the database
662-
*/
663-
$permissionsStmt = $this->getPDO()->prepare($sql);
664-
$permissionsStmt->bindValue(':_uid', $document->getId());
661+
/**
662+
* Get current permissions from the database
663+
*/
664+
$permissionsStmt = $this->getPDO()->prepare($sql);
665+
$permissionsStmt->bindValue(':_uid', $document->getId());
665666

666-
if ($this->sharedTables) {
667-
$permissionsStmt->bindValue(':_tenant', $this->tenant);
668-
}
667+
if ($this->sharedTables) {
668+
$permissionsStmt->bindValue(':_tenant', $this->tenant);
669+
}
669670

670-
$permissionsStmt->execute();
671-
$permissions = $permissionsStmt->fetchAll();
672-
$permissionsStmt->closeCursor();
671+
$permissionsStmt->execute();
672+
$permissions = $permissionsStmt->fetchAll();
673+
$permissionsStmt->closeCursor();
673674

674-
$initial = [];
675-
foreach (Database::PERMISSIONS as $type) {
676-
$initial[$type] = [];
677-
}
675+
$initial = [];
676+
foreach (Database::PERMISSIONS as $type) {
677+
$initial[$type] = [];
678+
}
678679

679-
$permissions = array_reduce($permissions, function (array $carry, array $item) {
680-
$carry[$item['_type']][] = $item['_permission'];
680+
$permissions = array_reduce($permissions, function (array $carry, array $item) {
681+
$carry[$item['_type']][] = $item['_permission'];
681682

682-
return $carry;
683-
}, $initial);
683+
return $carry;
684+
}, $initial);
684685

685-
/**
686-
* Get removed Permissions
687-
*/
688-
$removals = [];
689-
foreach (Database::PERMISSIONS as $type) {
690-
$diff = \array_diff($permissions[$type], $document->getPermissionsByType($type));
691-
if (!empty($diff)) {
692-
$removals[$type] = $diff;
686+
/**
687+
* Get removed Permissions
688+
*/
689+
$removals = [];
690+
foreach (Database::PERMISSIONS as $type) {
691+
$diff = \array_diff($permissions[$type], $document->getPermissionsByType($type));
692+
if (!empty($diff)) {
693+
$removals[$type] = $diff;
694+
}
693695
}
694-
}
695696

696-
/**
697-
* Get added Permissions
698-
*/
699-
$additions = [];
700-
foreach (Database::PERMISSIONS as $type) {
701-
$diff = \array_diff($document->getPermissionsByType($type), $permissions[$type]);
702-
if (!empty($diff)) {
703-
$additions[$type] = $diff;
697+
/**
698+
* Get added Permissions
699+
*/
700+
$additions = [];
701+
foreach (Database::PERMISSIONS as $type) {
702+
$diff = \array_diff($document->getPermissionsByType($type), $permissions[$type]);
703+
if (!empty($diff)) {
704+
$additions[$type] = $diff;
705+
}
704706
}
705-
}
706707

707-
/**
708-
* Query to remove permissions
709-
*/
710-
$removeQuery = '';
711-
if (!empty($removals)) {
712-
$removeQuery = ' AND (';
713-
foreach ($removals as $type => $permissions) {
714-
$removeQuery .= "(
708+
/**
709+
* Query to remove permissions
710+
*/
711+
$removeQuery = '';
712+
if (!empty($removals)) {
713+
$removeQuery = ' AND (';
714+
foreach ($removals as $type => $permissions) {
715+
$removeQuery .= "(
715716
_type = '{$type}'
716717
AND _permission IN (" . implode(', ', \array_map(fn (string $i) => ":_remove_{$type}_{$i}", \array_keys($permissions))) . ")
717718
)";
718-
if ($type !== \array_key_last($removals)) {
719-
$removeQuery .= ' OR ';
719+
if ($type !== \array_key_last($removals)) {
720+
$removeQuery .= ' OR ';
721+
}
720722
}
721723
}
722-
}
723-
if (!empty($removeQuery)) {
724-
$removeQuery .= ')';
725-
$sql = "
724+
if (!empty($removeQuery)) {
725+
$removeQuery .= ')';
726+
$sql = "
726727
DELETE
727728
FROM `{$this->getNamespace()}_{$name}_perms`
728729
WHERE _document = :_uid
729730
{$this->getTenantQuery($collection)}
730731
";
731732

732-
$removeQuery = $sql . $removeQuery;
733-
$removeQuery = $this->trigger(Database::EVENT_PERMISSIONS_DELETE, $removeQuery);
733+
$removeQuery = $sql . $removeQuery;
734+
$removeQuery = $this->trigger(Database::EVENT_PERMISSIONS_DELETE, $removeQuery);
734735

735-
$stmtRemovePermissions = $this->getPDO()->prepare($removeQuery);
736-
$stmtRemovePermissions->bindValue(':_uid', $document->getId());
736+
$stmtRemovePermissions = $this->getPDO()->prepare($removeQuery);
737+
$stmtRemovePermissions->bindValue(':_uid', $document->getId());
737738

738-
if ($this->sharedTables) {
739-
$stmtRemovePermissions->bindValue(':_tenant', $this->tenant);
740-
}
739+
if ($this->sharedTables) {
740+
$stmtRemovePermissions->bindValue(':_tenant', $this->tenant);
741+
}
741742

742-
foreach ($removals as $type => $permissions) {
743-
foreach ($permissions as $i => $permission) {
744-
$stmtRemovePermissions->bindValue(":_remove_{$type}_{$i}", $permission);
743+
foreach ($removals as $type => $permissions) {
744+
foreach ($permissions as $i => $permission) {
745+
$stmtRemovePermissions->bindValue(":_remove_{$type}_{$i}", $permission);
746+
}
745747
}
746748
}
747-
}
748749

749-
/**
750-
* Query to add permissions
751-
*/
752-
if (!empty($additions)) {
753-
$values = [];
754-
foreach ($additions as $type => $permissions) {
755-
foreach ($permissions as $i => $_) {
756-
$tenantQuery = $this->sharedTables ? ', :_tenant' : '';
757-
$values[] = "(:_uid, '{$type}', :_add_{$type}_{$i} {$tenantQuery})";
750+
/**
751+
* Query to add permissions
752+
*/
753+
if (!empty($additions)) {
754+
$values = [];
755+
foreach ($additions as $type => $permissions) {
756+
foreach ($permissions as $i => $_) {
757+
$tenantQuery = $this->sharedTables ? ', :_tenant' : '';
758+
$values[] = "(:_uid, '{$type}', :_add_{$type}_{$i} {$tenantQuery})";
759+
}
758760
}
759-
}
760761

761-
$tenantQuery = $this->sharedTables ? ', _tenant' : '';
762+
$tenantQuery = $this->sharedTables ? ', _tenant' : '';
762763

763-
$sql = "
764+
$sql = "
764765
INSERT INTO `{$this->getNamespace()}_{$name}_perms` (_document, _type, _permission {$tenantQuery})
765766
VALUES " . \implode(', ', $values);
766767

767-
$sql = $this->trigger(Database::EVENT_PERMISSIONS_CREATE, $sql);
768+
$sql = $this->trigger(Database::EVENT_PERMISSIONS_CREATE, $sql);
768769

769-
$stmtAddPermissions = $this->getPDO()->prepare($sql);
770+
$stmtAddPermissions = $this->getPDO()->prepare($sql);
770771

771-
$stmtAddPermissions->bindValue(":_uid", $document->getId());
772-
if ($this->sharedTables) {
773-
$stmtAddPermissions->bindValue(":_tenant", $this->tenant);
774-
}
772+
$stmtAddPermissions->bindValue(":_uid", $document->getId());
773+
if ($this->sharedTables) {
774+
$stmtAddPermissions->bindValue(":_tenant", $this->tenant);
775+
}
775776

776-
foreach ($additions as $type => $permissions) {
777-
foreach ($permissions as $i => $permission) {
778-
$stmtAddPermissions->bindValue(":_add_{$type}_{$i}", $permission);
777+
foreach ($additions as $type => $permissions) {
778+
foreach ($permissions as $i => $permission) {
779+
$stmtAddPermissions->bindValue(":_add_{$type}_{$i}", $permission);
780+
}
779781
}
780782
}
781783
}

0 commit comments

Comments
 (0)