Reputation: 61
I installed FOSUsrBundle. How to add a role from controller to user?
I tried this solution but doesn't work:
$user = $this->container->get('security.context')->getToken()->getUser();
$user->addRole('ROLE_USER');
Upvotes: 0
Views: 5218
Reputation: 51
Just add a role choice field with roles list on your userType form.
$builder->add('roleList', 'choice', array(
'choices' => array(
'ROLE_ADMIN' => 'ROLE_ADMIN',
'ROLE_USER' => 'ROLE_USER',
),
'property_path' => false,
'multiple' => true,
))
And in the controller just add this code to add role into user
$formData = $this->getRequest()->request->get($form->getName());
$roles = $formData['roleList'];
foreach($roles as $key => $value)
{
$user->addRole($value);
}
I think this is enough for assign roles into user the user interface.
Upvotes: 3