]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Security/Validator/WallabagUserPasswordValidator.php
Update to Symfony 2.7
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Security / Validator / WallabagUserPasswordValidator.php
CommitLineData
d9085c63
J
1<?php
2
3namespace Wallabag\CoreBundle\Security\Validator;
4
5use Symfony\Component\Security\Core\User\UserInterface;
75e9d1df 6use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
d9085c63
J
7use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
8use Symfony\Component\Validator\Constraint;
9use Symfony\Component\Validator\ConstraintValidator;
10use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
11use Symfony\Component\Validator\Exception\UnexpectedTypeException;
12use Symfony\Component\Security\Core\Validator\Constraints\UserPassword;
13
75e9d1df
JB
14/**
15 * @see Symfony\Component\Security\Core\Validator\Constraints\UserPasswordValidator
16 */
d9085c63
J
17class WallabagUserPasswordValidator extends ConstraintValidator
18{
19 private $securityContext;
20 private $encoderFactory;
21
75e9d1df 22 public function __construct(TokenStorageInterface $tokenStorage, EncoderFactoryInterface $encoderFactory)
d9085c63 23 {
75e9d1df 24 $this->tokenStorage = $tokenStorage;
d9085c63
J
25 $this->encoderFactory = $encoderFactory;
26 }
27
28 /**
29 * {@inheritdoc}
30 */
31 public function validate($password, Constraint $constraint)
32 {
33 if (!$constraint instanceof UserPassword) {
34 throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\UserPassword');
35 }
36
75e9d1df 37 $user = $this->tokenStorage->getToken()->getUser();
d9085c63
J
38
39 if (!$user instanceof UserInterface) {
40 throw new ConstraintDefinitionException('The User object must implement the UserInterface interface.');
41 }
42
43 // give username, it's used to hash the password
44 $encoder = $this->encoderFactory->getEncoder($user);
45 $encoder->setUsername($user->getUsername());
46
47 if (!$encoder->isPasswordValid($user->getPassword(), $password, $user->getSalt())) {
48 $this->context->addViolation($constraint->message);
49 }
50 }
51}