Skip to content

Commit

Permalink
Merge pull request #1068 from arodu/feature/lastlogin-config
Browse files Browse the repository at this point in the history
fix: config for updateLastLogin
  • Loading branch information
rochamarcelo authored Feb 7, 2024
2 parents 71ce812 + a7d7dbe commit 88f053f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 4 additions & 0 deletions config/users.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
// determines if registration workflow includes email validation
'validate' => true,
],
'Login' => [
'updateLastLogin' => true,
'lastLoginField' => 'last_login',
],
'Registration' => [
// determines if the register is enabled
'active' => true,
Expand Down
8 changes: 6 additions & 2 deletions src/Controller/Component/LoginComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,14 @@ protected function checkSafeHost(?string $queryRedirect = null): bool
*/
protected function updateLastLogin($user)
{
if (!Configure::read('Users.Login.updateLastLogin', true)) {
return;
}
$field = Configure::read('Users.Login.lastLoginField', 'last_login');
$now = \Cake\I18n\FrozenTime::now();
$user->set('last_login', $now);
$user->set($field, $now);
$this->getController()->getUsersTable()->updateAll(
['last_login' => $now->format('Y-m-d H:i:s')],
[$field => $now->format('Y-m-d H:i:s')],
['id' => $user->id]
);
}
Expand Down

0 comments on commit 88f053f

Please sign in to comment.