Skip to content

Commit

Permalink
feat(users): Use -1 for unknown firstLogin instead of setting it to c…
Browse files Browse the repository at this point in the history
…urrent date

Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
  • Loading branch information
come-nc committed Nov 21, 2024
1 parent 7e44535 commit fd366a6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
20 changes: 18 additions & 2 deletions core/Command/User/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return 1;
}
$groups = $this->groupManager->getUserGroupIds($user);
$firstLogin = $user->getFirstLogin();
$lastLogin = $user->getLastLogin();
if ($firstLogin < 0) {
$firstSeen = 'unknown';
} elseif ($firstLogin === 0) {
$firstSeen = 'never';
} else {
$firstSeen = date(\DateTimeInterface::ATOM, $firstLogin); // ISO-8601
}
if ($lastLogin < 0) {
$lastSeen = 'unknown';
} elseif ($lastLogin === 0) {
$lastSeen = 'never';
} else {
$lastSeen = date(\DateTimeInterface::ATOM, $lastLogin); // ISO-8601
}
$data = [
'user_id' => $user->getUID(),
'display_name' => $user->getDisplayName(),
Expand All @@ -56,8 +72,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
'groups' => $groups,
'quota' => $user->getQuota(),
'storage' => $this->getStorageInfo($user),
'first_seen' => date(\DateTimeInterface::ATOM, $user->getFirstLogin()), // ISO-8601
'last_seen' => date(\DateTimeInterface::ATOM, $user->getLastLogin()), // ISO-8601
'first_seen' => $firstSeen,
'last_seen' => $lastSeen,
'user_directory' => $user->getHome(),
'backend' => $user->getBackendClassName()
];
Expand Down
11 changes: 8 additions & 3 deletions lib/private/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,15 +228,20 @@ public function updateLastLoginTimestamp(): bool {
$previousLogin = $this->getLastLogin();
$firstLogin = $this->getFirstLogin();
$now = time();
$firstTimeLogin = $firstLogin === 0;
$firstTimeLogin = $previousLogin === 0;

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

if ($firstTimeLogin) {
$this->firstLogin = $now;
if ($firstLogin === 0) {
if ($firstTimeLogin) {
$this->firstLogin = $now;
} else {
/* Unknown first login, most likely was before upgrade to Nextcloud 31 */
$this->firstLogin = -1;
}
$this->config->setUserValue($this->uid, 'login', 'firstLogin', (string)$this->firstLogin);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/public/IUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function setDisplayName($displayName);
public function getLastLogin(): int;

/**
* Returns the timestamp of the user's first login or 0 if the user did never login
* 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)
*
* @since 31.0.0
*/
Expand Down

0 comments on commit fd366a6

Please sign in to comment.