LastSeen.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Pierre Ozoux <pierre@ozoux.net>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OC\Core\Command\User;
  27. use OC\Core\Command\Base;
  28. use OCP\IUser;
  29. use OCP\IUserManager;
  30. use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
  31. use Symfony\Component\Console\Input\InputArgument;
  32. use Symfony\Component\Console\Input\InputInterface;
  33. use Symfony\Component\Console\Input\InputOption;
  34. use Symfony\Component\Console\Output\OutputInterface;
  35. class LastSeen extends Base {
  36. public function __construct(
  37. protected IUserManager $userManager,
  38. ) {
  39. parent::__construct();
  40. }
  41. protected function configure(): void {
  42. $this
  43. ->setName('user:lastseen')
  44. ->setDescription('shows when the user was logged in last time')
  45. ->addArgument(
  46. 'uid',
  47. InputArgument::OPTIONAL,
  48. 'the username'
  49. )
  50. ->addOption(
  51. 'all',
  52. null,
  53. InputOption::VALUE_NONE,
  54. 'shows a list of when all users were last logged in'
  55. )
  56. ;
  57. }
  58. protected function execute(InputInterface $input, OutputInterface $output): int {
  59. $singleUserId = $input->getArgument('uid');
  60. if ($singleUserId) {
  61. $user = $this->userManager->get($singleUserId);
  62. if (is_null($user)) {
  63. $output->writeln('<error>User does not exist</error>');
  64. return 1;
  65. }
  66. $lastLogin = $user->getLastLogin();
  67. if ($lastLogin === 0) {
  68. $output->writeln($user->getUID() . ' has never logged in.');
  69. } else {
  70. $date = new \DateTime();
  71. $date->setTimestamp($lastLogin);
  72. $output->writeln($user->getUID() . "'s last login: " . $date->format('Y-m-d H:i'));
  73. }
  74. return 0;
  75. }
  76. if (!$input->getOption('all')) {
  77. $output->writeln("<error>Please specify a username, or \"--all\" to list all</error>");
  78. return 1;
  79. }
  80. $this->userManager->callForAllUsers(static function (IUser $user) use ($output) {
  81. $lastLogin = $user->getLastLogin();
  82. if ($lastLogin === 0) {
  83. $output->writeln($user->getUID() . ' has never logged in.');
  84. } else {
  85. $date = new \DateTime();
  86. $date->setTimestamp($lastLogin);
  87. $output->writeln($user->getUID() . "'s last login: " . $date->format('Y-m-d H:i'));
  88. }
  89. });
  90. return 0;
  91. }
  92. /**
  93. * @param string $argumentName
  94. * @param CompletionContext $context
  95. * @return string[]
  96. */
  97. public function completeArgumentValues($argumentName, CompletionContext $context) {
  98. if ($argumentName === 'uid') {
  99. return array_map(static fn (IUser $user) => $user->getUID(), $this->userManager->search($context->getCurrentWord()));
  100. }
  101. return [];
  102. }
  103. }