Setting.php 9.0 KB

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