phpsymfonysecurity-context

In Symfony2 how can I get a users full list of roles


I would like to pass the authenticated users list of roles to my front end apps, so I can use the same access control structure in the front and back end.

I was looking in the security / authentication classes as that is where the isGranted function are for me to do this

$this->container->get('security.context')->isGranted('ROLE_SUPER_ADMIN')

I can't find anything to get a list of roles though, is this not a supported feature?

nb: I don't want the entire role hierarchy, just the list of roles for the authenticated user


Solution

  • Assuming you're using the Symfony security component, the user interface which your user class implements has this already included:

    $user = $this->get('security.token_storage')->getToken()->getUser();
    var_dump($user->getRoles());
    

    http://api.symfony.com/3.1/Symfony/Component/Security/Core/User/UserInterface.html#method_getRoles