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

Skip to content

[Validator] set the password strength as a violation parameter #54479

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
7.1
---

* Add the calculated strength to violations in `PasswordStrengthValidator`
* Add support for `Stringable` values when using the `Cidr`, `CssColor`, `ExpressionSyntax` and `PasswordStrength` constraints
* Add `MacAddress` constraint
* Add `*_NO_PUBLIC`, `*_ONLY_PRIVATE` and `*_ONLY_RESERVED` versions to `Ip` constraint
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function validate(#[\SensitiveParameter] mixed $value, Constraint $constr
if ($strength < $constraint->minScore) {
$this->context->buildViolation($constraint->message)
->setCode(PasswordStrength::PASSWORD_STRENGTH_ERROR)
->setParameter('{{ strength }}', $strength)
->addViolation();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function testValidValues(string|\Stringable $value, int $expectedStrength

$this->buildViolation('The password strength is too low. Please use a stronger password.')
->setCode(PasswordStrength::PASSWORD_STRENGTH_ERROR)
->setParameter('{{ strength }}', $expectedStrength)
->assertRaised();
}

Expand All @@ -55,13 +56,15 @@ public static function getValidValues(): iterable
/**
* @dataProvider provideInvalidConstraints
*/
public function testThePasswordIsWeak(PasswordStrength $constraint, string $password, string $expectedMessage, string $expectedCode, array $parameters = [])
public function testThePasswordIsWeak(PasswordStrength $constraint, string $password, string $expectedMessage, string $expectedCode, string $strength)
{
$this->validator->validate($password, $constraint);

$this->buildViolation($expectedMessage)
->setCode($expectedCode)
->setParameters($parameters)
->setParameters([
'{{ strength }}' => $strength,
])
->assertRaised();
}

Expand All @@ -72,18 +75,21 @@ public static function provideInvalidConstraints(): iterable
'password',
'The password strength is too low. Please use a stronger password.',
PasswordStrength::PASSWORD_STRENGTH_ERROR,
'0',
];
yield [
new PasswordStrength(minScore: PasswordStrength::STRENGTH_VERY_STRONG),
'Good password?',
'The password strength is too low. Please use a stronger password.',
PasswordStrength::PASSWORD_STRENGTH_ERROR,
'1',
];
yield [
new PasswordStrength(message: 'This password should be strong.'),
'password',
'This password should be strong.',
PasswordStrength::PASSWORD_STRENGTH_ERROR,
'0',
];
}
}