2014-05-23 04:37:34 -04:00
|
|
|
<?php
|
2024-05-24 13:43:47 -04:00
|
|
|
|
2014-05-23 04:37:34 -04:00
|
|
|
/**
|
2024-05-24 13:43:47 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2014-05-23 04:37:34 -04:00
|
|
|
*/
|
|
|
|
|
namespace OC\Core\Command\User;
|
|
|
|
|
|
2022-04-08 05:40:05 -04:00
|
|
|
use OC\Core\Command\Base;
|
|
|
|
|
use OCP\IUser;
|
2015-04-23 06:40:13 -04:00
|
|
|
use OCP\IUserManager;
|
2022-04-08 05:40:05 -04:00
|
|
|
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
|
2019-11-22 14:52:10 -05:00
|
|
|
use Symfony\Component\Console\Input\InputArgument;
|
2014-05-23 04:37:34 -04:00
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
2023-08-03 01:30:45 -04:00
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
2014-05-23 04:37:34 -04:00
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
|
|
2022-04-08 05:40:05 -04:00
|
|
|
class LastSeen extends Base {
|
2023-06-13 04:37:56 -04:00
|
|
|
public function __construct(
|
|
|
|
|
protected IUserManager $userManager,
|
|
|
|
|
) {
|
2015-04-23 06:40:13 -04:00
|
|
|
parent::__construct();
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-03 01:30:45 -04:00
|
|
|
protected function configure(): void {
|
2014-05-23 04:37:34 -04:00
|
|
|
$this
|
|
|
|
|
->setName('user:lastseen')
|
2016-02-15 11:03:16 -05:00
|
|
|
->setDescription('shows when the user was logged in last time')
|
2014-05-23 04:37:34 -04:00
|
|
|
->addArgument(
|
|
|
|
|
'uid',
|
2023-08-03 01:30:45 -04:00
|
|
|
InputArgument::OPTIONAL,
|
2014-05-23 04:37:34 -04:00
|
|
|
'the username'
|
2023-08-03 01:30:45 -04:00
|
|
|
)
|
|
|
|
|
->addOption(
|
|
|
|
|
'all',
|
|
|
|
|
null,
|
|
|
|
|
InputOption::VALUE_NONE,
|
|
|
|
|
'shows a list of when all users were last logged in'
|
|
|
|
|
)
|
|
|
|
|
;
|
2014-05-23 04:37:34 -04:00
|
|
|
}
|
|
|
|
|
|
2024-03-13 05:24:22 -04:00
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int {
|
2023-08-03 01:30:45 -04:00
|
|
|
$singleUserId = $input->getArgument('uid');
|
2024-04-04 21:59:37 -04:00
|
|
|
|
2023-08-03 01:30:45 -04:00
|
|
|
if ($singleUserId) {
|
|
|
|
|
$user = $this->userManager->get($singleUserId);
|
|
|
|
|
if (is_null($user)) {
|
|
|
|
|
$output->writeln('<error>User does not exist</error>');
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2024-03-13 05:24:22 -04:00
|
|
|
|
|
|
|
|
$lastLogin = $user->getLastLogin();
|
|
|
|
|
if ($lastLogin === 0) {
|
|
|
|
|
$output->writeln($user->getUID() . ' has never logged in.');
|
|
|
|
|
} else {
|
|
|
|
|
$date = new \DateTime();
|
|
|
|
|
$date->setTimestamp($lastLogin);
|
2024-04-04 18:48:40 -04:00
|
|
|
$output->writeln($user->getUID() . "'s last login: " . $date->format('Y-m-d H:i:s T'));
|
2024-03-13 05:24:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$input->getOption('all')) {
|
2023-08-03 01:30:45 -04:00
|
|
|
$output->writeln('<error>Please specify a username, or "--all" to list all</error>');
|
|
|
|
|
return 1;
|
2014-05-23 04:37:34 -04:00
|
|
|
}
|
2024-03-13 05:24:22 -04:00
|
|
|
|
2025-05-14 09:51:42 -04:00
|
|
|
$this->userManager->callForAllUsers(static function (IUser $user) use ($output): void {
|
2024-03-13 05:24:22 -04:00
|
|
|
$lastLogin = $user->getLastLogin();
|
|
|
|
|
if ($lastLogin === 0) {
|
|
|
|
|
$output->writeln($user->getUID() . ' has never logged in.');
|
|
|
|
|
} else {
|
|
|
|
|
$date = new \DateTime();
|
|
|
|
|
$date->setTimestamp($lastLogin);
|
2024-04-04 18:48:40 -04:00
|
|
|
$output->writeln($user->getUID() . "'s last login: " . $date->format('Y-m-d H:i:s T'));
|
2024-03-13 05:24:22 -04:00
|
|
|
}
|
|
|
|
|
});
|
2020-06-26 08:54:51 -04:00
|
|
|
return 0;
|
2014-05-23 04:37:34 -04:00
|
|
|
}
|
2022-04-08 05:40:05 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $argumentName
|
|
|
|
|
* @param CompletionContext $context
|
|
|
|
|
* @return string[]
|
|
|
|
|
*/
|
|
|
|
|
public function completeArgumentValues($argumentName, CompletionContext $context) {
|
|
|
|
|
if ($argumentName === 'uid') {
|
|
|
|
|
return array_map(static fn (IUser $user) => $user->getUID(), $this->userManager->search($context->getCurrentWord()));
|
|
|
|
|
}
|
|
|
|
|
return [];
|
|
|
|
|
}
|
2014-05-23 04:37:34 -04:00
|
|
|
}
|