Skip to content

Commit fd366a6

Browse files
committed
feat(users): Use -1 for unknown firstLogin instead of setting it to current date
Signed-off-by: Côme Chilliet <[email protected]>
1 parent 7e44535 commit fd366a6

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

core/Command/User/Info.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,22 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4747
return 1;
4848
}
4949
$groups = $this->groupManager->getUserGroupIds($user);
50+
$firstLogin = $user->getFirstLogin();
51+
$lastLogin = $user->getLastLogin();
52+
if ($firstLogin < 0) {
53+
$firstSeen = 'unknown';
54+
} elseif ($firstLogin === 0) {
55+
$firstSeen = 'never';
56+
} else {
57+
$firstSeen = date(\DateTimeInterface::ATOM, $firstLogin); // ISO-8601
58+
}
59+
if ($lastLogin < 0) {
60+
$lastSeen = 'unknown';
61+
} elseif ($lastLogin === 0) {
62+
$lastSeen = 'never';
63+
} else {
64+
$lastSeen = date(\DateTimeInterface::ATOM, $lastLogin); // ISO-8601
65+
}
5066
$data = [
5167
'user_id' => $user->getUID(),
5268
'display_name' => $user->getDisplayName(),
@@ -56,8 +72,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
5672
'groups' => $groups,
5773
'quota' => $user->getQuota(),
5874
'storage' => $this->getStorageInfo($user),
59-
'first_seen' => date(\DateTimeInterface::ATOM, $user->getFirstLogin()), // ISO-8601
60-
'last_seen' => date(\DateTimeInterface::ATOM, $user->getLastLogin()), // ISO-8601
75+
'first_seen' => $firstSeen,
76+
'last_seen' => $lastSeen,
6177
'user_directory' => $user->getHome(),
6278
'backend' => $user->getBackendClassName()
6379
];

lib/private/User/User.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,15 +228,20 @@ public function updateLastLoginTimestamp(): bool {
228228
$previousLogin = $this->getLastLogin();
229229
$firstLogin = $this->getFirstLogin();
230230
$now = time();
231-
$firstTimeLogin = $firstLogin === 0;
231+
$firstTimeLogin = $previousLogin === 0;
232232

233233
if ($now - $previousLogin > 60) {
234234
$this->lastLogin = $now;
235235
$this->config->setUserValue($this->uid, 'login', 'lastLogin', (string)$this->lastLogin);
236236
}
237237

238-
if ($firstTimeLogin) {
239-
$this->firstLogin = $now;
238+
if ($firstLogin === 0) {
239+
if ($firstTimeLogin) {
240+
$this->firstLogin = $now;
241+
} else {
242+
/* Unknown first login, most likely was before upgrade to Nextcloud 31 */
243+
$this->firstLogin = -1;
244+
}
240245
$this->config->setUserValue($this->uid, 'login', 'firstLogin', (string)$this->firstLogin);
241246
}
242247

lib/public/IUser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function setDisplayName($displayName);
5353
public function getLastLogin(): int;
5454

5555
/**
56-
* Returns the timestamp of the user's first login or 0 if the user did never login
56+
* Returns the timestamp of the user's first login, 0 if the user did never login, or -1 if the data is unknown (first login was on an older version)
5757
*
5858
* @since 31.0.0
5959
*/

0 commit comments

Comments
 (0)