LastSeen.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Core\Command\User;
  8. use OC\Core\Command\Base;
  9. use OCP\IUser;
  10. use OCP\IUserManager;
  11. use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
  12. use Symfony\Component\Console\Input\InputArgument;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Input\InputOption;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. class LastSeen extends Base {
  17. public function __construct(
  18. protected IUserManager $userManager,
  19. ) {
  20. parent::__construct();
  21. }
  22. protected function configure(): void {
  23. $this
  24. ->setName('user:lastseen')
  25. ->setDescription('shows when the user was logged in last time')
  26. ->addArgument(
  27. 'uid',
  28. InputArgument::OPTIONAL,
  29. 'the username'
  30. )
  31. ->addOption(
  32. 'all',
  33. null,
  34. InputOption::VALUE_NONE,
  35. 'shows a list of when all users were last logged in'
  36. )
  37. ;
  38. }
  39. protected function execute(InputInterface $input, OutputInterface $output): int {
  40. $singleUserId = $input->getArgument('uid');
  41. if ($singleUserId) {
  42. $user = $this->userManager->get($singleUserId);
  43. if (is_null($user)) {
  44. $output->writeln('<error>User does not exist</error>');
  45. return 1;
  46. }
  47. $lastLogin = $user->getLastLogin();
  48. if ($lastLogin === 0) {
  49. $output->writeln($user->getUID() . ' has never logged in.');
  50. } else {
  51. $date = new \DateTime();
  52. $date->setTimestamp($lastLogin);
  53. $output->writeln($user->getUID() . "'s last login: " . $date->format('Y-m-d H:i:s T'));
  54. }
  55. return 0;
  56. }
  57. if (!$input->getOption('all')) {
  58. $output->writeln('<error>Please specify a username, or "--all" to list all</error>');
  59. return 1;
  60. }
  61. $this->userManager->callForAllUsers(static function (IUser $user) use ($output) {
  62. $lastLogin = $user->getLastLogin();
  63. if ($lastLogin === 0) {
  64. $output->writeln($user->getUID() . ' has never logged in.');
  65. } else {
  66. $date = new \DateTime();
  67. $date->setTimestamp($lastLogin);
  68. $output->writeln($user->getUID() . "'s last login: " . $date->format('Y-m-d H:i:s T'));
  69. }
  70. });
  71. return 0;
  72. }
  73. /**
  74. * @param string $argumentName
  75. * @param CompletionContext $context
  76. * @return string[]
  77. */
  78. public function completeArgumentValues($argumentName, CompletionContext $context) {
  79. if ($argumentName === 'uid') {
  80. return array_map(static fn (IUser $user) => $user->getUID(), $this->userManager->search($context->getCurrentWord()));
  81. }
  82. return [];
  83. }
  84. }