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

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

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

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

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

Q: how can I merge the roles from User entity and Group entity or

something I am missing here?

答:如果角色继承不够并且您想使用“组”,则需要在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();

}

/**

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

}

}

就这样.希望对你有所帮助.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值