Skip to content

Commit e26ecb4

Browse files
committed
Add --all option for occ user:lastseen
Signed-off-by: Jordan Brown <code@jore.cc>
1 parent 38480fd commit e26ecb4

File tree

1 file changed

+67
-45
lines changed

1 file changed

+67
-45
lines changed

core/Command/User/LastSeen.php

Lines changed: 67 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* @author Joas Schilling <coding@schilljs.com>
88
* @author Pierre Ozoux <pierre@ozoux.net>
99
* @author Roeland Jago Douma <roeland@famdouma.nl>
10+
* @author Jordan Brown <code@jore.cc>
1011
*
1112
* @license AGPL-3.0
1213
*
@@ -23,6 +24,7 @@
2324
* along with this program. If not, see <http://www.gnu.org/licenses/>
2425
*
2526
*/
27+
2628
namespace OC\Core\Command\User;
2729

2830
use OC\Core\Command\Base;
@@ -31,55 +33,75 @@
3133
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
3234
use Symfony\Component\Console\Input\InputArgument;
3335
use Symfony\Component\Console\Input\InputInterface;
36+
use Symfony\Component\Console\Input\InputOption;
3437
use Symfony\Component\Console\Output\OutputInterface;
3538

36-
class LastSeen extends Base {
37-
public function __construct(
38-
protected IUserManager $userManager,
39-
) {
40-
parent::__construct();
41-
}
39+
class LastSeen extends Base
40+
{
41+
public function __construct(
42+
protected IUserManager $userManager,
43+
) {
44+
parent::__construct();
45+
}
4246

43-
protected function configure() {
44-
$this
45-
->setName('user:lastseen')
46-
->setDescription('shows when the user was logged in last time')
47-
->addArgument(
48-
'uid',
49-
InputArgument::REQUIRED,
50-
'the username'
51-
);
52-
}
47+
protected function configure()
48+
{
49+
$this
50+
->setName('user:lastseen')
51+
->setDescription('shows when the user was logged in last time')
52+
->addArgument(
53+
'uid',
54+
InputArgument::OPTIONAL,
55+
'the username'
56+
)
57+
->addOption(
58+
'all',
59+
null,
60+
InputOption::VALUE_NONE,
61+
'shows a list of when all users were last logged in'
62+
)
63+
;
64+
}
5365

54-
protected function execute(InputInterface $input, OutputInterface $output): int {
55-
$user = $this->userManager->get($input->getArgument('uid'));
56-
if (is_null($user)) {
57-
$output->writeln('<error>User does not exist</error>');
58-
return 1;
59-
}
66+
protected function execute(InputInterface $input, OutputInterface $output): int
67+
{
68+
$singleUserId = $input->getArgument('uid');
69+
if ($singleUserId) {
70+
if (is_null($this->userManager->get($singleUserId))) {
71+
$output->writeln('<error>User does not exist</error>');
72+
return 1;
73+
}
74+
$users = $this->userManager->search($singleUserId);
75+
} elseif ($input->getOption('all')) {
76+
$users = $this->userManager->search('');
77+
} else {
78+
$output->writeln("<error>Please specify a username, or \"--all\" to list all</error>");
79+
return 1;
80+
}
6081

61-
$lastLogin = $user->getLastLogin();
62-
if ($lastLogin === 0) {
63-
$output->writeln('User ' . $user->getUID() .
64-
' has never logged in, yet.');
65-
} else {
66-
$date = new \DateTime();
67-
$date->setTimestamp($lastLogin);
68-
$output->writeln($user->getUID() .
69-
'`s last login: ' . $date->format('d.m.Y H:i'));
70-
}
71-
return 0;
72-
}
82+
foreach($users as $user) {
83+
$lastLogin = $user->getLastLogin();
84+
if ($lastLogin === 0) {
85+
$output->writeln($user->getUID() . ' has never logged in.');
86+
} else {
87+
$date = new \DateTime();
88+
$date->setTimestamp($lastLogin);
89+
$output->writeln($user->getUID() . "'s last login: " . $date->format('Y-m-d H:i'));
90+
}
91+
}
92+
return 0;
93+
}
7394

74-
/**
75-
* @param string $argumentName
76-
* @param CompletionContext $context
77-
* @return string[]
78-
*/
79-
public function completeArgumentValues($argumentName, CompletionContext $context) {
80-
if ($argumentName === 'uid') {
81-
return array_map(static fn (IUser $user) => $user->getUID(), $this->userManager->search($context->getCurrentWord()));
82-
}
83-
return [];
84-
}
95+
/**
96+
* @param string $argumentName
97+
* @param CompletionContext $context
98+
* @return string[]
99+
*/
100+
public function completeArgumentValues($argumentName, CompletionContext $context)
101+
{
102+
if ($argumentName === 'uid') {
103+
return array_map(static fn (IUser $user) => $user->getUID(), $this->userManager->search($context->getCurrentWord()));
104+
}
105+
return [];
106+
}
85107
}

0 commit comments

Comments
 (0)