Symfony 2 FOS UserBundle用户没有获得组的角色 [英] Symfony 2 FOS UserBundle users doesn't get group's role

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

问题描述

我正在使用FOS User Bundle来管理对我的应用程序的访问. 当我需要一个小组概念时,我已经按照文档.

I'm using FOS User Bundle to manage access to my app. As i need a group notion, i've implemented what i need as described in the doc group .

当我与用户登录时,创建用户和组BUT一切都很好,并且试图获得他的角色,除了USER_ROLE之外,他什么都没有.

Everything's fine for creating users and groups BUT when i'm logging in with a user, and trying to get his role, he has none but USER_ROLE.

这是我的用户

use FOS\UserBundle\Entity\User as BaseUser;
class RegistredUser extends BaseUser
{
/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @ORM\ManyToOne(targetEntity="myBundle\Entity\RegistredUserGroup")
 */
protected $group;
 [...]
}

这是我的小组课程

use FOS\UserBundle\Entity\Group as BaseGroup;
class RegistredUserGroup extends BaseGroup
{
/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

[...]
}

我的用户在RegistredUser表中没有设置任何角色,但是设置了groupId. 该组具有ROLE_ADMIN角色.

My User has none roles setted in the RegistredUser table, but has a groupId setted. This group have the ROLE_ADMIN role.

在我的布局中,我尝试检查这样的角色:

In my layout, i try to check role like this :

    {% if is_granted('ROLE_ADMIN') %}
    <dl class="menuIcon">
                    <dd><img src="{{ asset('bundles/bundle/images/user.png') }}" alt=""></dd>
                    <dd>{{ "menu.gererReferentiel"|trans }}</dd>
                    </dl>
    {% endif %}

但是Symfony没有显示任何内容.

But Symfony doesn't display anything.

我是否使用错误的功能来检查分组角色? 还是我做错了其他事情?

Am i using a bad function for checking groupe roles ? Or am i doing something else wrong ?

一种解决方案是让groups_role通过

A solution is getting groups_role by

{% if app.user != null and  app.user.group.hasRole('ROLE_ADMIN') %}

但是还有其他方法吗?

推荐答案

FOSUserBundle允许您将组与用户关联.组是 一种对角色集合进行分组的方法.小组的角色将是 授予属于它的所有用户.

FOSUserBundle allows you to associate groups to your users. Groups are a way to group a collection of roles. The roles of a group will be granted to all users belonging to it.

来自 使用具有FOSUserBundle的组

在您的情况下,您不应该检查

In your case you should not check

app.user.group.hasRole('ROLE_ADMIN')

如果用户有一个具有管理员角色的组,但请检查当前用户是否是某个组的成员,例如

if a user has a group which has the role admin, but check if the current user is member of a certain group, e.g.

app.user.hasGroup('ADMIN')

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

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