Reputation: 41
I have an application which only admin can create users and I use FOSUserBundle.
I can create user but I've got some problems when I want to login the created user.
They can't login.
I use the default login from FOSUserBundle and it works with users created from command line.
What I missed to do?
This is my createAction from my UserController:
public function createAction(Request $request)
{
$user = new User();
$form = $this->createForm(new UserType(), $user);
if ($request->getMethod() == 'POST') {
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
}
$this->get('session')->getFlashBag()->add('success', 'The user has been added!');
return $this->redirect($this->generateUrl('crm_users'));
}
return $this->render('LanCrmBundle:User:create.html.twig', array(
'form' => $form->createView(),
));
}
This is my buildForm:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('username')
->add('email')
->add('enabled')
->add('password')
->add('roles', 'choice', array('choices' => array('ROLE_USER' => 'User', 'ROLE_ADMIN' => 'Admin'),'multiple' => true));
}
Upvotes: 0
Views: 708
Reputation: 1
Maybe your default enabled value is false.
Try to set it directly :
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$user->setEnabled(true);
$em->persist($user);
$em->flush();
}
Or put it in your default value in entity class.
Upvotes: 0
Reputation: 258
You are not encrypting the password while creating the user.
Change the code inside form valid success to,
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$userManager = $this->container->get('fos_user.user_manager');
$user->setPlainPassword($user->getPassword());
$userManager->updatePassword($user);
$em->persist($user);
$em->flush();
}
Upvotes: 2