Skip to content

Commit

Permalink
Merge pull request #965 from arodu/feature/add-last_login
Browse files Browse the repository at this point in the history
add last_login in users table
  • Loading branch information
steinkel authored Oct 1, 2021
2 parents ddfc836 + c053503 commit 8913a7e
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 1 deletion.
24 changes: 24 additions & 0 deletions config/Migrations/20210929202041_AddLastLoginToUsers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
declare(strict_types=1);

use Migrations\AbstractMigration;

class AddLastLoginToUsers extends AbstractMigration
{
/**
* Change Method.
*
* More information on this method is available here:
* https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method
* @return void
*/
public function change()
{
$table = $this->table('users');
$table->addColumn('last_login', 'datetime', [
'default' => null,
'null' => true,
]);
$table->update();
}
}
Binary file added config/Migrations/schema-dump-default.lock
Binary file not shown.
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ parameters:

-
message: "#^Call to an undefined method Cake\\\\Controller\\\\Controller\\:\\:getUsersTable\\(\\)\\.$#"
count: 1
count: 2
path: src\Controller\Component\LoginComponent.php

-
Expand Down
17 changes: 17 additions & 0 deletions src/Controller/Component/LoginComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public function handleLogin($errorOnlyPost, $redirectFailure)
if ($result->isValid()) {
$user = $request->getAttribute('identity')->getOriginalData();
$this->handlePasswordRehash($service, $user, $request);
$this->updateLastLogin($user);

return $this->afterIdentifyUser($user);
}
Expand Down Expand Up @@ -218,4 +219,20 @@ protected function checkSafeHost(?string $queryRedirect = null): bool

return in_array($host, Configure::read('Users.AllowedRedirectHosts'));
}

/**
* Update last loging date
*
* @param \CakeDC\Users\Model\Entity\User $user User entity.
* @return void
*/
protected function updateLastLogin($user)
{
$now = \Cake\I18n\FrozenTime::now();
$user->set('last_login', $now);
$this->getController()->getUsersTable()->updateAll(
['last_login' => $now],
['id' => $user->id]
);
}
}
3 changes: 3 additions & 0 deletions tests/Fixture/UsersFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class UsersFixture extends TestFixture
'created' => ['type' => 'datetime', 'length' => null, 'null' => false, 'default' => null, 'comment' => '', 'precision' => null],
'modified' => ['type' => 'datetime', 'length' => null, 'null' => false, 'default' => null, 'comment' => '', 'precision' => null],
'additional_data' => ['type' => 'text', 'length' => null, 'null' => true, 'default' => null, 'comment' => '', 'precision' => null],
'last_login' => ['type' => 'datetime', 'length' => null, 'null' => true, 'default' => null, 'comment' => '', 'precision' => null],
'_constraints' => [
'primary' => ['type' => 'primary', 'columns' => ['id'], 'length' => []],
],
Expand Down Expand Up @@ -90,6 +91,7 @@ public function init(): void
'counter' => 1,
],
]),
'last_login' => '2015-06-24 17:33:54',
],
[
'id' => '00000000-0000-0000-0000-000000000002',
Expand All @@ -111,6 +113,7 @@ public function init(): void
'role' => 'admin',
'created' => '2015-06-24 17:33:54',
'modified' => '2015-06-24 17:33:54',
'last_login' => '2015-06-24 17:33:54',
],
[
'id' => '00000000-0000-0000-0000-000000000003',
Expand Down
6 changes: 6 additions & 0 deletions tests/TestCase/Controller/Traits/LoginTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ public function testLoginHappy()
$user = $this->Trait->getUsersTable()->get('00000000-0000-0000-0000-000000000002');
$passwordBefore = $user['password'];
$this->assertNotEmpty($passwordBefore);
$lastLoginBefore = $user['last_login'];
$this->assertNotEmpty($lastLoginBefore);
$this->_mockAuthentication($user->toArray(), $failures);
$this->Trait->Flash->expects($this->never())
->method('error');
Expand Down Expand Up @@ -130,6 +132,10 @@ public function testLoginHappy()
$userAfter = $this->Trait->getUsersTable()->get('00000000-0000-0000-0000-000000000002');
$passwordAfter = $userAfter['password'];
$this->assertSame($passwordBefore, $passwordAfter);
$lastLoginAfter = $userAfter['last_login'];
$this->assertNotEmpty($lastLoginAfter);
$now = \Cake\I18n\FrozenTime::now();
$this->assertEqualsWithDelta($lastLoginAfter->timestamp, $now->timestamp, 2);
}

/**
Expand Down

0 comments on commit 8913a7e

Please sign in to comment.