Skip to content
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

[1.x] Add option to force the password to have a special character. #65

Merged
merged 8 commits into from
Sep 22, 2020
Merged
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
78 changes: 70 additions & 8 deletions src/Rules/Password.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ class Password implements Rule
*/
protected $requireNumeric = false;

/**
* Indicates if the password must contain one special character.
*
* @var int
*/
protected $requireSpecialCharacter = false;

/**
* The message that should be used when validation fails.
*
Expand All @@ -52,6 +59,10 @@ public function passes($attribute, $value)
return false;
}

if ($this->requireSpecialCharacter && ! preg_match('/[\W_]/', $value)) {
return false;
}

return Str::length($value) >= $this->length;
}

Expand All @@ -66,14 +77,53 @@ public function message()
return $this->message;
}

if ($this->requireUppercase && ! $this->requireNumeric) {
return __('The :attribute must be at least :length characters and contain at least one uppercase character.', ['length' => $this->length]);
} elseif ($this->requireNumeric && ! $this->requireUppercase) {
return __('The :attribute must be at least :length characters and contain at least one number.', ['length' => $this->length]);
} elseif ($this->requireUppercase && $this->requireNumeric) {
return __('The :attribute must be at least :length characters and contain at least one uppercase character and number.', ['length' => $this->length]);
} else {
return __('The :attribute must be at least :length characters.', ['length' => $this->length]);
switch (true) {
case $this->requireUppercase
&& ! $this->requireNumeric
&& ! $this->requireSpecialCharacter:
return __('The :attribute must be at least :length characters and contain at least one uppercase character.', [
'length' => $this->length,
]);

case $this->requireNumeric
&& ! $this->requireUppercase
&& ! $this->requireSpecialCharacter:
return __('The :attribute must be at least :length characters and contain at least one number.', [
'length' => $this->length,
]);

case $this->requireSpecialCharacter
&& ! $this->requireUppercase
&& ! $this->requireNumeric:
return __('The :attribute must be at least :length characters and contain at least one special character.', [
'length' => $this->length,
]);

case $this->requireUppercase
&& $this->requireNumeric
&& ! $this->requireSpecialCharacter:
return __('The :attribute must be at least :length characters and contain at least one uppercase character and one number.', [
'length' => $this->length,
]);

case $this->requireUppercase
&& $this->requireSpecialCharacter
&& ! $this->requireNumeric:
return __('The :attribute must be at least :length characters and contain at least one uppercase character and one special character.', [
'length' => $this->length,
]);

case $this->requireUppercase
&& $this->requireNumeric
&& $this->requireSpecialCharacter:
return __('The :attribute must be at least :length characters and contain at least one uppercase character, one number, and one special character.', [
'length' => $this->length,
]);

default:
return __('The :attribute must be at least :length characters.', [
'length' => $this->length,
]);
}
}

Expand Down Expand Up @@ -114,6 +164,18 @@ public function requireNumeric()
return $this;
}

/**
* Indicate that at least one special character is required.
*
* @return $this
*/
public function requireSpecialCharacter()
{
$this->requireSpecialCharacter = true;

return $this;
}

/**
* Set the message that should be used when the rule fails.
*
Expand Down