Question

I am using Symfony2. I am trying to create a user in my application and save their info in the db. In anycase, I can persist the data to the db and it does store but it is not encrypting the password. Here is my code. Anyone help me out with this? Thanks!

    //search form
    $search_form = $this->createForm(new UserSearchType());

    //user form
    $user = new User();
    $form = $this->createForm(new UserAdminType(), $user);

    if ($request->isMethod('POST')) {

        //get the Account of this user and set it on the user being created.             
        $account = $this->getUser()->getAccount();
        $user->setAccount($account);

        //encode password and set on user
        $factory = $this->get('security.encoder_factory');
        $encoder = $factory->getEncoder($user);
        $password = $encoder->encodePassword($user->getPassword(), $user->getSalt());
        $user->setPassword($password);

        $form->bind($request);
        if ($form->isValid()) {

            $em = $this->getDoctrine()->getManager();
            $em->persist($user);
            $em->flush();

            //set flash message
            $message = "User ".$user->getUsername()." has been created with password ". $password;
            $this->get('session')->getFlashBag()->add('message', $message);

            return $this->redirect($this->generateUrl('user_list'));
        }
    }

    return array(
        'heading' => 'Create User',
        'form' => $form->createView(),
        'search_form' => $search_form->createView(),
    );
Était-ce utile?

La solution

Here is what I did to get the password updated.

 $password = $postData['password']['first'];
 $factory = $this->get('security.encoder_factory');
 $encoder = $factory->getEncoder($user);
 $password = $encoder->encodePassword($password, $user->getSalt());
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top