FOSUserBundle: User doesn't get roles of group

2019-05-29 10:07发布

I am using symfony 2.5 and trying to check if a user has a specific role. The tables are set up correctly in the database and the data is correct inserted:

In the database exists a user test@example.com with a mapped group admin which has defined the roles a:1:{i:0;s:10:"ROLE_ADMIN";}

I don't know why the roles aren't read correct. The debug-toolbar tells me, that i am only authenticated as ROLE_USER.

Code:

$securityContext = $this->container->get('security.context');    
$securityContext->isGranted('ROLE_ADMIN');

if ($securityContext->isGranted('ROLE_ADMIN')) {
    echo 'crazy coding magic happens here';
}

I have found this question (Symfony 2 FOS UserBundle users doesn't get group's role) which seems to be related to my question, but i am not satisfied with the answer, because i don't want to check the group-access but the role-access. In my case group permissions could change in the future.

Thanks for your help!

1条回答
可以哭但决不认输i
2楼-- · 2019-05-29 10:36

Okay - it seems i have found the solution by myself.

The problem is that you have to sign off the logged in user and sign in again to recognize changes in the group-role-mapping.

The code above is correct and after the is user is logged in again the correct roles are assigned.

查看更多
登录 后发表回答