【发布时间】:2012-04-01 15:50:07
【问题描述】:
我正在尝试在 Symfony2 中整合更改密码功能。我有一个“当前密码”字段、一个“新密码”字段和一个“确认新密码”字段,而我目前关注的部分是验证“当前密码”字段。
(顺便说一句,我现在意识到像FOSUserBundle 这样的东西可以为我处理很多这些事情,但是我已经根据官方 Symfony 文档构建了我的身份验证系统,但我没有现在有时间重做我所有的验证码。)
我想/希望我能做的是创建一个验证回调,它会说这样的话:
// Entity/User.php
public function currentPasswordIsValid(ExecutionContext $context)
{
$currentPassword = $whatever; // whatever the user submitted as their current password
$factory = $this->get('security.encoder_factory'); // Getting the factory this way doesn't work in this context.
$encoder = $factory->getEncoder($this);
$encryptedCurrentPassword = $encoder->encodePassword($this->getPassword(), $this->getSalt());
if ($encyptedCurrentPassword != $this->getPassword() {
$context->addViolation('Current password is not valid', array(), null);
}
}
正如您在我的 cmets 中看到的,上述代码不起作用至少有几个原因。我只会发布有关这些特定问题的具体问题,但也许我完全是在找错树。这就是我提出整体问题的原因。
那么,如何验证用户的密码?
【问题讨论】:
标签: symfony