Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f80df4c

Browse files
committed
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 | symfony/symfony-docs#11327 EUFOSSA Commits ------- 9a2787e [Validator] Add an option to disable NotCompromisedPasswordValidator
2 parents e05aaf9 + 9a2787e commit f80df4c

File tree

5 files changed

+28
-1
lines changed

5 files changed

+28
-1
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,10 @@ private function addValidationSection(ArrayNodeDefinition $rootNode)
834834
->end()
835835
->end()
836836
->end()
837+
->booleanNode('disable_not_compromised_password')
838+
->defaultFalse()
839+
->info('Disable NotCompromisedPassword Validator: the value will always be valid.')
840+
->end()
837841
->arrayNode('auto_mapping')
838842
->useAttributeAsKey('namespace')
839843
->normalizeKeys(false)

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,6 +1241,11 @@ private function registerValidationConfiguration(array $config, ContainerBuilder
12411241
if (!$propertyInfoEnabled || !$config['auto_mapping'] || !class_exists(PropertyInfoLoader::class)) {
12421242
$container->removeDefinition('validator.property_info_loader');
12431243
}
1244+
1245+
$container
1246+
->getDefinition('validator.not_compromised_password')
1247+
->setArgument(2, $config['disable_not_compromised_password'])
1248+
;
12441249
}
12451250

12461251
private function registerValidatorMapping(ContainerBuilder $container, array $config, array &$files)

src/Symfony/Bundle/FrameworkBundle/Resources/config/validator.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
<service id="validator.not_compromised_password" class="Symfony\Component\Validator\Constraints\NotCompromisedPasswordValidator">
6565
<argument type="service" id="http_client" on-invalid="null" />
6666
<argument>%kernel.charset%</argument>
67+
<argument type="constant">false</argument>
6768
<tag name="validator.constraint_validator" alias="Symfony\Component\Validator\Constraints\NotCompromisedPasswordValidator" />
6869
</service>
6970

src/Symfony/Component/Validator/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
}

src/Symfony/Component/Validator/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)