FOSUserBundle。用户没有得到组的角色[英] FOSUserBundle: User doesn't get roles of group

本文是小编为大家收集整理的关于FOSUserBundle。用户没有得到组的角色的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

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

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

我不知道为什么角色不正确. Debug-Toolbar告诉我,我只被认证为ROLE_USER.

代码:

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

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

我找到了这个问题( symfony "> symfony>似乎没有获得小组的角色),这似乎与我的问题有关,但我对答案不满意,因为我不想检查群体访问,而是扮演角色访问.在我的情况下,将来可能会发生变化.

感谢您的帮助!

推荐答案

好吧 - 似乎我自己找到了解决方案.

问题在于,您必须登录登录的用户并再次登录以识别组量映射中的更改.

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

本文地址:https://www.itbaoku.cn/post/2090799.html

问题描述

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!

推荐答案

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.