1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Smart\AuthenticationBundle\Validator\Constraints; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Validator\Constraint; |
6
|
|
|
use Symfony\Component\Validator\ConstraintValidator; |
7
|
|
|
use Symfony\Component\Validator\Exception\UnexpectedTypeException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* https://www.ssi.gouv.fr/uploads/IMG/pdf/NP_MDP_NoteTech.pdf. |
11
|
|
|
*/ |
12
|
|
|
class IsPasswordSafeValidator extends ConstraintValidator |
13
|
|
|
{ |
14
|
|
|
const MINIMAL_STRING_LENGTH = 10; |
15
|
|
|
const COINTANS_LOWER_CHARACTER_REGEX = '/[a-z]+/'; |
16
|
|
|
const COINTANS_UPPER_CHARACTER_REGEX = '/[A-Z]+/'; |
17
|
|
|
const COINTANS_NUMBER_REGEX = '/[0-9]+/'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param mixed $value |
21
|
|
|
* |
22
|
|
|
* @return void |
23
|
|
|
*/ |
24
|
|
|
public function validate($value, Constraint $constraint) |
25
|
|
|
{ |
26
|
|
|
if (!$constraint instanceof IsPasswordSafe) { |
27
|
|
|
throw new UnexpectedTypeException($constraint, IsPasswordSafe::class); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
// custom constraints should ignore null and empty values to allow |
31
|
|
|
// other constraints (NotBlank, NotNull, etc.) take care of that |
32
|
|
|
if (null === $value || '' === $value) { |
33
|
|
|
return; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if (!\is_string($value)) { |
37
|
|
|
throw new UnexpectedTypeException($value, 'string'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if (\strlen($value) < self::MINIMAL_STRING_LENGTH) { |
41
|
|
|
$this->context->buildViolation($constraint->lengthMessage)->addViolation(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if (!preg_match(self::COINTANS_LOWER_CHARACTER_REGEX, $value, $matches)) { |
45
|
|
|
$this->context->buildViolation($constraint->missingLowerCharacterMessage)->addViolation(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if (!preg_match(self::COINTANS_UPPER_CHARACTER_REGEX, $value, $matches)) { |
49
|
|
|
$this->context->buildViolation($constraint->missingUpperCharacterMessage)->addViolation(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if (!preg_match(self::COINTANS_NUMBER_REGEX, $value, $matches)) { |
53
|
|
|
$this->context->buildViolation($constraint->missingNumberMessage)->addViolation(); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|