在Symfony2中实现更改密码 [英] Implement change password in Symfony2
本文介绍了在Symfony2中实现更改密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
现在我使用这个:
$ builder-> add('password','repeated'数组(
'first_name'=>'新密码',
'second_name'=>'确认新密码',
'type'=>'密码'
));
为安全起见,还应包含当前的密码检查。
注意:我没有使用 FOSUserBundle
。
解决方案
自Symfony 2.3以来,您可以轻松使用 UserPassword
验证约束。
Acme\UserBundle\Form\Model\ChangePassword。 php
命名空间Acme\UserBundle\Form\Model;
使用Symfony\Component\Security\Core\Validator\Constraints作为SecurityAssert;
使用Symfony\Component\Validator\Constraints作为Assert;
class ChangePassword
{
/ **
* @ SecurityAssert\UserPassword(
* message =当前密码错误的值
*)
* /
protected $ oldPassword;
/ **
* @ Assert\Length(
* min = 6,
* minMessage =密码至少需要6个字符长
*)
* /
protected $ newPassword;
}
Acme\UserBundle\Form\ChangePasswordType.php
命名空间Acme\UserBundle\Form;
使用Symfony\Component\Form\AbstractType;
使用Symfony\Component\Form\FormBuilderInterface;
使用Symfony\Component\OptionsResolver\OptionsResolverInterface;
class ChangePasswordType extends AbstractType
{
public function buildForm(FormBuilderInterface $ builder,array $ options)
{
$ builder-> add('旧密码','密码');
$ builder-> add('newPassword','repeated',array(
'type'=>'password',
'invalid_message'=>'密码字段必须匹配',
'required'=> true,
'first_options'=>数组('label'=>'密码'),
'second_options'=> ('label'=>'重复密码'),
));
}
public function setDefaultOptions(OptionsResolverInterface $ resolver)
{
$ resolver-> setDefaults(array(
'data_class'=> Acme\UserBundle\Form\Model\ChangePassword',
));
}
public function getName()
{
return'change_passwd';
}
}
Acme\UserBundle\Controller\\ \\ DemoController.php
命名空间Acme\UserBundle\Controller;
使用Symfony\Bundle\FrameworkBundle\Controller\Controller;
使用Symfony\Component\HttpFoundation\Request;
使用Acme\UserBundle\Form\ChangePasswordType;
使用Acme\UserBundle\Form\Model\ChangePassword;
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()){
//执行一些动作,
//如使用MessageDigestPasswordEncoder进行编码,并持续
return $ this-> redirect($ this-> generateUrl('change_passwd_success'));
}
return $ this-> render('AcmeUserBundle:Demo:changePasswd.html.twig',array(
'form'=> $ form-> createView(),
));
}
}
What is the best way to implement change password functionality in Symfony2? Now I'm using this:
$builder->add('password', 'repeated', array(
'first_name' => 'New password',
'second_name' => 'Confirm new password',
'type' => 'password'
));
It should also contait current password check for security reason.
Note: I'm not using FOSUserBundle
.
解决方案
Since Symfony 2.3 you can easily use UserPassword
validation constraint.
Acme\UserBundle\Form\Model\ChangePassword.php
namespace Acme\UserBundle\Form\Model;
use Symfony\Component\Security\Core\Validator\Constraints as SecurityAssert;
use Symfony\Component\Validator\Constraints as Assert;
class ChangePassword
{
/**
* @SecurityAssert\UserPassword(
* message = "Wrong value for your current password"
* )
*/
protected $oldPassword;
/**
* @Assert\Length(
* min = 6,
* minMessage = "Password should by at least 6 chars long"
* )
*/
protected $newPassword;
}
Acme\UserBundle\Form\ChangePasswordType.php
namespace Acme\UserBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
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' => 'Acme\UserBundle\Form\Model\ChangePassword',
));
}
public function getName()
{
return 'change_passwd';
}
}
Acme\UserBundle\Controller\DemoController.php
namespace Acme\UserBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Acme\UserBundle\Form\ChangePasswordType;
use Acme\UserBundle\Form\Model\ChangePassword;
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屋!
查看全文