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

查看:106
本文介绍了FOSUserBundle组角色设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用FOSUserBundle和组"选项. 注册作品.组的创建也可以. 如果我创建2个组,则admin和client并手动将a:1:{i:0; s:10:"ROLE_ADMIN";}添加到fos_group表中的admin组中,然后在fos_user_user_group中将MANULLY设置和输入与用户登录到admin组,然后以该用户身份登录即可.

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.

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

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.

谢谢.

推荐答案

如何通过编程方式将用户分配给组?

$user->addGroup($group);

由于您正在使用FOSUserBundle,因此您的用户实体扩展了实现GroupableInterfaceFOS\UserBundle\Model.因此,您的用户类已经具有组方法getGroups, hasGroup($name), addGroup(GroupInterface $group), removeGroup(GroupInterface $group).供参考,请参见 https://github.com/FriendsOfSymfony/FOSUserBundle/blob /master/Model/User.php

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

如何为组分配角色?

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

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

您必须自己实现角色分配,fosuserbundle没有为此预先定义的形式.

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

这篇关于FOSUserBundle组角色设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆