Last active
October 27, 2021 06:05
-
-
Save stenno/3abf180666d6e4d396fc1c1b26836cf9 to your computer and use it in GitHub Desktop.
Getting role hierarchy of logged in user in Symfony 5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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