You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SetConfig.php 9.1KB

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