|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Silverback API Components Bundle Project |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Daniel West <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Silverback\ApiComponentsBundle\Validator\Constraints; |
|
15
|
|
|
|
|
16
|
|
|
use Silverback\ApiComponentsBundle\Repository\User\UserRepository; |
|
17
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
|
18
|
|
|
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface; |
|
19
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
|
20
|
|
|
use Symfony\Component\Security\Core\Validator\Constraints\UserPassword; |
|
21
|
|
|
use Symfony\Component\Validator\Constraint; |
|
22
|
|
|
use Symfony\Component\Validator\ConstraintValidator; |
|
23
|
|
|
use Symfony\Component\Validator\Exception\ConstraintDefinitionException; |
|
24
|
|
|
use Symfony\Component\Validator\Exception\UnexpectedTypeException; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @author Daniel West <[email protected]> |
|
28
|
|
|
*/ |
|
29
|
|
|
class UserPasswordValidator extends ConstraintValidator |
|
30
|
|
|
{ |
|
31
|
|
|
private TokenStorageInterface $tokenStorage; |
|
32
|
|
|
private EncoderFactoryInterface $encoderFactory; |
|
33
|
|
|
private UserRepository $userRepository; |
|
34
|
|
|
|
|
35
|
|
|
public function __construct(TokenStorageInterface $tokenStorage, EncoderFactoryInterface $encoderFactory, UserRepository $userRepository) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->tokenStorage = $tokenStorage; |
|
38
|
|
|
$this->encoderFactory = $encoderFactory; |
|
39
|
|
|
$this->userRepository = $userRepository; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* {@inheritdoc} |
|
44
|
|
|
*/ |
|
45
|
|
|
public function validate($password, Constraint $constraint): void |
|
46
|
|
|
{ |
|
47
|
|
|
if (!$constraint instanceof UserPassword) { |
|
48
|
|
|
throw new UnexpectedTypeException($constraint, UserPassword::class); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
if (null === $password || '' === $password) { |
|
52
|
|
|
$this->context->addViolation($constraint->message); |
|
53
|
|
|
|
|
54
|
|
|
return; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$databaselessUser = $this->tokenStorage->getToken()->getUser(); |
|
58
|
|
|
$user = $this->userRepository->find($databaselessUser->getId()); |
|
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
if (!$user instanceof UserInterface) { |
|
61
|
|
|
throw new ConstraintDefinitionException('The User object must implement the UserInterface interface.'); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$encoder = $this->encoderFactory->getEncoder($user); |
|
65
|
|
|
|
|
66
|
|
|
if (null === $user->getPassword() || !$encoder->isPasswordValid($user->getPassword(), $password, $user->getSalt())) { |
|
|
|
|
|
|
67
|
|
|
$this->context->addViolation($constraint->message); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|