|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Joas Schilling <[email protected]> |
|
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
|
|
|
|
|
23
|
|
|
namespace OC\Core\Command\User; |
|
24
|
|
|
|
|
25
|
|
|
use OC\Core\Command\Base; |
|
26
|
|
|
use OCP\IConfig; |
|
27
|
|
|
use OCP\IDBConnection; |
|
28
|
|
|
use OCP\IUser; |
|
29
|
|
|
use OCP\IUserManager; |
|
30
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
31
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
32
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
33
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
34
|
|
|
|
|
35
|
|
|
class Setting extends Base { |
|
36
|
|
|
/** @var IUserManager */ |
|
37
|
|
|
protected $userManager; |
|
38
|
|
|
|
|
39
|
|
|
/** @var IConfig */ |
|
40
|
|
|
protected $config; |
|
41
|
|
|
|
|
42
|
|
|
/** @var IDBConnection */ |
|
43
|
|
|
protected $connection; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param IUserManager $userManager |
|
47
|
|
|
* @param IConfig $config |
|
48
|
|
|
* @param IDBConnection $connection |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __construct(IUserManager $userManager, IConfig $config, IDBConnection $connection) { |
|
51
|
|
|
parent::__construct(); |
|
52
|
|
|
$this->userManager = $userManager; |
|
53
|
|
|
$this->config = $config; |
|
54
|
|
|
$this->connection = $connection; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
protected function configure() { |
|
58
|
|
|
parent::configure(); |
|
59
|
|
|
$this |
|
60
|
|
|
->setName('user:setting') |
|
61
|
|
|
->setDescription('Read and modify user settings') |
|
62
|
|
|
->addArgument( |
|
63
|
|
|
'uid', |
|
64
|
|
|
InputArgument::REQUIRED, |
|
65
|
|
|
'User ID used to login' |
|
66
|
|
|
) |
|
67
|
|
|
->addArgument( |
|
68
|
|
|
'app', |
|
69
|
|
|
InputArgument::OPTIONAL, |
|
70
|
|
|
'Restrict the settings to a given app', |
|
71
|
|
|
'' |
|
72
|
|
|
) |
|
73
|
|
|
->addArgument( |
|
74
|
|
|
'key', |
|
75
|
|
|
InputArgument::OPTIONAL, |
|
76
|
|
|
'Setting key to set, get or delete', |
|
77
|
|
|
'' |
|
78
|
|
|
) |
|
79
|
|
|
->addOption( |
|
80
|
|
|
'ignore-missing-user', |
|
81
|
|
|
null, |
|
82
|
|
|
InputOption::VALUE_NONE, |
|
83
|
|
|
'Use this option to ignore errors when the user does not exist' |
|
84
|
|
|
) |
|
85
|
|
|
|
|
86
|
|
|
// Get |
|
87
|
|
|
->addOption( |
|
88
|
|
|
'default-value', |
|
89
|
|
|
null, |
|
90
|
|
|
InputOption::VALUE_REQUIRED, |
|
91
|
|
|
'(Only applicable on get) If no default value is set and the config does not exist, the command will exit with 1' |
|
92
|
|
|
) |
|
93
|
|
|
|
|
94
|
|
|
// Set |
|
95
|
|
|
->addArgument( |
|
96
|
|
|
'value', |
|
97
|
|
|
InputArgument::OPTIONAL, |
|
98
|
|
|
'The new value of the setting', |
|
99
|
|
|
null |
|
100
|
|
|
) |
|
101
|
|
|
->addOption( |
|
102
|
|
|
'update-only', |
|
103
|
|
|
null, |
|
104
|
|
|
InputOption::VALUE_NONE, |
|
105
|
|
|
'Only updates the value, if it is not set before, it is not being added' |
|
106
|
|
|
) |
|
107
|
|
|
|
|
108
|
|
|
// Delete |
|
109
|
|
|
->addOption( |
|
110
|
|
|
'delete', |
|
111
|
|
|
null, |
|
112
|
|
|
InputOption::VALUE_NONE, |
|
113
|
|
|
'Specify this option to delete the config' |
|
114
|
|
|
) |
|
115
|
|
|
->addOption( |
|
116
|
|
|
'error-if-not-exists', |
|
117
|
|
|
null, |
|
118
|
|
|
InputOption::VALUE_NONE, |
|
119
|
|
|
'Checks whether the setting exists before deleting it' |
|
120
|
|
|
) |
|
121
|
|
|
; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
protected function checkInput(InputInterface $input) { |
|
125
|
|
|
$uid = $input->getArgument('uid'); |
|
126
|
|
|
if (!$input->getOption('ignore-missing-user') && !$this->userManager->userExists($uid)) { |
|
127
|
|
|
throw new \InvalidArgumentException('The user "' . $uid . '" does not exists.'); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
if ($input->getArgument('key') === '' && $input->hasParameterOption('--default-value')) { |
|
131
|
|
|
throw new \InvalidArgumentException('The "default-value" option can only be used when specifying a key.'); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
if ($input->getArgument('key') === '' && $input->getArgument('value') !== null) { |
|
135
|
|
|
throw new \InvalidArgumentException('The value argument can only be used when specifying a key.'); |
|
136
|
|
|
} |
|
137
|
|
|
if ($input->getArgument('value') !== null && $input->hasParameterOption('--default-value')) { |
|
138
|
|
|
throw new \InvalidArgumentException('The value argument can not be used together with "default-value".'); |
|
139
|
|
|
} |
|
140
|
|
|
if ($input->getOption('update-only') && $input->getArgument('value') === null) { |
|
141
|
|
|
throw new \InvalidArgumentException('The "update-only" option can only be used together with "value".'); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
if ($input->getArgument('key') === '' && $input->getOption('delete')) { |
|
145
|
|
|
throw new \InvalidArgumentException('The "delete" option can only be used when specifying a key.'); |
|
146
|
|
|
} |
|
147
|
|
|
if ($input->getOption('delete') && $input->hasParameterOption('--default-value')) { |
|
148
|
|
|
throw new \InvalidArgumentException('The "delete" option can not be used together with "default-value".'); |
|
149
|
|
|
} |
|
150
|
|
|
if ($input->getOption('delete') && $input->getArgument('value') !== null) { |
|
151
|
|
|
throw new \InvalidArgumentException('The "delete" option can not be used together with "value".'); |
|
152
|
|
|
} |
|
153
|
|
|
if ($input->getOption('error-if-not-exists') && !$input->getOption('delete')) { |
|
154
|
|
|
throw new \InvalidArgumentException('The "error-if-not-exists" option can only be used together with "delete".'); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) { |
|
159
|
|
|
try { |
|
160
|
|
|
$this->checkInput($input); |
|
161
|
|
|
} catch (\InvalidArgumentException $e) { |
|
162
|
|
|
$output->writeln('<error>' . $e->getMessage() . '</error>'); |
|
163
|
|
|
return 1; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
$uid = $input->getArgument('uid'); |
|
167
|
|
|
$app = $input->getArgument('app'); |
|
168
|
|
|
$key = $input->getArgument('key'); |
|
169
|
|
|
|
|
170
|
|
|
if ($key !== '') { |
|
171
|
|
|
$value = $this->config->getUserValue($uid, $app, $key, null); |
|
172
|
|
|
if ($input->getArgument('value') !== null) { |
|
173
|
|
|
if ($input->hasParameterOption('--update-only') && $value === null) { |
|
174
|
|
|
$output->writeln('<error>The setting does not exist for user "' . $uid . '".</error>'); |
|
175
|
|
|
return 1; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
View Code Duplication |
if ($app === 'settings' && $key === 'email') { |
|
|
|
|
|
|
179
|
|
|
$user = $this->userManager->get($uid); |
|
180
|
|
|
if ($user instanceof IUser) { |
|
181
|
|
|
$user->setEMailAddress($input->getArgument('value')); |
|
182
|
|
|
return 0; |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
$this->config->setUserValue($uid, $app, $key, $input->getArgument('value')); |
|
187
|
|
|
return 0; |
|
188
|
|
|
|
|
189
|
|
|
} else if ($input->hasParameterOption('--delete')) { |
|
190
|
|
|
if ($input->hasParameterOption('--error-if-not-exists') && $value === null) { |
|
191
|
|
|
$output->writeln('<error>The setting does not exist for user "' . $uid . '".</error>'); |
|
192
|
|
|
return 1; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
View Code Duplication |
if ($app === 'settings' && $key === 'email') { |
|
|
|
|
|
|
196
|
|
|
$user = $this->userManager->get($uid); |
|
197
|
|
|
if ($user instanceof IUser) { |
|
198
|
|
|
$user->setEMailAddress(''); |
|
199
|
|
|
return 0; |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
$this->config->deleteUserValue($uid, $app, $key); |
|
204
|
|
|
return 0; |
|
205
|
|
|
|
|
206
|
|
|
} else if ($value !== null) { |
|
207
|
|
|
$output->writeln($value); |
|
208
|
|
|
return 0; |
|
209
|
|
|
} else { |
|
210
|
|
|
if ($input->hasParameterOption('--default-value')) { |
|
211
|
|
|
$output->writeln($input->getOption('default-value')); |
|
212
|
|
|
return 0; |
|
213
|
|
|
} else { |
|
214
|
|
|
$output->writeln('<error>The setting does not exist for user "' . $uid . '".</error>'); |
|
215
|
|
|
return 1; |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
} else { |
|
219
|
|
|
$settings = $this->getUserSettings($uid, $app); |
|
220
|
|
|
$this->writeArrayInOutputFormat($input, $output, $settings); |
|
221
|
|
|
return 0; |
|
222
|
|
|
} |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
protected function getUserSettings($uid, $app) { |
|
226
|
|
|
$query = $this->connection->getQueryBuilder(); |
|
227
|
|
|
$query->select('*') |
|
228
|
|
|
->from('preferences') |
|
229
|
|
|
->where($query->expr()->eq('userid', $query->createNamedParameter($uid))); |
|
230
|
|
|
|
|
231
|
|
|
if ($app !== '') { |
|
232
|
|
|
$query->andWhere($query->expr()->eq('appid', $query->createNamedParameter($app))); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
$result = $query->execute(); |
|
236
|
|
|
$settings = []; |
|
237
|
|
|
while ($row = $result->fetch()) { |
|
238
|
|
|
$settings[$row['appid']][$row['configkey']] = $row['configvalue']; |
|
239
|
|
|
} |
|
240
|
|
|
$result->closeCursor(); |
|
241
|
|
|
|
|
242
|
|
|
return $settings; |
|
243
|
|
|
} |
|
244
|
|
|
} |
|
245
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.