From badd79de760d5f6391f20d5e8485bdb75c6803c2 Mon Sep 17 00:00:00 2001 From: Akhil Date: Wed, 22 Mar 2023 00:33:17 +0530 Subject: [PATCH 1/6] Add first login methods to IUser and User Signed-off-by: Akhil --- lib/private/User/LazyUser.php | 8 ++++++++ lib/private/User/Session.php | 1 + lib/private/User/User.php | 25 +++++++++++++++++++++++++ lib/public/IUser.php | 15 +++++++++++++++ tests/lib/User/UserTest.php | 20 ++++++++++++++++++++ 5 files changed, 69 insertions(+) diff --git a/lib/private/User/LazyUser.php b/lib/private/User/LazyUser.php index 096578b8f375d..9ba77a9b868d6 100644 --- a/lib/private/User/LazyUser.php +++ b/lib/private/User/LazyUser.php @@ -145,4 +145,12 @@ public function getQuota() { public function setQuota($quota) { $this->getUser()->setQuota($quota); } + + public function getFirstLogin() { + return $this->getUser()->getFirstLogin(); + } + + public function updateFirstLoginTimestamp() { + return $this->getUser()->updateFirstLoginTimestamp(); + } } diff --git a/lib/private/User/Session.php b/lib/private/User/Session.php index 3e45ebeab2b83..96a565ff39105 100644 --- a/lib/private/User/Session.php +++ b/lib/private/User/Session.php @@ -560,6 +560,7 @@ protected function prepareUserLogin($firstTimeLogin, $refreshCsrfToken = true) { // read only uses } + $user->updateFirstLoginTimestamp($user->getLastLogin()); // trigger any other initialization \OC::$server->getEventDispatcher()->dispatch(IUser::class . '::firstLogin', new GenericEvent($this->getUser())); } diff --git a/lib/private/User/User.php b/lib/private/User/User.php index 2d80dbc7adf27..cdc474ad307a7 100644 --- a/lib/private/User/User.php +++ b/lib/private/User/User.php @@ -89,6 +89,9 @@ class User implements IUser { /** @var int|null */ private $lastLogin; + /** @var int|null */ + private $firstLogin; + /** @var \OCP\IConfig */ private $config; @@ -243,6 +246,20 @@ public function getLastLogin() { return (int) $this->lastLogin; } + /** + * returns the timestamp of the user's first login or 0 if the user did never + * login + * + * @return int + */ + + public function getFirstLogin() { + if($this->firstLogin === null) { + $this->firstLogin = (int) $this->config->getUserValue($this->uid, 'login', 'firstLogin', 0); + } + return (int) $this->firstLogin; + } + /** * updates the timestamp of the most recent login of this user */ @@ -260,6 +277,14 @@ public function updateLastLoginTimestamp() { return $firstTimeLogin; } + /** + * updates the timestamp of the most recent login of this user + */ + public function updateFirstLoginTimestamp(int $timestamp) { + $this->firstLogin = $timestamp; + $this->config->setUserValue($this->uid, 'login', 'firstLogin', (string)$this->firstLogin); + } + /** * Delete the user * diff --git a/lib/public/IUser.php b/lib/public/IUser.php index 3a7e6ab1f11a4..6817e657ad36a 100644 --- a/lib/public/IUser.php +++ b/lib/public/IUser.php @@ -270,4 +270,19 @@ public function getQuota(); * @since 9.0.0 */ public function setQuota($quota); + + /** + * returns the timestamp of the user's first login or 0 if the user did never + * login + * + * @return int + * @since 27.0.0 + */ + public function getFirstLogin(); + + /** + * updates the timestamp of the first login of this user + * @since 27.0.0 + */ + public function updateFirstLoginTimestamp(int $timestamp); } diff --git a/tests/lib/User/UserTest.php b/tests/lib/User/UserTest.php index b8fa8efced0c0..fb2bb7085eccf 100644 --- a/tests/lib/User/UserTest.php +++ b/tests/lib/User/UserTest.php @@ -849,6 +849,26 @@ public function testGetLastLogin() { $this->assertSame(42, $user->getLastLogin()); } + public function testGetFirstLogin() { + /** + * @var Backend | MockObject $backend + */ + $backend = $this->createMock(\Test\Util\User\Dummy::class); + + $config = $this->createMock(IConfig::class); + $config->method('getUserValue') + ->willReturnCallback(function ($uid, $app, $key, $default) { + if ($uid === 'foo' && $app === 'login' && $key === 'firstLogin') { + return 42; + } else { + return $default; + } + }); + + $user = new User('foo', $backend, $this->dispatcher, null, $config); + $this->assertSame(42, $user->getFirstLogin()); + } + public function testSetEnabled() { /** * @var Backend | MockObject $backend From 64b870f2b67d28be42f358f5e78b8d600d4245ad Mon Sep 17 00:00:00 2001 From: Akhil Date: Wed, 22 Mar 2023 00:38:43 +0530 Subject: [PATCH 2/6] Add created to occ user:info Signed-off-by: Akhil --- core/Command/User/Info.php | 3 ++- lib/private/User/LazyUser.php | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/core/Command/User/Info.php b/core/Command/User/Info.php index 1e89a8d0911f5..9359f20e4f5b8 100644 --- a/core/Command/User/Info.php +++ b/core/Command/User/Info.php @@ -79,7 +79,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int 'storage' => $this->getStorageInfo($user), 'last_seen' => date(\DateTimeInterface::ATOM, $user->getLastLogin()), // ISO-8601 'user_directory' => $user->getHome(), - 'backend' => $user->getBackendClassName() + 'backend' => $user->getBackendClassName(), + 'created' => date(\DateTimeInterface::Atom, $user->getFirstLogin()) ]; $this->writeArrayInOutputFormat($input, $output, $data); return 0; diff --git a/lib/private/User/LazyUser.php b/lib/private/User/LazyUser.php index 9ba77a9b868d6..0a649e8671185 100644 --- a/lib/private/User/LazyUser.php +++ b/lib/private/User/LazyUser.php @@ -150,7 +150,7 @@ public function getFirstLogin() { return $this->getUser()->getFirstLogin(); } - public function updateFirstLoginTimestamp() { - return $this->getUser()->updateFirstLoginTimestamp(); + public function updateFirstLoginTimestamp(int $timestamp) { + return $this->getUser()->updateFirstLoginTimestamp($timestamp); } } From 5d64fe0ea65e1ad1324c85c53f2f17c956b5fc1b Mon Sep 17 00:00:00 2001 From: Akhil Date: Wed, 22 Mar 2023 00:52:23 +0530 Subject: [PATCH 3/6] Fix comment Signed-off-by: Akhil --- lib/private/User/User.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/private/User/User.php b/lib/private/User/User.php index cdc474ad307a7..7740578e0e535 100644 --- a/lib/private/User/User.php +++ b/lib/private/User/User.php @@ -278,7 +278,7 @@ public function updateLastLoginTimestamp() { } /** - * updates the timestamp of the most recent login of this user + * updates the timestamp of the first login of this user */ public function updateFirstLoginTimestamp(int $timestamp) { $this->firstLogin = $timestamp; From bf411a4f430601392ba3d5b28f831e5f551b4565 Mon Sep 17 00:00:00 2001 From: Akhil Date: Wed, 22 Mar 2023 17:25:14 +0530 Subject: [PATCH 4/6] Fix wrong type Signed-off-by: Akhil --- lib/private/User/Session.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/private/User/Session.php b/lib/private/User/Session.php index 96a565ff39105..dab302ca2456e 100644 --- a/lib/private/User/Session.php +++ b/lib/private/User/Session.php @@ -560,7 +560,8 @@ protected function prepareUserLogin($firstTimeLogin, $refreshCsrfToken = true) { // read only uses } - $user->updateFirstLoginTimestamp($user->getLastLogin()); + $userObj = $this->getUser(); + $userObj->updateFirstLoginTimestamp($userObj->getLastLogin()); // trigger any other initialization \OC::$server->getEventDispatcher()->dispatch(IUser::class . '::firstLogin', new GenericEvent($this->getUser())); } From 450cba7467213c9d78f7ff98a816c4eefff51e1a Mon Sep 17 00:00:00 2001 From: Akhil Date: Fri, 24 Mar 2023 16:36:54 +0530 Subject: [PATCH 5/6] Fix wrong ATOM type Signed-off-by: Akhil --- core/Command/User/Info.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/Command/User/Info.php b/core/Command/User/Info.php index 9359f20e4f5b8..17081726a0ac4 100644 --- a/core/Command/User/Info.php +++ b/core/Command/User/Info.php @@ -80,7 +80,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int 'last_seen' => date(\DateTimeInterface::ATOM, $user->getLastLogin()), // ISO-8601 'user_directory' => $user->getHome(), 'backend' => $user->getBackendClassName(), - 'created' => date(\DateTimeInterface::Atom, $user->getFirstLogin()) + 'created' => date(\DateTimeInterface::ATOM, $user->getFirstLogin()) ]; $this->writeArrayInOutputFormat($input, $output, $data); return 0; From 881e272812a6dcabbf91bece47202fa2a31164e3 Mon Sep 17 00:00:00 2001 From: Akhil Date: Mon, 27 Mar 2023 14:48:05 +0530 Subject: [PATCH 6/6] Fix CS Signed-off-by: Akhil --- lib/private/User/User.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/private/User/User.php b/lib/private/User/User.php index 7740578e0e535..d1fb25af3e095 100644 --- a/lib/private/User/User.php +++ b/lib/private/User/User.php @@ -254,7 +254,7 @@ public function getLastLogin() { */ public function getFirstLogin() { - if($this->firstLogin === null) { + if ($this->firstLogin === null) { $this->firstLogin = (int) $this->config->getUserValue($this->uid, 'login', 'firstLogin', 0); } return (int) $this->firstLogin;