SetConfig.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  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\Configuration;
  28. use OCA\User_LDAP\ConnectionFactory;
  29. use OCA\User_LDAP\Helper;
  30. use OCA\User_LDAP\LDAP;
  31. use Symfony\Component\Console\Command\Command;
  32. use Symfony\Component\Console\Input\InputArgument;
  33. use Symfony\Component\Console\Input\InputInterface;
  34. use Symfony\Component\Console\Output\OutputInterface;
  35. class SetConfig extends Command {
  36. protected function configure(): void {
  37. $this
  38. ->setName('ldap:set-config')
  39. ->setDescription('modifies an LDAP configuration')
  40. ->addArgument(
  41. 'configID',
  42. InputArgument::REQUIRED,
  43. 'the configuration ID'
  44. )
  45. ->addArgument(
  46. 'configKey',
  47. InputArgument::REQUIRED,
  48. 'the configuration key'
  49. )
  50. ->addArgument(
  51. 'configValue',
  52. InputArgument::REQUIRED,
  53. 'the new configuration value'
  54. )
  55. ;
  56. }
  57. protected function execute(InputInterface $input, OutputInterface $output): int {
  58. $helper = new Helper(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection());
  59. $availableConfigs = $helper->getServerConfigurationPrefixes();
  60. $configID = $input->getArgument('configID');
  61. if (!in_array($configID, $availableConfigs)) {
  62. $output->writeln("Invalid configID");
  63. return self::FAILURE;
  64. }
  65. $this->setValue(
  66. $configID,
  67. $input->getArgument('configKey'),
  68. $input->getArgument('configValue')
  69. );
  70. return self::SUCCESS;
  71. }
  72. /**
  73. * save the configuration value as provided
  74. */
  75. protected function setValue(string $configID, string $key, string $value): void {
  76. $configHolder = new Configuration($configID);
  77. $configHolder->$key = $value;
  78. $configHolder->saveConfiguration();
  79. $connectionFactory = new ConnectionFactory(new LDAP());
  80. $connectionFactory->get($configID)->clearCache();
  81. }
  82. }