FOSUserBundle,用户(角色)和组(角色)之间的关系?

FOSUserBundle, Relation between User (roles) and Group (roles)?

我对 FOSUserBundle 中的角色有点困惑。用户实体也有角色列,我们可以通过它为用户分配多个角色。根据 Managing users/roles/groups in FOSUserBundle 上发布的答案,我们不需要单独的实体或表来管理用户角色。

我还实现了 FOSUserBundle 组,其中还包含一组角色,用户可以分配给这些角色。所以,到目前为止,我在这里的理解是我们可以创建组来分配一组角色,以便可以使用组访问类似的角色。

现在,我遇到的问题是,如果可以单独从用户实体管理一组角色,或者我如何合并来自用户实体和组实体的角色或我缺少的东西,那么使用组的目的是什么在吗?

Q: what is the purpose of using groups if set of roles can be managed from User Entity alone

A:答案可以从Using Groups With FOSUserBundle:

中提取

Symfony2 supports role inheritance so inheriting roles from groups is not always needed. If the role inheritance is enough for your use case, it is better to use it instead of groups as it is more efficient (loading the groups triggers the database).


Q:如果你问自己:什么是角色组?当我需要使用一组角色?

A:实质上 "Group" 包含一组角色,按共同特征分类(就像电子商务的类别).例如,控制面板应用程序的用户,具有 ROLE_ADMIN 作为普通角色,可能属于 MANAGER 组,而其他人属于 REVISER (这取决于您的需要) .将用户角色分类到组中可以更轻松地控制大量用户对特定工具或服务的访问。因此,使用或不使用角色组完全取决于您要对您的应用程序执行的操作以及用户可以与之进行交互的数量。


Q: how can I merge the roles from User entity and Group entity or something I am missing here?

A:如果角色继承不够,想使用"groups",需要创建User->Group->Role实体之间的关联像这个实现示例:

用户实体:

use Doctrine\Common\Collections\ArrayCollection;

/**
 * USER ManyToMany with GROUP Unidirectional association
 * @var ArrayCollection
 * @ORM\ManyToMany(targetEntity="Group")
 * @ORM\JoinTable(name="user_groups")
 * @Assert\Valid
 */
protected $groups;

public function __construct()
{
    $this->groups = new ArrayCollection(); <-- add this line to the constructor
}

/**
 * Get all Groups added to the administrator
 * @return ArrayCollection $groups
 */
public function getGroups()
{
    return $this->groups;
}

/**
 * Add Group $group to the administrator
 *
 * @param \Group $group
 * @return void
 */
public function addGroup(Group $group)
{
    if (!$this->groups->contains($group)) {
        $this->groups->add($group);
    }
}

/**
 * Remove Group from the user
 * @param \Group $group
 * @return void
 */
public function removeGroup(Group $group)
{
    if ($this->groups->contains($group)) {
        $this->groups->removeElement($group);
    }
}

/**
 * Get all Roles added to the User
 * @return array
 */
public function getRoles()
{
    $roles = [];
    /** @var ArrayCollection $groups */
    $groups = $this->getGroups();

    foreach ($groups as $group) {
        $roles = array_merge($roles, $group->getRoles()->toArray());
    }

    $roles = array_unique($roles);

    # store the roles in the property to be serialized
    $this->roles = $roles;

    return $roles;
}

实体:

/**
 * GROUP ManyToMany with ROLE Unidirectional Association
 * @var ArrayCollection
 * @ORM\ManyToMany(targetEntity="Role")
 * @ORM\JoinTable(name="user_group_roles")
 * @Assert\Valid
 */
protected $roles;

public function __construct()
{
    $this->roles       = new ArrayCollection();
}

/**
 * Return all Roles added to this Group
 *
 * @return \Doctrine\Common\Collections\ArrayCollection $roles
 */
public function getRoles()
{
    return $this->roles;
}

/**
 * Add Role $role to the Group
 *
 * @param Role $role
 * @return void
 */
public function addRole(Role $role)
{
    if (! $this->roles->contains($role)) {
        $this->roles->add($role);
    }
}

/**
 * Remove Role from the Group
 *
 * @param Role $role
 * @return void
 */
public function removeRole(Role $role)
{
    if ($this->roles->contains($role)) {
        $this->roles->removeElement($role);
    }
}

就是这样。希望对你有帮助。