LastSeen.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 OCP\IUserManager;
  28. use Symfony\Component\Console\Command\Command;
  29. use Symfony\Component\Console\Input\InputArgument;
  30. use Symfony\Component\Console\Input\InputInterface;
  31. use Symfony\Component\Console\Output\OutputInterface;
  32. class LastSeen extends Command {
  33. /** @var IUserManager */
  34. protected $userManager;
  35. /**
  36. * @param IUserManager $userManager
  37. */
  38. public function __construct(IUserManager $userManager) {
  39. $this->userManager = $userManager;
  40. parent::__construct();
  41. }
  42. protected function configure() {
  43. $this
  44. ->setName('user:lastseen')
  45. ->setDescription('shows when the user was logged in last time')
  46. ->addArgument(
  47. 'uid',
  48. InputArgument::REQUIRED,
  49. 'the username'
  50. );
  51. }
  52. protected function execute(InputInterface $input, OutputInterface $output): int {
  53. $user = $this->userManager->get($input->getArgument('uid'));
  54. if (is_null($user)) {
  55. $output->writeln('<error>User does not exist</error>');
  56. return 1;
  57. }
  58. $lastLogin = $user->getLastLogin();
  59. if ($lastLogin === 0) {
  60. $output->writeln('User ' . $user->getUID() .
  61. ' has never logged in, yet.');
  62. } else {
  63. $date = new \DateTime();
  64. $date->setTimestamp($lastLogin);
  65. $output->writeln($user->getUID() .
  66. '`s last login: ' . $date->format('d.m.Y H:i'));
  67. }
  68. return 0;
  69. }
  70. }