ShowConfig.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 Johannes Leuker <j.leuker@hosting.de>
  9. * @author Laurens Post <Crote@users.noreply.github.com>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  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 OC\Core\Command\Base;
  29. use OCA\User_LDAP\Configuration;
  30. use OCA\User_LDAP\Helper;
  31. use Symfony\Component\Console\Helper\Table;
  32. use Symfony\Component\Console\Input\InputArgument;
  33. use Symfony\Component\Console\Input\InputInterface;
  34. use Symfony\Component\Console\Input\InputOption;
  35. use Symfony\Component\Console\Output\OutputInterface;
  36. class ShowConfig extends Base {
  37. public function __construct(
  38. protected Helper $helper,
  39. ) {
  40. parent::__construct();
  41. }
  42. protected function configure(): void {
  43. $this
  44. ->setName('ldap:show-config')
  45. ->setDescription('shows the LDAP configuration')
  46. ->addArgument(
  47. 'configID',
  48. InputArgument::OPTIONAL,
  49. 'will show the configuration of the specified id'
  50. )
  51. ->addOption(
  52. 'show-password',
  53. null,
  54. InputOption::VALUE_NONE,
  55. 'show ldap bind password'
  56. )
  57. ->addOption(
  58. 'output',
  59. null,
  60. InputOption::VALUE_OPTIONAL,
  61. 'Output format (table, plain, json or json_pretty, default is table)',
  62. 'table'
  63. )
  64. ;
  65. }
  66. protected function execute(InputInterface $input, OutputInterface $output): int {
  67. $availableConfigs = $this->helper->getServerConfigurationPrefixes();
  68. $configID = $input->getArgument('configID');
  69. if (!is_null($configID)) {
  70. $configIDs[] = $configID;
  71. if (!in_array($configIDs[0], $availableConfigs)) {
  72. $output->writeln("Invalid configID");
  73. return self::FAILURE;
  74. }
  75. } else {
  76. $configIDs = $availableConfigs;
  77. }
  78. $this->renderConfigs($configIDs, $input, $output);
  79. return self::SUCCESS;
  80. }
  81. /**
  82. * prints the LDAP configuration(s)
  83. *
  84. * @param string[] $configIDs
  85. */
  86. protected function renderConfigs(
  87. array $configIDs,
  88. InputInterface $input,
  89. OutputInterface $output,
  90. ): void {
  91. $renderTable = $input->getOption('output') === 'table' or $input->getOption('output') === null;
  92. $showPassword = $input->getOption('show-password');
  93. $configs = [];
  94. foreach ($configIDs as $id) {
  95. $configHolder = new Configuration($id);
  96. $configuration = $configHolder->getConfiguration();
  97. ksort($configuration);
  98. $rows = [];
  99. if ($renderTable) {
  100. foreach ($configuration as $key => $value) {
  101. if (is_array($value)) {
  102. $value = implode(';', $value);
  103. }
  104. if ($key === 'ldapAgentPassword' && !$showPassword) {
  105. $rows[] = [$key, '***'];
  106. } else {
  107. $rows[] = [$key, $value];
  108. }
  109. }
  110. $table = new Table($output);
  111. $table->setHeaders(['Configuration', $id]);
  112. $table->setRows($rows);
  113. $table->render();
  114. continue;
  115. }
  116. foreach ($configuration as $key => $value) {
  117. if ($key === 'ldapAgentPassword' && !$showPassword) {
  118. $rows[$key] = '***';
  119. } else {
  120. $rows[$key] = $value;
  121. }
  122. }
  123. $configs[$id] = $rows;
  124. }
  125. if (!$renderTable) {
  126. $this->writeArrayInOutputFormat($input, $output, $configs);
  127. }
  128. }
  129. }