0
Huston,我有一個問題:)以Symfony 2格式驗證舊密碼
我需要驗證舊密碼。
Symfony的已建成的密碼驗證,這是偉大的,但是...我需要重新編碼系統。
use Symfony\Component\Security\Core\Validator\Constraints as SecurityAssert;
/**
* User
*/
class User implements AdvancedUserInterface, \Serializable
{
/**
* @SecurityAssert\UserPassword()
*/
protected $oldPassword;
由於密碼是用mcrypt加密的,所以此返回始終爲false。
我曾試圖修改這一類
class UserPasswordValidator extends ConstraintValidator
{
private $tokenStorage;
private $encoderFactory;
public function __construct(TokenStorageInterface $tokenStorage, EncoderFactoryInterface $encoderFactory)
{
$this->tokenStorage = $tokenStorage;
$this->encoderFactory = $encoderFactory;
}
/**
* {@inheritdoc}
*/
public function validate($password, Constraint $constraint)
{
$user = $this->tokenStorage->getToken()->getUser();
$encoder = new CryptPasswordEncoder();
$oldPass = $user->getPassword();
if ($encoder->isPasswordValid($oldPass, $password, '')) {
$this->context->buildViolation($constraint->message)
->setParameter('%string%', $password)
->addViolation();
}
}
}
但是當我提交驗證值$密碼總是空