|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Zikula package. |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright Zikula Foundation - https://ziku.la/ |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Zikula\ZAuthModule\Validator\Constraints; |
|
15
|
|
|
|
|
16
|
|
|
use Symfony\Component\Validator\Constraint; |
|
17
|
|
|
use Symfony\Component\Validator\ConstraintValidator; |
|
18
|
|
|
use Symfony\Component\Validator\Exception\UnexpectedTypeException; |
|
19
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
|
20
|
|
|
use Zikula\ZAuthModule\Api\ApiInterface\PasswordApiInterface; |
|
21
|
|
|
use Zikula\ZAuthModule\Entity\RepositoryInterface\AuthenticationMappingRepositoryInterface; |
|
22
|
|
|
|
|
23
|
|
|
class ValidPasswordChangeValidator extends ConstraintValidator |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var AuthenticationMappingRepositoryInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
private $repository; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var TranslatorInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
private $translator; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var PasswordApiInterface |
|
37
|
|
|
*/ |
|
38
|
|
|
private $passwordApi; |
|
39
|
|
|
|
|
40
|
|
|
public function __construct(AuthenticationMappingRepositoryInterface $repository, TranslatorInterface $translator, PasswordApiInterface $passwordApi) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->repository = $repository; |
|
43
|
|
|
$this->translator = $translator; |
|
44
|
|
|
$this->passwordApi = $passwordApi; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function validate($data, Constraint $constraint) |
|
48
|
|
|
{ |
|
49
|
|
|
if (!$constraint instanceof ValidPasswordChange) { |
|
50
|
|
|
throw new UnexpectedTypeException($constraint, ValidPasswordChange::class); |
|
51
|
|
|
} |
|
52
|
|
|
$userEntity = $this->repository->findOneBy(['uid' => $data['uid']]); |
|
53
|
|
|
if ($userEntity) { |
|
54
|
|
|
$currentPass = $userEntity->getPass(); |
|
55
|
|
|
// is oldpass correct? |
|
56
|
|
|
if (empty($data['oldpass']) || !$this->passwordApi->passwordsMatch($data['oldpass'], $currentPass)) { |
|
57
|
|
|
$this->context->buildViolation($this->translator->trans('Old password is incorrect.', [], 'validators')) |
|
58
|
|
|
->atPath('oldpass') |
|
59
|
|
|
->addViolation(); |
|
60
|
|
|
} |
|
61
|
|
|
// oldpass == newpass? |
|
62
|
|
|
if (isset($data['pass']) && $data['oldpass'] === $data['pass']) { |
|
63
|
|
|
$this->context->buildViolation($this->translator->trans('Your new password cannot match your current password.', [], 'validators')) |
|
64
|
|
|
->atPath('pass') |
|
65
|
|
|
->addViolation(); |
|
66
|
|
|
} |
|
67
|
|
|
} else { |
|
68
|
|
|
$this->context->buildViolation($this->translator->trans('Could not find user to update.', [], 'validators')) |
|
69
|
|
|
->atPath('oldpass') |
|
70
|
|
|
->addViolation(); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|