Skip to content

Commit

Permalink
[8.x] Fix Password validation failure to allow errors after min rule (l…
Browse files Browse the repository at this point in the history
…aravel#40030)

* Change Password validation failure to allow errors after min rule

* Use `Validator::after()` to run Password rules.

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* Fix for uncompromised rule to not be called in case of failure

Co-authored-by: Mior Muhammad Zaki <crynobone@gmail.com>
  • Loading branch information
PovilasKorop and crynobone authored Dec 14, 2021
1 parent c122970 commit cd11f32
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 27 deletions.
53 changes: 28 additions & 25 deletions src/Illuminate/Validation/Rules/Password.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,36 +270,39 @@ public function passes($attribute, $value)
{
$this->messages = [];

$validator = Validator::make($this->data, [
$attribute => 'string|min:'.$this->min,
], $this->validator->customMessages, $this->validator->customAttributes);
$validator = Validator::make(
$this->data,
[$attribute => 'string|min:'.$this->min],
$this->validator->customMessages,
$this->validator->customAttributes
)->after(function ($validator) use ($attribute, $value) {
if (! is_string($value)) {
return;
}

$value = (string) $value;

if ($this->mixedCase && ! preg_match('/(\p{Ll}+.*\p{Lu})|(\p{Lu}+.*\p{Ll})/u', $value)) {
$validator->errors()->add($attribute, 'The :attribute must contain at least one uppercase and one lowercase letter.');
}

if ($this->letters && ! preg_match('/\pL/u', $value)) {
$validator->errors()->add($attribute, 'The :attribute must contain at least one letter.');
}

if ($this->symbols && ! preg_match('/\p{Z}|\p{S}|\p{P}/u', $value)) {
$validator->errors()->add($attribute, 'The :attribute must contain at least one symbol.');
}

if ($this->numbers && ! preg_match('/\pN/u', $value)) {
$validator->errors()->add($attribute, 'The :attribute must contain at least one number.');
}
});

if ($validator->fails()) {
return $this->fail($validator->messages()->all());
}

$value = (string) $value;

if ($this->mixedCase && ! preg_match('/(\p{Ll}+.*\p{Lu})|(\p{Lu}+.*\p{Ll})/u', $value)) {
$this->fail('The :attribute must contain at least one uppercase and one lowercase letter.');
}

if ($this->letters && ! preg_match('/\pL/u', $value)) {
$this->fail('The :attribute must contain at least one letter.');
}

if ($this->symbols && ! preg_match('/\p{Z}|\p{S}|\p{P}/u', $value)) {
$this->fail('The :attribute must contain at least one symbol.');
}

if ($this->numbers && ! preg_match('/\pN/u', $value)) {
$this->fail('The :attribute must contain at least one number.');
}

if (! empty($this->messages)) {
return false;
}

if ($this->uncompromised && ! Container::getInstance()->make(UncompromisedVerifier::class)->verify([
'value' => $value,
'threshold' => $this->compromisedThreshold,
Expand Down
18 changes: 16 additions & 2 deletions tests/Validation/ValidationPasswordRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,15 @@ public function testMessagesOrder()
'validation.required',
]);

$this->fails($makeRules(), ['foo', 'azdazd', '1231231'], [
$this->fails($makeRules(), ['foo', 'azdazd'], [
'validation.min.string',
'The my password must contain at least one uppercase and one lowercase letter.',
'The my password must contain at least one number.',
]);

$this->fails($makeRules(), ['1231231'], [
'validation.min.string',
'The my password must contain at least one uppercase and one lowercase letter.',
]);

$this->fails($makeRules(), ['4564654564564'], [
Expand All @@ -165,8 +172,15 @@ public function testMessagesOrder()

$this->passes($makeRules(), [null]);

$this->fails($makeRules(), ['foo', 'azdazd', '1231231'], [
$this->fails($makeRules(), ['foo', 'azdazd'], [
'validation.min.string',
'The my password must contain at least one symbol.',
]);

$this->fails($makeRules(), ['1231231'], [
'validation.min.string',
'The my password must contain at least one letter.',
'The my password must contain at least one symbol.',
]);

$this->fails($makeRules(), ['aaaaaaaaa', 'TJQSJQSIUQHS'], [
Expand Down

0 comments on commit cd11f32

Please sign in to comment.