SetConfig.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. namespace OC\Core\Command\Config\App;
  9. use OC\AppConfig;
  10. use OCP\Exceptions\AppConfigIncorrectTypeException;
  11. use OCP\Exceptions\AppConfigUnknownKeyException;
  12. use OCP\IAppConfig;
  13. use Symfony\Component\Console\Input\InputArgument;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Input\InputOption;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. use Symfony\Component\Console\Question\Question;
  18. class SetConfig extends Base {
  19. public function __construct(
  20. protected IAppConfig $appConfig,
  21. ) {
  22. parent::__construct();
  23. }
  24. protected function configure() {
  25. parent::configure();
  26. $this
  27. ->setName('config:app:set')
  28. ->setDescription('Set an app config value')
  29. ->addArgument(
  30. 'app',
  31. InputArgument::REQUIRED,
  32. 'Name of the app'
  33. )
  34. ->addArgument(
  35. 'name',
  36. InputArgument::REQUIRED,
  37. 'Name of the config to set'
  38. )
  39. ->addOption(
  40. 'value',
  41. null,
  42. InputOption::VALUE_REQUIRED,
  43. 'The new value of the config'
  44. )
  45. ->addOption(
  46. 'type',
  47. null,
  48. InputOption::VALUE_REQUIRED,
  49. 'Value type [string, integer, float, boolean, array]',
  50. 'string'
  51. )
  52. ->addOption(
  53. 'lazy',
  54. null,
  55. InputOption::VALUE_NEGATABLE,
  56. 'Set value as lazy loaded',
  57. )
  58. ->addOption(
  59. 'sensitive',
  60. null,
  61. InputOption::VALUE_NEGATABLE,
  62. 'Set value as sensitive',
  63. )
  64. ->addOption(
  65. 'update-only',
  66. null,
  67. InputOption::VALUE_NONE,
  68. 'Only updates the value, if it is not set before, it is not being added'
  69. )
  70. ;
  71. }
  72. protected function execute(InputInterface $input, OutputInterface $output): int {
  73. $appName = $input->getArgument('app');
  74. $configName = $input->getArgument('name');
  75. if (!($this->appConfig instanceof AppConfig)) {
  76. throw new \Exception('Only compatible with OC\AppConfig as it uses internal methods');
  77. }
  78. if ($input->hasParameterOption('--update-only') && !$this->appConfig->hasKey($appName, $configName)) {
  79. $output->writeln(
  80. '<comment>Config value ' . $configName . ' for app ' . $appName
  81. . ' not updated, as it has not been set before.</comment>'
  82. );
  83. return 1;
  84. }
  85. $type = $typeString = null;
  86. if ($input->hasParameterOption('--type')) {
  87. $typeString = $input->getOption('type');
  88. $type = $this->appConfig->convertTypeToInt($typeString);
  89. }
  90. /**
  91. * If --Value is not specified, returns an exception if no value exists in database
  92. * compare with current status in database and displays a reminder that this can break things.
  93. * confirmation is required by admin, unless --no-interaction
  94. */
  95. $updated = false;
  96. if (!$input->hasParameterOption('--value')) {
  97. if (!$input->getOption('lazy') && $this->appConfig->isLazy($appName, $configName) && $this->ask($input, $output, 'NOT LAZY')) {
  98. $updated = $this->appConfig->updateLazy($appName, $configName, false);
  99. }
  100. if ($input->getOption('lazy') && !$this->appConfig->isLazy($appName, $configName) && $this->ask($input, $output, 'LAZY')) {
  101. $updated = $this->appConfig->updateLazy($appName, $configName, true) || $updated;
  102. }
  103. if (!$input->getOption('sensitive') && $this->appConfig->isSensitive($appName, $configName) && $this->ask($input, $output, 'NOT SENSITIVE')) {
  104. $updated = $this->appConfig->updateSensitive($appName, $configName, false) || $updated;
  105. }
  106. if ($input->getOption('sensitive') && !$this->appConfig->isSensitive($appName, $configName) && $this->ask($input, $output, 'SENSITIVE')) {
  107. $updated = $this->appConfig->updateSensitive($appName, $configName, true) || $updated;
  108. }
  109. if ($type !== null && $type !== $this->appConfig->getValueType($appName, $configName) && $typeString !== null && $this->ask($input, $output, $typeString)) {
  110. $updated = $this->appConfig->updateType($appName, $configName, $type) || $updated;
  111. }
  112. } else {
  113. /**
  114. * If --type is specified in the command line, we upgrade the type in database
  115. * after a confirmation from admin.
  116. * If not we get the type from current stored value or VALUE_MIXED as default.
  117. */
  118. try {
  119. $currType = $this->appConfig->getValueType($appName, $configName);
  120. if ($type === null || $typeString === null || $type === $currType || !$this->ask($input, $output, $typeString)) {
  121. $type = $currType;
  122. } else {
  123. $updated = $this->appConfig->updateType($appName, $configName, $type);
  124. }
  125. } catch (AppConfigUnknownKeyException) {
  126. $type = $type ?? IAppConfig::VALUE_MIXED;
  127. }
  128. /**
  129. * if --lazy/--no-lazy option are set, compare with data stored in database.
  130. * If no data in database, or identical, continue.
  131. * If different, ask admin for confirmation.
  132. */
  133. $lazy = $input->getOption('lazy');
  134. try {
  135. $currLazy = $this->appConfig->isLazy($appName, $configName);
  136. if ($lazy === null || $lazy === $currLazy || !$this->ask($input, $output, ($lazy) ? 'LAZY' : 'NOT LAZY')) {
  137. $lazy = $currLazy;
  138. }
  139. } catch (AppConfigUnknownKeyException) {
  140. $lazy = $lazy ?? false;
  141. }
  142. /**
  143. * same with sensitive status
  144. */
  145. $sensitive = $input->getOption('sensitive');
  146. try {
  147. $currSensitive = $this->appConfig->isSensitive($appName, $configName, null);
  148. if ($sensitive === null || $sensitive === $currSensitive || !$this->ask($input, $output, ($sensitive) ? 'SENSITIVE' : 'NOT SENSITIVE')) {
  149. $sensitive = $currSensitive;
  150. }
  151. } catch (AppConfigUnknownKeyException) {
  152. $sensitive = $sensitive ?? false;
  153. }
  154. $value = (string)$input->getOption('value');
  155. switch ($type) {
  156. case IAppConfig::VALUE_MIXED:
  157. $updated = $this->appConfig->setValueMixed($appName, $configName, $value, $lazy, $sensitive);
  158. break;
  159. case IAppConfig::VALUE_STRING:
  160. $updated = $this->appConfig->setValueString($appName, $configName, $value, $lazy, $sensitive);
  161. break;
  162. case IAppConfig::VALUE_INT:
  163. if ($value !== ((string) ((int) $value))) {
  164. throw new AppConfigIncorrectTypeException('Value is not an integer');
  165. }
  166. $updated = $this->appConfig->setValueInt($appName, $configName, (int)$value, $lazy, $sensitive);
  167. break;
  168. case IAppConfig::VALUE_FLOAT:
  169. if ($value !== ((string) ((float) $value))) {
  170. throw new AppConfigIncorrectTypeException('Value is not a float');
  171. }
  172. $updated = $this->appConfig->setValueFloat($appName, $configName, (float)$value, $lazy, $sensitive);
  173. break;
  174. case IAppConfig::VALUE_BOOL:
  175. if (in_array(strtolower($value), ['true', '1', 'on', 'yes'])) {
  176. $valueBool = true;
  177. } elseif (in_array(strtolower($value), ['false', '0', 'off', 'no'])) {
  178. $valueBool = false;
  179. } else {
  180. throw new AppConfigIncorrectTypeException('Value is not a boolean, please use \'true\' or \'false\'');
  181. }
  182. $updated = $this->appConfig->setValueBool($appName, $configName, $valueBool, $lazy);
  183. break;
  184. case IAppConfig::VALUE_ARRAY:
  185. $valueArray = json_decode($value, true, flags: JSON_THROW_ON_ERROR);
  186. $valueArray = (is_array($valueArray)) ? $valueArray : throw new AppConfigIncorrectTypeException('Value is not an array');
  187. $updated = $this->appConfig->setValueArray($appName, $configName, $valueArray, $lazy, $sensitive);
  188. break;
  189. }
  190. }
  191. if ($updated) {
  192. $current = $this->appConfig->getDetails($appName, $configName);
  193. $output->writeln(
  194. sprintf(
  195. "<info>Config value '%s' for app '%s' is now set to '%s', stored as %s in %s</info>",
  196. $configName,
  197. $appName,
  198. $current['value'],
  199. $current['typeString'],
  200. $current['lazy'] ? 'lazy cache' : 'fast cache'
  201. )
  202. );
  203. } else {
  204. $output->writeln('<info>Config value were not updated</info>');
  205. }
  206. return 0;
  207. }
  208. private function ask(InputInterface $input, OutputInterface $output, string $request): bool {
  209. $helper = $this->getHelper('question');
  210. if ($input->getOption('no-interaction')) {
  211. return true;
  212. }
  213. $output->writeln(sprintf('You are about to set config value %s as <info>%s</info>',
  214. '<info>' . $input->getArgument('app') . '</info>/<info>' . $input->getArgument('name') . '</info>',
  215. strtoupper($request)
  216. ));
  217. $output->writeln('');
  218. $output->writeln('<comment>This might break thing, affect performance on your instance or its security!</comment>');
  219. $result = (strtolower((string)$helper->ask(
  220. $input,
  221. $output,
  222. new Question('<comment>Confirm this action by typing \'yes\'</comment>: '))) === 'yes');
  223. $output->writeln(($result) ? 'done' : 'cancelled');
  224. $output->writeln('');
  225. return $result;
  226. }
  227. }