|
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
|
|
|
* @param Constraint $constraint |
|
22
|
|
|
* |
|
23
|
|
|
* @return void |
|
24
|
|
|
*/ |
|
25
|
|
|
public function validate($value, Constraint $constraint) |
|
26
|
|
|
{ |
|
27
|
|
|
if (!$constraint instanceof IsPasswordSafe) { |
|
28
|
|
|
throw new UnexpectedTypeException($constraint, IsPasswordSafe::class); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
// custom constraints should ignore null and empty values to allow |
|
32
|
|
|
// other constraints (NotBlank, NotNull, etc.) take care of that |
|
33
|
|
|
if (null === $value || '' === $value) { |
|
34
|
|
|
return; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
if (!is_string($value)) { |
|
38
|
|
|
throw new UnexpectedTypeException($value, 'string'); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
if (strlen($value) < self::MINIMAL_STRING_LENGTH) { |
|
42
|
|
|
$this->context->buildViolation($constraint->lengthMessage)->addViolation(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
if (!preg_match(self::COINTANS_LOWER_CHARACTER_REGEX, $value, $matches)) { |
|
46
|
|
|
$this->context->buildViolation($constraint->missingLowerCharacterMessage)->addViolation(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
if (!preg_match(self::COINTANS_UPPER_CHARACTER_REGEX, $value, $matches)) { |
|
50
|
|
|
$this->context->buildViolation($constraint->missingUpperCharacterMessage)->addViolation(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
if (!preg_match(self::COINTANS_NUMBER_REGEX, $value, $matches)) { |
|
54
|
|
|
$this->context->buildViolation($constraint->missingNumberMessage)->addViolation(); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|