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.

Setting.php 8.6KB

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