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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With