feature #30932 [Validator] Add an option to disable NotCompromisedPasswordValidator (lyrixx)

This PR was merged into the 4.3-dev branch.

Discussion
----------

[Validator] Add an option to disable NotCompromisedPasswordValidator

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #30871
| License       | MIT
| Doc PR        | https://github.com/symfony/symfony-docs/issues/11327

EUFOSSA

Commits
-------

9a2787e89a [Validator] Add an option to disable NotCompromisedPasswordValidator
This commit is contained in:
Fabien Potencier 2019-04-06 21:23:33 +02:00
commit f80df4cea5
5 changed files with 28 additions and 1 deletions

View File

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

View File

@ -1241,6 +1241,11 @@ class FrameworkExtension extends Extension
if (!$propertyInfoEnabled || !$config['auto_mapping'] || !class_exists(PropertyInfoLoader::class)) { if (!$propertyInfoEnabled || !$config['auto_mapping'] || !class_exists(PropertyInfoLoader::class)) {
$container->removeDefinition('validator.property_info_loader'); $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) 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"> <service id="validator.not_compromised_password" class="Symfony\Component\Validator\Constraints\NotCompromisedPasswordValidator">
<argument type="service" id="http_client" on-invalid="null" /> <argument type="service" id="http_client" on-invalid="null" />
<argument>%kernel.charset%</argument> <argument>%kernel.charset%</argument>
<argument type="constant">false</argument>
<tag name="validator.constraint_validator" alias="Symfony\Component\Validator\Constraints\NotCompromisedPasswordValidator" /> <tag name="validator.constraint_validator" alias="Symfony\Component\Validator\Constraints\NotCompromisedPasswordValidator" />
</service> </service>

View File

@ -32,8 +32,9 @@ class NotCompromisedPasswordValidator extends ConstraintValidator
private $httpClient; private $httpClient;
private $charset; 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)) { 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)); 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->httpClient = $httpClient ?? HttpClient::create();
$this->charset = $charset; $this->charset = $charset;
$this->disabled = $disabled;
} }
/** /**
@ -54,6 +56,10 @@ class NotCompromisedPasswordValidator extends ConstraintValidator
throw new UnexpectedTypeException($constraint, NotCompromisedPassword::class); throw new UnexpectedTypeException($constraint, NotCompromisedPassword::class);
} }
if ($this->disabled) {
return;
}
if (null !== $value && !is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) { if (null !== $value && !is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string'); throw new UnexpectedTypeException($value, 'string');
} }

View File

@ -60,6 +60,17 @@ class NotCompromisedPasswordValidatorTest extends ConstraintValidatorTestCase
$this->assertNoViolation(); $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() public function testInvalidPassword()
{ {
$constraint = new NotCompromisedPassword(); $constraint = new NotCompromisedPassword();