FOSUserBundle:用户未获得组的角色 [英] FOSUserBundle: User doesn't get roles of group

查看:76
本文介绍了FOSUserBundle:用户未获得组的角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用symfony 2.5并尝试检查用户是否具有特定角色.在数据库中正确设置了表,并正确插入了数据:

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:

数据库中存在一个用户test@example.com,该用户test@example.com具有已定义角色a:1:{i:0;s:10:"ROLE_ADMIN";}

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";}

我不知道为什么角色阅读不正确.调试工具栏告诉我,我仅被认证为ROLE_USER.

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

代码:

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

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

我找到了这个问题( Symfony 2 FOS UserBundle用户没有获得组的角色),这似乎与我的问题有关,但我对答案不满意,因为我不想检查组访问权限,而是要检查角色访问权限.就我而言,组权限将来可能会更改.

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.

感谢您的帮助!

推荐答案

好的-看来我自己找到了解决方案.

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.

上面的代码正确,并且在is用户再次登录后,分配了正确的角色.

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

这篇关于FOSUserBundle:用户未获得组的角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆