2

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

mike
  • 1,583
  • 1
  • 19
  • 35
  • Possible duplicate of [How to retrieve full role hierarchy in Symfony](http://stackoverflow.com/questions/8928085/how-to-retrieve-full-role-hierarchy-in-symfony) – A.L Sep 13 '16 at 20:32
  • 1
    I just want the authenticated users roles, not the entire heirachy – mike Sep 13 '16 at 20:34
  • Ok, this other question may help you: http://stackoverflow.com/questions/11246774/symfony2-getting-the-list-of-user-roles-in-formbuilder (not the first answer) – A.L Sep 13 '16 at 20:37
  • Thanks, it's not quite what I'm looking for. I've put together a simple repository function to grab the info I need. I'll add an answer in case anyone else comes across this – mike Sep 13 '16 at 20:51
  • Sorry, it didn't check the second link, the answers don't show how to get all the roles of an user. – A.L Sep 13 '16 at 20:52
  • no worries, I have a simple solution anyway, just wanted to see if there was something already there that I could use instead. – mike Sep 13 '16 at 20:56

2 Answers2

2

I ended up adding a new repository function and a service method to get this info.

MyProject/UserBundle/Entity/Repository/UserRepository

public function getRoles($userId)
{
    $queryBuilder = $this->createQueryBuilder('u');
    $queryBuilder
        ->select('u.id, u.roles AS user_roles, g.roles AS group_roles')
        ->leftJoin('u.groups', 'g')
        ->andWhere('u.id = :user_id')
        ->setParameter('user_id', $userId);

    return $queryBuilder->getQuery()->getArrayResult();
}

MyProject/UserBundle/Service/UserService

public function getUserRoles($user)
{
    $groupRoles = $this->repository->getRoles($user->getId());

    $roles = array('user_roles' => array(), 'group_roles' => array());
    foreach ($groupRoles as $groupRole) {
        $roles['user_roles'] = array_merge($roles['user_roles'], $groupRole['user_roles']);
        $roles['group_roles'] = array_merge($roles['group_roles'], $groupRole['group_roles']);
    }

    return $roles;
}

This gives me an array like this

"roles":{
    "user_roles":[],
    "group_roles":["ROLE_ADMIN","ROLE_ONE","ROLE_TWO","ROLE_BEST"]
}
mike
  • 1,583
  • 1
  • 19
  • 35
1

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

Sam Dufel
  • 17,560
  • 3
  • 48
  • 51