Reputation: 6042
So, I've read through the documentation for the bundle, but there's nothing describing how to list existing roles or to assign/change rolls for a specific user. I need to be able to:
So, how is this done?
Upvotes: 1
Views: 1096
Reputation: 6206
You can do something like this:
$roleHierarchy = $container->getParameter('security.role_hierarchy.roles');
$roles = array_keys($roleHierarchy);
$form = $this->createForm(new UserFormType($roles, $user->getRoles()), $user);
In your UserFormType you can add the roles field like this:
protected $roles;
protected $userRoles;
public function __construct($roles, $userRoles)
{
foreach ($roles as $role) {
$theRoles[$role] = $role;
}
$this->roles = $theRoles;
$this->userRoles = $userRoles;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('roles', 'choice', array(
'choices' => $this->roles,
'data' => $this->userRoles,
'expanded' => true,
'multiple' => true,
));
}
Upvotes: 4