Setting.php 9.0 KB

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