PHP   发布时间:2022-04-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了php – FOSUserBundle,用户(角色)和组(角色)之间的关系?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我对FOSUserBundle中的角色感到有些困惑.用户实体还具有角色列,通过该列我们可以为用户分配多个角色.根据 Managing users/roles/groups in FOSUserBundle发布的答案,我们不需要单独的实体或表来管理用户角色.

我还实现了FOSUserBundle组,它还包含一组角色,用户可以分配给这些角色.所以,我在这里理解的是,我们可以创建组以分配一组角色,以便可以使用组访问类似的角色.

现在,我在哪里卡住的是,什么是使用组如果设置的角色可以从用户实体单独或我怎么能合并来自用户实体和集团实体或东西,我很想念这里的角色进行管理的目的是什么?

解决方法

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

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

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

答:如果角色继承不够并且您想使用“组”,则需要在User – >之间创建关联.组 – >像这个实现示例的角色实体:

用户实体:

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,用户(角色)和组(角色)之间的关系?全部内容,希望文章能够帮你解决php – FOSUserBundle,用户(角色)和组(角色)之间的关系?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。