Skip to content

Commit 0f1a8a4

Browse files
committed
refactor: move validation rules to Config\AuthSession
1 parent 852bfb9 commit 0f1a8a4

5 files changed

Lines changed: 40 additions & 36 deletions

File tree

src/Config/AuthSession.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace CodeIgniter\Shield\Config;
4+
5+
use CodeIgniter\Config\BaseConfig;
6+
7+
/**
8+
* Config for Session Authenticator
9+
*/
10+
class AuthSession extends BaseConfig
11+
{
12+
/**
13+
* The validation rules for username
14+
*
15+
* @var string[]
16+
*/
17+
public array $usernameValidationRules = [
18+
'required',
19+
'max_length[30]',
20+
'min_length[3]',
21+
'regex_match[/\A[a-zA-Z0-9\.]+\z/]',
22+
];
23+
24+
/**
25+
* The validation rules for email
26+
*
27+
* @var string[]
28+
*/
29+
public array $emailValidationRules = [
30+
'required',
31+
'max_length[254]',
32+
'valid_email',
33+
];
34+
}

src/Controllers/LoginController.php

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -64,41 +64,12 @@ public function loginAction(): RedirectResponse
6464
protected function getValidationRules(): array
6565
{
6666
return setting('Validation.login') ?? [
67-
//'username' => static::getUsernameRules(),
68-
'email' => static::getEmailRules(),
67+
//'username' => config('AuthSession')->usernameValidationRules,
68+
'email' => config('AuthSession')->emailValidationRules,
6969
'password' => 'required',
7070
];
7171
}
7272

73-
/**
74-
* Returns the validation rules for username
75-
*
76-
* @return string[]
77-
*/
78-
public static function getUsernameRules(): array
79-
{
80-
return [
81-
'required',
82-
'max_length[30]',
83-
'min_length[3]',
84-
'regex_match[/\A[a-zA-Z0-9\.]+\z/]',
85-
];
86-
}
87-
88-
/**
89-
* Returns the validation rules for email
90-
*
91-
* @return string[]
92-
*/
93-
public static function getEmailRules(): array
94-
{
95-
return [
96-
'required',
97-
'max_length[254]',
98-
'valid_email',
99-
];
100-
}
101-
10273
/**
10374
* Logs the current user out.
10475
*/

src/Controllers/MagicLinkController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ private function recordLoginAttempt(
191191
protected function getValidationRules(): array
192192
{
193193
return [
194-
'email' => LoginController::getEmailRules(),
194+
'email' => config('AuthSession')->emailValidationRules,
195195
];
196196
}
197197
}

src/Controllers/RegisterController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,11 @@ protected function getUserEntity(): User
131131
protected function getValidationRules(): array
132132
{
133133
$registrationUsernameRules = array_merge(
134-
LoginController::getUsernameRules(),
134+
config('AuthSession')->usernameValidationRules,
135135
['is_unique[users.username]']
136136
);
137137
$registrationEmailRules = array_merge(
138-
LoginController::getEmailRules(),
138+
config('AuthSession')->emailValidationRules,
139139
['is_unique[auth_identities.secret]']
140140
);
141141

tests/Controllers/LoginTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use CodeIgniter\Config\Factories;
66
use CodeIgniter\I18n\Time;
77
use CodeIgniter\Shield\Authentication\Actions\Email2FA;
8-
use CodeIgniter\Shield\Controllers\LoginController;
98
use CodeIgniter\Test\DatabaseTestTrait;
109
use CodeIgniter\Test\FeatureTestTrait;
1110
use Config\Services;
@@ -124,7 +123,7 @@ public function testLoginActionUsernameSuccess(): void
124123
'password' => 'required',
125124
];
126125
};
127-
$config->login['username'] = LoginController::getUsernameRules();
126+
$config->login['username'] = config('AuthSession')->usernameValidationRules;
128127
Factories::injectMock('config', 'Validation', $config);
129128

130129
$this->user->createEmailIdentity([

0 commit comments

Comments
 (0)