Skip to content

Commit 11b0d98

Browse files
committed
Check $permissions keys
1 parent 03ba92c commit 11b0d98

File tree

5 files changed

+30
-13
lines changed

5 files changed

+30
-13
lines changed

phpunit.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
convertNoticesToExceptions="true"
88
convertWarningsToExceptions="true"
99
processIsolation="false"
10-
stopOnFailure="false"
10+
stopOnFailure="true"
1111
>
1212
<testsuites>
1313
<testsuite name="unit">

src/Database/Adapter/SQL.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Utopia\Database\Exception as DatabaseException;
1212
use Utopia\Database\Exception\Duplicate as DuplicateException;
1313
use Utopia\Database\Exception\NotFound as NotFoundException;
14+
use Utopia\Database\Exception\Query as QueryException;
1415
use Utopia\Database\Exception\Transaction as TransactionException;
1516
use Utopia\Database\Query;
1617

@@ -426,10 +427,20 @@ public function updateDocuments(string $collection, Document $updates, array $do
426427
$attributes['_createdAt'] = $updates->getCreatedAt();
427428
}
428429

429-
if (!empty($updates->getPermissions())) {
430+
if ($updates->offsetExists('$permissions')) {
430431
$attributes['_permissions'] = json_encode($updates->getPermissions());
431432
}
432433

434+
// if ($updatePermissions) {
435+
// $originalPermissions = $document->getPermissions();
436+
// $currentPermissions = $updates->getPermissions();
437+
//
438+
// sort($originalPermissions);
439+
// sort($currentPermissions);
440+
//
441+
// $skipPermissionsUpdate = ($originalPermissions === $currentPermissions);
442+
// }
443+
433444
if (empty($attributes)) {
434445
return 0;
435446
}
@@ -484,15 +495,14 @@ public function updateDocuments(string $collection, Document $updates, array $do
484495
$affected = $stmt->rowCount();
485496

486497
// Permissions logic
487-
if (!empty($updates->getPermissions())) {
498+
if ($updates->offsetExists('$permissions')) {
488499
$removeQueries = [];
489500
$removeBindValues = [];
490501

491502
$addQuery = '';
492503
$addBindValues = [];
493504

494505
foreach ($documents as $index => $document) {
495-
// Permissions logic
496506
$sql = "
497507
SELECT _type, _permission
498508
FROM {$this->getSQLTable($name . '_perms')}

src/Database/Database.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4375,13 +4375,16 @@ public function updateDocuments(
43754375
if (!empty($cursor) && $cursor->getCollection() !== $collection->getId()) {
43764376
throw new DatabaseException("Cursor document must be from the same Collection.");
43774377
}
4378+
43784379
unset($updates['$id']);
43794380
unset($updates['$tenant']);
4381+
43804382
if (($updates->getCreatedAt() === null || !$this->preserveDates)) {
43814383
unset($updates['$createdAt']);
43824384
} else {
43834385
$updates['$createdAt'] = $updates->getCreatedAt();
43844386
}
4387+
43854388
if ($this->adapter->getSharedTables()) {
43864389
$updates['$tenant'] = $this->adapter->getTenant();
43874390
}
@@ -4431,7 +4434,7 @@ public function updateDocuments(
44314434
}
44324435

44334436
$this->withTransaction(function () use ($collection, $updates, &$batch) {
4434-
foreach ($batch as &$document) {
4437+
foreach ($batch as $index => $document) {
44354438
$new = new Document(\array_merge($document->getArrayCopy(), $updates->getArrayCopy()));
44364439

44374440
if ($this->resolveRelationships) {
@@ -4451,7 +4454,7 @@ public function updateDocuments(
44514454
throw new ConflictException('Document was updated after the request timestamp');
44524455
}
44534456

4454-
$document = $this->encode($collection, $document);
4457+
$batch[$index] = $this->encode($collection, $document);
44554458
}
44564459

44574460
$this->adapter->updateDocuments(

tests/e2e/Adapter/Base.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717

1818
abstract class Base extends TestCase
1919
{
20-
use CollectionTests;
21-
use DocumentTests;
22-
use AttributeTests;
23-
use IndexTests;
20+
// use CollectionTests;
21+
// use DocumentTests;
22+
// use AttributeTests;
23+
// use IndexTests;
2424
use PermissionTests;
2525
use RelationshipTests;
2626
use GeneralTests;

tests/e2e/Adapter/Scopes/PermissionTests.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,20 @@ public function testUnsetPermissions(): void
122122
}
123123

124124
$documents = $database->find(__FUNCTION__);
125+
$this->assertEquals(0, count($documents));
126+
127+
Authorization::disable();
128+
$documents = $database->find(__FUNCTION__);
129+
Authorization::reset();
125130

126131
$this->assertEquals(3, count($documents));
127132

128133
foreach ($documents as $document) {
129134
$this->assertEquals('Richard Nixon', $document->getAttribute('president'));
130-
/**
131-
* This is failing!!!
132-
*/
133135
$this->assertEquals([], $document->getPermissions());
134136
}
137+
138+
$this->assertEquals('shmuel', 'fogel');
135139
}
136140

137141
public function testCreateDocumentsEmptyPermission(): void

0 commit comments

Comments
 (0)