Skip to content

Commit

Permalink
Add --all option for occ user:lastseen.
Browse files Browse the repository at this point in the history
Signed-off-by: Jordan Brown <code@jore.cc>
  • Loading branch information
nooblag committed Aug 3, 2023
1 parent f9370e4 commit a7b8933
Showing 1 changed file with 67 additions and 45 deletions.
112 changes: 67 additions & 45 deletions core/Command/User/LastSeen.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* @author Joas Schilling <coding@schilljs.com>
* @author Pierre Ozoux <pierre@ozoux.net>
* @author Roeland Jago Douma <roeland@famdouma.nl>
* @author Jordan Brown <code@jore.cc>
*
* @license AGPL-3.0
*
Expand All @@ -23,6 +24,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace OC\Core\Command\User;

use OC\Core\Command\Base;
Expand All @@ -31,55 +33,75 @@
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class LastSeen extends Base {
public function __construct(
protected IUserManager $userManager,
) {
parent::__construct();
}
class LastSeen extends Base
{
public function __construct(
protected IUserManager $userManager,
) {
parent::__construct();
}

protected function configure() {
$this
->setName('user:lastseen')
->setDescription('shows when the user was logged in last time')
->addArgument(
'uid',
InputArgument::REQUIRED,
'the username'
);
}
protected function configure()
{
$this
->setName('user:lastseen')
->setDescription('shows the time when a user was last logged in')
->addArgument(
'uid',
InputArgument::OPTIONAL,
'the username'
)
->addOption(
'all',
null,
InputOption::VALUE_NONE,
'shows a list of when all users were last logged in'
)
;
}

protected function execute(InputInterface $input, OutputInterface $output): int {
$user = $this->userManager->get($input->getArgument('uid'));
if (is_null($user)) {
$output->writeln('<error>User does not exist</error>');
return 1;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$singleUserId = $input->getArgument('uid');
if ($singleUserId) {
if (is_null($this->userManager->get($singleUserId))) {
$output->writeln('<error>User does not exist</error>');
return 1;
}
$users = $this->userManager->search($singleUserId);
} elseif ($input->getOption('all')) {
$users = $this->userManager->search('');
} else {
$output->writeln("<error>Please specify a username, or \"--all\" to list all</error>");
return 1;
}

$lastLogin = $user->getLastLogin();
if ($lastLogin === 0) {
$output->writeln('User ' . $user->getUID() .
' has never logged in, yet.');
} else {
$date = new \DateTime();
$date->setTimestamp($lastLogin);
$output->writeln($user->getUID() .
'`s last login: ' . $date->format('d.m.Y H:i'));
}
return 0;
}
foreach($users as $user) {
$lastLogin = $user->getLastLogin();
if ($lastLogin === 0) {
$output->writeln($user->getUID() . ' has never logged in.');
} else {
$date = new \DateTime();
$date->setTimestamp($lastLogin);
$output->writeln($user->getUID() . "'s last login: " . $date->format('Y-m-d H:i'));
}
}
return 0;
}

/**
* @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 [];
}
/**
* @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 [];
}
}

0 comments on commit a7b8933

Please sign in to comment.