Capitan Duke
Capitan Duke

Reputation: 135

enconde password from a form

I'm new on PHP symfony, I'm working with FOSUserbundle, I created a new view for the user change password but it was so hard to embeded the change password form from the FOSUserBundle so I created manually a html form and add some code in the controller of the view and everything is working fine, the change pasword works on my DB but the problem is that the password is plaintext and I need it encode (encrypt), how do I encode my password to send it to the db?

Here is the form in my twig view:

<form action="{{path ('fos_user_profile_change_password')}}" method="POST" id="form_user_change_password">                                       
    <div class="form-group">
        <div>
        <label for="fos_user_change_password_form_current_password" class="required">Contraseña actual</label>

        <input type="password" id="fos_user_change_password_form_current_password" name="fos_user_change_password_form[current_password]" required="required">
        </div>

        <div>
        <label for="new_password" class="required">Nueva contraseña</label>
        <input type="password" id="new_password" name="new_password" required="required">
        </div>

        <div>
        <label for="new_password_repeat" class="required">Repita la contraseña</label>
        <input type="password" name="new_password_repeat" id="new_password_repeat" required="required">
        </div>
        <div>

        <input type="submit" value="{{ 'change_password.submit'|trans }}" class="btn btn-primary btn-block uppercase" />
        </div>
    </div>
</form>

and here is my controller:

/**
 * Change user password.
 *
 * @param Request $request
 *
 * @return Response
 */
public function changePasswordAction(Request $request)
{
    $userManager = $this->get('fos_user.user_manager');       
    $user = $this->getUser();
    $new_password = $_POST['new_password'];
    $user->setPassword($new_password);
    $userManager->updateUser($user);
    $url = $this->generateUrl('fos_user_profile_edit');
    $response = new RedirectResponse($url);

    return $response;
}

Please I'm a newbie on PHP and Symfony3 :)

Upvotes: 0

Views: 187

Answers (1)

Forer
Forer

Reputation: 1055

From above comments and code, it looks like you only want to override the change_password_content.html.twig template of FOSUserBundle. This will allow you to use the existing ChangePassword Form Type and ChangePasswordController for FOSUserBundle in your own template.

The reference to do this is here: https://symfony.com/doc/current/bundles/FOSUserBundle/overriding_templates.html

To help you with some implementation details:

  1. Go to vendor/friendsofsymfony/user-bundle/Resources/views/ChangePassword/change_password.html.twig and copy the contents of this file.
  2. Then go to app/Resources/views and create a folder named ChangedPassword and in that create a new file and paste the content you copied in (1) above. Save the file as change_password.html.twig - what you have done in steps (1) and (2) is override the template in FOSUserBundle with your own template in app/Resources/views. The content of your override template is just identical to that FOSUserBundle right now.
  3. Now go ahead and make any changes you need to this override template: like change the extended layout to use your own and add any additional markup you may need around the fos_user_content block. Save.

You have now done a correct override of the FOSUserBundle template. If you need even further control over the content template containing the form, go ahead and repeat the above for the template named change_password_content.html.twig and change the include in your change_password.html.twig template to ":ChangePassword/change_password_content.html.twig".

The above will now mean that FOSUserBundle will use your override templates instead of the existing ones in the bundle giving you full control over mark-up, layout, styling, etc.

And in doing the override above correctly, you won't need to override the ChangePasswordController - so you will get the password hashing you need via the UserManager in FOSUserBundle.

Upvotes: 1

Related Questions