ShowRemnants.php 3.4 KB

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