From 348b15726c06064f76356abb2eb516509f7855bb Mon Sep 17 00:00:00 2001 From: fogelito Date: Mon, 31 Mar 2025 14:45:00 +0300 Subject: [PATCH 1/3] Cast tenant --- src/Database/Database.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index 6e91260e0..e8145d46b 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -3863,6 +3863,9 @@ public function updateDocument(string $collection, string $id, Document $documen $document = $this->withTransaction(function () use ($collection, $id, $document) { $time = DateTime::now(); + /** + * @var $old Document + */ $old = Authorization::skip(fn () => $this->silent( fn () => $this->getDocument($collection->getId(), $id, forUpdate: true) @@ -3873,7 +3876,7 @@ public function updateDocument(string $collection, string $id, Document $documen $document['$createdAt'] = $old->getCreatedAt(); // Make sure user doesn't switch createdAt if ($this->adapter->getSharedTables()) { - $document['$tenant'] = $old->getAttribute('$tenant'); // Make sure user doesn't switch tenant + $document['$tenant'] = $old->getTenant(); // Make sure user doesn't switch tenant } $document = new Document($document); From 393cbc3e97777890d40cd51059c3123ab065d08e Mon Sep 17 00:00:00 2001 From: fogelito Date: Mon, 31 Mar 2025 14:45:50 +0300 Subject: [PATCH 2/3] Remove hint --- src/Database/Database.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index e8145d46b..df2dee94f 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -3863,9 +3863,6 @@ public function updateDocument(string $collection, string $id, Document $documen $document = $this->withTransaction(function () use ($collection, $id, $document) { $time = DateTime::now(); - /** - * @var $old Document - */ $old = Authorization::skip(fn () => $this->silent( fn () => $this->getDocument($collection->getId(), $id, forUpdate: true) From 4b1aec42000b8190e29d2ce0981e0601fbf228c8 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 2 Apr 2025 12:30:01 +1300 Subject: [PATCH 3/3] Use getTenant for all cases --- src/Database/Database.php | 14 ++++++-------- tests/e2e/Adapter/Base.php | 6 +++--- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index df2dee94f..5e2658b77 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -1260,7 +1260,7 @@ public function updateCollection(string $id, array $permissions, bool $documentS if ( $this->adapter->getSharedTables() - && $collection->getAttribute('$tenant') != $this->adapter->getTenant() + && $collection->getTenant() !== $this->adapter->getTenant() ) { throw new NotFoundException('Collection not found'); } @@ -1288,13 +1288,11 @@ public function getCollection(string $id): Document { $collection = $this->silent(fn () => $this->getDocument(self::METADATA, $id)); - $tenant = $collection->getAttribute('$tenant'); - if ( $id !== self::METADATA && $this->adapter->getSharedTables() - && $tenant !== null - && $tenant != $this->adapter->getTenant() + && $collection->getTenant() !== null + && $collection->getTenant() !== $this->adapter->getTenant() ) { return new Document(); } @@ -1341,7 +1339,7 @@ public function getSizeOfCollection(string $collection): int throw new NotFoundException('Collection not found'); } - if ($this->adapter->getSharedTables() && $collection->getAttribute('$tenant') != $this->adapter->getTenant()) { + if ($this->adapter->getSharedTables() && $collection->getTenant() !== $this->adapter->getTenant()) { throw new NotFoundException('Collection not found'); } @@ -1367,7 +1365,7 @@ public function getSizeOfCollectionOnDisk(string $collection): int throw new NotFoundException('Collection not found'); } - if ($this->adapter->getSharedTables() && $collection->getAttribute('$tenant') != $this->adapter->getTenant()) { + if ($this->adapter->getSharedTables() && $collection->getTenant() !== $this->adapter->getTenant()) { throw new NotFoundException('Collection not found'); } @@ -1390,7 +1388,7 @@ public function deleteCollection(string $id): bool throw new NotFoundException('Collection not found'); } - if ($this->adapter->getSharedTables() && $collection->getAttribute('$tenant') != $this->adapter->getTenant()) { + if ($this->adapter->getSharedTables() && $collection->getTenant() !== $this->adapter->getTenant()) { throw new NotFoundException('Collection not found'); } diff --git a/tests/e2e/Adapter/Base.php b/tests/e2e/Adapter/Base.php index e0df2acf5..c06b6c500 100644 --- a/tests/e2e/Adapter/Base.php +++ b/tests/e2e/Adapter/Base.php @@ -16302,7 +16302,7 @@ public function testSharedTables(): void $doc = $database->getDocument('people', $docId); $this->assertEquals('Spiderman', $doc['name']); - $this->assertEquals($tenant1, $doc->getAttribute('$tenant')); + $this->assertEquals($tenant1, $doc->getTenant()); /** * Remove Permissions @@ -16315,7 +16315,7 @@ public function testSharedTables(): void $doc = $database->getDocument('people', $docId); $this->assertEquals([Permission::read(Role::any())], $doc['$permissions']); - $this->assertEquals($tenant1, $doc->getAttribute('$tenant')); + $this->assertEquals($tenant1, $doc->getTenant()); /** * Add Permissions @@ -16512,7 +16512,7 @@ public function testSharedTablesTenantPerDocument(): void ->getDocument(__FUNCTION__, $doc2Id); $this->assertEquals('Batman', $doc['name']); - $this->assertEquals(2, $doc->getAttribute('$tenant')); + $this->assertEquals(2, $doc->getTenant()); // Ensure no read cross-tenant $docs = $database