I have the FOSUserBundle fully set up with an homemade LDAP Controller that allows users to login with their already existing logins from the Active Directory.
What I want is to allow the admin to edit any user role from the backend. Any idea how I can do that ? (I don't want to use the SonataBundle).
I already made the FormType:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$permissions = array(
'ROLE_USER' => 'Aucun droit',
'ROLE_CONSULT' => 'Consultation',
'ROLE_SUPER_ADMIN' => 'Administrateur'
);
$builder
->add(
'id',
'entity',
array(
'class' => 'MainBundle:Users',
'property' => 'displayName',
'label' => 'Choisir l\'utilisateur',
'attr' => array(
'class' => "select2"
)
)
)
->add(
'role',
'choice',
array(
'label' => 'Rôle à attribuer',
'choices' => $permissions,
'attr' => array(
'class' => "select2"
)
)
)
->add(
'save',
'submit',
array(
'label' => 'Sauvegarder',
'attr' => array(
'class' => 'btn'
)
)
);
}
And the controller, which is what I am really not sure since I don't know when to select the specified user to edit :
public function editUserAction(Request $request)
{
$request = $this->container->get('request');
$editUser = $this->getDoctrine()->getRepository('MainBundle:Users')->findAll();
$formEditUser = $this->createForm(new ChangeUserRoleType(), $editUser);
$formEditUser->handleRequest($request);
if ($formEditUser->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($editUser);
$em->flush();
return $this->render(
'ReportingAdminBundle:Admin:index.html.twig',
array(
'editUserForm' => $formEditUser->createView()
)
);
}
return $this->render(
'ReportingAdminBundle:Admin:index.html.twig',
array(
'editUserForm' => $formEditUser->createView()
)
);
}
Alright, I solved this.
The BuildForm of the form :
public function buildForm(FormBuilderInterface $builder, array $options)
{
$permissions = array(
'ROLE_USER' => 'First role',
'ROLE_CONSULT' => 'Second role',
'ROLE_SUPER_ADMIN' => 'Third role'
);
$builder
->add(
'id',
'entity',
array(
'class' => 'MainBundle:Users',
'property' => 'displayName',
'label' => 'Choose the user',
)
)
->add(
'role',
'choice',
array(
'label' => 'Choose the role',
'choices' => $permissions,
)
)
->add(
'save',
'submit'
);
}
The function on the controller:
public function editRoleAction(Request $request)
{
$request = $this->container->get('request');
$formEditUser = $this->createForm(new ChangeUserRoleType());
$formEditUser->handleRequest($request);
if ($formEditUser->isValid()) {
// Getting the variable of the form
$selectedUser = $request->request->get('value');
// Getting the user infos
$editUser = $this->getDoctrine()->getRepository('MainBundle:Users')->find($selectedUser);
// Using the UserManager (from the FOSUserBundle)
$userManager = $this->container->get('fos_user.user_manager');
$user = $userManager->findUserByUsername($editUser->getUsername());
// Changing the role of the user
$user->setRoles(array($selectedUser['role']));
// Updating the user
$userManager->updateUser($user);
}
return $this->render(
'ReportingAdminBundle:Admin:index.html.twig',
array(
'editUserForm' => $formEditUser->createView()
)
);
}
I am sure you can tweak this to add more roles if you want, like by setting the option "multiple" to "true" on the choice field.
I hope I could help anyone having the same issue I had !