]>
Commit | Line | Data |
---|---|---|
d9085c63 J |
1 | <?php |
2 | ||
3 | namespace Wallabag\CoreBundle\Security\Validator; | |
4 | ||
5 | use Symfony\Component\Security\Core\User\UserInterface; | |
75e9d1df | 6 | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
d9085c63 J |
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 | ||
75e9d1df JB |
14 | /** |
15 | * @see Symfony\Component\Security\Core\Validator\Constraints\UserPasswordValidator | |
16 | */ | |
d9085c63 J |
17 | class 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 | } |