|
7 | 7 | * @author Joas Schilling <coding@schilljs.com> |
8 | 8 | * @author Pierre Ozoux <pierre@ozoux.net> |
9 | 9 | * @author Roeland Jago Douma <roeland@famdouma.nl> |
| 10 | + * @author Jordan Brown <code@jore.cc> |
10 | 11 | * |
11 | 12 | * @license AGPL-3.0 |
12 | 13 | * |
|
23 | 24 | * along with this program. If not, see <http://www.gnu.org/licenses/> |
24 | 25 | * |
25 | 26 | */ |
| 27 | + |
26 | 28 | namespace OC\Core\Command\User; |
27 | 29 |
|
28 | 30 | use OC\Core\Command\Base; |
|
31 | 33 | use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext; |
32 | 34 | use Symfony\Component\Console\Input\InputArgument; |
33 | 35 | use Symfony\Component\Console\Input\InputInterface; |
| 36 | +use Symfony\Component\Console\Input\InputOption; |
34 | 37 | use Symfony\Component\Console\Output\OutputInterface; |
35 | 38 |
|
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 | + } |
42 | 46 |
|
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 | + } |
53 | 65 |
|
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 | + } |
60 | 81 |
|
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 | + } |
73 | 94 |
|
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 | + } |
85 | 107 | } |
0 commit comments