Closed
Description
The problem
As many of you know, the getRoles()
method of the UserInterface
doesn't take into account the role hierarchy that you may have defined in your application.
That's why usually you must end up with a code similar to the following:
use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
use Symfony\Component\Security\Core\User\UserInterface;
private $roleHierarchy;
public function __construct(RoleHierarchyInterface $roleHierarchy = null)
{
$this->roleHierarchy = $roleHierarchy;
}
private function userHasRole(UserInterface $user, $roleName)
{
if (null === $this->roleHierarchy) {
return in_array($roleName, $user->getRoles(), true);
}
foreach ($this->roleHierarchy->getReachableRoles($user->getRoles()) as $role) {
if ($roleName === $role->getRole()) {
return true;
}
}
return false;
}
The solution
The obvious solution would be to provide a method such as ->hasRole(string $role_name)
. What do you think?
Metadata
Metadata
Assignees
Labels
No labels