LastSeen.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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\Output\OutputInterface;
  34. class LastSeen extends Base {
  35. protected IUserManager $userManager;
  36. public function __construct(IUserManager $userManager) {
  37. $this->userManager = $userManager;
  38. parent::__construct();
  39. }
  40. protected function configure() {
  41. $this
  42. ->setName('user:lastseen')
  43. ->setDescription('shows when the user was logged in last time')
  44. ->addArgument(
  45. 'uid',
  46. InputArgument::REQUIRED,
  47. 'the username'
  48. );
  49. }
  50. protected function execute(InputInterface $input, OutputInterface $output): int {
  51. $user = $this->userManager->get($input->getArgument('uid'));
  52. if (is_null($user)) {
  53. $output->writeln('<error>User does not exist</error>');
  54. return 1;
  55. }
  56. $lastLogin = $user->getLastLogin();
  57. if ($lastLogin === 0) {
  58. $output->writeln('User ' . $user->getUID() .
  59. ' has never logged in, yet.');
  60. } else {
  61. $date = new \DateTime();
  62. $date->setTimestamp($lastLogin);
  63. $output->writeln($user->getUID() .
  64. '`s last login: ' . $date->format('d.m.Y H:i'));
  65. }
  66. return 0;
  67. }
  68. /**
  69. * @param string $argumentName
  70. * @param CompletionContext $context
  71. * @return string[]
  72. */
  73. public function completeArgumentValues($argumentName, CompletionContext $context) {
  74. if ($argumentName === 'uid') {
  75. return array_map(static fn (IUser $user) => $user->getUID(), $this->userManager->search($context->getCurrentWord()));
  76. }
  77. return [];
  78. }
  79. }