FOSUserBundle: User doesn't get roles of group

我只是一个虾纸丫 提交于 2019-12-08 04:03:11

问题


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:


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.



来源:https://stackoverflow.com/questions/25654270/fosuserbundle-user-doesnt-get-roles-of-group

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!