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

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

问题描述

我正在使用FOS用户捆绑包来管理对我的应用程序的访问. 由于我需要一个小组的概念,我已经实施了我所需的内容,如doc .

一切都很好,适用于创建用户和组,但是当我与用户登录并试图扮演角色时,他只有用户_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. 该小组具有角色_ADMIN角色.

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

    {% 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没有显示任何内容.

我是否使用不良功能来检查集体角色? 还是我做错了什么?

一个解决方案正在通过

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

但是还有其他方法可以这样做吗?

推荐答案

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

来自

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

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

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

app.user.hasGroup('ADMIN')

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

问题描述

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 .

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.

Here is my User

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;
 [...]
}

Here is my Group class

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

[...]
}

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 %}

But Symfony doesn't display anything.

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

A solution is getting groups_role by

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

But is there any other way for doing this ?

推荐答案

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.

From Using Groups With 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')