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


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

我对FOSUserBundle中的角色有点困惑。用户实体还具有角色列,通过该列我们可以向用户分配多个角色。根据 FOSUserBundle 中管理用户/角色/组中发布的答案,我们不需要单独的实体或表来管理用户角色。

我还实现了 FOSUserBundle 组,其中还包含一组角色,可以将用户分配给这些角色。因此,据我所知,可以创建组来分配一组角色,以便可以使用组访问类似的角色。

现在,我

陷入困境的是,如果角色集可以单独从用户实体管理,或者如何合并来自用户实体和组实体的角色或我在这里缺少的东西,那么使用组的目的是什么?

:如果角色集可以仅从用户实体进行管理,那么使用组的目的是什么

:答案可以从使用具有FOSUserBundle的组中提取:

Symfony2支持角色继承,因此从组继承角色是 并不总是需要。如果角色继承足以供您使用 在这种情况下,最好使用它而不是组,因为它更有效 (加载组会触发数据库)。


问:

如果您自己:什么是角色组?或者何时需要使用角色组?

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


:如何合并用户实体和组实体中的角色,或者 我在这里缺少什么?

:如果角色继承不够,并且您想使用"组",则需要在用户 -> 组 -> 角色实体之间创建关联,如以下实现示例所示:

用户实体:

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

就这样。希望对您有所帮助。