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