Skip to content

Commit 8452124

Browse files
committed
[Validator] Add an option to disable NotCompromisedPasswordValidator
1 parent f8d0081 commit 8452124

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

Constraints/NotCompromisedPasswordValidator.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,17 @@ class NotCompromisedPasswordValidator extends ConstraintValidator
3232

3333
private $httpClient;
3434
private $charset;
35+
private $disabled;
3536

36-
public function __construct(HttpClientInterface $httpClient = null, string $charset = 'UTF-8')
37+
public function __construct(HttpClientInterface $httpClient = null, string $charset = 'UTF-8', bool $disabled = false)
3738
{
3839
if (null === $httpClient && !class_exists(HttpClient::class)) {
3940
throw new \LogicException(sprintf('The "%s" class requires the "HttpClient" component. Try running "composer require symfony/http-client".', self::class));
4041
}
4142

4243
$this->httpClient = $httpClient ?? HttpClient::create();
4344
$this->charset = $charset;
45+
$this->disabled = $disabled;
4446
}
4547

4648
/**
@@ -54,6 +56,10 @@ public function validate($value, Constraint $constraint)
5456
throw new UnexpectedTypeException($constraint, NotCompromisedPassword::class);
5557
}
5658

59+
if ($this->disabled) {
60+
return;
61+
}
62+
5763
if (null !== $value && !is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
5864
throw new UnexpectedTypeException($value, 'string');
5965
}

Tests/Constraints/NotCompromisedPasswordValidatorTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@ public function testEmptyStringIsValid()
6060
$this->assertNoViolation();
6161
}
6262

63+
public function testInvalidPasswordButDisabled()
64+
{
65+
$r = new \ReflectionProperty($this->validator, 'disabled');
66+
$r->setAccessible(true);
67+
$r->setValue($this->validator, true);
68+
69+
$this->validator->validate(self::PASSWORD_LEAKED, new NotCompromisedPassword());
70+
71+
$this->assertNoViolation();
72+
}
73+
6374
public function testInvalidPassword()
6475
{
6576
$constraint = new NotCompromisedPassword();

0 commit comments

Comments
 (0)