Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FOSUserBundle group role setup

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.

like image 841
cbaltatescu Avatar asked Feb 24 '13 21:02

cbaltatescu


1 Answers

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.

like image 98
UpCat Avatar answered Sep 21 '22 12:09

UpCat