我有一个使用 FOSUSerBundle 在 Symfony2.0 上运行的应用程序。
连接该应用程序的用户每 3 个月必须更改一次密码,密码已就位且正在运行。
今天,如果用户每 3 个月写入与前一个密码相同的新密码,则无需验证,他还可以使用该密码 3 个月。
我想对密码重置添加限制,并强制用户提供与之前不同的密码。
我不知道该怎么做。我尝试创建一个验证器,但验证器中没有我的用户 ID...
有什么线索吗?
我找到了一个解决方案,它有效,但我仍然有一个小问题:当我提交新密码时,如果我尝试输入与前一个密码相同的密码,我的表单无效(这很好!),但错误消息当我使用时,我的树枝没有显示:
{{ form_widget(edit_form) }}
知道为什么吗?
无论如何,这是我的解决方案:
我在类级别添加了一个验证器来验证密码。为此,我在我的 User 类上添加了一个验证器:
// \src\Acme\UserBundle\Resources\config\validation.xml
<class name="Acme\UserBundle\Entity\User">
<constraint name="Acme\UserBundle\Validator\Constraints\IsDifferentPassword">
<option name="message">Password has to be different from the previous one</option>
</constraint>
<property name="plainPassword">
<constraint name="NotBlank">
<option name="message">fos_user.password.blank</option>
</constraint>
</property>
</class>
我在类级别创建了一个验证器:
// \src\Acme\UserBundle\Validator\Constraints\IsDifferentPassword.php
namespace Acme\UserBundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
/**
* @Annotation
*/
class IsDifferentPassword extends Constraint
{
public $message = 'Password has to be different from the previous one';
public function getTargets()
{
return Constraint::CLASS_CONSTRAINT;
}
}
And :
// \src\Acme\UserBundle\Validator\Constraints\IsDifferentPasswordValidator.php
namespace Acme\UserBundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder;
class IsDifferentPasswordValidator extends ConstraintValidator
{
public function isValid($value, Constraint $constraint)
{
$encoder = new MessageDigestPasswordEncoder();
$password = $encoder->encodePassword($value->getPlainPassword(), $value->getSalt());
$oldPassword = $value->getPassword();
if($password == $oldPassword){
return false;
}
return true;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)