1
0

ShowConfig.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 OC\Core\Command\Base;
  9. use OCA\User_LDAP\Configuration;
  10. use OCA\User_LDAP\Helper;
  11. use Symfony\Component\Console\Helper\Table;
  12. use Symfony\Component\Console\Input\InputArgument;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Input\InputOption;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. class ShowConfig extends Base {
  17. public function __construct(
  18. protected Helper $helper,
  19. ) {
  20. parent::__construct();
  21. }
  22. protected function configure(): void {
  23. $this
  24. ->setName('ldap:show-config')
  25. ->setDescription('shows the LDAP configuration')
  26. ->addArgument(
  27. 'configID',
  28. InputArgument::OPTIONAL,
  29. 'will show the configuration of the specified id'
  30. )
  31. ->addOption(
  32. 'show-password',
  33. null,
  34. InputOption::VALUE_NONE,
  35. 'show ldap bind password'
  36. )
  37. ->addOption(
  38. 'output',
  39. null,
  40. InputOption::VALUE_OPTIONAL,
  41. 'Output format (table, plain, json or json_pretty, default is table)',
  42. 'table'
  43. )
  44. ;
  45. }
  46. protected function execute(InputInterface $input, OutputInterface $output): int {
  47. $availableConfigs = $this->helper->getServerConfigurationPrefixes();
  48. $configID = $input->getArgument('configID');
  49. if (!is_null($configID)) {
  50. $configIDs[] = $configID;
  51. if (!in_array($configIDs[0], $availableConfigs)) {
  52. $output->writeln('Invalid configID');
  53. return self::FAILURE;
  54. }
  55. } else {
  56. $configIDs = $availableConfigs;
  57. }
  58. $this->renderConfigs($configIDs, $input, $output);
  59. return self::SUCCESS;
  60. }
  61. /**
  62. * prints the LDAP configuration(s)
  63. *
  64. * @param string[] $configIDs
  65. */
  66. protected function renderConfigs(
  67. array $configIDs,
  68. InputInterface $input,
  69. OutputInterface $output,
  70. ): void {
  71. $renderTable = $input->getOption('output') === 'table' or $input->getOption('output') === null;
  72. $showPassword = $input->getOption('show-password');
  73. $configs = [];
  74. foreach ($configIDs as $id) {
  75. $configHolder = new Configuration($id);
  76. $configuration = $configHolder->getConfiguration();
  77. ksort($configuration);
  78. $rows = [];
  79. if ($renderTable) {
  80. foreach ($configuration as $key => $value) {
  81. if (is_array($value)) {
  82. $value = implode(';', $value);
  83. }
  84. if ($key === 'ldapAgentPassword' && !$showPassword) {
  85. $rows[] = [$key, '***'];
  86. } else {
  87. $rows[] = [$key, $value];
  88. }
  89. }
  90. $table = new Table($output);
  91. $table->setHeaders(['Configuration', $id]);
  92. $table->setRows($rows);
  93. $table->render();
  94. continue;
  95. }
  96. foreach ($configuration as $key => $value) {
  97. if ($key === 'ldapAgentPassword' && !$showPassword) {
  98. $rows[$key] = '***';
  99. } else {
  100. $rows[$key] = $value;
  101. }
  102. }
  103. $configs[$id] = $rows;
  104. }
  105. if (!$renderTable) {
  106. $this->writeArrayInOutputFormat($input, $output, $configs);
  107. }
  108. }
  109. }