0

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));
}

2 Answers 2

2

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();
}
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.