【问题标题】:FOSUserBundle, Relation between User (roles) and Group (roles)?FOSUserBundle,用户(角色)和组(角色)之间的关系?
【发布时间】:2015-05-18 08:50:03
【问题描述】:

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

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

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

【问题讨论】:

    标签: php symfony fosuserbundle


    【解决方案1】:

    Q:如果可以单独从用户实体管理一组角色,那么使用组的目的是什么

    A:可以从Using Groups With FOSUserBundle中提取答案:

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


    :如果您问自己:什么是角色组?我什么时候需要使用角色组?

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


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

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

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

    【讨论】:

    • 感谢您的澄清。能比以前多懂一点。
    • 我没有看到任何角色 FOSUserBundle 中的模型想知道角色是否为 GroupInterface?
    猜你喜欢
    • 1970-01-01
    • 2014-11-09
    • 1970-01-01
    • 1970-01-01
    • 2016-10-18
    • 2022-12-07
    • 1970-01-01
    • 1970-01-01
    • 2014-06-05
    相关资源
    最近更新 更多