在 Symfony2 中实现更改密码 [英] Implement change password in Symfony2

查看:29
本文介绍了在 Symfony2 中实现更改密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Symfony2 中实现更改密码功能的最佳方法是什么?现在我正在使用这个:

What is the best way to implement change password functionality in Symfony2? Right now I'm using this:

$builder->add('password', 'repeated', array(
    'first_name' => 'New password',
    'second_name' => 'Confirm new password',
    'type' => 'password'
));

出于安全原因,它还应包含当前密码检查.

It should also contain the current password check for security reasons.

注意:我没有使用 FOSUserBundle.

推荐答案

自 Symfony 2.3 以来,您可以轻松使用 UserPassword 验证约束.

Since Symfony 2.3 you can easily use UserPassword validation constraint.

AcmeUserBundleFormModelChangePassword.php

namespace AcmeUserBundleFormModel;

use SymfonyComponentSecurityCoreValidatorConstraints as SecurityAssert;
use SymfonyComponentValidatorConstraints as Assert;

class ChangePassword
{
    /**
     * @SecurityAssertUserPassword(
     *     message = "Wrong value for your current password"
     * )
     */
     protected $oldPassword;

    /**
     * @AssertLength(
     *     min = 6,
     *     minMessage = "Password should be at least 6 chars long"
     * )
     */
     protected $newPassword;
}

AcmeUserBundleFormChangePasswordType.php

namespace AcmeUserBundleForm;

use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolverInterface;

class ChangePasswordType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('oldPassword', 'password');
        $builder->add('newPassword', 'repeated', array(
            'type' => 'password',
            'invalid_message' => 'The password fields must match.',
            'required' => true,
            'first_options'  => array('label' => 'Password'),
            'second_options' => array('label' => 'Repeat Password'),
        ));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AcmeUserBundleFormModelChangePassword',
        ));
    }

    public function getName()
    {
        return 'change_passwd';
    }
}

AcmeUserBundleControllerDemoController.php

namespace AcmeUserBundleController;

use SymfonyBundleFrameworkBundleControllerController;
use SymfonyComponentHttpFoundationRequest;
use AcmeUserBundleFormChangePasswordType;
use AcmeUserBundleFormModelChangePassword;

class DemoController extends Controller
{
    public function changePasswdAction(Request $request)
    {
      $changePasswordModel = new ChangePassword();
      $form = $this->createForm(new ChangePasswordType(), $changePasswordModel);

      $form->handleRequest($request);

      if ($form->isSubmitted() && $form->isValid()) {
          // perform some action,
          // such as encoding with MessageDigestPasswordEncoder and persist
          return $this->redirect($this->generateUrl('change_passwd_success'));
      }

      return $this->render('AcmeUserBundle:Demo:changePasswd.html.twig', array(
          'form' => $form->createView(),
      ));      
    }
}

这篇关于在 Symfony2 中实现更改密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆