Skip to content

[Security] Add retrieval of encompassing role names #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 7.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/Security/Core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ CHANGELOG
=========


7.1
---

* Add `getEncompassingRoleNames` method to `RoleHierarchyInterface`

7.0
---

Expand Down
16 changes: 16 additions & 0 deletions src/Symfony/Component/Security/Core/Role/RoleHierarchy.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,22 @@ public function getReachableRoleNames(array $roles): array
return array_values(array_unique($reachableRoles));
}

public function getEncompassingRoleNames(array $roles): array
{
$encompassingRoles = $roles;

foreach ($roles as $role) {
foreach ($this->map as $parent => $children) {
if (in_array($role, $children)) {
$encompassingRoles[] = $parent;
}

}
}

return array_values(array_unique($encompassingRoles));
}

protected function buildRoleMap(): void
{
$this->map = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
/**
* RoleHierarchyInterface is the interface for a role hierarchy.
*
* @method array getEncompassingRoleNames(array $roles)
*
* @author Fabien Potencier <[email protected]>
*/
interface RoleHierarchyInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,21 @@ public function testGetReachableRoleNames()
$this->assertEquals(['ROLE_SUPER_ADMIN', 'ROLE_ADMIN', 'ROLE_FOO', 'ROLE_USER'], $role->getReachableRoleNames(['ROLE_SUPER_ADMIN']));
$this->assertEquals(['ROLE_SUPER_ADMIN', 'ROLE_ADMIN', 'ROLE_FOO', 'ROLE_USER'], $role->getReachableRoleNames(['ROLE_SUPER_ADMIN', 'ROLE_SUPER_ADMIN']));
}

public function testGetEncompassingRoleNames()
{
$role = new RoleHierarchy([
'ROLE_ADMIN' => ['ROLE_USER'],
'ROLE_SUPER_ADMIN' => ['ROLE_ADMIN', 'ROLE_FOO'],
'ROLE_USER' => ['ROLE_BAR'],
]);

$this->assertEquals(['ROLE_SUPER_ADMIN'], $role->getEncompassingRoleNames(['ROLE_SUPER_ADMIN']));
$this->assertEquals(['ROLE_ADMIN', 'ROLE_SUPER_ADMIN'], $role->getEncompassingRoleNames(['ROLE_ADMIN']));
$this->assertEquals(['ROLE_USER', 'ROLE_ADMIN', 'ROLE_SUPER_ADMIN'], $role->getEncompassingRoleNames(['ROLE_USER']));
$this->assertEquals(['ROLE_BAR', 'ROLE_ADMIN', 'ROLE_SUPER_ADMIN', 'ROLE_USER'], $role->getEncompassingRoleNames(['ROLE_BAR']));
$this->assertEquals(['ROLE_SUPER_ADMIN'], $role->getEncompassingRoleNames(['ROLE_SUPER_ADMIN', 'ROLE_SUPER_ADMIN']));
$this->assertEquals(['ROLE_SUPER_ADMIN', 'ROLE_USER', 'ROLE_ADMIN'], $role->getEncompassingRoleNames(['ROLE_SUPER_ADMIN', 'ROLE_USER']));
$this->assertEquals(['ROLE_BAR', 'ROLE_FOO','ROLE_ADMIN', 'ROLE_SUPER_ADMIN', 'ROLE_USER'], $role->getEncompassingRoleNames(['ROLE_BAR', 'ROLE_FOO']));
}
}