Skip to content

Commit

Permalink
Improve and test getting items by user id
Browse files Browse the repository at this point in the history
  • Loading branch information
arogachev committed Nov 21, 2023
1 parent 0756b02 commit a58c8eb
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 16 deletions.
14 changes: 12 additions & 2 deletions src/ItemsStorageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,23 @@ public function clear(): void;
*/
public function getAll(): array;

/**
* Returns roles and permission by the given names' list.
*
* @param string[] $names List of role and/or permission names.
*
* @return array Array of role and permission instances indexed by their corresponding names.
* @psalm-return ItemsIndexedByName
*/
public function getByNames(array $names): array;

/**
* Returns the named role or permission.
*
* @param string $name The role or the permission name.
*
* @return Permission|Role|null The role or the permission corresponding to the specified name. `null` is returned if no such
* item.
* @return Permission|Role|null The role or the permission corresponding to the specified name. `null` is returned
* if there is no such item.
*/
public function get(string $name): Permission|Role|null;

Expand Down
10 changes: 7 additions & 3 deletions src/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,14 @@ public function getItemsByUserId(int|Stringable|string $userId): array
{
$userId = (string) $userId;
$assignments = $this->assignmentsStorage->getByUserId($userId);
$items = $this->getDefaultRoles();
$parentItems = array_merge(
$this->getDefaultRoles(),
$this->itemsStorage->getByNames(array_keys($assignments)),
);
$items = $parentItems;

foreach ($assignments as $assignment) {
$items = array_merge($items, $this->itemsStorage->getAllChildren($assignment->getItemName()));
foreach ($parentItems as $item) {
$items = array_merge($items, $this->itemsStorage->getAllChildren($item->getName()));
}

return $items;
Expand Down
24 changes: 13 additions & 11 deletions tests/Common/ManagerLogicTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -509,23 +509,27 @@ public function testAssignPermissionDirectlyWhenItIsEnabled(): void
$this->assertTrue($manager->userHasPermission('reader', 'updateAnyPost'));
}

public function testGetRolesByUser(): void
public function testGetItemsByUserId(): void
{
$manager = $this->createFilledManager();
$this->assertEqualsCanonicalizing(
['myDefaultRole', 'reader', 'readPost', 'Fast Metabolism'],
array_keys($this->createFilledManager()->getItemsByUserId('reader A'))
);
}

$this->assertEquals(
public function testGetRolesByUserId(): void
{
$this->assertEqualsCanonicalizing(
['myDefaultRole', 'reader'],
array_keys($manager->getRolesByUserId('reader A'))
array_keys($this->createFilledManager()->getRolesByUserId('reader A'))
);
}

public function testGetChildRoles(): void
{
$manager = $this->createFilledManager();

$this->assertEqualsCanonicalizing(
['reader', 'author'],
array_keys($manager->getChildRoles('admin'))
array_keys($this->createFilledManager()->getChildRoles('admin'))
);
}

Expand All @@ -551,13 +555,11 @@ public function testGetPermissionsByRoleName(): void
$this->assertEmpty($manager->getPermissionsByRoleName('guest'));
}

public function testGetPermissionsByUser(): void
public function testGetPermissionsByUserId(): void
{
$manager = $this->createFilledManager();

$this->assertEqualsCanonicalizing(
['deletePost', 'publishPost', 'createPost', 'updatePost', 'readPost'],
array_keys($manager->getPermissionsByUserId('author B'))
array_keys($this->createFilledManager()->getPermissionsByUserId('author B'))
);
}

Expand Down
5 changes: 5 additions & 0 deletions tests/Support/FakeItemsStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public function getAll(): array
return $this->items;
}

public function getByNames(array $names): array
{
return array_filter($this->getAll(), static fn (Item $item): bool => in_array($item->getName(), $names));
}

public function get(string $name): Permission|Role|null
{
return $this->items[$name] ?? null;
Expand Down

0 comments on commit a58c8eb

Please sign in to comment.