Skip to content

Commit 7dbb302

Browse files
committed
Refactor authorization checks to use instance methods in Database and Mongo classes, improving consistency and readability. Update DocumentTests to remove unnecessary whitespace.
1 parent c6ad970 commit 7dbb302

5 files changed

Lines changed: 40 additions & 42 deletions

File tree

src/Database/Adapter/Mongo.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1868,8 +1868,8 @@ public function find(Document $collection, array $queries = [], ?int $limit = 25
18681868
}
18691869

18701870
// permissions
1871-
if (Authorization::$status) {
1872-
$roles = \implode('|', Authorization::getRoles());
1871+
if ($this->authorization->getStatus()) {
1872+
$roles = \implode('|', $this->authorization->getRoles());
18731873
$filters['_permissions']['$in'] = [new Regex("{$forPermission}\\(\".*(?:{$roles}).*\"\\)", 'i')];
18741874
}
18751875

@@ -2115,8 +2115,8 @@ public function count(Document $collection, array $queries = [], ?int $max = nul
21152115
}
21162116

21172117
// Add permissions filter if authorization is enabled
2118-
if (Authorization::$status) {
2119-
$roles = \implode('|', Authorization::getRoles());
2118+
if ($this->authorization->getStatus()) {
2119+
$roles = \implode('|', $this->authorization->getRoles());
21202120
$filters['_permissions']['$in'] = [new Regex("read\\(\".*(?:{$roles}).*\"\\)", 'i')];
21212121
}
21222122

@@ -2205,8 +2205,8 @@ public function sum(Document $collection, string $attribute, array $queries = []
22052205
}
22062206

22072207
// permissions
2208-
if (Authorization::$status) { // skip if authorization is disabled
2209-
$roles = \implode('|', Authorization::getRoles());
2208+
if ($this->authorization->getStatus()) { // skip if authorization is disabled
2209+
$roles = \implode('|', $this->authorization->getRoles());
22102210
$filters['_permissions']['$in'] = [new Regex("read\\(\".*(?:{$roles}).*\"\\)", 'i')];
22112211
}
22122212

src/Database/Database.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7236,10 +7236,7 @@ public function sum(string $collection, string $attribute, array $queries = [],
72367236
}
72377237
}
72387238

7239-
$authorization = new Authorization(self::PERMISSION_READ);
7240-
if ($authorization->isValid($collection->getRead())) {
7241-
$skipAuth = true;
7242-
}
7239+
$skipAuth = $this->authorization->isValid(new Input(self::PERMISSION_READ, $collection->getRead()));
72437240

72447241
$relationships = \array_filter(
72457242
$collection->getAttribute('attributes', []),
@@ -7257,7 +7254,7 @@ public function sum(string $collection, string $attribute, array $queries = [],
72577254
$queries = $queriesOrNull;
72587255

72597256
$getSum = fn () => $this->adapter->sum($collection, $attribute, $queries, $max);
7260-
$sum = $skipAuth ?? false ? Authorization::skip($getSum) : $getSum();
7257+
$sum = $skipAuth ? $this->authorization->skip($getSum) : $getSum();
72617258

72627259
$this->trigger(self::EVENT_DOCUMENT_SUM, $sum);
72637260

tests/e2e/Adapter/Schemaless/MongoDBTest.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static function getAdapterName(): string
2929
* @return Database
3030
* @throws Exception
3131
*/
32-
public static function getDatabase(): Database
32+
public function getDatabase(): Database
3333
{
3434
if (!is_null(self::$database)) {
3535
return self::$database;
@@ -53,6 +53,7 @@ public static function getDatabase(): Database
5353
$database = new Database(new Mongo($client), $cache);
5454
$database->getAdapter()->setSupportForAttributes(false);
5555
$database
56+
->setAuthorization(self::$authorization)
5657
->setDatabase($schema)
5758
->setNamespace(static::$namespace = 'myapp_' . uniqid());
5859

@@ -73,9 +74,9 @@ public function testCreateExistsDelete(): void
7374
{
7475
// Mongo creates databases on the fly, so exists would always pass. So we override this test to remove the exists check.
7576
$this->assertNotNull(static::getDatabase()->create());
76-
$this->assertEquals(true, static::getDatabase()->delete($this->testDatabase));
77-
$this->assertEquals(true, static::getDatabase()->create());
78-
$this->assertEquals(static::getDatabase(), static::getDatabase()->setDatabase($this->testDatabase));
77+
$this->assertEquals(true, $this->getDatabase()->delete($this->testDatabase));
78+
$this->assertEquals(true, $this->getDatabase()->create());
79+
$this->assertEquals($this->getDatabase(), $this->getDatabase()->setDatabase($this->testDatabase));
7980
}
8081

8182
public function testRenameAttribute(): void
@@ -98,12 +99,12 @@ public function testKeywords(): void
9899
$this->assertTrue(true);
99100
}
100101

101-
protected static function deleteColumn(string $collection, string $column): bool
102+
protected function deleteColumn(string $collection, string $column): bool
102103
{
103104
return true;
104105
}
105106

106-
protected static function deleteIndex(string $collection, string $index): bool
107+
protected function deleteIndex(string $collection, string $index): bool
107108
{
108109
return true;
109110
}

tests/e2e/Adapter/Scopes/DocumentTests.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3943,7 +3943,7 @@ public function testSum(): void
39433943
$this->assertEquals(2019, $sum);
39443944

39453945
$this->getDatabase()->getAuthorization()->removeRole('user:x');
3946-
3946+
39473947
$sum = $database->sum('movies', 'year', [Query::equal('year', [2019]),]);
39483948
$this->assertEquals(2019 + 2019, $sum);
39493949
$sum = $database->sum('movies', 'year');

tests/e2e/Adapter/Scopes/SchemalessTests.php

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ trait SchemalessTests
2020
public function testSchemalessDocumentOperation(): void
2121
{
2222
/** @var Database $database */
23-
$database = static::getDatabase();
23+
$database = $this->getDatabase();
2424

2525
if ($database->getAdapter()->getSupportForAttributes()) {
2626
$this->expectNotToPerformAssertions();
@@ -117,7 +117,7 @@ public function testSchemalessDocumentOperation(): void
117117
public function testSchemalessDocumentInvalidInteralAttributeValidation(): void
118118
{
119119
/** @var Database $database */
120-
$database = static::getDatabase();
120+
$database = $this->getDatabase();
121121

122122
// test to ensure internal attributes are checked during creating schemaless document
123123
if ($database->getAdapter()->getSupportForAttributes()) {
@@ -156,7 +156,7 @@ public function testSchemalessDocumentInvalidInteralAttributeValidation(): void
156156
public function testSchemalessSelectionOnUnknownAttributes(): void
157157
{
158158
/** @var Database $database */
159-
$database = static::getDatabase();
159+
$database = $this->getDatabase();
160160

161161
if ($database->getAdapter()->getSupportForAttributes()) {
162162
$this->expectNotToPerformAssertions();
@@ -203,7 +203,7 @@ public function testSchemalessSelectionOnUnknownAttributes(): void
203203
public function testSchemalessIncrement(): void
204204
{
205205
/** @var Database $database */
206-
$database = static::getDatabase();
206+
$database = $this->getDatabase();
207207

208208
if ($database->getAdapter()->getSupportForAttributes()) {
209209
$this->expectNotToPerformAssertions();
@@ -257,7 +257,7 @@ public function testSchemalessIncrement(): void
257257
public function testSchemalessDecrement(): void
258258
{
259259
/** @var Database $database */
260-
$database = static::getDatabase();
260+
$database = $this->getDatabase();
261261

262262
if ($database->getAdapter()->getSupportForAttributes()) {
263263
$this->expectNotToPerformAssertions();
@@ -311,7 +311,7 @@ public function testSchemalessDecrement(): void
311311
public function testSchemalessUpdateDocumentWithQuery(): void
312312
{
313313
/** @var Database $database */
314-
$database = static::getDatabase();
314+
$database = $this->getDatabase();
315315

316316
if ($database->getAdapter()->getSupportForAttributes()) {
317317
$this->expectNotToPerformAssertions();
@@ -369,7 +369,7 @@ public function testSchemalessUpdateDocumentWithQuery(): void
369369
public function testSchemalessDeleteDocumentWithQuery(): void
370370
{
371371
/** @var Database $database */
372-
$database = static::getDatabase();
372+
$database = $this->getDatabase();
373373

374374
if ($database->getAdapter()->getSupportForAttributes()) {
375375
$this->expectNotToPerformAssertions();
@@ -412,7 +412,7 @@ public function testSchemalessDeleteDocumentWithQuery(): void
412412
public function testSchemalessUpdateDocumentsWithQuery(): void
413413
{
414414
/** @var Database $database */
415-
$database = static::getDatabase();
415+
$database = $this->getDatabase();
416416

417417
if ($database->getAdapter()->getSupportForAttributes()) {
418418
$this->expectNotToPerformAssertions();
@@ -507,7 +507,7 @@ public function testSchemalessUpdateDocumentsWithQuery(): void
507507
public function testSchemalessDeleteDocumentsWithQuery(): void
508508
{
509509
/** @var Database $database */
510-
$database = static::getDatabase();
510+
$database = $this->getDatabase();
511511

512512
if ($database->getAdapter()->getSupportForAttributes()) {
513513
$this->expectNotToPerformAssertions();
@@ -589,7 +589,7 @@ public function testSchemalessDeleteDocumentsWithQuery(): void
589589
public function testSchemalessOperationsWithCallback(): void
590590
{
591591
/** @var Database $database */
592-
$database = static::getDatabase();
592+
$database = $this->getDatabase();
593593

594594
if ($database->getAdapter()->getSupportForAttributes()) {
595595
$this->expectNotToPerformAssertions();
@@ -677,7 +677,7 @@ public function testSchemalessOperationsWithCallback(): void
677677
public function testSchemalessIndexCreateListDelete(): void
678678
{
679679
/** @var Database $database */
680-
$database = static::getDatabase();
680+
$database = $this->getDatabase();
681681

682682
if ($database->getAdapter()->getSupportForAttributes()) {
683683
$this->expectNotToPerformAssertions();
@@ -723,7 +723,7 @@ public function testSchemalessIndexCreateListDelete(): void
723723
public function testSchemalessIndexDuplicatePrevention(): void
724724
{
725725
/** @var Database $database */
726-
$database = static::getDatabase();
726+
$database = $this->getDatabase();
727727

728728
if ($database->getAdapter()->getSupportForAttributes()) {
729729
$this->expectNotToPerformAssertions();
@@ -754,7 +754,7 @@ public function testSchemalessIndexDuplicatePrevention(): void
754754
public function testSchemalessPermissions(): void
755755
{
756756
/** @var Database $database */
757-
$database = static::getDatabase();
757+
$database = $this->getDatabase();
758758

759759
if ($database->getAdapter()->getSupportForAttributes()) {
760760
$this->expectNotToPerformAssertions();
@@ -776,18 +776,18 @@ public function testSchemalessPermissions(): void
776776
$this->assertFalse($doc->isEmpty());
777777

778778
// Without roles, cannot read
779-
Authorization::cleanRoles();
779+
$database->getAuthorization()->cleanRoles();
780780
$empty = $database->getDocument($col, 'd1');
781781
$this->assertTrue($empty->isEmpty());
782782

783783
// With any role, can read
784-
Authorization::setRole(Role::any()->toString());
784+
$database->getAuthorization()->addRole(Role::any()->toString());
785785
$fetched = $database->getDocument($col, 'd1');
786786
$this->assertEquals('value', $fetched->getAttribute('field'));
787787

788788
// Attempt update without update permission
789-
Authorization::cleanRoles();
790-
Authorization::setRole(Role::any()->toString());
789+
$database->getAuthorization()->cleanRoles();
790+
$database->getAuthorization()->addRole(Role::any()->toString());
791791
try {
792792
$database->updateDocument($col, 'd1', new Document(['field' => 'updated']));
793793
$this->fail('Failed to throw exception');
@@ -796,7 +796,7 @@ public function testSchemalessPermissions(): void
796796
}
797797

798798
// Grant update permission and update
799-
Authorization::skip(function () use ($database, $col) {
799+
$database->getAuthorization()->skip(function () use ($database, $col) {
800800
$database->updateDocument($col, 'd1', new Document([
801801
'$permissions' => [
802802
Permission::read(Role::any()),
@@ -809,7 +809,7 @@ public function testSchemalessPermissions(): void
809809
$this->assertEquals('updated', $updated->getAttribute('field'));
810810

811811
// Creating without any roles should fail
812-
Authorization::cleanRoles();
812+
$database->getAuthorization()->cleanRoles();
813813
try {
814814
$database->createDocument($col, new Document([
815815
'field' => 'x'
@@ -820,13 +820,13 @@ public function testSchemalessPermissions(): void
820820
}
821821

822822
$database->deleteCollection($col);
823-
Authorization::cleanRoles();
823+
$database->getAuthorization()->cleanRoles();
824824
}
825825

826826
public function testSchemalessInternalAttributes(): void
827827
{
828828
/** @var Database $database */
829-
$database = static::getDatabase();
829+
$database = $this->getDatabase();
830830

831831
if ($database->getAdapter()->getSupportForAttributes()) {
832832
$this->expectNotToPerformAssertions();
@@ -836,7 +836,7 @@ public function testSchemalessInternalAttributes(): void
836836
$col = uniqid('sl_internal_full');
837837
$database->createCollection($col);
838838

839-
Authorization::setRole(Role::any()->toString());
839+
$database->getAuthorization()->addRole(Role::any()->toString());
840840

841841
$doc = $database->createDocument($col, new Document([
842842
'$id' => 'i1',
@@ -930,13 +930,13 @@ public function testSchemalessInternalAttributes(): void
930930
$database->setPreserveDates(false);
931931

932932
$database->deleteCollection($col);
933-
Authorization::cleanRoles();
933+
$database->getAuthorization()->cleanRoles();
934934
}
935935

936936
public function testSchemalessDates(): void
937937
{
938938
/** @var Database $database */
939-
$database = static::getDatabase();
939+
$database = $this->getDatabase();
940940

941941
if ($database->getAdapter()->getSupportForAttributes()) {
942942
$this->expectNotToPerformAssertions();

0 commit comments

Comments
 (0)