Skip to content

Instantly share code, notes, and snippets.

@stenno
Last active October 27, 2021 06:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save stenno/3abf180666d6e4d396fc1c1b26836cf9 to your computer and use it in GitHub Desktop.
Save stenno/3abf180666d6e4d396fc1c1b26836cf9 to your computer and use it in GitHub Desktop.
Getting role hierarchy of logged in user in Symfony 5
<?php
namespace App\Controller;
use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
class SecurityController extends AbstractController
{
/* ... */
public function __construct(RoleHierarchyInterface $roleHierarchy)
{
$this->roleHierarchy = $roleHierarchy;
}
/* ... */
/**
* @Route("/roles", name="app_roles")
*/
public function getRoles() {
$user = $this->getUser();
if (!$user) {
return $this->json([]);
}
$userRoles = $user->getRoles();
$allRoles = $this->roleHierarchy->getReachableRoleNames($userRoles);
return $this->json($allRoles);
}
/* ... */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment