|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace LM\AuthAbstractor\Form\Constraint; |
|
6
|
|
|
|
|
7
|
|
|
use Symfony\Component\Validator\Constraint; |
|
8
|
|
|
use Symfony\Component\Validator\ConstraintValidator; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* The validator class associated with ValidNewPassword. |
|
12
|
|
|
* |
|
13
|
|
|
* @see \LM\AuthAbstractor\Form\Constraint\ValidNewPassword |
|
14
|
|
|
*/ |
|
15
|
|
|
class ValidNewPasswordValidator extends ConstraintValidator |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* Validates a given password and adds errors to the form if it has errors. |
|
19
|
|
|
* |
|
20
|
|
|
* @internal |
|
21
|
|
|
*/ |
|
22
|
|
|
public function validate($password, Constraint $constraint) |
|
23
|
|
|
{ |
|
24
|
|
|
$pwdValidator = $constraint->getPwdValidator(); |
|
|
|
|
|
|
25
|
|
|
$pwdConfig = $constraint->getConfig()->getPwdSettings(); |
|
|
|
|
|
|
26
|
|
|
if (true === $pwdConfig['enforce_min_length']) { |
|
27
|
|
|
$pwdMinLength = $pwdConfig['min_length']; |
|
28
|
|
|
if (mb_strlen($password, 'utf-8') < $pwdMinLength) { |
|
29
|
|
|
$this->addError("Your password needs to be at least {$pwdMinLength} characters long", $password); |
|
30
|
|
|
} |
|
31
|
|
|
} |
|
32
|
|
|
if (true === $pwdConfig['numbers']) { |
|
33
|
|
|
switch (preg_match('/[0-9]/', $password)) { |
|
34
|
|
|
case 0: |
|
35
|
|
|
$this->addError('Your password needs to contain numbers.', $password); |
|
36
|
|
|
break; |
|
37
|
|
|
|
|
38
|
|
|
case false: |
|
|
|
|
|
|
39
|
|
|
throw new Exception(); |
|
|
|
|
|
|
40
|
|
|
break; |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
if (true === $pwdConfig['special_chars']) { |
|
44
|
|
|
if (false === $pwdValidator->hasSpecialChars($password)) { |
|
45
|
|
|
$this->addError('Your password needs to contain special characters', $password); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
if (true === $pwdConfig['uppercase']) { |
|
49
|
|
|
switch (preg_match('/[A-Z]/', $password)) { |
|
50
|
|
|
case 0: |
|
51
|
|
|
$this->addError('Your password needs to contain uppercase letters.', $password); |
|
52
|
|
|
break; |
|
53
|
|
|
|
|
54
|
|
|
case false: |
|
|
|
|
|
|
55
|
|
|
throw new Exception(); |
|
56
|
|
|
break; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @internal |
|
63
|
|
|
*/ |
|
64
|
|
|
private function addError(string $message, string $password): void |
|
65
|
|
|
{ |
|
66
|
|
|
$this |
|
67
|
|
|
->context |
|
68
|
|
|
->buildViolation($message) |
|
69
|
|
|
->setParameter('{{ string }}', $password) |
|
70
|
|
|
->addViolation() |
|
71
|
|
|
; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|