ShowRemnants.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\User_LDAP\Command;
  8. use OCA\User_LDAP\User\DeletedUsersIndex;
  9. use OCP\IDateTimeFormatter;
  10. use Symfony\Component\Console\Command\Command;
  11. use Symfony\Component\Console\Helper\Table;
  12. use Symfony\Component\Console\Input\InputInterface;
  13. use Symfony\Component\Console\Input\InputOption;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. class ShowRemnants extends Command {
  16. public function __construct(
  17. protected DeletedUsersIndex $dui,
  18. protected IDateTimeFormatter $dateFormatter,
  19. ) {
  20. parent::__construct();
  21. }
  22. protected function configure(): void {
  23. $this
  24. ->setName('ldap:show-remnants')
  25. ->setDescription('shows which users are not available on LDAP anymore, but have remnants in Nextcloud.')
  26. ->addOption('json', null, InputOption::VALUE_NONE, 'return JSON array instead of pretty table.')
  27. ->addOption('short-date', null, InputOption::VALUE_NONE, 'show dates in Y-m-d format');
  28. }
  29. protected function formatDate(int $timestamp, string $default, bool $showShortDate): string {
  30. if (!($timestamp > 0)) {
  31. return $default;
  32. }
  33. if ($showShortDate) {
  34. return date('Y-m-d', $timestamp);
  35. }
  36. return $this->dateFormatter->formatDate($timestamp);
  37. }
  38. /**
  39. * executes the command, i.e. creates and outputs a table of LDAP users marked as deleted
  40. *
  41. * {@inheritdoc}
  42. */
  43. protected function execute(InputInterface $input, OutputInterface $output): int {
  44. /** @var \Symfony\Component\Console\Helper\Table $table */
  45. $table = new Table($output);
  46. $table->setHeaders([
  47. 'Nextcloud name', 'Display Name', 'LDAP UID', 'LDAP DN', 'Last Login',
  48. 'Detected on', 'Dir', 'Sharer'
  49. ]);
  50. $rows = [];
  51. $resultSet = $this->dui->getUsers();
  52. foreach ($resultSet as $user) {
  53. $rows[] = [
  54. 'ocName' => $user->getOCName(),
  55. 'displayName' => $user->getDisplayName(),
  56. 'uid' => $user->getUID(),
  57. 'dn' => $user->getDN(),
  58. 'lastLogin' => $this->formatDate($user->getLastLogin(), '-', (bool)$input->getOption('short-date')),
  59. 'detectedOn' => $this->formatDate($user->getDetectedOn(), 'unknown', (bool)$input->getOption('short-date')),
  60. 'homePath' => $user->getHomePath(),
  61. 'sharer' => $user->getHasActiveShares() ? 'Y' : 'N',
  62. ];
  63. }
  64. if ($input->getOption('json')) {
  65. $output->writeln(json_encode($rows));
  66. } else {
  67. $table->setRows($rows);
  68. $table->render();
  69. }
  70. return self::SUCCESS;
  71. }
  72. }