ShowConfig.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. /** @var \OCA\User_LDAP\Helper */
  38. protected $helper;
  39. /**
  40. * @param Helper $helper
  41. */
  42. public function __construct(Helper $helper) {
  43. $this->helper = $helper;
  44. parent::__construct();
  45. }
  46. protected function configure() {
  47. $this
  48. ->setName('ldap:show-config')
  49. ->setDescription('shows the LDAP configuration')
  50. ->addArgument(
  51. 'configID',
  52. InputArgument::OPTIONAL,
  53. 'will show the configuration of the specified id'
  54. )
  55. ->addOption(
  56. 'show-password',
  57. null,
  58. InputOption::VALUE_NONE,
  59. 'show ldap bind password'
  60. )
  61. ->addOption(
  62. 'output',
  63. null,
  64. InputOption::VALUE_OPTIONAL,
  65. 'Output format (table, plain, json or json_pretty, default is table)',
  66. 'table'
  67. )
  68. ;
  69. }
  70. protected function execute(InputInterface $input, OutputInterface $output): int {
  71. $availableConfigs = $this->helper->getServerConfigurationPrefixes();
  72. $configID = $input->getArgument('configID');
  73. if (!is_null($configID)) {
  74. $configIDs[] = $configID;
  75. if (!in_array($configIDs[0], $availableConfigs)) {
  76. $output->writeln("Invalid configID");
  77. return 1;
  78. }
  79. } else {
  80. $configIDs = $availableConfigs;
  81. }
  82. $this->renderConfigs($configIDs, $input, $output);
  83. return 0;
  84. }
  85. /**
  86. * prints the LDAP configuration(s)
  87. * @param string[] configID(s)
  88. * @param InputInterface $input
  89. * @param OutputInterface $output
  90. */
  91. protected function renderConfigs($configIDs, $input, $output) {
  92. $renderTable = $input->getOption('output') === 'table' or $input->getOption('output') === null;
  93. $showPassword = $input->getOption('show-password');
  94. $configs = [];
  95. foreach ($configIDs as $id) {
  96. $configHolder = new Configuration($id);
  97. $configuration = $configHolder->getConfiguration();
  98. ksort($configuration);
  99. $rows = [];
  100. if ($renderTable) {
  101. foreach ($configuration as $key => $value) {
  102. if (is_array($value)) {
  103. $value = implode(';', $value);
  104. }
  105. if ($key === 'ldapAgentPassword' && !$showPassword) {
  106. $rows[] = [$key, '***'];
  107. } else {
  108. $rows[] = [$key, $value];
  109. }
  110. }
  111. $table = new Table($output);
  112. $table->setHeaders(['Configuration', $id]);
  113. $table->setRows($rows);
  114. $table->render();
  115. } else {
  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. }
  126. if (!$renderTable) {
  127. $this->writeArrayInOutputFormat($input, $output, $configs);
  128. }
  129. }
  130. }