Setting.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. $uid = $input->getArgument('uid');
  109. if (!$input->getOption('ignore-missing-user') && !$this->userManager->userExists($uid)) {
  110. throw new \InvalidArgumentException('The user "' . $uid . '" does not exist.');
  111. }
  112. if ($input->getArgument('key') === '' && $input->hasParameterOption('--default-value')) {
  113. throw new \InvalidArgumentException('The "default-value" option can only be used when specifying a key.');
  114. }
  115. if ($input->getArgument('key') === '' && $input->getArgument('value') !== null) {
  116. throw new \InvalidArgumentException('The value argument can only be used when specifying a key.');
  117. }
  118. if ($input->getArgument('value') !== null && $input->hasParameterOption('--default-value')) {
  119. throw new \InvalidArgumentException('The value argument can not be used together with "default-value".');
  120. }
  121. if ($input->getOption('update-only') && $input->getArgument('value') === null) {
  122. throw new \InvalidArgumentException('The "update-only" option can only be used together with "value".');
  123. }
  124. if ($input->getArgument('key') === '' && $input->getOption('delete')) {
  125. throw new \InvalidArgumentException('The "delete" option can only be used when specifying a key.');
  126. }
  127. if ($input->getOption('delete') && $input->hasParameterOption('--default-value')) {
  128. throw new \InvalidArgumentException('The "delete" option can not be used together with "default-value".');
  129. }
  130. if ($input->getOption('delete') && $input->getArgument('value') !== null) {
  131. throw new \InvalidArgumentException('The "delete" option can not be used together with "value".');
  132. }
  133. if ($input->getOption('error-if-not-exists') && !$input->getOption('delete')) {
  134. throw new \InvalidArgumentException('The "error-if-not-exists" option can only be used together with "delete".');
  135. }
  136. }
  137. protected function execute(InputInterface $input, OutputInterface $output): int {
  138. try {
  139. $this->checkInput($input);
  140. } catch (\InvalidArgumentException $e) {
  141. $output->writeln('<error>' . $e->getMessage() . '</error>');
  142. return 1;
  143. }
  144. $uid = $input->getArgument('uid');
  145. $app = $input->getArgument('app');
  146. $key = $input->getArgument('key');
  147. if ($key !== '') {
  148. $value = $this->config->getUserValue($uid, $app, $key, null);
  149. if ($input->getArgument('value') !== null) {
  150. if ($input->hasParameterOption('--update-only') && $value === null) {
  151. $output->writeln('<error>The setting does not exist for user "' . $uid . '".</error>');
  152. return 1;
  153. }
  154. if ($app === 'settings' && in_array($key, ['email', 'display_name'])) {
  155. $user = $this->userManager->get($uid);
  156. if ($user instanceof IUser) {
  157. if ($key === 'email') {
  158. $user->setEMailAddress($input->getArgument('value'));
  159. } elseif ($key === 'display_name') {
  160. if (!$user->setDisplayName($input->getArgument('value'))) {
  161. if ($user->getDisplayName() === $input->getArgument('value')) {
  162. $output->writeln('<error>New and old display name are the same</error>');
  163. } elseif ($input->getArgument('value') === '') {
  164. $output->writeln('<error>New display name can\'t be empty</error>');
  165. } else {
  166. $output->writeln('<error>Could not set display name</error>');
  167. }
  168. return 1;
  169. }
  170. }
  171. // setEmailAddress and setDisplayName both internally set the value
  172. return 0;
  173. }
  174. }
  175. $this->config->setUserValue($uid, $app, $key, $input->getArgument('value'));
  176. return 0;
  177. } elseif ($input->hasParameterOption('--delete')) {
  178. if ($input->hasParameterOption('--error-if-not-exists') && $value === null) {
  179. $output->writeln('<error>The setting does not exist for user "' . $uid . '".</error>');
  180. return 1;
  181. }
  182. if ($app === 'settings' && in_array($key, ['email', 'display_name'])) {
  183. $user = $this->userManager->get($uid);
  184. if ($user instanceof IUser) {
  185. if ($key === 'email') {
  186. $user->setEMailAddress('');
  187. // setEmailAddress already deletes the value
  188. return 0;
  189. } elseif ($key === 'display_name') {
  190. $output->writeln('<error>Display name can\'t be deleted.</error>');
  191. return 1;
  192. }
  193. }
  194. }
  195. $this->config->deleteUserValue($uid, $app, $key);
  196. return 0;
  197. } elseif ($value !== null) {
  198. $output->writeln($value);
  199. return 0;
  200. } elseif ($input->hasParameterOption('--default-value')) {
  201. $output->writeln($input->getOption('default-value'));
  202. return 0;
  203. } else {
  204. if ($app === 'settings' && $key === 'display_name') {
  205. $user = $this->userManager->get($uid);
  206. $output->writeln($user->getDisplayName());
  207. return 0;
  208. }
  209. $output->writeln('<error>The setting does not exist for user "' . $uid . '".</error>');
  210. return 1;
  211. }
  212. } else {
  213. $settings = $this->getUserSettings($uid, $app);
  214. $this->writeArrayInOutputFormat($input, $output, $settings);
  215. return 0;
  216. }
  217. }
  218. protected function getUserSettings($uid, $app) {
  219. $settings = $this->config->getAllUserValues($uid);
  220. if ($app !== '') {
  221. if (isset($settings[$app])) {
  222. $settings = [$app => $settings[$app]];
  223. } else {
  224. $settings = [];
  225. }
  226. }
  227. $user = $this->userManager->get($uid);
  228. $settings['settings']['display_name'] = $user->getDisplayName();
  229. return $settings;
  230. }
  231. /**
  232. * @param string $argumentName
  233. * @param CompletionContext $context
  234. * @return string[]
  235. */
  236. public function completeArgumentValues($argumentName, CompletionContext $context) {
  237. if ($argumentName === 'uid') {
  238. return array_map(static fn (IUser $user) => $user->getUID(), $this->userManager->search($context->getCurrentWord()));
  239. }
  240. if ($argumentName === 'app') {
  241. $userId = $context->getWordAtIndex($context->getWordIndex() - 1);
  242. $settings = $this->getUserSettings($userId, '');
  243. return array_keys($settings);
  244. }
  245. if ($argumentName === 'key') {
  246. $userId = $context->getWordAtIndex($context->getWordIndex() - 2);
  247. $app = $context->getWordAtIndex($context->getWordIndex() - 1);
  248. $settings = $this->getUserSettings($userId, $app);
  249. return array_keys($settings[$app]);
  250. }
  251. return [];
  252. }
  253. }