From e382dc7f7d41d8cedc7813a52900ac2f9663fc8c Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Wed, 28 May 2014 21:22:31 +0200 Subject: [PATCH] Fixed rare bug regarding last login time --- src/Auth.php | 3 ++- src/Controllers/UserController.php | 2 +- tests/Tests/MiscTests/AuthTest.php | 16 ++++++++++++---- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/Auth.php b/src/Auth.php index 6ac61c8b..667b9139 100644 --- a/src/Auth.php +++ b/src/Auth.php @@ -36,9 +36,10 @@ class Auth setcookie('auth', TextHelper::encrypt($token), time() + 365 * 24 * 3600, '/'); } + $user->setLastLoginTime(time()); + self::setCurrentUser($user); - $user->setLastLoginTime(time()); UserModel::save($user); } diff --git a/src/Controllers/UserController.php b/src/Controllers/UserController.php index ed6f95b8..4e5e953d 100644 --- a/src/Controllers/UserController.php +++ b/src/Controllers/UserController.php @@ -147,7 +147,7 @@ class UserController extends AbstractController $user = Api::run( new EditUserSettingsJob(), [ - JobArgs::ARG_USER_ENTITY => Auth::getCurrentUser(), + JobArgs::ARG_USER_ENTITY => $user, JobArgs::ARG_NEW_SETTINGS => [UserSettings::SETTING_SAFETY => $desiredSafety], ]); diff --git a/tests/Tests/MiscTests/AuthTest.php b/tests/Tests/MiscTests/AuthTest.php index 14774971..6ff0f05f 100644 --- a/tests/Tests/MiscTests/AuthTest.php +++ b/tests/Tests/MiscTests/AuthTest.php @@ -16,8 +16,8 @@ class AuthTest extends AbstractTest $this->assert->isTrue(Auth::isLoggedIn()); - $user = UserModel::getByName($user->getName()); - $this->assert->areEqual(time(), $user->getLastLoginTime()); + $this->assertValidUser(UserModel::getByName($user->getName())); + $this->assertValidUser(Auth::getCurrentUser()); } public function testAutoLogin() @@ -28,6 +28,8 @@ class AuthTest extends AbstractTest $user = $this->prepareValidUser(); UserModel::save($user); + $this->assert->isNull($user->getLastLoginTime()); + $token = base64_encode('existing') . '|' . base64_encode('bleee'); $_COOKIE['auth'] = TextHelper::encrypt($token); @@ -38,8 +40,8 @@ class AuthTest extends AbstractTest $this->assert->isTrue(Auth::isLoggedIn()); - $user = UserModel::getByName($user->getName()); - $this->assert->areEqual(time(), $user->getLastLoginTime()); + $this->assertValidUser(UserModel::getByName($user->getName())); + $this->assertValidUser(Auth::getCurrentUser()); } public function testAutoLoginInvalidToken() @@ -247,4 +249,10 @@ class AuthTest extends AbstractTest $user->setPassword('bleee'); return $user; } + + private function assertValidUser($user) + { + $this->assert->areEqual('existing', $user->getName()); + $this->assert->areEqual(time(), $user->getLastLoginTime()); + } }