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