Skip to content
This repository was archived by the owner on Oct 1, 2021. It is now read-only.

Commit 8ecf5fd

Browse files
authored
fix: add lowercase support to password rule (#4)
1 parent 9cf0748 commit 8ecf5fd

File tree

7 files changed

+181
-17
lines changed

7 files changed

+181
-17
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,4 @@ Temporary Items
5454

5555
# Custom
5656
icons.html
57+
.php_cs.cache

.php_cs.cache

Lines changed: 0 additions & 1 deletion
This file was deleted.

resources/lang/en/forms.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@
2020
'needs_uppercase' => 'One uppercase character',
2121
'needs_numeric' => 'One number',
2222
'needs_special_character' => 'One special character',
23-
'is_too_short' => '12 characters minumum',
23+
'needs_minimum_length' => '12 characters minumum',
2424
],
2525
];

resources/views/components/password-rules.blade.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
<div class="flex flex-1">
55
{{ $slot }}
66
</div>
7-
7+
88
<div class="flex flex-wrap -mx-4 text-sm">
99
@foreach($passwordRules as $ruleName => $ruleIsValid)
10-
<div class="flex items-center w-1/2 px-4 my-1 @if($ruleIsValid) text-theme-secondary-600 @else text-theme-secondary-500 @endif">
11-
<span class="block w-2 h-2 mr-2 rounded-full @if($ruleIsValid) bg-theme-primary-500 @else bg-theme-secondary-400 @endif"></span>
10+
<div class="flex items-center w-1/2 px-4 my-1 @if($ruleIsValid) text-theme-secondary-600 @else text-theme-secondary-500 @endif">
11+
<span class="block w-2 h-2 mr-2 rounded-full @if($ruleIsValid) bg-theme-primary-500 @else bg-theme-secondary-400 @endif"></span>
1212

13-
<span>
14-
@lang('fortify::forms.password_rules.' . Str::snake($ruleName))
15-
</span>
16-
</div>
13+
<span>
14+
@lang('fortify::forms.password_rules.' . Str::snake($ruleName))
15+
</span>
16+
</div>
1717
@endforeach
1818
</div>
1919
</div>

src/Actions/PasswordValidationRules.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace ARKEcosystem\Fortify\Actions;
44

5-
use Laravel\Fortify\Rules\Password;
5+
use ARKEcosystem\Fortify\Rules\Password;
66

77
trait PasswordValidationRules
88
{
@@ -16,7 +16,12 @@ protected function passwordRules()
1616
return [
1717
'required',
1818
'string',
19-
(new Password())->length(12)->requireUppercase()->requireNumeric()->requireSpecialCharacter(),
19+
(new Password())
20+
->length(12)
21+
->requireLowercase()
22+
->requireUppercase()
23+
->requireNumeric()
24+
->requireSpecialCharacter(),
2025
'confirmed',
2126
];
2227
}

src/Components/Concerns/ValidatesPassword.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace ARKEcosystem\Fortify\Components\Concerns;
44

5-
use Laravel\Fortify\Rules\Password;
5+
use ARKEcosystem\Fortify\Rules\Password;
66

77
trait ValidatesPassword
88
{
@@ -11,7 +11,7 @@ trait ValidatesPassword
1111
'needsUppercase' => false,
1212
'needsNumeric' => false,
1313
'needsSpecialCharacter' => false,
14-
'isTooShort' => false,
14+
'needsMinimumLength' => false,
1515
];
1616

1717
public function updatedStatePassword($password)
@@ -20,13 +20,13 @@ public function updatedStatePassword($password)
2020

2121
$passwordValidator = (new Password())
2222
->length(12)
23+
->requireLowercase()
2324
->requireUppercase()
2425
->requireNumeric()
2526
->requireSpecialCharacter();
2627

27-
collect($this->passwordRules)
28-
->each(function ($val, $ruleName) use ($passwordValidator, $password) {
29-
$this->passwordRules[$ruleName] = ! $passwordValidator->{$ruleName}($password);
30-
});
28+
foreach (array_keys($this->passwordRules) as $ruleName) {
29+
$this->passwordRules[$ruleName] = ! $passwordValidator->{$ruleName}($password);
30+
}
3131
}
3232
}

src/Rules/Password.php

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<?php
2+
3+
namespace ARKEcosystem\Fortify\Rules;
4+
5+
use Illuminate\Support\Str;
6+
use Laravel\Fortify\Rules\Password as Fortify;
7+
8+
class Password extends Fortify
9+
{
10+
/**
11+
* Indicates if the password must contain one lowercase character.
12+
*
13+
* @var bool
14+
*/
15+
protected $requireLowercase = false;
16+
17+
/**
18+
* Determine if the validation rule passes.
19+
*
20+
* @param string $attribute
21+
* @param mixed $value
22+
*
23+
* @return bool
24+
*/
25+
public function passes($attribute, $value)
26+
{
27+
if ($this->needsLowercase($value)) {
28+
return false;
29+
}
30+
31+
if ($this->needsUppercase($value)) {
32+
return false;
33+
}
34+
35+
if ($this->needsNumeric($value)) {
36+
return false;
37+
}
38+
39+
if ($this->needsSpecialCharacter($value)) {
40+
return false;
41+
}
42+
43+
return $this->needsMinimumLength($value);
44+
}
45+
46+
/**
47+
* Get the validation error message.
48+
*
49+
* @return string
50+
*/
51+
public function message()
52+
{
53+
if ($this->message) {
54+
return $this->message;
55+
}
56+
57+
switch (true) {
58+
case $this->requireUppercase
59+
&& ! $this->requireNumeric
60+
&& ! $this->requireSpecialCharacter:
61+
return __('The :attribute must be at least :length characters and contain at least one uppercase character.', [
62+
'length' => $this->length,
63+
]);
64+
65+
case $this->requireNumeric
66+
&& ! $this->requireUppercase
67+
&& ! $this->requireSpecialCharacter:
68+
return __('The :attribute must be at least :length characters and contain at least one number.', [
69+
'length' => $this->length,
70+
]);
71+
72+
case $this->requireSpecialCharacter
73+
&& ! $this->requireUppercase
74+
&& ! $this->requireNumeric:
75+
return __('The :attribute must be at least :length characters and contain at least one special character.', [
76+
'length' => $this->length,
77+
]);
78+
79+
case $this->requireUppercase
80+
&& $this->requireNumeric
81+
&& ! $this->requireSpecialCharacter:
82+
return __('The :attribute must be at least :length characters and contain at least one uppercase character and one number.', [
83+
'length' => $this->length,
84+
]);
85+
86+
case $this->requireUppercase
87+
&& $this->requireSpecialCharacter
88+
&& ! $this->requireNumeric:
89+
return __('The :attribute must be at least :length characters and contain at least one uppercase character and one special character.', [
90+
'length' => $this->length,
91+
]);
92+
93+
case $this->requireUppercase
94+
&& $this->requireNumeric
95+
&& $this->requireSpecialCharacter:
96+
return __('The :attribute must be at least :length characters and contain at least one uppercase character, one number, and one special character.', [
97+
'length' => $this->length,
98+
]);
99+
100+
default:
101+
return __('The :attribute must be at least :length characters.', [
102+
'length' => $this->length,
103+
]);
104+
}
105+
}
106+
107+
/**
108+
* Indicate that at least one lowercase character is required.
109+
*
110+
* @return $this
111+
*/
112+
public function requireLowercase()
113+
{
114+
$this->requireLowercase = true;
115+
116+
return $this;
117+
}
118+
119+
public function needsLowercase(string $value): bool
120+
{
121+
if (! $this->requireUppercase) {
122+
return false;
123+
}
124+
125+
return ! preg_match('/[a-z]/', $value);
126+
}
127+
128+
public function needsUppercase(string $value): bool
129+
{
130+
if (! $this->requireLowercase) {
131+
return false;
132+
}
133+
134+
return ! preg_match('/[A-Z]/', $value);
135+
}
136+
137+
public function needsNumeric(string $value): bool
138+
{
139+
if (! $this->requireNumeric) {
140+
return false;
141+
}
142+
143+
return ! preg_match('/[0-9]/', $value);
144+
}
145+
146+
public function needsSpecialCharacter(string $value): bool
147+
{
148+
if (! $this->requireSpecialCharacter) {
149+
return false;
150+
}
151+
152+
return ! preg_match('/[\W_]/', $value);
153+
}
154+
155+
public function needsMinimumLength(string $value): bool
156+
{
157+
return ! (Str::length($value) >= $this->length);
158+
}
159+
}

0 commit comments

Comments
 (0)