Skip to content

Commit 38cdc1a

Browse files
committed
refactor: get rid of Authenticatable/UserProvider, use User/UserModel
1 parent fb71b7d commit 38cdc1a

File tree

13 files changed

+37
-33
lines changed

13 files changed

+37
-33
lines changed

src/Auth.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use CodeIgniter\Shield\Authentication\Authentication;
77
use CodeIgniter\Shield\Authentication\AuthenticationException;
88
use CodeIgniter\Shield\Authentication\AuthenticatorInterface;
9+
use CodeIgniter\Shield\Entities\User;
910
use CodeIgniter\Shield\Interfaces\Authenticatable;
1011
use CodeIgniter\Shield\Interfaces\UserProvider;
1112

@@ -20,6 +21,9 @@
2021
* @method void loginById($userId)
2122
* @method bool logout()
2223
* @method void recordActive()
24+
*
25+
* Authenticators\Session:
26+
* @method $this remember(bool $shouldRemember = true)
2327
*/
2428
class Auth
2529
{
@@ -64,7 +68,7 @@ public function getAuthenticator(): AuthenticatorInterface
6468
/**
6569
* Returns the current user, if logged in.
6670
*/
67-
public function user(): ?Authenticatable
71+
public function user(): ?User
6872
{
6973
return $this->getAuthenticator()->loggedIn()
7074
? $this->getAuthenticator()->getUser()

src/Authentication/AuthenticatorInterface.php

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

33
namespace CodeIgniter\Shield\Authentication;
44

5-
use CodeIgniter\Shield\Interfaces\Authenticatable;
5+
use CodeIgniter\Shield\Entities\User;
66
use CodeIgniter\Shield\Result;
77

88
interface AuthenticatorInterface
@@ -32,7 +32,7 @@ public function loggedIn(): bool;
3232
*
3333
* @see https://codeigniter4.github.io/CodeIgniter4/extending/authentication.html
3434
*/
35-
public function login(Authenticatable $user): bool;
35+
public function login(User $user): bool;
3636

3737
/**
3838
* Logs a user in based on their ID.
@@ -55,7 +55,7 @@ public function logout(): bool;
5555
/**
5656
* Returns the currently logged in user.
5757
*/
58-
public function getUser(): ?Authenticatable;
58+
public function getUser(): ?User;
5959

6060
/**
6161
* Updates the user's last active date.

src/Authentication/Authenticators/AccessTokens.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class AccessTokens implements AuthenticatorInterface
2828
*
2929
* @todo Fix interface issue described above
3030
*/
31-
protected ?Authenticatable $user = null;
31+
protected ?User $user = null;
3232

3333
protected LoginModel $loginModel;
3434

@@ -150,7 +150,7 @@ public function loggedIn(): bool
150150
/**
151151
* Logs the given user in by saving them to the class.
152152
*/
153-
public function login(Authenticatable $user): bool
153+
public function login(User $user): bool
154154
{
155155
$this->user = $user;
156156

@@ -200,7 +200,7 @@ public function forget(?int $id): void
200200
/**
201201
* Returns the currently logged in user.
202202
*/
203-
public function getUser(): ?Authenticatable
203+
public function getUser(): ?User
204204
{
205205
return $this->user;
206206
}

src/Authentication/Authenticators/Session.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Session implements AuthenticatorInterface
2525
*/
2626
protected UserProvider $provider;
2727

28-
protected ?Authenticatable $user = null;
28+
protected ?User $user = null;
2929
protected LoginModel $loginModel;
3030

3131
/**
@@ -81,7 +81,7 @@ public function attempt(array $credentials): Result
8181
return $result;
8282
}
8383

84-
/** @var Authenticatable $user */
84+
/** @var User $user */
8585
$user = $result->extraInfo();
8686

8787
$this->login($user);
@@ -170,7 +170,7 @@ public function loggedIn(): bool
170170
/**
171171
* Logs the given user in.
172172
*/
173-
public function login(Authenticatable $user): bool
173+
public function login(User $user): bool
174174
{
175175
// @todo Authenticatable should define getEmailIdentity() or this should require User
176176
$this->user = $user;
@@ -281,7 +281,7 @@ public function forget($userId = null): void
281281
/**
282282
* Returns the current user instance.
283283
*/
284-
public function getUser(): ?Authenticatable
284+
public function getUser(): ?User
285285
{
286286
return $this->user;
287287
}

src/Authentication/Passwords.php

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

55
use CodeIgniter\Shield\Authentication\Passwords\ValidatorInterface;
66
use CodeIgniter\Shield\Config\Auth;
7-
use CodeIgniter\Shield\Interfaces\Authenticatable;
7+
use CodeIgniter\Shield\Entities\User;
88
use CodeIgniter\Shield\Result;
99

1010
/**
@@ -79,7 +79,7 @@ public function needsRehash(string $hashedPassword): bool
7979
*
8080
* @throws AuthenticationException
8181
*/
82-
public function check(string $password, ?Authenticatable $user = null): Result
82+
public function check(string $password, ?User $user = null): Result
8383
{
8484
if (null === $user) {
8585
throw AuthenticationException::forNoEntityProvided();

src/Authentication/Passwords/NothingPersonalValidator.php

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

33
namespace CodeIgniter\Shield\Authentication\Passwords;
44

5-
use CodeIgniter\Shield\Interfaces\Authenticatable;
5+
use CodeIgniter\Shield\Entities\User;
66
use CodeIgniter\Shield\Result;
77

88
/**
@@ -18,7 +18,7 @@ class NothingPersonalValidator extends BaseValidator implements ValidatorInterfa
1818
* If true is returned the password will be passed to next validator.
1919
* If false is returned the validation process will be immediately stopped.
2020
*/
21-
public function check(string $password, ?Authenticatable $user = null): Result
21+
public function check(string $password, ?User $user = null): Result
2222
{
2323
$password = \strtolower($password);
2424

@@ -52,7 +52,7 @@ public function check(string $password, ?Authenticatable $user = null): Result
5252
*
5353
* @return bool
5454
*/
55-
protected function isNotPersonal(string $password, ?Authenticatable $user)
55+
protected function isNotPersonal(string $password, ?User $user)
5656
{
5757
$userName = \strtolower($user->username);
5858
$email = \strtolower($user->email);
@@ -153,7 +153,7 @@ protected function isNotPersonal(string $password, ?Authenticatable $user)
153153
*
154154
* @return bool
155155
*/
156-
protected function isNotSimilar(string $password, ?Authenticatable $user)
156+
protected function isNotSimilar(string $password, ?User $user)
157157
{
158158
$maxSimilarity = (float) $this->config->maxSimilarity;
159159
// sanity checking - working range 1-100, 0 is off

src/Authentication/Passwords/ValidatorInterface.php

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

33
namespace CodeIgniter\Shield\Authentication\Passwords;
44

5-
use CodeIgniter\Shield\Interfaces\Authenticatable;
5+
use CodeIgniter\Shield\Entities\User;
66
use CodeIgniter\Shield\Result;
77

88
/**
@@ -19,7 +19,7 @@ interface ValidatorInterface
1919
* the password will be passed to any remaining validators.
2020
* False will immediately stop validation process
2121
*/
22-
public function check(string $password, ?Authenticatable $user = null): Result;
22+
public function check(string $password, ?User $user = null): Result;
2323

2424
/**
2525
* Returns the error string that should be displayed to the user.

src/Authentication/Traits/UserProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ trait UserProvider
1515
*
1616
* @param int|string $id
1717
*/
18-
public function findById($id): ?Authenticatable
18+
public function findById($id): ?User
1919
{
2020
return $this->find($id);
2121
}
@@ -25,7 +25,7 @@ public function findById($id): ?Authenticatable
2525
*
2626
* @param array<string, string> $credentials
2727
*/
28-
public function findByCredentials(array $credentials): ?Authenticatable
28+
public function findByCredentials(array $credentials): ?User
2929
{
3030
// Email is stored in an identity so remove that here
3131
$email = $credentials['email'] ?? null;

src/Controllers/RegisterController.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
use CodeIgniter\Events\Events;
77
use CodeIgniter\HTTP\RedirectResponse;
88
use CodeIgniter\Shield\Entities\User;
9-
use CodeIgniter\Shield\Interfaces\Authenticatable;
10-
use CodeIgniter\Shield\Interfaces\UserProvider;
9+
use CodeIgniter\Shield\Models\UserModel;
1110
use CodeIgniter\Validation\Validation;
1211

1312
/**
@@ -104,19 +103,19 @@ public function registerAction(): RedirectResponse
104103
/**
105104
* Returns the User provider
106105
*/
107-
protected function getUserProvider(): UserProvider
106+
protected function getUserProvider(): UserModel
108107
{
109108
$provider = model(setting('Auth.userProvider'));
110109

111-
assert($provider instanceof UserProvider, 'Config Auth.userProvider is not a valid UserProvider.');
110+
assert($provider instanceof UserModel, 'Config Auth.userProvider is not a valid UserProvider.');
112111

113112
return $provider;
114113
}
115114

116115
/**
117116
* Returns the Entity class that should be used
118117
*/
119-
protected function getUserEntity(): Authenticatable
118+
protected function getUserEntity(): User
120119
{
121120
return new User();
122121
}

src/Entities/AccessToken.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace CodeIgniter\Shield\Entities;
44

55
use CodeIgniter\Entity\Entity;
6-
use CodeIgniter\Shield\Interfaces\Authenticatable;
76

87
/**
98
* Class AccessToken
@@ -32,7 +31,7 @@ class AccessToken extends Entity
3231
/**
3332
* Returns the user associated with this token.
3433
*/
35-
public function user(): ?Authenticatable
34+
public function user(): ?User
3635
{
3736
if (empty($this->attributes['user'])) {
3837
helper('auth');

0 commit comments

Comments
 (0)