ShowRemnants.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author scolebrook <scolebrook@mac.com>
  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 OCA\User_LDAP\Command;
  28. use OCA\User_LDAP\User\DeletedUsersIndex;
  29. use OCP\IDateTimeFormatter;
  30. use Symfony\Component\Console\Command\Command;
  31. use Symfony\Component\Console\Helper\Table;
  32. use Symfony\Component\Console\Input\InputInterface;
  33. use Symfony\Component\Console\Input\InputOption;
  34. use Symfony\Component\Console\Output\OutputInterface;
  35. class ShowRemnants extends Command {
  36. public function __construct(
  37. protected DeletedUsersIndex $dui,
  38. protected IDateTimeFormatter $dateFormatter,
  39. ) {
  40. parent::__construct();
  41. }
  42. protected function configure(): void {
  43. $this
  44. ->setName('ldap:show-remnants')
  45. ->setDescription('shows which users are not available on LDAP anymore, but have remnants in Nextcloud.')
  46. ->addOption('json', null, InputOption::VALUE_NONE, 'return JSON array instead of pretty table.')
  47. ->addOption('short-date', null, InputOption::VALUE_NONE, 'show dates in Y-m-d format');
  48. }
  49. protected function formatDate(int $timestamp, string $default, bool $showShortDate): string {
  50. if (!($timestamp > 0)) {
  51. return $default;
  52. }
  53. if ($showShortDate) {
  54. return date('Y-m-d', $timestamp);
  55. }
  56. return $this->dateFormatter->formatDate($timestamp);
  57. }
  58. /**
  59. * executes the command, i.e. creates and outputs a table of LDAP users marked as deleted
  60. *
  61. * {@inheritdoc}
  62. */
  63. protected function execute(InputInterface $input, OutputInterface $output): int {
  64. /** @var \Symfony\Component\Console\Helper\Table $table */
  65. $table = new Table($output);
  66. $table->setHeaders([
  67. 'Nextcloud name', 'Display Name', 'LDAP UID', 'LDAP DN', 'Last Login',
  68. 'Detected on', 'Dir', 'Sharer'
  69. ]);
  70. $rows = [];
  71. $resultSet = $this->dui->getUsers();
  72. foreach ($resultSet as $user) {
  73. $rows[] = [
  74. 'ocName' => $user->getOCName(),
  75. 'displayName' => $user->getDisplayName(),
  76. 'uid' => $user->getUID(),
  77. 'dn' => $user->getDN(),
  78. 'lastLogin' => $this->formatDate($user->getLastLogin(), '-', (bool)$input->getOption('short-date')),
  79. 'detectedOn' => $this->formatDate($user->getDetectedOn(), 'unknown', (bool)$input->getOption('short-date')),
  80. 'homePath' => $user->getHomePath(),
  81. 'sharer' => $user->getHasActiveShares() ? 'Y' : 'N',
  82. ];
  83. }
  84. if ($input->getOption('json')) {
  85. $output->writeln(json_encode($rows));
  86. } else {
  87. $table->setRows($rows);
  88. $table->render();
  89. }
  90. return self::SUCCESS;
  91. }
  92. }