1
0

ShowRemnants.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. /** @var \OCA\User_LDAP\User\DeletedUsersIndex */
  37. protected $dui;
  38. /** @var \OCP\IDateTimeFormatter */
  39. protected $dateFormatter;
  40. /**
  41. * @param DeletedUsersIndex $dui
  42. * @param IDateTimeFormatter $dateFormatter
  43. */
  44. public function __construct(DeletedUsersIndex $dui, IDateTimeFormatter $dateFormatter) {
  45. $this->dui = $dui;
  46. $this->dateFormatter = $dateFormatter;
  47. parent::__construct();
  48. }
  49. protected function configure() {
  50. $this
  51. ->setName('ldap:show-remnants')
  52. ->setDescription('shows which users are not available on LDAP anymore, but have remnants in Nextcloud.')
  53. ->addOption('json', null, InputOption::VALUE_NONE, 'return JSON array instead of pretty table.')
  54. ->addOption('short-date', null, InputOption::VALUE_NONE, 'show dates in Y-m-d format');
  55. }
  56. protected function formatDate(int $timestamp, string $default, bool $showShortDate) {
  57. if (!($timestamp > 0)) {
  58. return $default;
  59. }
  60. if ($showShortDate) {
  61. return date('Y-m-d', $timestamp);
  62. }
  63. return $this->dateFormatter->formatDate($timestamp);
  64. }
  65. /**
  66. * executes the command, i.e. creates and outputs a table of LDAP users marked as deleted
  67. *
  68. * {@inheritdoc}
  69. */
  70. protected function execute(InputInterface $input, OutputInterface $output): int {
  71. /** @var \Symfony\Component\Console\Helper\Table $table */
  72. $table = new Table($output);
  73. $table->setHeaders([
  74. 'Nextcloud name', 'Display Name', 'LDAP UID', 'LDAP DN', 'Last Login',
  75. 'Detected on', 'Dir', 'Sharer'
  76. ]);
  77. $rows = [];
  78. $resultSet = $this->dui->getUsers();
  79. foreach ($resultSet as $user) {
  80. $rows[] = [
  81. 'ocName' => $user->getOCName(),
  82. 'displayName' => $user->getDisplayName(),
  83. 'uid' => $user->getUID(),
  84. 'dn' => $user->getDN(),
  85. 'lastLogin' => $this->formatDate($user->getLastLogin(), '-', (bool)$input->getOption('short-date')),
  86. 'detectedOn' => $this->formatDate($user->getDetectedOn(), 'unknown', (bool)$input->getOption('short-date')),
  87. 'homePath' => $user->getHomePath(),
  88. 'sharer' => $user->getHasActiveShares() ? 'Y' : 'N',
  89. ];
  90. }
  91. if ($input->getOption('json')) {
  92. $output->writeln(json_encode($rows));
  93. } else {
  94. $table->setRows($rows);
  95. $table->render();
  96. }
  97. return 0;
  98. }
  99. }