]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Security/Validator/WallabagUserPasswordValidator.php
Update to Symfony 2.7
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Security / Validator / WallabagUserPasswordValidator.php
1 <?php
2
3 namespace Wallabag\CoreBundle\Security\Validator;
4
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;
13
14 /**
15 * @see Symfony\Component\Security\Core\Validator\Constraints\UserPasswordValidator
16 */
17 class WallabagUserPasswordValidator extends ConstraintValidator
18 {
19 private $securityContext;
20 private $encoderFactory;
21
22 public function __construct(TokenStorageInterface $tokenStorage, EncoderFactoryInterface $encoderFactory)
23 {
24 $this->tokenStorage = $tokenStorage;
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
37 $user = $this->tokenStorage->getToken()->getUser();
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 }