SetConfig.php 8.6 KB

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