Created
June 16, 2019 23:12
-
-
Save JoshuaDoshua/920b9fb57acd542ade414cabfecd5bce to your computer and use it in GitHub Desktop.
Laravel: Password Requirements Rule
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
... | |
use App\Rules\PasswordRequirementRule; | |
class SaveUserRequest extends FormRequest | |
{ | |
... | |
public function rules() { | |
... | |
$rules['password'] = [ | |
'required', | |
'confirmed', | |
(new PasswordRequirementRule) | |
]; | |
... | |
} | |
... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Rules; | |
use Illuminate\Contracts\Validation\Rule; | |
class PasswordRequirementsRule implements Rule | |
{ | |
// @var string | |
//private $regex; | |
// @var int | |
private $minLength = 8; | |
// @var bool | |
private $needsNumber = true; | |
// @var bool | |
private $needsLowercaseLetter = true; | |
// @var bool | |
private $needsUppercaseLetter = true; | |
// @var bool | |
private $needsSpecialCharacter = true; | |
// @var bool (faith in humanity) | |
private $isValid = true; | |
// @var array | |
private $errors = []; | |
/** | |
* Create a new rule instance. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
//$this->regex = "/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{6,}$/"; | |
} | |
/** | |
* Check the value against the rule's minimum length. | |
* | |
* @param $string $value | |
* @return bool | |
*/ | |
private function checkLength(string $value): bool | |
{ | |
return strlen($value) >= $this->minLength; | |
} | |
/** | |
* Check the value for a number. | |
* | |
* @param $string $value | |
* @return bool | |
*/ | |
private function checkNumber(string $value) | |
{ | |
return (bool) preg_match('/[0-9]{1,}/', $value); | |
} | |
/** | |
* Check the value for a lowercase letter. | |
* | |
* @param $string $value | |
* @return bool | |
*/ | |
private function checkLowercaseLetter(string $value): bool | |
{ | |
return (bool) preg_match('/[a-z]{1,}/', $value); | |
} | |
/** | |
* Check the value for a uppercase letter. | |
* | |
* @param $string $value | |
* @return bool | |
*/ | |
private function checkUppercaseLetter(string $value): bool | |
{ | |
return (bool) preg_match('/[A-Z]{1,}/', $value); | |
} | |
/** | |
* Check the value for a special character. | |
* | |
* @param $string $value | |
* @return bool | |
*/ | |
private function checkSpecialCharacter(string $value): bool | |
{ | |
$chars = '/[\`\~\!\@\#\$\%\^\&\*\(\)\_\+\-\=\{\}\|\[\]\\\:\"\;\'\<\>\?\,\.\/]{1,}/'; | |
return (bool) preg_match($chars, $value); | |
} | |
/** | |
* Determine if the validation rule passes. | |
* | |
* @see \Illuminate\Contracts\Validation\Rule | |
* @param string $attribute | |
* @param mixed $value | |
* @return bool | |
*/ | |
public function passes($attribute, $value) | |
{ | |
if ($this->minLength > 0 && !$this->checkLength($value)) | |
$this->errors[] = "a minimum of {$this->minLength} characters"; | |
if ($this->needsNumber && !$this->checkNumber($value)) | |
$this->errors[] = "at least one number"; | |
if ($this->needsLowercaseLetter && !$this->checkLowercaseLetter($value)) | |
$this->errors[] = "at least one lowercase character"; | |
if ($this->needsUppercaseLetter && !$this->checkUppercaseLetter($value)) | |
$this->errors[] = "at least one uppercase character"; | |
if ($this->needsSpecialCharacter && !$this->checkSpecialCharacter($value)) | |
$this->errors[] = "at least one special character"; | |
return count($this->errors) === 0; | |
} | |
/** | |
* Get the validation error message. | |
* | |
* @see \Illuminate\Contracts\Validation\Rule | |
* @return string | |
*/ | |
public function message() | |
{ | |
switch (count($this->errors)): | |
case 1: | |
$missing = $this->errors[0]; | |
break; | |
case 2: | |
$missing = "{$this->errors[0]} and {$this->errors[1]}"; | |
break; | |
default: | |
$last = array_pop($this->errors); | |
$this->errors[] = "and {$last}"; | |
$missing = implode(", ", $this->errors); | |
endswitch; // count errors | |
return "Password must include {$missing}."; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment