Skip to content

Commit 8e0d6f1

Browse files
authored
Merge pull request #281 from 2002Bishwajeet/fix-permission-object
Fix: $permissions returns as object rather than array
2 parents d821d1f + 770ef5b commit 8e0d6f1

4 files changed

Lines changed: 62 additions & 2 deletions

File tree

src/Database/Document.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function getCollection(): string
7979
*/
8080
public function getPermissions(): array
8181
{
82-
return \array_unique($this->getAttribute('$permissions', []));
82+
return \array_values(\array_unique($this->getAttribute('$permissions', [])));
8383
}
8484

8585
/**

src/Database/Helpers/Permission.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public static function aggregate(?array $permissions, array $allowed = Database:
170170
}
171171
}
172172
}
173-
return $mutated;
173+
return \array_values(\array_unique($mutated));
174174
}
175175

176176
/**

tests/Database/PermissionTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,5 +284,23 @@ public function testAggregation(): void
284284

285285
$parsed = Permission::aggregate($permissions, [Database::PERMISSION_UPDATE, Database::PERMISSION_DELETE]);
286286
$this->assertEquals(['update("any")', 'delete("any")'], $parsed);
287+
288+
$permissions = [
289+
'read("any")',
290+
'read("user:123")',
291+
'read("user:123")',
292+
'write("user:123")',
293+
'update("user:123")',
294+
'delete("user:123")'
295+
];
296+
297+
$parsed = Permission::aggregate($permissions, Database::PERMISSIONS);
298+
$this->assertEquals([
299+
'read("any")',
300+
'read("user:123")',
301+
'create("user:123")',
302+
'update("user:123")',
303+
'delete("user:123")',
304+
], $parsed);
287305
}
288306
}

tests/Database/Validator/PermissionsTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,4 +297,46 @@ public function testInvalidPermissions(): void
297297
$this->assertFalse($object->isValid($permissions));
298298
$this->assertEquals('You can only provide up to 100 permissions.', $object->getDescription());
299299
}
300+
301+
/*
302+
* Test for checking duplicate methods input. The getPermissions should return an a list array
303+
*/
304+
public function testDuplicateMethods(): void
305+
{
306+
$validator = new Permissions();
307+
308+
$user = ID::unique();
309+
310+
$document = new Document([
311+
'$id' => uniqid(),
312+
'$collection' => uniqid(),
313+
'$permissions' => [
314+
Permission::read(Role::any()),
315+
Permission::read(Role::user($user)),
316+
Permission::read(Role::user($user)),
317+
Permission::write(Role::user($user)),
318+
Permission::update(Role::user($user)),
319+
Permission::delete(Role::user($user)),
320+
],
321+
'title' => 'This is a test.',
322+
'list' => [
323+
'one'
324+
],
325+
'children' => [
326+
new Document(['name' => 'x']),
327+
new Document(['name' => 'y']),
328+
new Document(['name' => 'z']),
329+
]
330+
]);
331+
$this->assertTrue($validator->isValid($document->getPermissions()));
332+
$permissions = $document->getPermissions();
333+
$this->assertEquals(5, count($permissions));
334+
$this->assertEquals([
335+
'read("any")',
336+
'read("user:' . $user . '")',
337+
'write("user:' . $user . '")',
338+
'update("user:' . $user . '")',
339+
'delete("user:' . $user . '")',
340+
], $permissions);
341+
}
300342
}

0 commit comments

Comments
 (0)