[Validator] Add an option to disable NotCompromisedPasswordValidator

This commit is contained in:
Grégoire Pineau 2019-04-06 21:16:20 +02:00
parent 4c78e60ad5
commit 9a2787e89a
5 changed files with 28 additions and 1 deletions

View File

@ -834,6 +834,10 @@ class Configuration implements ConfigurationInterface
->end()
->end()
->end()
->booleanNode('disable_not_compromised_password')
->defaultFalse()
->info('Disable NotCompromisedPassword Validator: the value will always be valid.')
->end()
->arrayNode('auto_mapping')
->useAttributeAsKey('namespace')
->normalizeKeys(false)

View File

@ -1229,6 +1229,11 @@ class FrameworkExtension extends Extension
if (!$propertyInfoEnabled || !$config['auto_mapping'] || !class_exists(PropertyInfoLoader::class)) {
$container->removeDefinition('validator.property_info_loader');
}
$container
->getDefinition('validator.not_compromised_password')
->setArgument(2, $config['disable_not_compromised_password'])
;
}
private function registerValidatorMapping(ContainerBuilder $container, array $config, array &$files)

View File

@ -64,6 +64,7 @@
<service id="validator.not_compromised_password" class="Symfony\Component\Validator\Constraints\NotCompromisedPasswordValidator">
<argument type="service" id="http_client" on-invalid="null" />
<argument>%kernel.charset%</argument>
<argument type="constant">false</argument>
<tag name="validator.constraint_validator" alias="Symfony\Component\Validator\Constraints\NotCompromisedPasswordValidator" />
</service>

View File

@ -32,8 +32,9 @@ class NotCompromisedPasswordValidator extends ConstraintValidator
private $httpClient;
private $charset;
private $disabled;
public function __construct(HttpClientInterface $httpClient = null, string $charset = 'UTF-8')
public function __construct(HttpClientInterface $httpClient = null, string $charset = 'UTF-8', bool $disabled = false)
{
if (null === $httpClient && !class_exists(HttpClient::class)) {
throw new \LogicException(sprintf('The "%s" class requires the "HttpClient" component. Try running "composer require symfony/http-client".', self::class));
@ -41,6 +42,7 @@ class NotCompromisedPasswordValidator extends ConstraintValidator
$this->httpClient = $httpClient ?? HttpClient::create();
$this->charset = $charset;
$this->disabled = $disabled;
}
/**
@ -54,6 +56,10 @@ class NotCompromisedPasswordValidator extends ConstraintValidator
throw new UnexpectedTypeException($constraint, NotCompromisedPassword::class);
}
if ($this->disabled) {
return;
}
if (null !== $value && !is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
}

View File

@ -60,6 +60,17 @@ class NotCompromisedPasswordValidatorTest extends ConstraintValidatorTestCase
$this->assertNoViolation();
}
public function testInvalidPasswordButDisabled()
{
$r = new \ReflectionProperty($this->validator, 'disabled');
$r->setAccessible(true);
$r->setValue($this->validator, true);
$this->validator->validate(self::PASSWORD_LEAKED, new NotCompromisedPassword());
$this->assertNoViolation();
}
public function testInvalidPassword()
{
$constraint = new NotCompromisedPassword();