3 namespace Wallabag\CoreBundle\Security\Validator
;
5 use Symfony\Component\Security\Core\User\UserInterface
;
6 use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface
;
7 use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface
;
8 use Symfony\Component\Validator\Constraint
;
9 use Symfony\Component\Validator\ConstraintValidator
;
10 use Symfony\Component\Validator\Exception\ConstraintDefinitionException
;
11 use Symfony\Component\Validator\Exception\UnexpectedTypeException
;
12 use Symfony\Component\Security\Core\Validator\Constraints\UserPassword
;
15 * @see Symfony\Component\Security\Core\Validator\Constraints\UserPasswordValidator
17 class WallabagUserPasswordValidator
extends ConstraintValidator
19 private $securityContext;
20 private $encoderFactory;
22 public function __construct(TokenStorageInterface
$tokenStorage, EncoderFactoryInterface
$encoderFactory)
24 $this->tokenStorage
= $tokenStorage;
25 $this->encoderFactory
= $encoderFactory;
31 public function validate($password, Constraint
$constraint)
33 if (!$constraint instanceof UserPassword
) {
34 throw new UnexpectedTypeException($constraint, __NAMESPACE__
.'\UserPassword');
37 $user = $this->tokenStorage
->getToken()->getUser();
39 if (!$user instanceof UserInterface
) {
40 throw new ConstraintDefinitionException('The User object must implement the UserInterface interface.');
43 // give username, it's used to hash the password
44 $encoder = $this->encoderFactory
->getEncoder($user);
45 $encoder->setUsername($user->getUsername());
47 if (!$encoder->isPasswordValid($user->getPassword(), $password, $user->getSalt())) {
48 $this->context
->addViolation($constraint->message
);