Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feature #30932 [Validator] Add an option to disable NotCompromisedPas…
…swordValidator (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        | symfony/symfony-docs#11327

EUFOSSA

Commits
-------

9a2787e [Validator] Add an option to disable NotCompromisedPasswordValidator
  • Loading branch information
fabpot committed Apr 6, 2019
2 parents e05aaf9 + 9a2787e commit f80df4c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
Expand Up @@ -834,6 +834,10 @@ private function addValidationSection(ArrayNodeDefinition $rootNode)
->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)
Expand Down
Expand Up @@ -1241,6 +1241,11 @@ private function registerValidationConfiguration(array $config, ContainerBuilder
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)
Expand Down
Expand Up @@ -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>

Expand Down
Expand Up @@ -32,15 +32,17 @@ 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));
}

$this->httpClient = $httpClient ?? HttpClient::create();
$this->charset = $charset;
$this->disabled = $disabled;
}

/**
Expand All @@ -54,6 +56,10 @@ public function validate($value, Constraint $constraint)
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');
}
Expand Down
Expand Up @@ -60,6 +60,17 @@ public function testEmptyStringIsValid()
$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();
Expand Down

0 comments on commit f80df4c

Please sign in to comment.