Setting.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Johannes Leuker <j.leuker@hosting.de>
  8. * @author Kim Brose <kim.brose@rwth-aachen.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  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 OC\Core\Command\User;
  27. use OC\Core\Command\Base;
  28. use OCP\IConfig;
  29. use OCP\IUser;
  30. use OCP\IUserManager;
  31. use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
  32. use Symfony\Component\Console\Input\InputArgument;
  33. use Symfony\Component\Console\Input\InputInterface;
  34. use Symfony\Component\Console\Input\InputOption;
  35. use Symfony\Component\Console\Output\OutputInterface;
  36. class Setting extends Base {
  37. public function __construct(
  38. protected IUserManager $userManager,
  39. protected IConfig $config,
  40. ) {
  41. parent::__construct();
  42. }
  43. protected function configure() {
  44. parent::configure();
  45. $this
  46. ->setName('user:setting')
  47. ->setDescription('Read and modify user settings')
  48. ->addArgument(
  49. 'uid',
  50. InputArgument::REQUIRED,
  51. 'Account ID used to login'
  52. )
  53. ->addArgument(
  54. 'app',
  55. InputArgument::OPTIONAL,
  56. 'Restrict the settings to a given app',
  57. ''
  58. )
  59. ->addArgument(
  60. 'key',
  61. InputArgument::OPTIONAL,
  62. 'Setting key to set, get or delete',
  63. ''
  64. )
  65. ->addOption(
  66. 'ignore-missing-user',
  67. null,
  68. InputOption::VALUE_NONE,
  69. 'Use this option to ignore errors when the user does not exist'
  70. )
  71. // Get
  72. ->addOption(
  73. 'default-value',
  74. null,
  75. InputOption::VALUE_REQUIRED,
  76. '(Only applicable on get) If no default value is set and the config does not exist, the command will exit with 1'
  77. )
  78. // Set
  79. ->addArgument(
  80. 'value',
  81. InputArgument::OPTIONAL,
  82. 'The new value of the setting',
  83. null
  84. )
  85. ->addOption(
  86. 'update-only',
  87. null,
  88. InputOption::VALUE_NONE,
  89. 'Only updates the value, if it is not set before, it is not being added'
  90. )
  91. // Delete
  92. ->addOption(
  93. 'delete',
  94. null,
  95. InputOption::VALUE_NONE,
  96. 'Specify this option to delete the config'
  97. )
  98. ->addOption(
  99. 'error-if-not-exists',
  100. null,
  101. InputOption::VALUE_NONE,
  102. 'Checks whether the setting exists before deleting it'
  103. )
  104. ;
  105. }
  106. protected function checkInput(InputInterface $input) {
  107. if (!$input->getOption('ignore-missing-user')) {
  108. $uid = $input->getArgument('uid');
  109. $user = $this->userManager->get($uid);
  110. if (!$user) {
  111. throw new \InvalidArgumentException('The user "' . $uid . '" does not exist.');
  112. }
  113. // normalize uid
  114. $input->setArgument('uid', $user->getUID());
  115. }
  116. if ($input->getArgument('key') === '' && $input->hasParameterOption('--default-value')) {
  117. throw new \InvalidArgumentException('The "default-value" option can only be used when specifying a key.');
  118. }
  119. if ($input->getArgument('key') === '' && $input->getArgument('value') !== null) {
  120. throw new \InvalidArgumentException('The value argument can only be used when specifying a key.');
  121. }
  122. if ($input->getArgument('value') !== null && $input->hasParameterOption('--default-value')) {
  123. throw new \InvalidArgumentException('The value argument can not be used together with "default-value".');
  124. }
  125. if ($input->getOption('update-only') && $input->getArgument('value') === null) {
  126. throw new \InvalidArgumentException('The "update-only" option can only be used together with "value".');
  127. }
  128. if ($input->getArgument('key') === '' && $input->getOption('delete')) {
  129. throw new \InvalidArgumentException('The "delete" option can only be used when specifying a key.');
  130. }
  131. if ($input->getOption('delete') && $input->hasParameterOption('--default-value')) {
  132. throw new \InvalidArgumentException('The "delete" option can not be used together with "default-value".');
  133. }
  134. if ($input->getOption('delete') && $input->getArgument('value') !== null) {
  135. throw new \InvalidArgumentException('The "delete" option can not be used together with "value".');
  136. }
  137. if ($input->getOption('error-if-not-exists') && !$input->getOption('delete')) {
  138. throw new \InvalidArgumentException('The "error-if-not-exists" option can only be used together with "delete".');
  139. }
  140. }
  141. protected function execute(InputInterface $input, OutputInterface $output): int {
  142. try {
  143. $this->checkInput($input);
  144. } catch (\InvalidArgumentException $e) {
  145. $output->writeln('<error>' . $e->getMessage() . '</error>');
  146. return 1;
  147. }
  148. $uid = $input->getArgument('uid');
  149. $app = $input->getArgument('app');
  150. $key = $input->getArgument('key');
  151. if ($key !== '') {
  152. $value = $this->config->getUserValue($uid, $app, $key, null);
  153. if ($input->getArgument('value') !== null) {
  154. if ($input->hasParameterOption('--update-only') && $value === null) {
  155. $output->writeln('<error>The setting does not exist for user "' . $uid . '".</error>');
  156. return 1;
  157. }
  158. if ($app === 'settings' && in_array($key, ['email', 'display_name'])) {
  159. $user = $this->userManager->get($uid);
  160. if ($user instanceof IUser) {
  161. if ($key === 'email') {
  162. $user->setEMailAddress($input->getArgument('value'));
  163. } elseif ($key === 'display_name') {
  164. if (!$user->setDisplayName($input->getArgument('value'))) {
  165. if ($user->getDisplayName() === $input->getArgument('value')) {
  166. $output->writeln('<error>New and old display name are the same</error>');
  167. } elseif ($input->getArgument('value') === '') {
  168. $output->writeln('<error>New display name can\'t be empty</error>');
  169. } else {
  170. $output->writeln('<error>Could not set display name</error>');
  171. }
  172. return 1;
  173. }
  174. }
  175. // setEmailAddress and setDisplayName both internally set the value
  176. return 0;
  177. }
  178. }
  179. $this->config->setUserValue($uid, $app, $key, $input->getArgument('value'));
  180. return 0;
  181. } elseif ($input->hasParameterOption('--delete')) {
  182. if ($input->hasParameterOption('--error-if-not-exists') && $value === null) {
  183. $output->writeln('<error>The setting does not exist for user "' . $uid . '".</error>');
  184. return 1;
  185. }
  186. if ($app === 'settings' && in_array($key, ['email', 'display_name'])) {
  187. $user = $this->userManager->get($uid);
  188. if ($user instanceof IUser) {
  189. if ($key === 'email') {
  190. $user->setEMailAddress('');
  191. // setEmailAddress already deletes the value
  192. return 0;
  193. } elseif ($key === 'display_name') {
  194. $output->writeln('<error>Display name can\'t be deleted.</error>');
  195. return 1;
  196. }
  197. }
  198. }
  199. $this->config->deleteUserValue($uid, $app, $key);
  200. return 0;
  201. } elseif ($value !== null) {
  202. $output->writeln($value);
  203. return 0;
  204. } elseif ($input->hasParameterOption('--default-value')) {
  205. $output->writeln($input->getOption('default-value'));
  206. return 0;
  207. } else {
  208. if ($app === 'settings' && $key === 'display_name') {
  209. $user = $this->userManager->get($uid);
  210. $output->writeln($user->getDisplayName());
  211. return 0;
  212. }
  213. $output->writeln('<error>The setting does not exist for user "' . $uid . '".</error>');
  214. return 1;
  215. }
  216. } else {
  217. $settings = $this->getUserSettings($uid, $app);
  218. $this->writeArrayInOutputFormat($input, $output, $settings);
  219. return 0;
  220. }
  221. }
  222. protected function getUserSettings($uid, $app) {
  223. $settings = $this->config->getAllUserValues($uid);
  224. if ($app !== '') {
  225. if (isset($settings[$app])) {
  226. $settings = [$app => $settings[$app]];
  227. } else {
  228. $settings = [];
  229. }
  230. }
  231. $user = $this->userManager->get($uid);
  232. $settings['settings']['display_name'] = $user->getDisplayName();
  233. return $settings;
  234. }
  235. /**
  236. * @param string $argumentName
  237. * @param CompletionContext $context
  238. * @return string[]
  239. */
  240. public function completeArgumentValues($argumentName, CompletionContext $context) {
  241. if ($argumentName === 'uid') {
  242. return array_map(static fn (IUser $user) => $user->getUID(), $this->userManager->search($context->getCurrentWord()));
  243. }
  244. if ($argumentName === 'app') {
  245. $userId = $context->getWordAtIndex($context->getWordIndex() - 1);
  246. $settings = $this->getUserSettings($userId, '');
  247. return array_keys($settings);
  248. }
  249. if ($argumentName === 'key') {
  250. $userId = $context->getWordAtIndex($context->getWordIndex() - 2);
  251. $app = $context->getWordAtIndex($context->getWordIndex() - 1);
  252. $settings = $this->getUserSettings($userId, $app);
  253. return array_keys($settings[$app]);
  254. }
  255. return [];
  256. }
  257. }