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

标签 php symfony fosuserbundle

我对 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).


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

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


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

A:如果角色继承不够,你想使用“组”,你需要在用户 -> 组 -> 角色实体之间创建关联,就像这个实现示例:

用户实体:

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

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

关于php - FOSUserBundle,用户(角色)和组(角色)之间的关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29073450/

相关文章:

初学者的 PHP 类继承

php - 回溯迷宫生成(将所有内容转换为二维数组)

php - 从数组访问类变量

symfony - 用户区域设置在第一次请求时不起作用

html - Symfony2 Twig form_widget 自定义 html

javascript - AngularJS 验证和带方括号的字段名称

c#和ffpmeg命令行问题

php - Symfony2/Doctrine 中的实体和模型有什么区别

php - FOSUserBundle 不发送电子邮件帐户验证

php - FOSUserBundle : configuration error (handler)