Skip to content

Commit 3f443a8

Browse files
committed
Merge branch 'develop' into quick-start-docs
2 parents f1de212 + ac7f1de commit 3f443a8

15 files changed

Lines changed: 128 additions & 36 deletions

File tree

docs/customization.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,32 @@ class LoginController extends ShieldLogin
8686
}
8787
}
8888
```
89+
90+
## Custom validation rules
91+
92+
### Registration
93+
94+
Shield has the following rules for registration:
95+
96+
```php
97+
[
98+
'username' => 'required|alpha_numeric_space|min_length[3]|is_unique[users.username]',
99+
'email' => 'required|valid_email|is_unique[auth_identities.secret]',
100+
'password' => 'required|strong_password',
101+
'password_confirm' => 'required|matches[password]',
102+
];
103+
```
104+
105+
If you need a different set of rules for registration, you can specify them in your `Validation` configuration (**app\Config\Validation.php**) like:
106+
107+
```php
108+
//--------------------------------------------------------------------
109+
// Rules
110+
//--------------------------------------------------------------------
111+
public $registration = [
112+
'username' => 'required|alpha_numeric_space|min_length[3]|is_unique[users.username]',
113+
'email' => 'required|valid_email|is_unique[auth_identities.secret]',
114+
'password' => 'required|strong_password',
115+
'password_confirm' => 'required|matches[password]',
116+
];
117+
```

src/Authentication/Authenticators/AccessTokens.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public function check(array $credentials): Result
124124
]);
125125
}
126126

127-
$token->last_used_at = Time::now()->toDateTimeString();
127+
$token->last_used_at = Time::now()->format('Y-m-d H:i:s');
128128
$identityModel->save($token);
129129

130130
// Ensure the token is set as the current token

src/Authentication/Authenticators/Session.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -836,10 +836,9 @@ protected function rememberUser(User $user): void
836836

837837
private function calcExpires(): string
838838
{
839-
return date(
840-
'Y-m-d H:i:s',
841-
time() + setting('Auth.sessionConfig')['rememberLength']
842-
);
839+
$timestamp = Time::now()->getTimestamp() + setting('Auth.sessionConfig')['rememberLength'];
840+
841+
return Time::createFromTimestamp($timestamp)->format('Y-m-d H:i:s');
843842
}
844843

845844
private function setRememberMeCookie(string $rawToken): void

src/Authorization/Traits/Authorizable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ private function saveGroupsOrPermissions(string $type, $model, array $cache): vo
367367
$inserts[] = [
368368
'user_id' => $this->id,
369369
$type => $item,
370-
'created_at' => Time::now()->toDateTimeString(),
370+
'created_at' => Time::now()->format('Y-m-d H:i:s'),
371371
];
372372
}
373373

src/Controllers/LoginController.php

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
namespace CodeIgniter\Shield\Controllers;
44

55
use App\Controllers\BaseController;
6-
use CodeIgniter\HTTP\IncomingRequest;
76
use CodeIgniter\HTTP\RedirectResponse;
8-
use CodeIgniter\HTTP\Response;
97

108
class LoginController extends BaseController
119
{
@@ -27,18 +25,21 @@ public function loginView()
2725

2826
/**
2927
* Attempts to log the user in.
30-
*
31-
* @return Response|string
3228
*/
33-
public function loginAction()
29+
public function loginAction(): RedirectResponse
3430
{
35-
/** @var IncomingRequest $request */
36-
$request = service('request');
31+
// Validate here first, since some things,
32+
// like the password, can only be validated properly here.
33+
$rules = $this->getValidationRules();
34+
35+
if (! $this->validate($rules)) {
36+
return redirect()->back()->withInput()->with('errors', $this->validator->getErrors());
37+
}
3738

38-
$credentials = $request->getPost(setting('Auth.validFields'));
39+
$credentials = $this->request->getPost(setting('Auth.validFields'));
3940
$credentials = array_filter($credentials);
40-
$credentials['password'] = $request->getPost('password');
41-
$remember = (bool) $request->getPost('remember');
41+
$credentials['password'] = $this->request->getPost('password');
42+
$remember = (bool) $this->request->getPost('remember');
4243

4344
// Attempt to login
4445
$result = auth('session')->remember($remember)->attempt($credentials);
@@ -56,11 +57,23 @@ public function loginAction()
5657
}
5758

5859
/**
59-
* Logs the current user out.
60+
* Returns the rules that should be used for validation.
6061
*
61-
* @return Response|string
62+
* @return string[]
63+
*/
64+
protected function getValidationRules(): array
65+
{
66+
return setting('Validation.login') ?? [
67+
//'username' => 'required|max_length[30]|alpha_numeric_space|min_length[3]',
68+
'email' => 'required|max_length[254]|valid_email',
69+
'password' => 'required',
70+
];
71+
}
72+
73+
/**
74+
* Logs the current user out.
6275
*/
63-
public function logoutAction()
76+
public function logoutAction(): RedirectResponse
6477
{
6578
auth()->logout();
6679

src/Controllers/MagicLinkController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function loginAction()
8585
'user_id' => $user->id,
8686
'type' => Session::ID_TYPE_MAGIC_LINK,
8787
'secret' => $token,
88-
'expires' => Time::now()->addSeconds(setting('Auth.magicLinkLifetime'))->toDateTimeString(),
88+
'expires' => Time::now()->addSeconds(setting('Auth.magicLinkLifetime'))->format('Y-m-d H:i:s'),
8989
]);
9090

9191
// Send the user an email with the code

src/Controllers/RegisterController.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use CodeIgniter\Shield\Authentication\Authenticators\Session;
99
use CodeIgniter\Shield\Entities\User;
1010
use CodeIgniter\Shield\Models\UserModel;
11-
use CodeIgniter\Validation\Validation;
1211

1312
/**
1413
* Class RegisterController
@@ -53,11 +52,8 @@ public function registerAction(): RedirectResponse
5352
// like the password, can only be validated properly here.
5453
$rules = $this->getValidationRules();
5554

56-
/** @var Validation $validation */
57-
$validation = service('validation');
58-
5955
if (! $this->validate($rules)) {
60-
return redirect()->back()->withInput()->with('errors', $validation->getErrors());
56+
return redirect()->back()->withInput()->with('errors', $this->validator->getErrors());
6157
}
6258

6359
// Save the user
@@ -134,7 +130,7 @@ protected function getUserEntity(): User
134130
*/
135131
protected function getValidationRules(): array
136132
{
137-
return [
133+
return setting('Validation.registration') ?? [
138134
'username' => 'required|alpha_numeric_space|min_length[3]|is_unique[users.username]',
139135
'email' => 'required|valid_email|is_unique[auth_identities.secret]',
140136
'password' => 'required|strong_password',

src/Models/LoginModel.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function recordLoginAttempt(
6161
'id_type' => $idType,
6262
'identifier' => $identifier,
6363
'user_id' => $userId,
64-
'date' => date('Y-m-d H:i:s'),
64+
'date' => Time::now()->format('Y-m-d H:i:s'),
6565
'success' => (int) $success,
6666
]);
6767

@@ -91,7 +91,7 @@ public function fake(Generator &$faker): Login
9191
'id_type' => Session::ID_TYPE_EMAIL_PASSWORD,
9292
'identifier' => $faker->email,
9393
'user_id' => null,
94-
'date' => Time::parse('-1 day')->toDateTimeString(),
94+
'date' => Time::parse('-1 day')->format('Y-m-d H:i:s'),
9595
'success' => true,
9696
]);
9797
}

src/Models/TokenLoginModel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function fake(Generator &$faker): Login
2222
'ip_address' => $faker->ipv4,
2323
'identifier' => 'token: ' . random_string('crypto', 64),
2424
'user_id' => fake(UserModel::class)->id,
25-
'date' => Time::parse('-1 day')->toDateTimeString(),
25+
'date' => Time::parse('-1 day')->format('Y-m-d H:i:s'),
2626
'success' => true,
2727
]);
2828
}

src/Models/UserIdentityModel.php

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

33
namespace CodeIgniter\Shield\Models;
44

5+
use CodeIgniter\I18n\Time;
56
use CodeIgniter\Model;
67
use CodeIgniter\Shield\Authentication\Authenticators\AccessTokens;
78
use CodeIgniter\Shield\Authentication\Authenticators\Session;
@@ -247,7 +248,7 @@ public function getIdentitiesByTypes(User $user, array $types): array
247248
*/
248249
public function touchIdentity(UserIdentity $identity): void
249250
{
250-
$identity->last_used_at = date('Y-m-d H:i:s');
251+
$identity->last_used_at = Time::now()->format('Y-m-d H:i:s');
251252

252253
$return = $this->save($identity);
253254

0 commit comments

Comments
 (0)