-
Notifications
You must be signed in to change notification settings - Fork 138
refactor: validation rules #861
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
kenjis
merged 11 commits into
codeigniter4:develop
from
kenjis:refactor-validation-rules
Sep 30, 2023
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c5a4b45
refactor: move `label` for validation rules to Config\Auth
kenjis 84df605
refactor: rename classname and method name
kenjis cc481f5
refactor: extract methods for password fields
kenjis 82fe260
refactor: add private property
kenjis d1c0658
refactor: move validation rules to ValidationRules
kenjis e25fe07
refactor: if setting returns values, returns it
kenjis 95ecdc1
docs: update doc comments
kenjis 114cfba
fix: wrong password validation rules for login
kenjis 76f8c97
refactor: do not use array_merge()
kenjis b080a71
refactor: shorten variable names
kenjis 8badb41
docs: update default validation rules
kenjis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CodeIgniter\Shield\Validation; | ||
|
||
use CodeIgniter\Shield\Authentication\Passwords; | ||
use CodeIgniter\Shield\Config\Auth; | ||
|
||
class ValidationRules | ||
{ | ||
private Auth $config; | ||
|
||
/** | ||
* Auth Table names | ||
*/ | ||
private array $tables; | ||
|
||
public function __construct() | ||
{ | ||
/** @var Auth $authConfig */ | ||
$authConfig = config('Auth'); | ||
|
||
$this->config = $authConfig; | ||
$this->tables = $this->config->tables; | ||
} | ||
|
||
public function getRegistrationRules(): array | ||
{ | ||
helper('setting'); | ||
|
||
$setting = setting('Validation.registration'); | ||
if ($setting !== null) { | ||
return $setting; | ||
} | ||
|
||
$usernameRules = $this->config->usernameValidationRules; | ||
$usernameRules['rules'][] = sprintf( | ||
'is_unique[%s.username]', | ||
$this->tables['users'] | ||
); | ||
|
||
$emailRules = $this->config->emailValidationRules; | ||
$emailRules['rules'][] = sprintf( | ||
'is_unique[%s.secret]', | ||
$this->tables['identities'] | ||
); | ||
|
||
$passwordRules = $this->getPasswordRules(); | ||
$passwordRules['rules'][] = 'strong_password[]'; | ||
|
||
return [ | ||
'username' => $usernameRules, | ||
'email' => $emailRules, | ||
'password' => $passwordRules, | ||
'password_confirm' => $this->getPasswordConfirmRules(), | ||
]; | ||
} | ||
|
||
public function getLoginRules(): array | ||
{ | ||
helper('setting'); | ||
|
||
return setting('Validation.login') ?? [ | ||
// 'username' => $this->config->usernameValidationRules, | ||
'email' => $this->config->emailValidationRules, | ||
'password' => $this->getPasswordRules(), | ||
]; | ||
} | ||
|
||
public function getPasswordRules(): array | ||
{ | ||
return [ | ||
'label' => 'Auth.password', | ||
'rules' => ['required', Passwords::getMaxLengthRule()], | ||
'errors' => [ | ||
'max_byte' => 'Auth.errorPasswordTooLongBytes', | ||
], | ||
]; | ||
} | ||
|
||
public function getPasswordConfirmRules(): array | ||
{ | ||
return [ | ||
'label' => 'Auth.passwordConfirm', | ||
'rules' => 'required|matches[password]', | ||
]; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok,
shouldn't the changes be applied to the documentation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.