Skip to content

Commit b9ba929

Browse files
authored
Merge pull request #664 from utopia-php/dat-610-fix-createDocuments-attributes
Enhance permission handling in SQL adapter and add tests for document…
2 parents 4fc1fb3 + 7c7af88 commit b9ba929

File tree

2 files changed

+64
-5
lines changed

2 files changed

+64
-5
lines changed

src/Database/Adapter/SQL.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1864,6 +1864,7 @@ public function createDocuments(string $collection, array $documents): array
18641864
$batchKeys = [];
18651865
$bindValues = [];
18661866
$permissions = [];
1867+
$bindValuesPermissions = [];
18671868

18681869
foreach ($documents as $index => $document) {
18691870
$attributes = $document->getAttributes();
@@ -1902,6 +1903,10 @@ public function createDocuments(string $collection, array $documents): array
19021903
$permission = \str_replace('"', '', $permission);
19031904
$permission = "('{$type}', '{$permission}', :_uid_{$index} {$tenantBind})";
19041905
$permissions[] = $permission;
1906+
$bindValuesPermissions[":_uid_{$index}"] = $document->getId();
1907+
if ($this->sharedTables) {
1908+
$bindValuesPermissions[":_tenant_{$index}"] = $document->getTenant();
1909+
}
19051910
}
19061911
}
19071912
}
@@ -1930,11 +1935,8 @@ public function createDocuments(string $collection, array $documents): array
19301935

19311936
$stmtPermissions = $this->getPDO()->prepare($sqlPermissions);
19321937

1933-
foreach ($documents as $index => $document) {
1934-
$stmtPermissions->bindValue(":_uid_{$index}", $document->getId());
1935-
if ($this->sharedTables) {
1936-
$stmtPermissions->bindValue(":_tenant_{$index}", $document->getTenant());
1937-
}
1938+
foreach ($bindValuesPermissions as $key => $value) {
1939+
$stmtPermissions->bindValue($key, $value, $this->getPDOType($value));
19381940
}
19391941

19401942
$this->execute($stmtPermissions);

tests/e2e/Adapter/Scopes/DocumentTests.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5846,4 +5846,61 @@ public function testUpdateDocumentsCount(): void
58465846

58475847
$database->deleteCollection($collectionName);
58485848
}
5849+
5850+
public function testCreateUpdateDocumentsMismatch(): void
5851+
{
5852+
/** @var Database $database */
5853+
$database = static::getDatabase();
5854+
5855+
// with different set of attributes
5856+
$colName = "docs_with_diff";
5857+
$database->createCollection($colName);
5858+
$database->createAttribute($colName, 'key', Database::VAR_STRING, 50, true);
5859+
$database->createAttribute($colName, 'value', Database::VAR_STRING, 50, false, 'value');
5860+
$permissions = [Permission::read(Role::any()), Permission::write(Role::any()),Permission::update(Role::any())];
5861+
$docs = [
5862+
new Document([
5863+
'$id' => 'doc1',
5864+
'key' => 'doc1',
5865+
]),
5866+
new Document([
5867+
'$id' => 'doc2',
5868+
'key' => 'doc2',
5869+
'value' => 'test',
5870+
]),
5871+
new Document([
5872+
'$id' => 'doc3',
5873+
'$permissions' => $permissions,
5874+
'key' => 'doc3'
5875+
]),
5876+
];
5877+
$this->assertEquals(3, $database->createDocuments($colName, $docs));
5878+
// we should get only one document as read permission provided to the last document only
5879+
$addedDocs = $database->find($colName);
5880+
$this->assertCount(1, $addedDocs);
5881+
$doc = $addedDocs[0];
5882+
$this->assertEquals('doc3', $doc->getId());
5883+
$this->assertNotEmpty($doc->getPermissions());
5884+
$this->assertCount(3, $doc->getPermissions());
5885+
5886+
$database->createDocument($colName, new Document([
5887+
'$id' => 'doc4',
5888+
'$permissions' => $permissions,
5889+
'key' => 'doc4'
5890+
]));
5891+
5892+
$this->assertEquals(2, $database->updateDocuments($colName, new Document(['key' => 'new doc'])));
5893+
$doc = $database->getDocument($colName, 'doc4');
5894+
$this->assertEquals('doc4', $doc->getId());
5895+
$this->assertEquals('value', $doc->getAttribute('value'));
5896+
5897+
$addedDocs = $database->find($colName);
5898+
$this->assertCount(2, $addedDocs);
5899+
foreach ($addedDocs as $doc) {
5900+
$this->assertNotEmpty($doc->getPermissions());
5901+
$this->assertCount(3, $doc->getPermissions());
5902+
$this->assertEquals('value', $doc->getAttribute('value'));
5903+
}
5904+
$database->deleteCollection($colName);
5905+
}
58495906
}

0 commit comments

Comments
 (0)