Skip to content

Commit 6530a8a

Browse files
authored
Merge pull request #549 from utopia-php/fix-tenant
Cast tenant
2 parents 740063b + 1b7d8b7 commit 6530a8a

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

Diff for: src/Database/Database.php

+7-9
Original file line numberDiff line numberDiff line change
@@ -1260,7 +1260,7 @@ public function updateCollection(string $id, array $permissions, bool $documentS
12601260

12611261
if (
12621262
$this->adapter->getSharedTables()
1263-
&& $collection->getAttribute('$tenant') != $this->adapter->getTenant()
1263+
&& $collection->getTenant() !== $this->adapter->getTenant()
12641264
) {
12651265
throw new NotFoundException('Collection not found');
12661266
}
@@ -1288,13 +1288,11 @@ public function getCollection(string $id): Document
12881288
{
12891289
$collection = $this->silent(fn () => $this->getDocument(self::METADATA, $id));
12901290

1291-
$tenant = $collection->getAttribute('$tenant');
1292-
12931291
if (
12941292
$id !== self::METADATA
12951293
&& $this->adapter->getSharedTables()
1296-
&& $tenant !== null
1297-
&& $tenant != $this->adapter->getTenant()
1294+
&& $collection->getTenant() !== null
1295+
&& $collection->getTenant() !== $this->adapter->getTenant()
12981296
) {
12991297
return new Document();
13001298
}
@@ -1341,7 +1339,7 @@ public function getSizeOfCollection(string $collection): int
13411339
throw new NotFoundException('Collection not found');
13421340
}
13431341

1344-
if ($this->adapter->getSharedTables() && $collection->getAttribute('$tenant') != $this->adapter->getTenant()) {
1342+
if ($this->adapter->getSharedTables() && $collection->getTenant() !== $this->adapter->getTenant()) {
13451343
throw new NotFoundException('Collection not found');
13461344
}
13471345

@@ -1367,7 +1365,7 @@ public function getSizeOfCollectionOnDisk(string $collection): int
13671365
throw new NotFoundException('Collection not found');
13681366
}
13691367

1370-
if ($this->adapter->getSharedTables() && $collection->getAttribute('$tenant') != $this->adapter->getTenant()) {
1368+
if ($this->adapter->getSharedTables() && $collection->getTenant() !== $this->adapter->getTenant()) {
13711369
throw new NotFoundException('Collection not found');
13721370
}
13731371

@@ -1390,7 +1388,7 @@ public function deleteCollection(string $id): bool
13901388
throw new NotFoundException('Collection not found');
13911389
}
13921390

1393-
if ($this->adapter->getSharedTables() && $collection->getAttribute('$tenant') != $this->adapter->getTenant()) {
1391+
if ($this->adapter->getSharedTables() && $collection->getTenant() !== $this->adapter->getTenant()) {
13941392
throw new NotFoundException('Collection not found');
13951393
}
13961394

@@ -3873,7 +3871,7 @@ public function updateDocument(string $collection, string $id, Document $documen
38733871
$document['$createdAt'] = $old->getCreatedAt(); // Make sure user doesn't switch createdAt
38743872

38753873
if ($this->adapter->getSharedTables()) {
3876-
$document['$tenant'] = $old->getAttribute('$tenant'); // Make sure user doesn't switch tenant
3874+
$document['$tenant'] = $old->getTenant(); // Make sure user doesn't switch tenant
38773875
}
38783876

38793877
$document = new Document($document);

Diff for: tests/e2e/Adapter/Base.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -16302,7 +16302,7 @@ public function testSharedTables(): void
1630216302

1630316303
$doc = $database->getDocument('people', $docId);
1630416304
$this->assertEquals('Spiderman', $doc['name']);
16305-
$this->assertEquals($tenant1, $doc->getAttribute('$tenant'));
16305+
$this->assertEquals($tenant1, $doc->getTenant());
1630616306

1630716307
/**
1630816308
* Remove Permissions
@@ -16315,7 +16315,7 @@ public function testSharedTables(): void
1631516315

1631616316
$doc = $database->getDocument('people', $docId);
1631716317
$this->assertEquals([Permission::read(Role::any())], $doc['$permissions']);
16318-
$this->assertEquals($tenant1, $doc->getAttribute('$tenant'));
16318+
$this->assertEquals($tenant1, $doc->getTenant());
1631916319

1632016320
/**
1632116321
* Add Permissions
@@ -16512,7 +16512,7 @@ public function testSharedTablesTenantPerDocument(): void
1651216512
->getDocument(__FUNCTION__, $doc2Id);
1651316513

1651416514
$this->assertEquals('Batman', $doc['name']);
16515-
$this->assertEquals(2, $doc->getAttribute('$tenant'));
16515+
$this->assertEquals(2, $doc->getTenant());
1651616516

1651716517
// Ensure no read cross-tenant
1651816518
$docs = $database

0 commit comments

Comments
 (0)