FOSUserBundle组的角色设置[英] FOSUserBundle group role setup

本文是小编为大家收集整理的关于FOSUserBundle组的角色设置的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在使用fosuserbundle和组选项. 注册有效.组的创建也有效. 如果我创建2个组,管理员和客户端,然后手动添加A:1:{i:0; s:10:" cromor_admin";}在fos_group表中的管理组,然后在fos_user_user_group中进行manully setup和输入,该conte cinted and ofer_user_user_group中的连接用户到管理组,与该用户一起登录将以管理员登录.

但是,这种方法显然存在一些缺点. 我现在可以用什么将用户添加到组?使用promoter命令行选项只会将角色添加到该用户,但在FOS_USER表中,并且由于我使用组,我怀疑不再使用FOS_USER表的角色列.如果它仍然有目的,我该如何编程为组分配给组?

我的另一个大问题是如何将角色分配给小组.也许我没有得到整个小组的想法,但是我希望能够在创建时添加一个小组的角色,但是"新"表格只要求组名,没有't似乎存在相当于组的用户升级命令.

谢谢.

推荐答案

如何编程分配用户?

$user->addGroup($group);

由于您使用的是fosuserBundle,您的用户实体扩展了FOS\UserBundle\Model实现了GroupableInterface.因此,您的用户类已经具有组方法getGroups, hasGroup($name), addGroup(GroupInterface $group), removeGroup(GroupInterface $group).有关参考,请参见此处/master/model/user.php

如何为组分配角色?

$em = $this->getDoctrine()->getEntityManager();

$group = new Group();
$group->setRoles(array());
$group->addRole('ROLE_ACTOR');
$em->persist($group);
$em->flush();

您必须自己实施角色分配,fosuserbundle没有为此的预定义表格.

本文地址:https://www.itbaoku.cn/post/2090759.html

问题描述

I am using the FOSUserBundle and the Group option. Registration works. Creation of groups works too. If I create 2 groups, admin and client and MANUALLY add a:1:{i:0;s:10:"ROLE_ADMIN";} to the admin group in the fos_group table and then MANULLY setup and entry in the fos_user_user_group that ties an user to the admin group, logging in with that user will be able to log in as an admin.

However, there are, obviously, some disadvantages to this method. What can I use to add an user to a group now? Using the promote command-line option will just add the role to that user but in the fos_user table, and since I'm using groups I suspect that the roles column of the fos_user table is no longer used. And if it still serves a purpose, how do I assign an user to a group programmatically?

My other big question is how do I assign roles to groups. Maybe I'm not getting something about the whole Groups idea, but I would have expected to be able to add the role(s) for a group at creation time, but the "new" form only asks for the group name and there doesn't seem to exist an equivalent of the user promote command for groups.

Thank you.

推荐答案

How do I assign an user to a group programmatically?

$user->addGroup($group);

Since your are using FOSUserBundle your user entity extends FOS\UserBundle\Model which implements GroupableInterface. So your user class already has group methods getGroups, hasGroup($name), addGroup(GroupInterface $group), removeGroup(GroupInterface $group). For reference look here https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Model/User.php

How do I assign roles to groups?

$em = $this->getDoctrine()->getEntityManager();

$group = new Group();
$group->setRoles(array());
$group->addRole('ROLE_ACTOR');
$em->persist($group);
$em->flush();

You have to implement the role assignment on your own, fosuserbundle does not have predefined forms for this.