简体   繁体   English

Symfony2 用户密码更改

[英]Symfony2 user password change

Good morning, i'm trying to do a password change form but without any success so far i've been doing research on this part and i didn't find any useful results on google so i came here :D早上好,我正在尝试做一个密码更改表,但到目前为止没有任何成功我一直在做这方面的研究,但我没有在谷歌上找到任何有用的结果,所以我来到这里:D

This is my controller action for password change这是我的密码更改控制器操作

/**
 *
 * @Route("/user/change-password", name="change_password")
 * @Template()
 *
 */
public function changePasswordAction(Request $request) {
    $changePasswordModel = new ChangePassword();

    $form = $this->createForm(new ChangePasswordType(), $changePasswordModel);

    $form->handleRequest($request);

    if($form->isValid()) {
        $changePassword = $form->getData();

        $factory = $this->get('security.encoder_factory');
        $encoder = $factory->getEncoder($changePassword->getUser());

        var_dump($encoder);
        $password = $encoder->encodePassword($changePassword->getUser()->getPlainPassword(), $changePassword->getUser()->getSalt());

        $changePassword->getUser()->setPassword($password);

        $em->persist($changePassword->getUser());
        $em->flush();

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

    return array(
            'form' => $form->createView(),
        );
}

Password Change Model密码更改模型

/**
 * @Assert\Type(type="AppBundle\Entity\User")
 * @Assert\Valid()
 */
protected $user;

/**
 * @SecurityAssert\UserPassword(
 *     message = "Wrong value for your current password"
 * )
 */
 protected $password;

/**
 * @Assert\Length(
 *     min = 6,
 *     minMessage = "Password should by at least 6 chars long"
 * )
 */
 protected $plainPassword;

public function setUser(User $user)
{
    $this->user = $user;
}

public function getUser()
{
    return $this->user;
}

public function setPassword($password) {

    $this->password = $password;

    return $this;
}

public function getPassword() {

    return $this->password;
}

public function setPlainPassword($plainPassword) {

    $this->plainPassword = $plainPassword;

    return $this;
}

public function getPlainPassword() {

    return $this->plainPassword;
}

Password Type密码类型

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('password', 'password');
    $builder->add('plainPassword', 'repeated', array(
        'type' => 'password',
        'invalid_message' => 'The password fields must match.',
        'required' => true,
        'first_options'  => array('label' => 'Password'),
        'second_options' => array('label' => 'Repeat Password'),
    ));
    $builder->add('ChangePassword', 'submit');
}

My current error is :我目前的错误是:

No encoder has been configured for account "".
500 Internal Server Error - RuntimeException 

Forgot to add the security.yml忘记添加 security.yml

    encoders:
    AppBundle\Entity\User:
        algorithm: bcrypt
        cost: 15
# http://symfony.com/doc/current/book/security.html#where-do-users-come-from-user-providers
providers:
    in_memory:
        memory: ~
    db_provider:
        entity:
            class: AppBundle\Entity\User

With your current code you never set the actual user for which you want to change the password.使用您当前的代码,您永远不会设置要更改密码的实际用户。 Because of this, the following line fails, as null is passed as the argument:因此,以下行失败,因为null作为参数传递:

 $factory->getEncoder($changePassword->getUser());

If you want to stick to your current implemenation, you only need to realize that ChangePassword is actually just a data transfer object which needs to be set up correctly.如果你想坚持你目前的实现,你只需要意识到ChangePassword实际上只是一个需要正确设置的数据传输对象 Parts of this setup will come through the form handling ( ChangePasswordType ) other parts have to be set explicitly by you.此设置的一部分将通过表单处理 ( ChangePasswordType ) 其他部分必须由您明确设置。 So, for example just add:因此,例如只需添加:

$changePasswordModel = new ChangePassword();
$changePasswordModel->setUser($this->getUser());

Personally I think this aproach is a good idea because it makes the code more semantic (or expressive if you will).我个人认为这种方法是一个好主意,因为它使代码更具语义(或者如果你愿意的话)。 Just remember to not assume to much about implicit magic on the side of symfony.请记住不要过多地假设 symfony 方面的隐含魔法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM