SetConfig.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 OCA\User_LDAP\Configuration;
  9. use OCA\User_LDAP\ConnectionFactory;
  10. use OCA\User_LDAP\Helper;
  11. use OCA\User_LDAP\LDAP;
  12. use Symfony\Component\Console\Command\Command;
  13. use Symfony\Component\Console\Input\InputArgument;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. class SetConfig extends Command {
  17. protected function configure(): void {
  18. $this
  19. ->setName('ldap:set-config')
  20. ->setDescription('modifies an LDAP configuration')
  21. ->addArgument(
  22. 'configID',
  23. InputArgument::REQUIRED,
  24. 'the configuration ID'
  25. )
  26. ->addArgument(
  27. 'configKey',
  28. InputArgument::REQUIRED,
  29. 'the configuration key'
  30. )
  31. ->addArgument(
  32. 'configValue',
  33. InputArgument::REQUIRED,
  34. 'the new configuration value'
  35. )
  36. ;
  37. }
  38. protected function execute(InputInterface $input, OutputInterface $output): int {
  39. $helper = new Helper(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection());
  40. $availableConfigs = $helper->getServerConfigurationPrefixes();
  41. $configID = $input->getArgument('configID');
  42. if (!in_array($configID, $availableConfigs)) {
  43. $output->writeln('Invalid configID');
  44. return self::FAILURE;
  45. }
  46. $this->setValue(
  47. $configID,
  48. $input->getArgument('configKey'),
  49. $input->getArgument('configValue')
  50. );
  51. return self::SUCCESS;
  52. }
  53. /**
  54. * save the configuration value as provided
  55. */
  56. protected function setValue(string $configID, string $key, string $value): void {
  57. $configHolder = new Configuration($configID);
  58. $configHolder->$key = $value;
  59. $configHolder->saveConfiguration();
  60. $connectionFactory = new ConnectionFactory(new LDAP());
  61. $connectionFactory->get($configID)->clearCache();
  62. }
  63. }