Setting.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. *
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OC\Core\Command\User;
  23. use OC\Core\Command\Base;
  24. use OCP\IConfig;
  25. use OCP\IDBConnection;
  26. use OCP\IUser;
  27. use OCP\IUserManager;
  28. use Symfony\Component\Console\Input\InputInterface;
  29. use Symfony\Component\Console\Input\InputOption;
  30. use Symfony\Component\Console\Output\OutputInterface;
  31. use Symfony\Component\Console\Input\InputArgument;
  32. class Setting extends Base {
  33. /** @var IUserManager */
  34. protected $userManager;
  35. /** @var IConfig */
  36. protected $config;
  37. /** @var IDBConnection */
  38. protected $connection;
  39. /**
  40. * @param IUserManager $userManager
  41. * @param IConfig $config
  42. * @param IDBConnection $connection
  43. */
  44. public function __construct(IUserManager $userManager, IConfig $config, IDBConnection $connection) {
  45. parent::__construct();
  46. $this->userManager = $userManager;
  47. $this->config = $config;
  48. $this->connection = $connection;
  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 exists.');
  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) {
  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' && $key === 'email') {
  161. $user = $this->userManager->get($uid);
  162. if ($user instanceof IUser) {
  163. $user->setEMailAddress($input->getArgument('value'));
  164. return 0;
  165. }
  166. }
  167. $this->config->setUserValue($uid, $app, $key, $input->getArgument('value'));
  168. return 0;
  169. } else if ($input->hasParameterOption('--delete')) {
  170. if ($input->hasParameterOption('--error-if-not-exists') && $value === null) {
  171. $output->writeln('<error>The setting does not exist for user "' . $uid . '".</error>');
  172. return 1;
  173. }
  174. if ($app === 'settings' && $key === 'email') {
  175. $user = $this->userManager->get($uid);
  176. if ($user instanceof IUser) {
  177. $user->setEMailAddress('');
  178. return 0;
  179. }
  180. }
  181. $this->config->deleteUserValue($uid, $app, $key);
  182. return 0;
  183. } else if ($value !== null) {
  184. $output->writeln($value);
  185. return 0;
  186. } else {
  187. if ($input->hasParameterOption('--default-value')) {
  188. $output->writeln($input->getOption('default-value'));
  189. return 0;
  190. } else {
  191. $output->writeln('<error>The setting does not exist for user "' . $uid . '".</error>');
  192. return 1;
  193. }
  194. }
  195. } else {
  196. $settings = $this->getUserSettings($uid, $app);
  197. $this->writeArrayInOutputFormat($input, $output, $settings);
  198. return 0;
  199. }
  200. }
  201. protected function getUserSettings($uid, $app) {
  202. $query = $this->connection->getQueryBuilder();
  203. $query->select('*')
  204. ->from('preferences')
  205. ->where($query->expr()->eq('userid', $query->createNamedParameter($uid)));
  206. if ($app !== '') {
  207. $query->andWhere($query->expr()->eq('appid', $query->createNamedParameter($app)));
  208. }
  209. $result = $query->execute();
  210. $settings = [];
  211. while ($row = $result->fetch()) {
  212. $settings[$row['appid']][$row['configkey']] = $row['configvalue'];
  213. }
  214. $result->closeCursor();
  215. return $settings;
  216. }
  217. }