File tree Expand file tree Collapse file tree 3 files changed +22
-14
lines changed
Expand file tree Collapse file tree 3 files changed +22
-14
lines changed Original file line number Diff line number Diff line change @@ -224,21 +224,13 @@ public function getPasswordHash(): ?string
224224 }
225225
226226 /**
227- * Returns the last login information for this
228- * user as
227+ * Returns the last login information for this user as
229228 */
230- public function lastLogin (bool $ allowFailed = false ): ?Login
229+ public function lastLogin (): ?Login
231230 {
231+ /** @var LoginModel $logins */
232232 $ logins = model (LoginModel::class);
233233
234- if (! $ allowFailed ) {
235- $ logins ->where ('success ' , 1 )
236- ->where ('user_id ' , $ this ->attributes ['id ' ] ?? null );
237- }
238-
239- return $ logins
240- ->where ('identifier ' , $ this ->getEmail ())
241- ->orderBy ('date ' , 'desc ' )
242- ->first ();
234+ return $ logins ->lastLogin ($ this );
243235 }
244236}
Original file line number Diff line number Diff line change 55use CodeIgniter \I18n \Time ;
66use CodeIgniter \Model ;
77use CodeIgniter \Shield \Entities \Login ;
8+ use CodeIgniter \Shield \Entities \User ;
89use Exception ;
910use Faker \Generator ;
1011
@@ -57,6 +58,17 @@ public function recordLoginAttempt(
5758 $ this ->checkQueryReturn ($ return );
5859 }
5960
61+ /**
62+ * Returns the last login information for the user
63+ */
64+ public function lastLogin (User $ user ): ?Login
65+ {
66+ return $ this ->where ('success ' , 1 )
67+ ->where ('user_id ' , $ user ->id )
68+ ->orderBy ('id ' , 'desc ' )
69+ ->first ();
70+ }
71+
6072 /**
6173 * Generate a fake login for testing
6274 *
Original file line number Diff line number Diff line change @@ -76,7 +76,11 @@ public function testLastLogin()
7676 // No logins found.
7777 $ this ->assertNull ($ this ->user ->lastLogin ());
7878
79- $ login = fake (
79+ fake (
80+ LoginModel::class,
81+ ['identifier ' => $ this ->user ->email , 'user_id ' => $ this ->user ->id ]
82+ );
83+ $ login2 = fake (
8084 LoginModel::class,
8185 ['identifier ' => $ this ->user ->email , 'user_id ' => $ this ->user ->id ]
8286 );
@@ -88,7 +92,7 @@ public function testLastLogin()
8892 $ last = $ this ->user ->lastLogin ();
8993
9094 $ this ->assertInstanceOf (Login::class, $ last ); // @phpstan-ignore-line
91- $ this ->assertSame ($ login ->id , $ last ->id );
95+ $ this ->assertSame ($ login2 ->id , $ last ->id );
9296 $ this ->assertInstanceOf (Time::class, $ last ->date );
9397 }
9498
You can’t perform that action at this time.
0 commit comments