LastSeen.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 Morris Jobke <hey@morrisjobke.de>
  9. * @author Pierre Ozoux <pierre@ozoux.net>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OC\Core\Command\User;
  28. use OCP\IUserManager;
  29. use Symfony\Component\Console\Command\Command;
  30. use Symfony\Component\Console\Input\InputArgument;
  31. use Symfony\Component\Console\Input\InputInterface;
  32. use Symfony\Component\Console\Output\OutputInterface;
  33. class LastSeen extends Command {
  34. /** @var IUserManager */
  35. protected $userManager;
  36. /**
  37. * @param IUserManager $userManager
  38. */
  39. public function __construct(IUserManager $userManager) {
  40. $this->userManager = $userManager;
  41. parent::__construct();
  42. }
  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. }
  53. protected function execute(InputInterface $input, OutputInterface $output): int {
  54. $user = $this->userManager->get($input->getArgument('uid'));
  55. if (is_null($user)) {
  56. $output->writeln('<error>User does not exist</error>');
  57. return 1;
  58. }
  59. $lastLogin = $user->getLastLogin();
  60. if ($lastLogin === 0) {
  61. $output->writeln('User ' . $user->getUID() .
  62. ' has never logged in, yet.');
  63. } else {
  64. $date = new \DateTime();
  65. $date->setTimestamp($lastLogin);
  66. $output->writeln($user->getUID() .
  67. '`s last login: ' . $date->format('d.m.Y H:i'));
  68. }
  69. return 0;
  70. }
  71. }