FOSUserBundle group role setup

非 Y 不嫁゛ 提交于 2019-12-21 01:44:38

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/15057088/fosuserbundle-group-role-setup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!