@@ -29,28 +29,28 @@ |
||
29 | 29 | * Read user defined mounts from the legacy mount.json |
30 | 30 | */ |
31 | 31 | class UserLegacyStoragesService extends LegacyStoragesService { |
32 | - /** |
|
33 | - * @var IUserSession |
|
34 | - */ |
|
35 | - private $userSession; |
|
32 | + /** |
|
33 | + * @var IUserSession |
|
34 | + */ |
|
35 | + private $userSession; |
|
36 | 36 | |
37 | - /** |
|
38 | - * @param BackendService $backendService |
|
39 | - * @param IUserSession $userSession |
|
40 | - */ |
|
41 | - public function __construct(BackendService $backendService, IUserSession $userSession) { |
|
42 | - $this->backendService = $backendService; |
|
43 | - $this->userSession = $userSession; |
|
44 | - } |
|
37 | + /** |
|
38 | + * @param BackendService $backendService |
|
39 | + * @param IUserSession $userSession |
|
40 | + */ |
|
41 | + public function __construct(BackendService $backendService, IUserSession $userSession) { |
|
42 | + $this->backendService = $backendService; |
|
43 | + $this->userSession = $userSession; |
|
44 | + } |
|
45 | 45 | |
46 | - /** |
|
47 | - * Read legacy config data |
|
48 | - * |
|
49 | - * @return array list of storage configs |
|
50 | - */ |
|
51 | - protected function readLegacyConfig() { |
|
52 | - // read user config |
|
53 | - $user = $this->userSession->getUser()->getUID(); |
|
54 | - return \OC_Mount_Config::readData($user); |
|
55 | - } |
|
46 | + /** |
|
47 | + * Read legacy config data |
|
48 | + * |
|
49 | + * @return array list of storage configs |
|
50 | + */ |
|
51 | + protected function readLegacyConfig() { |
|
52 | + // read user config |
|
53 | + $user = $this->userSession->getUser()->getUID(); |
|
54 | + return \OC_Mount_Config::readData($user); |
|
55 | + } |
|
56 | 56 | } |
@@ -32,456 +32,456 @@ |
||
32 | 32 | * Stores the mount config in the database |
33 | 33 | */ |
34 | 34 | class DBConfigService { |
35 | - const MOUNT_TYPE_ADMIN = 1; |
|
36 | - const MOUNT_TYPE_PERSONAl = 2; |
|
37 | - |
|
38 | - const APPLICABLE_TYPE_GLOBAL = 1; |
|
39 | - const APPLICABLE_TYPE_GROUP = 2; |
|
40 | - const APPLICABLE_TYPE_USER = 3; |
|
41 | - |
|
42 | - /** |
|
43 | - * @var IDBConnection |
|
44 | - */ |
|
45 | - private $connection; |
|
46 | - |
|
47 | - /** |
|
48 | - * @var ICrypto |
|
49 | - */ |
|
50 | - private $crypto; |
|
51 | - |
|
52 | - /** |
|
53 | - * DBConfigService constructor. |
|
54 | - * |
|
55 | - * @param IDBConnection $connection |
|
56 | - * @param ICrypto $crypto |
|
57 | - */ |
|
58 | - public function __construct(IDBConnection $connection, ICrypto $crypto) { |
|
59 | - $this->connection = $connection; |
|
60 | - $this->crypto = $crypto; |
|
61 | - } |
|
62 | - |
|
63 | - /** |
|
64 | - * @param int $mountId |
|
65 | - * @return array |
|
66 | - */ |
|
67 | - public function getMountById($mountId) { |
|
68 | - $builder = $this->connection->getQueryBuilder(); |
|
69 | - $query = $builder->select(['mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'type']) |
|
70 | - ->from('external_mounts', 'm') |
|
71 | - ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); |
|
72 | - $mounts = $this->getMountsFromQuery($query); |
|
73 | - if (count($mounts) > 0) { |
|
74 | - return $mounts[0]; |
|
75 | - } else { |
|
76 | - return null; |
|
77 | - } |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * Get all configured mounts |
|
82 | - * |
|
83 | - * @return array |
|
84 | - */ |
|
85 | - public function getAllMounts() { |
|
86 | - $builder = $this->connection->getQueryBuilder(); |
|
87 | - $query = $builder->select(['mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'type']) |
|
88 | - ->from('external_mounts'); |
|
89 | - return $this->getMountsFromQuery($query); |
|
90 | - } |
|
91 | - |
|
92 | - public function getMountsForUser($userId, $groupIds) { |
|
93 | - $builder = $this->connection->getQueryBuilder(); |
|
94 | - $query = $builder->select(['m.mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'm.type']) |
|
95 | - ->from('external_mounts', 'm') |
|
96 | - ->innerJoin('m', 'external_applicable', 'a', $builder->expr()->eq('m.mount_id', 'a.mount_id')) |
|
97 | - ->where($builder->expr()->orX( |
|
98 | - $builder->expr()->andX( // global mounts |
|
99 | - $builder->expr()->eq('a.type', $builder->createNamedParameter(self::APPLICABLE_TYPE_GLOBAL, IQueryBuilder::PARAM_INT)), |
|
100 | - $builder->expr()->isNull('a.value') |
|
101 | - ), |
|
102 | - $builder->expr()->andX( // mounts for user |
|
103 | - $builder->expr()->eq('a.type', $builder->createNamedParameter(self::APPLICABLE_TYPE_USER, IQueryBuilder::PARAM_INT)), |
|
104 | - $builder->expr()->eq('a.value', $builder->createNamedParameter($userId)) |
|
105 | - ), |
|
106 | - $builder->expr()->andX( // mounts for group |
|
107 | - $builder->expr()->eq('a.type', $builder->createNamedParameter(self::APPLICABLE_TYPE_GROUP, IQueryBuilder::PARAM_INT)), |
|
108 | - $builder->expr()->in('a.value', $builder->createNamedParameter($groupIds, IQueryBuilder::PARAM_INT_ARRAY)) |
|
109 | - ) |
|
110 | - )); |
|
111 | - |
|
112 | - return $this->getMountsFromQuery($query); |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * Get admin defined mounts |
|
117 | - * |
|
118 | - * @return array |
|
119 | - */ |
|
120 | - public function getAdminMounts() { |
|
121 | - $builder = $this->connection->getQueryBuilder(); |
|
122 | - $query = $builder->select(['mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'type']) |
|
123 | - ->from('external_mounts') |
|
124 | - ->where($builder->expr()->eq('type', $builder->expr()->literal(self::MOUNT_TYPE_ADMIN, IQueryBuilder::PARAM_INT))); |
|
125 | - return $this->getMountsFromQuery($query); |
|
126 | - } |
|
127 | - |
|
128 | - protected function getForQuery(IQueryBuilder $builder, $type, $value) { |
|
129 | - $query = $builder->select(['m.mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'm.type']) |
|
130 | - ->from('external_mounts', 'm') |
|
131 | - ->innerJoin('m', 'external_applicable', 'a', $builder->expr()->eq('m.mount_id', 'a.mount_id')) |
|
132 | - ->where($builder->expr()->eq('a.type', $builder->createNamedParameter($type, IQueryBuilder::PARAM_INT))); |
|
133 | - |
|
134 | - if (is_null($value)) { |
|
135 | - $query = $query->andWhere($builder->expr()->isNull('a.value')); |
|
136 | - } else { |
|
137 | - $query = $query->andWhere($builder->expr()->eq('a.value', $builder->createNamedParameter($value))); |
|
138 | - } |
|
139 | - |
|
140 | - return $query; |
|
141 | - } |
|
142 | - |
|
143 | - /** |
|
144 | - * Get mounts by applicable |
|
145 | - * |
|
146 | - * @param int $type any of the self::APPLICABLE_TYPE_ constants |
|
147 | - * @param string|null $value user_id, group_id or null for global mounts |
|
148 | - * @return array |
|
149 | - */ |
|
150 | - public function getMountsFor($type, $value) { |
|
151 | - $builder = $this->connection->getQueryBuilder(); |
|
152 | - $query = $this->getForQuery($builder, $type, $value); |
|
153 | - |
|
154 | - return $this->getMountsFromQuery($query); |
|
155 | - } |
|
156 | - |
|
157 | - /** |
|
158 | - * Get admin defined mounts by applicable |
|
159 | - * |
|
160 | - * @param int $type any of the self::APPLICABLE_TYPE_ constants |
|
161 | - * @param string|null $value user_id, group_id or null for global mounts |
|
162 | - * @return array |
|
163 | - */ |
|
164 | - public function getAdminMountsFor($type, $value) { |
|
165 | - $builder = $this->connection->getQueryBuilder(); |
|
166 | - $query = $this->getForQuery($builder, $type, $value); |
|
167 | - $query->andWhere($builder->expr()->eq('m.type', $builder->expr()->literal(self::MOUNT_TYPE_ADMIN, IQueryBuilder::PARAM_INT))); |
|
168 | - |
|
169 | - return $this->getMountsFromQuery($query); |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * Get admin defined mounts for multiple applicable |
|
174 | - * |
|
175 | - * @param int $type any of the self::APPLICABLE_TYPE_ constants |
|
176 | - * @param string[] $values user_ids or group_ids |
|
177 | - * @return array |
|
178 | - */ |
|
179 | - public function getAdminMountsForMultiple($type, array $values) { |
|
180 | - $builder = $this->connection->getQueryBuilder(); |
|
181 | - $params = array_map(function ($value) use ($builder) { |
|
182 | - return $builder->createNamedParameter($value, IQueryBuilder::PARAM_STR); |
|
183 | - }, $values); |
|
184 | - |
|
185 | - $query = $builder->select(['m.mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'm.type']) |
|
186 | - ->from('external_mounts', 'm') |
|
187 | - ->innerJoin('m', 'external_applicable', 'a', $builder->expr()->eq('m.mount_id', 'a.mount_id')) |
|
188 | - ->where($builder->expr()->eq('a.type', $builder->createNamedParameter($type, IQueryBuilder::PARAM_INT))) |
|
189 | - ->andWhere($builder->expr()->in('a.value', $params)); |
|
190 | - $query->andWhere($builder->expr()->eq('m.type', $builder->expr()->literal(self::MOUNT_TYPE_ADMIN, IQueryBuilder::PARAM_INT))); |
|
191 | - |
|
192 | - return $this->getMountsFromQuery($query); |
|
193 | - } |
|
194 | - |
|
195 | - /** |
|
196 | - * Get user defined mounts by applicable |
|
197 | - * |
|
198 | - * @param int $type any of the self::APPLICABLE_TYPE_ constants |
|
199 | - * @param string|null $value user_id, group_id or null for global mounts |
|
200 | - * @return array |
|
201 | - */ |
|
202 | - public function getUserMountsFor($type, $value) { |
|
203 | - $builder = $this->connection->getQueryBuilder(); |
|
204 | - $query = $this->getForQuery($builder, $type, $value); |
|
205 | - $query->andWhere($builder->expr()->eq('m.type', $builder->expr()->literal(self::MOUNT_TYPE_PERSONAl, IQueryBuilder::PARAM_INT))); |
|
206 | - |
|
207 | - return $this->getMountsFromQuery($query); |
|
208 | - } |
|
209 | - |
|
210 | - /** |
|
211 | - * Add a mount to the database |
|
212 | - * |
|
213 | - * @param string $mountPoint |
|
214 | - * @param string $storageBackend |
|
215 | - * @param string $authBackend |
|
216 | - * @param int $priority |
|
217 | - * @param int $type self::MOUNT_TYPE_ADMIN or self::MOUNT_TYPE_PERSONAL |
|
218 | - * @return int the id of the new mount |
|
219 | - */ |
|
220 | - public function addMount($mountPoint, $storageBackend, $authBackend, $priority, $type) { |
|
221 | - if (!$priority) { |
|
222 | - $priority = 100; |
|
223 | - } |
|
224 | - $builder = $this->connection->getQueryBuilder(); |
|
225 | - $query = $builder->insert('external_mounts') |
|
226 | - ->values([ |
|
227 | - 'mount_point' => $builder->createNamedParameter($mountPoint, IQueryBuilder::PARAM_STR), |
|
228 | - 'storage_backend' => $builder->createNamedParameter($storageBackend, IQueryBuilder::PARAM_STR), |
|
229 | - 'auth_backend' => $builder->createNamedParameter($authBackend, IQueryBuilder::PARAM_STR), |
|
230 | - 'priority' => $builder->createNamedParameter($priority, IQueryBuilder::PARAM_INT), |
|
231 | - 'type' => $builder->createNamedParameter($type, IQueryBuilder::PARAM_INT) |
|
232 | - ]); |
|
233 | - $query->execute(); |
|
234 | - return (int)$this->connection->lastInsertId('*PREFIX*external_mounts'); |
|
235 | - } |
|
236 | - |
|
237 | - /** |
|
238 | - * Remove a mount from the database |
|
239 | - * |
|
240 | - * @param int $mountId |
|
241 | - */ |
|
242 | - public function removeMount($mountId) { |
|
243 | - $builder = $this->connection->getQueryBuilder(); |
|
244 | - $query = $builder->delete('external_mounts') |
|
245 | - ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); |
|
246 | - $query->execute(); |
|
247 | - |
|
248 | - $query = $builder->delete('external_applicable') |
|
249 | - ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); |
|
250 | - $query->execute(); |
|
251 | - |
|
252 | - $query = $builder->delete('external_config') |
|
253 | - ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); |
|
254 | - $query->execute(); |
|
255 | - |
|
256 | - $query = $builder->delete('external_options') |
|
257 | - ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); |
|
258 | - $query->execute(); |
|
259 | - } |
|
260 | - |
|
261 | - /** |
|
262 | - * @param int $mountId |
|
263 | - * @param string $newMountPoint |
|
264 | - */ |
|
265 | - public function setMountPoint($mountId, $newMountPoint) { |
|
266 | - $builder = $this->connection->getQueryBuilder(); |
|
267 | - |
|
268 | - $query = $builder->update('external_mounts') |
|
269 | - ->set('mount_point', $builder->createNamedParameter($newMountPoint)) |
|
270 | - ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); |
|
271 | - |
|
272 | - $query->execute(); |
|
273 | - } |
|
274 | - |
|
275 | - /** |
|
276 | - * @param int $mountId |
|
277 | - * @param string $newAuthBackend |
|
278 | - */ |
|
279 | - public function setAuthBackend($mountId, $newAuthBackend) { |
|
280 | - $builder = $this->connection->getQueryBuilder(); |
|
281 | - |
|
282 | - $query = $builder->update('external_mounts') |
|
283 | - ->set('auth_backend', $builder->createNamedParameter($newAuthBackend)) |
|
284 | - ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); |
|
285 | - |
|
286 | - $query->execute(); |
|
287 | - } |
|
288 | - |
|
289 | - /** |
|
290 | - * @param int $mountId |
|
291 | - * @param string $key |
|
292 | - * @param string $value |
|
293 | - */ |
|
294 | - public function setConfig($mountId, $key, $value) { |
|
295 | - if ($key === 'password') { |
|
296 | - $value = $this->encryptValue($value); |
|
297 | - } |
|
298 | - $count = $this->connection->insertIfNotExist('*PREFIX*external_config', [ |
|
299 | - 'mount_id' => $mountId, |
|
300 | - 'key' => $key, |
|
301 | - 'value' => $value |
|
302 | - ], ['mount_id', 'key']); |
|
303 | - if ($count === 0) { |
|
304 | - $builder = $this->connection->getQueryBuilder(); |
|
305 | - $query = $builder->update('external_config') |
|
306 | - ->set('value', $builder->createNamedParameter($value, IQueryBuilder::PARAM_STR)) |
|
307 | - ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))) |
|
308 | - ->andWhere($builder->expr()->eq('key', $builder->createNamedParameter($key, IQueryBuilder::PARAM_STR))); |
|
309 | - $query->execute(); |
|
310 | - } |
|
311 | - } |
|
312 | - |
|
313 | - /** |
|
314 | - * @param int $mountId |
|
315 | - * @param string $key |
|
316 | - * @param string $value |
|
317 | - */ |
|
318 | - public function setOption($mountId, $key, $value) { |
|
319 | - |
|
320 | - $count = $this->connection->insertIfNotExist('*PREFIX*external_options', [ |
|
321 | - 'mount_id' => $mountId, |
|
322 | - 'key' => $key, |
|
323 | - 'value' => json_encode($value) |
|
324 | - ], ['mount_id', 'key']); |
|
325 | - if ($count === 0) { |
|
326 | - $builder = $this->connection->getQueryBuilder(); |
|
327 | - $query = $builder->update('external_options') |
|
328 | - ->set('value', $builder->createNamedParameter(json_encode($value), IQueryBuilder::PARAM_STR)) |
|
329 | - ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))) |
|
330 | - ->andWhere($builder->expr()->eq('key', $builder->createNamedParameter($key, IQueryBuilder::PARAM_STR))); |
|
331 | - $query->execute(); |
|
332 | - } |
|
333 | - } |
|
334 | - |
|
335 | - public function addApplicable($mountId, $type, $value) { |
|
336 | - $this->connection->insertIfNotExist('*PREFIX*external_applicable', [ |
|
337 | - 'mount_id' => $mountId, |
|
338 | - 'type' => $type, |
|
339 | - 'value' => $value |
|
340 | - ], ['mount_id', 'type', 'value']); |
|
341 | - } |
|
342 | - |
|
343 | - public function removeApplicable($mountId, $type, $value) { |
|
344 | - $builder = $this->connection->getQueryBuilder(); |
|
345 | - $query = $builder->delete('external_applicable') |
|
346 | - ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))) |
|
347 | - ->andWhere($builder->expr()->eq('type', $builder->createNamedParameter($type, IQueryBuilder::PARAM_INT))); |
|
348 | - |
|
349 | - if (is_null($value)) { |
|
350 | - $query = $query->andWhere($builder->expr()->isNull('value')); |
|
351 | - } else { |
|
352 | - $query = $query->andWhere($builder->expr()->eq('value', $builder->createNamedParameter($value, IQueryBuilder::PARAM_STR))); |
|
353 | - } |
|
354 | - |
|
355 | - $query->execute(); |
|
356 | - } |
|
357 | - |
|
358 | - private function getMountsFromQuery(IQueryBuilder $query) { |
|
359 | - $result = $query->execute(); |
|
360 | - $mounts = $result->fetchAll(); |
|
361 | - $uniqueMounts = []; |
|
362 | - foreach ($mounts as $mount) { |
|
363 | - $id = $mount['mount_id']; |
|
364 | - if (!isset($uniqueMounts[$id])) { |
|
365 | - $uniqueMounts[$id] = $mount; |
|
366 | - } |
|
367 | - } |
|
368 | - $uniqueMounts = array_values($uniqueMounts); |
|
369 | - |
|
370 | - $mountIds = array_map(function ($mount) { |
|
371 | - return $mount['mount_id']; |
|
372 | - }, $uniqueMounts); |
|
373 | - $mountIds = array_values(array_unique($mountIds)); |
|
374 | - |
|
375 | - $applicable = $this->getApplicableForMounts($mountIds); |
|
376 | - $config = $this->getConfigForMounts($mountIds); |
|
377 | - $options = $this->getOptionsForMounts($mountIds); |
|
378 | - |
|
379 | - return array_map(function ($mount, $applicable, $config, $options) { |
|
380 | - $mount['type'] = (int)$mount['type']; |
|
381 | - $mount['priority'] = (int)$mount['priority']; |
|
382 | - $mount['applicable'] = $applicable; |
|
383 | - $mount['config'] = $config; |
|
384 | - $mount['options'] = $options; |
|
385 | - return $mount; |
|
386 | - }, $uniqueMounts, $applicable, $config, $options); |
|
387 | - } |
|
388 | - |
|
389 | - /** |
|
390 | - * Get mount options from a table grouped by mount id |
|
391 | - * |
|
392 | - * @param string $table |
|
393 | - * @param string[] $fields |
|
394 | - * @param int[] $mountIds |
|
395 | - * @return array [$mountId => [['field1' => $value1, ...], ...], ...] |
|
396 | - */ |
|
397 | - private function selectForMounts($table, array $fields, array $mountIds) { |
|
398 | - if (count($mountIds) === 0) { |
|
399 | - return []; |
|
400 | - } |
|
401 | - $builder = $this->connection->getQueryBuilder(); |
|
402 | - $fields[] = 'mount_id'; |
|
403 | - $placeHolders = array_map(function ($id) use ($builder) { |
|
404 | - return $builder->createPositionalParameter($id, IQueryBuilder::PARAM_INT); |
|
405 | - }, $mountIds); |
|
406 | - $query = $builder->select($fields) |
|
407 | - ->from($table) |
|
408 | - ->where($builder->expr()->in('mount_id', $placeHolders)); |
|
409 | - $rows = $query->execute()->fetchAll(); |
|
410 | - |
|
411 | - $result = []; |
|
412 | - foreach ($mountIds as $mountId) { |
|
413 | - $result[$mountId] = []; |
|
414 | - } |
|
415 | - foreach ($rows as $row) { |
|
416 | - if (isset($row['type'])) { |
|
417 | - $row['type'] = (int)$row['type']; |
|
418 | - } |
|
419 | - $result[$row['mount_id']][] = $row; |
|
420 | - } |
|
421 | - return $result; |
|
422 | - } |
|
423 | - |
|
424 | - /** |
|
425 | - * @param int[] $mountIds |
|
426 | - * @return array [$id => [['type' => $type, 'value' => $value], ...], ...] |
|
427 | - */ |
|
428 | - public function getApplicableForMounts($mountIds) { |
|
429 | - return $this->selectForMounts('external_applicable', ['type', 'value'], $mountIds); |
|
430 | - } |
|
431 | - |
|
432 | - /** |
|
433 | - * @param int[] $mountIds |
|
434 | - * @return array [$id => ['key1' => $value1, ...], ...] |
|
435 | - */ |
|
436 | - public function getConfigForMounts($mountIds) { |
|
437 | - $mountConfigs = $this->selectForMounts('external_config', ['key', 'value'], $mountIds); |
|
438 | - return array_map([$this, 'createKeyValueMap'], $mountConfigs); |
|
439 | - } |
|
440 | - |
|
441 | - /** |
|
442 | - * @param int[] $mountIds |
|
443 | - * @return array [$id => ['key1' => $value1, ...], ...] |
|
444 | - */ |
|
445 | - public function getOptionsForMounts($mountIds) { |
|
446 | - $mountOptions = $this->selectForMounts('external_options', ['key', 'value'], $mountIds); |
|
447 | - $optionsMap = array_map([$this, 'createKeyValueMap'], $mountOptions); |
|
448 | - return array_map(function (array $options) { |
|
449 | - return array_map(function ($option) { |
|
450 | - return json_decode($option); |
|
451 | - }, $options); |
|
452 | - }, $optionsMap); |
|
453 | - } |
|
454 | - |
|
455 | - /** |
|
456 | - * @param array $keyValuePairs [['key'=>$key, 'value=>$value], ...] |
|
457 | - * @return array ['key1' => $value1, ...] |
|
458 | - */ |
|
459 | - private function createKeyValueMap(array $keyValuePairs) { |
|
460 | - $decryptedPairts = array_map(function ($pair) { |
|
461 | - if ($pair['key'] === 'password') { |
|
462 | - $pair['value'] = $this->decryptValue($pair['value']); |
|
463 | - } |
|
464 | - return $pair; |
|
465 | - }, $keyValuePairs); |
|
466 | - $keys = array_map(function ($pair) { |
|
467 | - return $pair['key']; |
|
468 | - }, $decryptedPairts); |
|
469 | - $values = array_map(function ($pair) { |
|
470 | - return $pair['value']; |
|
471 | - }, $decryptedPairts); |
|
472 | - |
|
473 | - return array_combine($keys, $values); |
|
474 | - } |
|
475 | - |
|
476 | - private function encryptValue($value) { |
|
477 | - return $this->crypto->encrypt($value); |
|
478 | - } |
|
479 | - |
|
480 | - private function decryptValue($value) { |
|
481 | - try { |
|
482 | - return $this->crypto->decrypt($value); |
|
483 | - } catch (\Exception $e) { |
|
484 | - return $value; |
|
485 | - } |
|
486 | - } |
|
35 | + const MOUNT_TYPE_ADMIN = 1; |
|
36 | + const MOUNT_TYPE_PERSONAl = 2; |
|
37 | + |
|
38 | + const APPLICABLE_TYPE_GLOBAL = 1; |
|
39 | + const APPLICABLE_TYPE_GROUP = 2; |
|
40 | + const APPLICABLE_TYPE_USER = 3; |
|
41 | + |
|
42 | + /** |
|
43 | + * @var IDBConnection |
|
44 | + */ |
|
45 | + private $connection; |
|
46 | + |
|
47 | + /** |
|
48 | + * @var ICrypto |
|
49 | + */ |
|
50 | + private $crypto; |
|
51 | + |
|
52 | + /** |
|
53 | + * DBConfigService constructor. |
|
54 | + * |
|
55 | + * @param IDBConnection $connection |
|
56 | + * @param ICrypto $crypto |
|
57 | + */ |
|
58 | + public function __construct(IDBConnection $connection, ICrypto $crypto) { |
|
59 | + $this->connection = $connection; |
|
60 | + $this->crypto = $crypto; |
|
61 | + } |
|
62 | + |
|
63 | + /** |
|
64 | + * @param int $mountId |
|
65 | + * @return array |
|
66 | + */ |
|
67 | + public function getMountById($mountId) { |
|
68 | + $builder = $this->connection->getQueryBuilder(); |
|
69 | + $query = $builder->select(['mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'type']) |
|
70 | + ->from('external_mounts', 'm') |
|
71 | + ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); |
|
72 | + $mounts = $this->getMountsFromQuery($query); |
|
73 | + if (count($mounts) > 0) { |
|
74 | + return $mounts[0]; |
|
75 | + } else { |
|
76 | + return null; |
|
77 | + } |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * Get all configured mounts |
|
82 | + * |
|
83 | + * @return array |
|
84 | + */ |
|
85 | + public function getAllMounts() { |
|
86 | + $builder = $this->connection->getQueryBuilder(); |
|
87 | + $query = $builder->select(['mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'type']) |
|
88 | + ->from('external_mounts'); |
|
89 | + return $this->getMountsFromQuery($query); |
|
90 | + } |
|
91 | + |
|
92 | + public function getMountsForUser($userId, $groupIds) { |
|
93 | + $builder = $this->connection->getQueryBuilder(); |
|
94 | + $query = $builder->select(['m.mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'm.type']) |
|
95 | + ->from('external_mounts', 'm') |
|
96 | + ->innerJoin('m', 'external_applicable', 'a', $builder->expr()->eq('m.mount_id', 'a.mount_id')) |
|
97 | + ->where($builder->expr()->orX( |
|
98 | + $builder->expr()->andX( // global mounts |
|
99 | + $builder->expr()->eq('a.type', $builder->createNamedParameter(self::APPLICABLE_TYPE_GLOBAL, IQueryBuilder::PARAM_INT)), |
|
100 | + $builder->expr()->isNull('a.value') |
|
101 | + ), |
|
102 | + $builder->expr()->andX( // mounts for user |
|
103 | + $builder->expr()->eq('a.type', $builder->createNamedParameter(self::APPLICABLE_TYPE_USER, IQueryBuilder::PARAM_INT)), |
|
104 | + $builder->expr()->eq('a.value', $builder->createNamedParameter($userId)) |
|
105 | + ), |
|
106 | + $builder->expr()->andX( // mounts for group |
|
107 | + $builder->expr()->eq('a.type', $builder->createNamedParameter(self::APPLICABLE_TYPE_GROUP, IQueryBuilder::PARAM_INT)), |
|
108 | + $builder->expr()->in('a.value', $builder->createNamedParameter($groupIds, IQueryBuilder::PARAM_INT_ARRAY)) |
|
109 | + ) |
|
110 | + )); |
|
111 | + |
|
112 | + return $this->getMountsFromQuery($query); |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * Get admin defined mounts |
|
117 | + * |
|
118 | + * @return array |
|
119 | + */ |
|
120 | + public function getAdminMounts() { |
|
121 | + $builder = $this->connection->getQueryBuilder(); |
|
122 | + $query = $builder->select(['mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'type']) |
|
123 | + ->from('external_mounts') |
|
124 | + ->where($builder->expr()->eq('type', $builder->expr()->literal(self::MOUNT_TYPE_ADMIN, IQueryBuilder::PARAM_INT))); |
|
125 | + return $this->getMountsFromQuery($query); |
|
126 | + } |
|
127 | + |
|
128 | + protected function getForQuery(IQueryBuilder $builder, $type, $value) { |
|
129 | + $query = $builder->select(['m.mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'm.type']) |
|
130 | + ->from('external_mounts', 'm') |
|
131 | + ->innerJoin('m', 'external_applicable', 'a', $builder->expr()->eq('m.mount_id', 'a.mount_id')) |
|
132 | + ->where($builder->expr()->eq('a.type', $builder->createNamedParameter($type, IQueryBuilder::PARAM_INT))); |
|
133 | + |
|
134 | + if (is_null($value)) { |
|
135 | + $query = $query->andWhere($builder->expr()->isNull('a.value')); |
|
136 | + } else { |
|
137 | + $query = $query->andWhere($builder->expr()->eq('a.value', $builder->createNamedParameter($value))); |
|
138 | + } |
|
139 | + |
|
140 | + return $query; |
|
141 | + } |
|
142 | + |
|
143 | + /** |
|
144 | + * Get mounts by applicable |
|
145 | + * |
|
146 | + * @param int $type any of the self::APPLICABLE_TYPE_ constants |
|
147 | + * @param string|null $value user_id, group_id or null for global mounts |
|
148 | + * @return array |
|
149 | + */ |
|
150 | + public function getMountsFor($type, $value) { |
|
151 | + $builder = $this->connection->getQueryBuilder(); |
|
152 | + $query = $this->getForQuery($builder, $type, $value); |
|
153 | + |
|
154 | + return $this->getMountsFromQuery($query); |
|
155 | + } |
|
156 | + |
|
157 | + /** |
|
158 | + * Get admin defined mounts by applicable |
|
159 | + * |
|
160 | + * @param int $type any of the self::APPLICABLE_TYPE_ constants |
|
161 | + * @param string|null $value user_id, group_id or null for global mounts |
|
162 | + * @return array |
|
163 | + */ |
|
164 | + public function getAdminMountsFor($type, $value) { |
|
165 | + $builder = $this->connection->getQueryBuilder(); |
|
166 | + $query = $this->getForQuery($builder, $type, $value); |
|
167 | + $query->andWhere($builder->expr()->eq('m.type', $builder->expr()->literal(self::MOUNT_TYPE_ADMIN, IQueryBuilder::PARAM_INT))); |
|
168 | + |
|
169 | + return $this->getMountsFromQuery($query); |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * Get admin defined mounts for multiple applicable |
|
174 | + * |
|
175 | + * @param int $type any of the self::APPLICABLE_TYPE_ constants |
|
176 | + * @param string[] $values user_ids or group_ids |
|
177 | + * @return array |
|
178 | + */ |
|
179 | + public function getAdminMountsForMultiple($type, array $values) { |
|
180 | + $builder = $this->connection->getQueryBuilder(); |
|
181 | + $params = array_map(function ($value) use ($builder) { |
|
182 | + return $builder->createNamedParameter($value, IQueryBuilder::PARAM_STR); |
|
183 | + }, $values); |
|
184 | + |
|
185 | + $query = $builder->select(['m.mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'm.type']) |
|
186 | + ->from('external_mounts', 'm') |
|
187 | + ->innerJoin('m', 'external_applicable', 'a', $builder->expr()->eq('m.mount_id', 'a.mount_id')) |
|
188 | + ->where($builder->expr()->eq('a.type', $builder->createNamedParameter($type, IQueryBuilder::PARAM_INT))) |
|
189 | + ->andWhere($builder->expr()->in('a.value', $params)); |
|
190 | + $query->andWhere($builder->expr()->eq('m.type', $builder->expr()->literal(self::MOUNT_TYPE_ADMIN, IQueryBuilder::PARAM_INT))); |
|
191 | + |
|
192 | + return $this->getMountsFromQuery($query); |
|
193 | + } |
|
194 | + |
|
195 | + /** |
|
196 | + * Get user defined mounts by applicable |
|
197 | + * |
|
198 | + * @param int $type any of the self::APPLICABLE_TYPE_ constants |
|
199 | + * @param string|null $value user_id, group_id or null for global mounts |
|
200 | + * @return array |
|
201 | + */ |
|
202 | + public function getUserMountsFor($type, $value) { |
|
203 | + $builder = $this->connection->getQueryBuilder(); |
|
204 | + $query = $this->getForQuery($builder, $type, $value); |
|
205 | + $query->andWhere($builder->expr()->eq('m.type', $builder->expr()->literal(self::MOUNT_TYPE_PERSONAl, IQueryBuilder::PARAM_INT))); |
|
206 | + |
|
207 | + return $this->getMountsFromQuery($query); |
|
208 | + } |
|
209 | + |
|
210 | + /** |
|
211 | + * Add a mount to the database |
|
212 | + * |
|
213 | + * @param string $mountPoint |
|
214 | + * @param string $storageBackend |
|
215 | + * @param string $authBackend |
|
216 | + * @param int $priority |
|
217 | + * @param int $type self::MOUNT_TYPE_ADMIN or self::MOUNT_TYPE_PERSONAL |
|
218 | + * @return int the id of the new mount |
|
219 | + */ |
|
220 | + public function addMount($mountPoint, $storageBackend, $authBackend, $priority, $type) { |
|
221 | + if (!$priority) { |
|
222 | + $priority = 100; |
|
223 | + } |
|
224 | + $builder = $this->connection->getQueryBuilder(); |
|
225 | + $query = $builder->insert('external_mounts') |
|
226 | + ->values([ |
|
227 | + 'mount_point' => $builder->createNamedParameter($mountPoint, IQueryBuilder::PARAM_STR), |
|
228 | + 'storage_backend' => $builder->createNamedParameter($storageBackend, IQueryBuilder::PARAM_STR), |
|
229 | + 'auth_backend' => $builder->createNamedParameter($authBackend, IQueryBuilder::PARAM_STR), |
|
230 | + 'priority' => $builder->createNamedParameter($priority, IQueryBuilder::PARAM_INT), |
|
231 | + 'type' => $builder->createNamedParameter($type, IQueryBuilder::PARAM_INT) |
|
232 | + ]); |
|
233 | + $query->execute(); |
|
234 | + return (int)$this->connection->lastInsertId('*PREFIX*external_mounts'); |
|
235 | + } |
|
236 | + |
|
237 | + /** |
|
238 | + * Remove a mount from the database |
|
239 | + * |
|
240 | + * @param int $mountId |
|
241 | + */ |
|
242 | + public function removeMount($mountId) { |
|
243 | + $builder = $this->connection->getQueryBuilder(); |
|
244 | + $query = $builder->delete('external_mounts') |
|
245 | + ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); |
|
246 | + $query->execute(); |
|
247 | + |
|
248 | + $query = $builder->delete('external_applicable') |
|
249 | + ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); |
|
250 | + $query->execute(); |
|
251 | + |
|
252 | + $query = $builder->delete('external_config') |
|
253 | + ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); |
|
254 | + $query->execute(); |
|
255 | + |
|
256 | + $query = $builder->delete('external_options') |
|
257 | + ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); |
|
258 | + $query->execute(); |
|
259 | + } |
|
260 | + |
|
261 | + /** |
|
262 | + * @param int $mountId |
|
263 | + * @param string $newMountPoint |
|
264 | + */ |
|
265 | + public function setMountPoint($mountId, $newMountPoint) { |
|
266 | + $builder = $this->connection->getQueryBuilder(); |
|
267 | + |
|
268 | + $query = $builder->update('external_mounts') |
|
269 | + ->set('mount_point', $builder->createNamedParameter($newMountPoint)) |
|
270 | + ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); |
|
271 | + |
|
272 | + $query->execute(); |
|
273 | + } |
|
274 | + |
|
275 | + /** |
|
276 | + * @param int $mountId |
|
277 | + * @param string $newAuthBackend |
|
278 | + */ |
|
279 | + public function setAuthBackend($mountId, $newAuthBackend) { |
|
280 | + $builder = $this->connection->getQueryBuilder(); |
|
281 | + |
|
282 | + $query = $builder->update('external_mounts') |
|
283 | + ->set('auth_backend', $builder->createNamedParameter($newAuthBackend)) |
|
284 | + ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); |
|
285 | + |
|
286 | + $query->execute(); |
|
287 | + } |
|
288 | + |
|
289 | + /** |
|
290 | + * @param int $mountId |
|
291 | + * @param string $key |
|
292 | + * @param string $value |
|
293 | + */ |
|
294 | + public function setConfig($mountId, $key, $value) { |
|
295 | + if ($key === 'password') { |
|
296 | + $value = $this->encryptValue($value); |
|
297 | + } |
|
298 | + $count = $this->connection->insertIfNotExist('*PREFIX*external_config', [ |
|
299 | + 'mount_id' => $mountId, |
|
300 | + 'key' => $key, |
|
301 | + 'value' => $value |
|
302 | + ], ['mount_id', 'key']); |
|
303 | + if ($count === 0) { |
|
304 | + $builder = $this->connection->getQueryBuilder(); |
|
305 | + $query = $builder->update('external_config') |
|
306 | + ->set('value', $builder->createNamedParameter($value, IQueryBuilder::PARAM_STR)) |
|
307 | + ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))) |
|
308 | + ->andWhere($builder->expr()->eq('key', $builder->createNamedParameter($key, IQueryBuilder::PARAM_STR))); |
|
309 | + $query->execute(); |
|
310 | + } |
|
311 | + } |
|
312 | + |
|
313 | + /** |
|
314 | + * @param int $mountId |
|
315 | + * @param string $key |
|
316 | + * @param string $value |
|
317 | + */ |
|
318 | + public function setOption($mountId, $key, $value) { |
|
319 | + |
|
320 | + $count = $this->connection->insertIfNotExist('*PREFIX*external_options', [ |
|
321 | + 'mount_id' => $mountId, |
|
322 | + 'key' => $key, |
|
323 | + 'value' => json_encode($value) |
|
324 | + ], ['mount_id', 'key']); |
|
325 | + if ($count === 0) { |
|
326 | + $builder = $this->connection->getQueryBuilder(); |
|
327 | + $query = $builder->update('external_options') |
|
328 | + ->set('value', $builder->createNamedParameter(json_encode($value), IQueryBuilder::PARAM_STR)) |
|
329 | + ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))) |
|
330 | + ->andWhere($builder->expr()->eq('key', $builder->createNamedParameter($key, IQueryBuilder::PARAM_STR))); |
|
331 | + $query->execute(); |
|
332 | + } |
|
333 | + } |
|
334 | + |
|
335 | + public function addApplicable($mountId, $type, $value) { |
|
336 | + $this->connection->insertIfNotExist('*PREFIX*external_applicable', [ |
|
337 | + 'mount_id' => $mountId, |
|
338 | + 'type' => $type, |
|
339 | + 'value' => $value |
|
340 | + ], ['mount_id', 'type', 'value']); |
|
341 | + } |
|
342 | + |
|
343 | + public function removeApplicable($mountId, $type, $value) { |
|
344 | + $builder = $this->connection->getQueryBuilder(); |
|
345 | + $query = $builder->delete('external_applicable') |
|
346 | + ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))) |
|
347 | + ->andWhere($builder->expr()->eq('type', $builder->createNamedParameter($type, IQueryBuilder::PARAM_INT))); |
|
348 | + |
|
349 | + if (is_null($value)) { |
|
350 | + $query = $query->andWhere($builder->expr()->isNull('value')); |
|
351 | + } else { |
|
352 | + $query = $query->andWhere($builder->expr()->eq('value', $builder->createNamedParameter($value, IQueryBuilder::PARAM_STR))); |
|
353 | + } |
|
354 | + |
|
355 | + $query->execute(); |
|
356 | + } |
|
357 | + |
|
358 | + private function getMountsFromQuery(IQueryBuilder $query) { |
|
359 | + $result = $query->execute(); |
|
360 | + $mounts = $result->fetchAll(); |
|
361 | + $uniqueMounts = []; |
|
362 | + foreach ($mounts as $mount) { |
|
363 | + $id = $mount['mount_id']; |
|
364 | + if (!isset($uniqueMounts[$id])) { |
|
365 | + $uniqueMounts[$id] = $mount; |
|
366 | + } |
|
367 | + } |
|
368 | + $uniqueMounts = array_values($uniqueMounts); |
|
369 | + |
|
370 | + $mountIds = array_map(function ($mount) { |
|
371 | + return $mount['mount_id']; |
|
372 | + }, $uniqueMounts); |
|
373 | + $mountIds = array_values(array_unique($mountIds)); |
|
374 | + |
|
375 | + $applicable = $this->getApplicableForMounts($mountIds); |
|
376 | + $config = $this->getConfigForMounts($mountIds); |
|
377 | + $options = $this->getOptionsForMounts($mountIds); |
|
378 | + |
|
379 | + return array_map(function ($mount, $applicable, $config, $options) { |
|
380 | + $mount['type'] = (int)$mount['type']; |
|
381 | + $mount['priority'] = (int)$mount['priority']; |
|
382 | + $mount['applicable'] = $applicable; |
|
383 | + $mount['config'] = $config; |
|
384 | + $mount['options'] = $options; |
|
385 | + return $mount; |
|
386 | + }, $uniqueMounts, $applicable, $config, $options); |
|
387 | + } |
|
388 | + |
|
389 | + /** |
|
390 | + * Get mount options from a table grouped by mount id |
|
391 | + * |
|
392 | + * @param string $table |
|
393 | + * @param string[] $fields |
|
394 | + * @param int[] $mountIds |
|
395 | + * @return array [$mountId => [['field1' => $value1, ...], ...], ...] |
|
396 | + */ |
|
397 | + private function selectForMounts($table, array $fields, array $mountIds) { |
|
398 | + if (count($mountIds) === 0) { |
|
399 | + return []; |
|
400 | + } |
|
401 | + $builder = $this->connection->getQueryBuilder(); |
|
402 | + $fields[] = 'mount_id'; |
|
403 | + $placeHolders = array_map(function ($id) use ($builder) { |
|
404 | + return $builder->createPositionalParameter($id, IQueryBuilder::PARAM_INT); |
|
405 | + }, $mountIds); |
|
406 | + $query = $builder->select($fields) |
|
407 | + ->from($table) |
|
408 | + ->where($builder->expr()->in('mount_id', $placeHolders)); |
|
409 | + $rows = $query->execute()->fetchAll(); |
|
410 | + |
|
411 | + $result = []; |
|
412 | + foreach ($mountIds as $mountId) { |
|
413 | + $result[$mountId] = []; |
|
414 | + } |
|
415 | + foreach ($rows as $row) { |
|
416 | + if (isset($row['type'])) { |
|
417 | + $row['type'] = (int)$row['type']; |
|
418 | + } |
|
419 | + $result[$row['mount_id']][] = $row; |
|
420 | + } |
|
421 | + return $result; |
|
422 | + } |
|
423 | + |
|
424 | + /** |
|
425 | + * @param int[] $mountIds |
|
426 | + * @return array [$id => [['type' => $type, 'value' => $value], ...], ...] |
|
427 | + */ |
|
428 | + public function getApplicableForMounts($mountIds) { |
|
429 | + return $this->selectForMounts('external_applicable', ['type', 'value'], $mountIds); |
|
430 | + } |
|
431 | + |
|
432 | + /** |
|
433 | + * @param int[] $mountIds |
|
434 | + * @return array [$id => ['key1' => $value1, ...], ...] |
|
435 | + */ |
|
436 | + public function getConfigForMounts($mountIds) { |
|
437 | + $mountConfigs = $this->selectForMounts('external_config', ['key', 'value'], $mountIds); |
|
438 | + return array_map([$this, 'createKeyValueMap'], $mountConfigs); |
|
439 | + } |
|
440 | + |
|
441 | + /** |
|
442 | + * @param int[] $mountIds |
|
443 | + * @return array [$id => ['key1' => $value1, ...], ...] |
|
444 | + */ |
|
445 | + public function getOptionsForMounts($mountIds) { |
|
446 | + $mountOptions = $this->selectForMounts('external_options', ['key', 'value'], $mountIds); |
|
447 | + $optionsMap = array_map([$this, 'createKeyValueMap'], $mountOptions); |
|
448 | + return array_map(function (array $options) { |
|
449 | + return array_map(function ($option) { |
|
450 | + return json_decode($option); |
|
451 | + }, $options); |
|
452 | + }, $optionsMap); |
|
453 | + } |
|
454 | + |
|
455 | + /** |
|
456 | + * @param array $keyValuePairs [['key'=>$key, 'value=>$value], ...] |
|
457 | + * @return array ['key1' => $value1, ...] |
|
458 | + */ |
|
459 | + private function createKeyValueMap(array $keyValuePairs) { |
|
460 | + $decryptedPairts = array_map(function ($pair) { |
|
461 | + if ($pair['key'] === 'password') { |
|
462 | + $pair['value'] = $this->decryptValue($pair['value']); |
|
463 | + } |
|
464 | + return $pair; |
|
465 | + }, $keyValuePairs); |
|
466 | + $keys = array_map(function ($pair) { |
|
467 | + return $pair['key']; |
|
468 | + }, $decryptedPairts); |
|
469 | + $values = array_map(function ($pair) { |
|
470 | + return $pair['value']; |
|
471 | + }, $decryptedPairts); |
|
472 | + |
|
473 | + return array_combine($keys, $values); |
|
474 | + } |
|
475 | + |
|
476 | + private function encryptValue($value) { |
|
477 | + return $this->crypto->encrypt($value); |
|
478 | + } |
|
479 | + |
|
480 | + private function decryptValue($value) { |
|
481 | + try { |
|
482 | + return $this->crypto->decrypt($value); |
|
483 | + } catch (\Exception $e) { |
|
484 | + return $value; |
|
485 | + } |
|
486 | + } |
|
487 | 487 | } |
@@ -30,183 +30,183 @@ |
||
30 | 30 | * Read mount config from legacy mount.json |
31 | 31 | */ |
32 | 32 | abstract class LegacyStoragesService { |
33 | - /** @var BackendService */ |
|
34 | - protected $backendService; |
|
33 | + /** @var BackendService */ |
|
34 | + protected $backendService; |
|
35 | 35 | |
36 | - /** |
|
37 | - * Read legacy config data |
|
38 | - * |
|
39 | - * @return array list of mount configs |
|
40 | - */ |
|
41 | - abstract protected function readLegacyConfig(); |
|
36 | + /** |
|
37 | + * Read legacy config data |
|
38 | + * |
|
39 | + * @return array list of mount configs |
|
40 | + */ |
|
41 | + abstract protected function readLegacyConfig(); |
|
42 | 42 | |
43 | - /** |
|
44 | - * Copy legacy storage options into the given storage config object. |
|
45 | - * |
|
46 | - * @param StorageConfig $storageConfig storage config to populate |
|
47 | - * @param string $mountType mount type |
|
48 | - * @param string $applicable applicable user or group |
|
49 | - * @param array $storageOptions legacy storage options |
|
50 | - * |
|
51 | - * @return StorageConfig populated storage config |
|
52 | - */ |
|
53 | - protected function populateStorageConfigWithLegacyOptions( |
|
54 | - &$storageConfig, |
|
55 | - $mountType, |
|
56 | - $applicable, |
|
57 | - $storageOptions |
|
58 | - ) { |
|
59 | - $backend = $this->backendService->getBackend($storageOptions['backend']); |
|
60 | - if (!$backend) { |
|
61 | - throw new \UnexpectedValueException('Invalid backend ' . $storageOptions['backend']); |
|
62 | - } |
|
63 | - $storageConfig->setBackend($backend); |
|
64 | - if (isset($storageOptions['authMechanism']) && $storageOptions['authMechanism'] !== 'builtin::builtin') { |
|
65 | - $authMechanism = $this->backendService->getAuthMechanism($storageOptions['authMechanism']); |
|
66 | - } else { |
|
67 | - $authMechanism = $backend->getLegacyAuthMechanism($storageOptions); |
|
68 | - $storageOptions['authMechanism'] = 'null'; // to make error handling easier |
|
69 | - } |
|
70 | - if (!$authMechanism) { |
|
71 | - throw new \UnexpectedValueException('Invalid authentication mechanism ' . $storageOptions['authMechanism']); |
|
72 | - } |
|
73 | - $storageConfig->setAuthMechanism($authMechanism); |
|
74 | - $storageConfig->setBackendOptions($storageOptions['options']); |
|
75 | - if (isset($storageOptions['mountOptions'])) { |
|
76 | - $storageConfig->setMountOptions($storageOptions['mountOptions']); |
|
77 | - } |
|
78 | - if (!isset($storageOptions['priority'])) { |
|
79 | - $storageOptions['priority'] = $backend->getPriority(); |
|
80 | - } |
|
81 | - $storageConfig->setPriority($storageOptions['priority']); |
|
82 | - if ($mountType === \OC_Mount_Config::MOUNT_TYPE_USER) { |
|
83 | - $applicableUsers = $storageConfig->getApplicableUsers(); |
|
84 | - if ($applicable !== 'all') { |
|
85 | - $applicableUsers[] = $applicable; |
|
86 | - $storageConfig->setApplicableUsers($applicableUsers); |
|
87 | - } |
|
88 | - } else if ($mountType === \OC_Mount_Config::MOUNT_TYPE_GROUP) { |
|
89 | - $applicableGroups = $storageConfig->getApplicableGroups(); |
|
90 | - $applicableGroups[] = $applicable; |
|
91 | - $storageConfig->setApplicableGroups($applicableGroups); |
|
92 | - } |
|
93 | - return $storageConfig; |
|
94 | - } |
|
43 | + /** |
|
44 | + * Copy legacy storage options into the given storage config object. |
|
45 | + * |
|
46 | + * @param StorageConfig $storageConfig storage config to populate |
|
47 | + * @param string $mountType mount type |
|
48 | + * @param string $applicable applicable user or group |
|
49 | + * @param array $storageOptions legacy storage options |
|
50 | + * |
|
51 | + * @return StorageConfig populated storage config |
|
52 | + */ |
|
53 | + protected function populateStorageConfigWithLegacyOptions( |
|
54 | + &$storageConfig, |
|
55 | + $mountType, |
|
56 | + $applicable, |
|
57 | + $storageOptions |
|
58 | + ) { |
|
59 | + $backend = $this->backendService->getBackend($storageOptions['backend']); |
|
60 | + if (!$backend) { |
|
61 | + throw new \UnexpectedValueException('Invalid backend ' . $storageOptions['backend']); |
|
62 | + } |
|
63 | + $storageConfig->setBackend($backend); |
|
64 | + if (isset($storageOptions['authMechanism']) && $storageOptions['authMechanism'] !== 'builtin::builtin') { |
|
65 | + $authMechanism = $this->backendService->getAuthMechanism($storageOptions['authMechanism']); |
|
66 | + } else { |
|
67 | + $authMechanism = $backend->getLegacyAuthMechanism($storageOptions); |
|
68 | + $storageOptions['authMechanism'] = 'null'; // to make error handling easier |
|
69 | + } |
|
70 | + if (!$authMechanism) { |
|
71 | + throw new \UnexpectedValueException('Invalid authentication mechanism ' . $storageOptions['authMechanism']); |
|
72 | + } |
|
73 | + $storageConfig->setAuthMechanism($authMechanism); |
|
74 | + $storageConfig->setBackendOptions($storageOptions['options']); |
|
75 | + if (isset($storageOptions['mountOptions'])) { |
|
76 | + $storageConfig->setMountOptions($storageOptions['mountOptions']); |
|
77 | + } |
|
78 | + if (!isset($storageOptions['priority'])) { |
|
79 | + $storageOptions['priority'] = $backend->getPriority(); |
|
80 | + } |
|
81 | + $storageConfig->setPriority($storageOptions['priority']); |
|
82 | + if ($mountType === \OC_Mount_Config::MOUNT_TYPE_USER) { |
|
83 | + $applicableUsers = $storageConfig->getApplicableUsers(); |
|
84 | + if ($applicable !== 'all') { |
|
85 | + $applicableUsers[] = $applicable; |
|
86 | + $storageConfig->setApplicableUsers($applicableUsers); |
|
87 | + } |
|
88 | + } else if ($mountType === \OC_Mount_Config::MOUNT_TYPE_GROUP) { |
|
89 | + $applicableGroups = $storageConfig->getApplicableGroups(); |
|
90 | + $applicableGroups[] = $applicable; |
|
91 | + $storageConfig->setApplicableGroups($applicableGroups); |
|
92 | + } |
|
93 | + return $storageConfig; |
|
94 | + } |
|
95 | 95 | |
96 | - /** |
|
97 | - * Read the external storages config |
|
98 | - * |
|
99 | - * @return StorageConfig[] map of storage id to storage config |
|
100 | - */ |
|
101 | - public function getAllStorages() { |
|
102 | - $mountPoints = $this->readLegacyConfig(); |
|
103 | - /** |
|
104 | - * Here is the how the horribly messy mount point array looks like |
|
105 | - * from the mount.json file: |
|
106 | - * |
|
107 | - * $storageOptions = $mountPoints[$mountType][$applicable][$mountPath] |
|
108 | - * |
|
109 | - * - $mountType is either "user" or "group" |
|
110 | - * - $applicable is the name of a user or group (or the current user for personal mounts) |
|
111 | - * - $mountPath is the mount point path (where the storage must be mounted) |
|
112 | - * - $storageOptions is a map of storage options: |
|
113 | - * - "priority": storage priority |
|
114 | - * - "backend": backend identifier |
|
115 | - * - "class": LEGACY backend class name |
|
116 | - * - "options": backend-specific options |
|
117 | - * - "authMechanism": authentication mechanism identifier |
|
118 | - * - "mountOptions": mount-specific options (ex: disable previews, scanner, etc) |
|
119 | - */ |
|
120 | - // group by storage id |
|
121 | - /** @var StorageConfig[] $storages */ |
|
122 | - $storages = []; |
|
123 | - // for storages without id (legacy), group by config hash for |
|
124 | - // later processing |
|
125 | - $storagesWithConfigHash = []; |
|
126 | - foreach ($mountPoints as $mountType => $applicables) { |
|
127 | - foreach ($applicables as $applicable => $mountPaths) { |
|
128 | - foreach ($mountPaths as $rootMountPath => $storageOptions) { |
|
129 | - $currentStorage = null; |
|
130 | - /** |
|
131 | - * Flag whether the config that was read already has an id. |
|
132 | - * If not, it will use a config hash instead and generate |
|
133 | - * a proper id later |
|
134 | - * |
|
135 | - * @var boolean |
|
136 | - */ |
|
137 | - $hasId = false; |
|
138 | - // the root mount point is in the format "/$user/files/the/mount/point" |
|
139 | - // we remove the "/$user/files" prefix |
|
140 | - $parts = explode('/', ltrim($rootMountPath, '/'), 3); |
|
141 | - if (count($parts) < 3) { |
|
142 | - // something went wrong, skip |
|
143 | - \OCP\Util::writeLog( |
|
144 | - 'files_external', |
|
145 | - 'Could not parse mount point "' . $rootMountPath . '"', |
|
146 | - \OCP\Util::ERROR |
|
147 | - ); |
|
148 | - continue; |
|
149 | - } |
|
150 | - $relativeMountPath = rtrim($parts[2], '/'); |
|
151 | - // note: we cannot do this after the loop because the decrypted config |
|
152 | - // options might be needed for the config hash |
|
153 | - $storageOptions['options'] = \OC_Mount_Config::decryptPasswords($storageOptions['options']); |
|
154 | - if (!isset($storageOptions['backend'])) { |
|
155 | - $storageOptions['backend'] = $storageOptions['class']; // legacy compat |
|
156 | - } |
|
157 | - if (!isset($storageOptions['authMechanism'])) { |
|
158 | - $storageOptions['authMechanism'] = null; // ensure config hash works |
|
159 | - } |
|
160 | - if (isset($storageOptions['id'])) { |
|
161 | - $configId = (int)$storageOptions['id']; |
|
162 | - if (isset($storages[$configId])) { |
|
163 | - $currentStorage = $storages[$configId]; |
|
164 | - } |
|
165 | - $hasId = true; |
|
166 | - } else { |
|
167 | - // missing id in legacy config, need to generate |
|
168 | - // but at this point we don't know the max-id, so use |
|
169 | - // first group it by config hash |
|
170 | - $storageOptions['mountpoint'] = $rootMountPath; |
|
171 | - $configId = \OC_Mount_Config::makeConfigHash($storageOptions); |
|
172 | - if (isset($storagesWithConfigHash[$configId])) { |
|
173 | - $currentStorage = $storagesWithConfigHash[$configId]; |
|
174 | - } |
|
175 | - } |
|
176 | - if (is_null($currentStorage)) { |
|
177 | - // create new |
|
178 | - $currentStorage = new StorageConfig($configId); |
|
179 | - $currentStorage->setMountPoint($relativeMountPath); |
|
180 | - } |
|
181 | - try { |
|
182 | - $this->populateStorageConfigWithLegacyOptions( |
|
183 | - $currentStorage, |
|
184 | - $mountType, |
|
185 | - $applicable, |
|
186 | - $storageOptions |
|
187 | - ); |
|
188 | - if ($hasId) { |
|
189 | - $storages[$configId] = $currentStorage; |
|
190 | - } else { |
|
191 | - $storagesWithConfigHash[$configId] = $currentStorage; |
|
192 | - } |
|
193 | - } catch (\UnexpectedValueException $e) { |
|
194 | - // don't die if a storage backend doesn't exist |
|
195 | - \OCP\Util::writeLog( |
|
196 | - 'files_external', |
|
197 | - 'Could not load storage: "' . $e->getMessage() . '"', |
|
198 | - \OCP\Util::ERROR |
|
199 | - ); |
|
200 | - } |
|
201 | - } |
|
202 | - } |
|
203 | - } |
|
96 | + /** |
|
97 | + * Read the external storages config |
|
98 | + * |
|
99 | + * @return StorageConfig[] map of storage id to storage config |
|
100 | + */ |
|
101 | + public function getAllStorages() { |
|
102 | + $mountPoints = $this->readLegacyConfig(); |
|
103 | + /** |
|
104 | + * Here is the how the horribly messy mount point array looks like |
|
105 | + * from the mount.json file: |
|
106 | + * |
|
107 | + * $storageOptions = $mountPoints[$mountType][$applicable][$mountPath] |
|
108 | + * |
|
109 | + * - $mountType is either "user" or "group" |
|
110 | + * - $applicable is the name of a user or group (or the current user for personal mounts) |
|
111 | + * - $mountPath is the mount point path (where the storage must be mounted) |
|
112 | + * - $storageOptions is a map of storage options: |
|
113 | + * - "priority": storage priority |
|
114 | + * - "backend": backend identifier |
|
115 | + * - "class": LEGACY backend class name |
|
116 | + * - "options": backend-specific options |
|
117 | + * - "authMechanism": authentication mechanism identifier |
|
118 | + * - "mountOptions": mount-specific options (ex: disable previews, scanner, etc) |
|
119 | + */ |
|
120 | + // group by storage id |
|
121 | + /** @var StorageConfig[] $storages */ |
|
122 | + $storages = []; |
|
123 | + // for storages without id (legacy), group by config hash for |
|
124 | + // later processing |
|
125 | + $storagesWithConfigHash = []; |
|
126 | + foreach ($mountPoints as $mountType => $applicables) { |
|
127 | + foreach ($applicables as $applicable => $mountPaths) { |
|
128 | + foreach ($mountPaths as $rootMountPath => $storageOptions) { |
|
129 | + $currentStorage = null; |
|
130 | + /** |
|
131 | + * Flag whether the config that was read already has an id. |
|
132 | + * If not, it will use a config hash instead and generate |
|
133 | + * a proper id later |
|
134 | + * |
|
135 | + * @var boolean |
|
136 | + */ |
|
137 | + $hasId = false; |
|
138 | + // the root mount point is in the format "/$user/files/the/mount/point" |
|
139 | + // we remove the "/$user/files" prefix |
|
140 | + $parts = explode('/', ltrim($rootMountPath, '/'), 3); |
|
141 | + if (count($parts) < 3) { |
|
142 | + // something went wrong, skip |
|
143 | + \OCP\Util::writeLog( |
|
144 | + 'files_external', |
|
145 | + 'Could not parse mount point "' . $rootMountPath . '"', |
|
146 | + \OCP\Util::ERROR |
|
147 | + ); |
|
148 | + continue; |
|
149 | + } |
|
150 | + $relativeMountPath = rtrim($parts[2], '/'); |
|
151 | + // note: we cannot do this after the loop because the decrypted config |
|
152 | + // options might be needed for the config hash |
|
153 | + $storageOptions['options'] = \OC_Mount_Config::decryptPasswords($storageOptions['options']); |
|
154 | + if (!isset($storageOptions['backend'])) { |
|
155 | + $storageOptions['backend'] = $storageOptions['class']; // legacy compat |
|
156 | + } |
|
157 | + if (!isset($storageOptions['authMechanism'])) { |
|
158 | + $storageOptions['authMechanism'] = null; // ensure config hash works |
|
159 | + } |
|
160 | + if (isset($storageOptions['id'])) { |
|
161 | + $configId = (int)$storageOptions['id']; |
|
162 | + if (isset($storages[$configId])) { |
|
163 | + $currentStorage = $storages[$configId]; |
|
164 | + } |
|
165 | + $hasId = true; |
|
166 | + } else { |
|
167 | + // missing id in legacy config, need to generate |
|
168 | + // but at this point we don't know the max-id, so use |
|
169 | + // first group it by config hash |
|
170 | + $storageOptions['mountpoint'] = $rootMountPath; |
|
171 | + $configId = \OC_Mount_Config::makeConfigHash($storageOptions); |
|
172 | + if (isset($storagesWithConfigHash[$configId])) { |
|
173 | + $currentStorage = $storagesWithConfigHash[$configId]; |
|
174 | + } |
|
175 | + } |
|
176 | + if (is_null($currentStorage)) { |
|
177 | + // create new |
|
178 | + $currentStorage = new StorageConfig($configId); |
|
179 | + $currentStorage->setMountPoint($relativeMountPath); |
|
180 | + } |
|
181 | + try { |
|
182 | + $this->populateStorageConfigWithLegacyOptions( |
|
183 | + $currentStorage, |
|
184 | + $mountType, |
|
185 | + $applicable, |
|
186 | + $storageOptions |
|
187 | + ); |
|
188 | + if ($hasId) { |
|
189 | + $storages[$configId] = $currentStorage; |
|
190 | + } else { |
|
191 | + $storagesWithConfigHash[$configId] = $currentStorage; |
|
192 | + } |
|
193 | + } catch (\UnexpectedValueException $e) { |
|
194 | + // don't die if a storage backend doesn't exist |
|
195 | + \OCP\Util::writeLog( |
|
196 | + 'files_external', |
|
197 | + 'Could not load storage: "' . $e->getMessage() . '"', |
|
198 | + \OCP\Util::ERROR |
|
199 | + ); |
|
200 | + } |
|
201 | + } |
|
202 | + } |
|
203 | + } |
|
204 | 204 | |
205 | - // convert parameter values |
|
206 | - foreach ($storages as $storage) { |
|
207 | - $storage->getBackend()->validateStorageDefinition($storage); |
|
208 | - $storage->getAuthMechanism()->validateStorageDefinition($storage); |
|
209 | - } |
|
210 | - return $storages; |
|
211 | - } |
|
205 | + // convert parameter values |
|
206 | + foreach ($storages as $storage) { |
|
207 | + $storage->getBackend()->validateStorageDefinition($storage); |
|
208 | + $storage->getAuthMechanism()->validateStorageDefinition($storage); |
|
209 | + } |
|
210 | + return $storages; |
|
211 | + } |
|
212 | 212 | } |
@@ -30,46 +30,46 @@ |
||
30 | 30 | */ |
31 | 31 | trait UserTrait { |
32 | 32 | |
33 | - /** @var IUserSession */ |
|
34 | - protected $userSession; |
|
33 | + /** @var IUserSession */ |
|
34 | + protected $userSession; |
|
35 | 35 | |
36 | - /** |
|
37 | - * User override |
|
38 | - * |
|
39 | - * @var IUser|null |
|
40 | - */ |
|
41 | - private $user = null; |
|
36 | + /** |
|
37 | + * User override |
|
38 | + * |
|
39 | + * @var IUser|null |
|
40 | + */ |
|
41 | + private $user = null; |
|
42 | 42 | |
43 | - /** |
|
44 | - * @return IUser|null |
|
45 | - */ |
|
46 | - protected function getUser() { |
|
47 | - if ($this->user) { |
|
48 | - return $this->user; |
|
49 | - } |
|
50 | - return $this->userSession->getUser(); |
|
51 | - } |
|
43 | + /** |
|
44 | + * @return IUser|null |
|
45 | + */ |
|
46 | + protected function getUser() { |
|
47 | + if ($this->user) { |
|
48 | + return $this->user; |
|
49 | + } |
|
50 | + return $this->userSession->getUser(); |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * Override the user from the session |
|
55 | - * Unset with ->resetUser() when finished! |
|
56 | - * |
|
57 | - * @param IUser |
|
58 | - * @return self |
|
59 | - */ |
|
60 | - public function setUser(IUser $user) { |
|
61 | - $this->user = $user; |
|
62 | - return $this; |
|
63 | - } |
|
53 | + /** |
|
54 | + * Override the user from the session |
|
55 | + * Unset with ->resetUser() when finished! |
|
56 | + * |
|
57 | + * @param IUser |
|
58 | + * @return self |
|
59 | + */ |
|
60 | + public function setUser(IUser $user) { |
|
61 | + $this->user = $user; |
|
62 | + return $this; |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * Reset the user override |
|
67 | - * |
|
68 | - * @return self |
|
69 | - */ |
|
70 | - public function resetUser() { |
|
71 | - $this->user = null; |
|
72 | - return $this; |
|
73 | - } |
|
65 | + /** |
|
66 | + * Reset the user override |
|
67 | + * |
|
68 | + * @return self |
|
69 | + */ |
|
70 | + public function resetUser() { |
|
71 | + $this->user = null; |
|
72 | + return $this; |
|
73 | + } |
|
74 | 74 | } |
75 | 75 |
@@ -40,105 +40,105 @@ |
||
40 | 40 | * (aka personal storages) |
41 | 41 | */ |
42 | 42 | class UserStoragesService extends StoragesService { |
43 | - use UserTrait; |
|
43 | + use UserTrait; |
|
44 | 44 | |
45 | - /** |
|
46 | - * Create a user storages service |
|
47 | - * |
|
48 | - * @param BackendService $backendService |
|
49 | - * @param DBConfigService $dbConfig |
|
50 | - * @param IUserSession $userSession user session |
|
51 | - * @param IUserMountCache $userMountCache |
|
52 | - */ |
|
53 | - public function __construct( |
|
54 | - BackendService $backendService, |
|
55 | - DBConfigService $dbConfig, |
|
56 | - IUserSession $userSession, |
|
57 | - IUserMountCache $userMountCache |
|
58 | - ) { |
|
59 | - $this->userSession = $userSession; |
|
60 | - parent::__construct($backendService, $dbConfig, $userMountCache); |
|
61 | - } |
|
45 | + /** |
|
46 | + * Create a user storages service |
|
47 | + * |
|
48 | + * @param BackendService $backendService |
|
49 | + * @param DBConfigService $dbConfig |
|
50 | + * @param IUserSession $userSession user session |
|
51 | + * @param IUserMountCache $userMountCache |
|
52 | + */ |
|
53 | + public function __construct( |
|
54 | + BackendService $backendService, |
|
55 | + DBConfigService $dbConfig, |
|
56 | + IUserSession $userSession, |
|
57 | + IUserMountCache $userMountCache |
|
58 | + ) { |
|
59 | + $this->userSession = $userSession; |
|
60 | + parent::__construct($backendService, $dbConfig, $userMountCache); |
|
61 | + } |
|
62 | 62 | |
63 | - protected function readDBConfig() { |
|
64 | - return $this->dbConfig->getUserMountsFor(DBConfigService::APPLICABLE_TYPE_USER, $this->getUser()->getUID()); |
|
65 | - } |
|
63 | + protected function readDBConfig() { |
|
64 | + return $this->dbConfig->getUserMountsFor(DBConfigService::APPLICABLE_TYPE_USER, $this->getUser()->getUID()); |
|
65 | + } |
|
66 | 66 | |
67 | - /** |
|
68 | - * Triggers $signal for all applicable users of the given |
|
69 | - * storage |
|
70 | - * |
|
71 | - * @param StorageConfig $storage storage data |
|
72 | - * @param string $signal signal to trigger |
|
73 | - */ |
|
74 | - protected function triggerHooks(StorageConfig $storage, $signal) { |
|
75 | - $user = $this->getUser()->getUID(); |
|
67 | + /** |
|
68 | + * Triggers $signal for all applicable users of the given |
|
69 | + * storage |
|
70 | + * |
|
71 | + * @param StorageConfig $storage storage data |
|
72 | + * @param string $signal signal to trigger |
|
73 | + */ |
|
74 | + protected function triggerHooks(StorageConfig $storage, $signal) { |
|
75 | + $user = $this->getUser()->getUID(); |
|
76 | 76 | |
77 | - // trigger hook for the current user |
|
78 | - $this->triggerApplicableHooks( |
|
79 | - $signal, |
|
80 | - $storage->getMountPoint(), |
|
81 | - \OC_Mount_Config::MOUNT_TYPE_USER, |
|
82 | - [$user] |
|
83 | - ); |
|
84 | - } |
|
77 | + // trigger hook for the current user |
|
78 | + $this->triggerApplicableHooks( |
|
79 | + $signal, |
|
80 | + $storage->getMountPoint(), |
|
81 | + \OC_Mount_Config::MOUNT_TYPE_USER, |
|
82 | + [$user] |
|
83 | + ); |
|
84 | + } |
|
85 | 85 | |
86 | - /** |
|
87 | - * Triggers signal_create_mount or signal_delete_mount to |
|
88 | - * accommodate for additions/deletions in applicableUsers |
|
89 | - * and applicableGroups fields. |
|
90 | - * |
|
91 | - * @param StorageConfig $oldStorage old storage data |
|
92 | - * @param StorageConfig $newStorage new storage data |
|
93 | - */ |
|
94 | - protected function triggerChangeHooks(StorageConfig $oldStorage, StorageConfig $newStorage) { |
|
95 | - // if mount point changed, it's like a deletion + creation |
|
96 | - if ($oldStorage->getMountPoint() !== $newStorage->getMountPoint()) { |
|
97 | - $this->triggerHooks($oldStorage, Filesystem::signal_delete_mount); |
|
98 | - $this->triggerHooks($newStorage, Filesystem::signal_create_mount); |
|
99 | - } |
|
100 | - } |
|
86 | + /** |
|
87 | + * Triggers signal_create_mount or signal_delete_mount to |
|
88 | + * accommodate for additions/deletions in applicableUsers |
|
89 | + * and applicableGroups fields. |
|
90 | + * |
|
91 | + * @param StorageConfig $oldStorage old storage data |
|
92 | + * @param StorageConfig $newStorage new storage data |
|
93 | + */ |
|
94 | + protected function triggerChangeHooks(StorageConfig $oldStorage, StorageConfig $newStorage) { |
|
95 | + // if mount point changed, it's like a deletion + creation |
|
96 | + if ($oldStorage->getMountPoint() !== $newStorage->getMountPoint()) { |
|
97 | + $this->triggerHooks($oldStorage, Filesystem::signal_delete_mount); |
|
98 | + $this->triggerHooks($newStorage, Filesystem::signal_create_mount); |
|
99 | + } |
|
100 | + } |
|
101 | 101 | |
102 | - protected function getType() { |
|
103 | - return DBConfigService::MOUNT_TYPE_PERSONAl; |
|
104 | - } |
|
102 | + protected function getType() { |
|
103 | + return DBConfigService::MOUNT_TYPE_PERSONAl; |
|
104 | + } |
|
105 | 105 | |
106 | - /** |
|
107 | - * Add new storage to the configuration |
|
108 | - * |
|
109 | - * @param StorageConfig $newStorage storage attributes |
|
110 | - * |
|
111 | - * @return StorageConfig storage config, with added id |
|
112 | - */ |
|
113 | - public function addStorage(StorageConfig $newStorage) { |
|
114 | - $newStorage->setApplicableUsers([$this->getUser()->getUID()]); |
|
115 | - $config = parent::addStorage($newStorage); |
|
116 | - return $config; |
|
117 | - } |
|
106 | + /** |
|
107 | + * Add new storage to the configuration |
|
108 | + * |
|
109 | + * @param StorageConfig $newStorage storage attributes |
|
110 | + * |
|
111 | + * @return StorageConfig storage config, with added id |
|
112 | + */ |
|
113 | + public function addStorage(StorageConfig $newStorage) { |
|
114 | + $newStorage->setApplicableUsers([$this->getUser()->getUID()]); |
|
115 | + $config = parent::addStorage($newStorage); |
|
116 | + return $config; |
|
117 | + } |
|
118 | 118 | |
119 | - /** |
|
120 | - * Update storage to the configuration |
|
121 | - * |
|
122 | - * @param StorageConfig $updatedStorage storage attributes |
|
123 | - * |
|
124 | - * @return StorageConfig storage config |
|
125 | - * @throws NotFoundException if the given storage does not exist in the config |
|
126 | - */ |
|
127 | - public function updateStorage(StorageConfig $updatedStorage) { |
|
128 | - $updatedStorage->setApplicableUsers([$this->getUser()->getUID()]); |
|
129 | - return parent::updateStorage($updatedStorage); |
|
130 | - } |
|
119 | + /** |
|
120 | + * Update storage to the configuration |
|
121 | + * |
|
122 | + * @param StorageConfig $updatedStorage storage attributes |
|
123 | + * |
|
124 | + * @return StorageConfig storage config |
|
125 | + * @throws NotFoundException if the given storage does not exist in the config |
|
126 | + */ |
|
127 | + public function updateStorage(StorageConfig $updatedStorage) { |
|
128 | + $updatedStorage->setApplicableUsers([$this->getUser()->getUID()]); |
|
129 | + return parent::updateStorage($updatedStorage); |
|
130 | + } |
|
131 | 131 | |
132 | - /** |
|
133 | - * Get the visibility type for this controller, used in validation |
|
134 | - * |
|
135 | - * @return string BackendService::VISIBILITY_* constants |
|
136 | - */ |
|
137 | - public function getVisibilityType() { |
|
138 | - return BackendService::VISIBILITY_PERSONAL; |
|
139 | - } |
|
132 | + /** |
|
133 | + * Get the visibility type for this controller, used in validation |
|
134 | + * |
|
135 | + * @return string BackendService::VISIBILITY_* constants |
|
136 | + */ |
|
137 | + public function getVisibilityType() { |
|
138 | + return BackendService::VISIBILITY_PERSONAL; |
|
139 | + } |
|
140 | 140 | |
141 | - protected function isApplicable(StorageConfig $config) { |
|
142 | - return ($config->getApplicableUsers() === [$this->getUser()->getUID()]) && $config->getType() === StorageConfig::MOUNT_TYPE_PERSONAl; |
|
143 | - } |
|
141 | + protected function isApplicable(StorageConfig $config) { |
|
142 | + return ($config->getApplicableUsers() === [$this->getUser()->getUID()]) && $config->getType() === StorageConfig::MOUNT_TYPE_PERSONAl; |
|
143 | + } |
|
144 | 144 | } |
@@ -41,107 +41,107 @@ |
||
41 | 41 | */ |
42 | 42 | class Application extends App implements IBackendProvider, IAuthMechanismProvider { |
43 | 43 | |
44 | - public function __construct(array $urlParams = array()) { |
|
45 | - parent::__construct('files_external', $urlParams); |
|
46 | - |
|
47 | - $container = $this->getContainer(); |
|
48 | - |
|
49 | - $container->registerService('OCP\Files\Config\IUserMountCache', function (IAppContainer $c) { |
|
50 | - return $c->getServer()->query('UserMountCache'); |
|
51 | - }); |
|
52 | - |
|
53 | - $backendService = $container->query('OCA\\Files_External\\Service\\BackendService'); |
|
54 | - $backendService->registerBackendProvider($this); |
|
55 | - $backendService->registerAuthMechanismProvider($this); |
|
56 | - |
|
57 | - // force-load auth mechanisms since some will register hooks |
|
58 | - // TODO: obsolete these and use the TokenProvider to get the user's password from the session |
|
59 | - $this->getAuthMechanisms(); |
|
60 | - |
|
61 | - // app developers: do NOT depend on this! it will disappear with oC 9.0! |
|
62 | - \OC::$server->getEventDispatcher()->dispatch( |
|
63 | - 'OCA\\Files_External::loadAdditionalBackends' |
|
64 | - ); |
|
65 | - } |
|
66 | - |
|
67 | - /** |
|
68 | - * Register settings templates |
|
69 | - */ |
|
70 | - public function registerSettings() { |
|
71 | - $container = $this->getContainer(); |
|
72 | - $userSession = $container->getServer()->getUserSession(); |
|
73 | - if (!$userSession->isLoggedIn()) { |
|
74 | - return; |
|
75 | - } |
|
76 | - $backendService = $container->query('OCA\\Files_External\\Service\\BackendService'); |
|
77 | - |
|
78 | - /** @var \OCA\Files_External\Service\UserGlobalStoragesService $userGlobalStoragesService */ |
|
79 | - $userGlobalStoragesService = $container->query('OCA\Files_External\Service\UserGlobalStoragesService'); |
|
80 | - if (count($userGlobalStoragesService->getStorages()) > 0 || $backendService->isUserMountingAllowed()) { |
|
81 | - \OCP\App::registerPersonal('files_external', 'personal'); |
|
82 | - } |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * @{inheritdoc} |
|
87 | - */ |
|
88 | - public function getBackends() { |
|
89 | - $container = $this->getContainer(); |
|
90 | - |
|
91 | - $backends = [ |
|
92 | - $container->query('OCA\Files_External\Lib\Backend\Local'), |
|
93 | - $container->query('OCA\Files_External\Lib\Backend\FTP'), |
|
94 | - $container->query('OCA\Files_External\Lib\Backend\DAV'), |
|
95 | - $container->query('OCA\Files_External\Lib\Backend\OwnCloud'), |
|
96 | - $container->query('OCA\Files_External\Lib\Backend\SFTP'), |
|
97 | - $container->query('OCA\Files_External\Lib\Backend\AmazonS3'), |
|
98 | - $container->query('OCA\Files_External\Lib\Backend\Dropbox'), |
|
99 | - $container->query('OCA\Files_External\Lib\Backend\Google'), |
|
100 | - $container->query('OCA\Files_External\Lib\Backend\Swift'), |
|
101 | - $container->query('OCA\Files_External\Lib\Backend\SFTP_Key'), |
|
102 | - $container->query('OCA\Files_External\Lib\Backend\SMB'), |
|
103 | - $container->query('OCA\Files_External\Lib\Backend\SMB_OC'), |
|
104 | - ]; |
|
105 | - |
|
106 | - return $backends; |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * @{inheritdoc} |
|
111 | - */ |
|
112 | - public function getAuthMechanisms() { |
|
113 | - $container = $this->getContainer(); |
|
114 | - |
|
115 | - return [ |
|
116 | - // AuthMechanism::SCHEME_NULL mechanism |
|
117 | - $container->query('OCA\Files_External\Lib\Auth\NullMechanism'), |
|
118 | - |
|
119 | - // AuthMechanism::SCHEME_BUILTIN mechanism |
|
120 | - $container->query('OCA\Files_External\Lib\Auth\Builtin'), |
|
121 | - |
|
122 | - // AuthMechanism::SCHEME_PASSWORD mechanisms |
|
123 | - $container->query('OCA\Files_External\Lib\Auth\Password\Password'), |
|
124 | - $container->query('OCA\Files_External\Lib\Auth\Password\SessionCredentials'), |
|
125 | - $container->query('OCA\Files_External\Lib\Auth\Password\LoginCredentials'), |
|
126 | - $container->query('OCA\Files_External\Lib\Auth\Password\UserProvided'), |
|
127 | - $container->query('OCA\Files_External\Lib\Auth\Password\GlobalAuth'), |
|
128 | - |
|
129 | - // AuthMechanism::SCHEME_OAUTH1 mechanisms |
|
130 | - $container->query('OCA\Files_External\Lib\Auth\OAuth1\OAuth1'), |
|
131 | - |
|
132 | - // AuthMechanism::SCHEME_OAUTH2 mechanisms |
|
133 | - $container->query('OCA\Files_External\Lib\Auth\OAuth2\OAuth2'), |
|
134 | - |
|
135 | - // AuthMechanism::SCHEME_PUBLICKEY mechanisms |
|
136 | - $container->query('OCA\Files_External\Lib\Auth\PublicKey\RSA'), |
|
137 | - |
|
138 | - // AuthMechanism::SCHEME_OPENSTACK mechanisms |
|
139 | - $container->query('OCA\Files_External\Lib\Auth\OpenStack\OpenStack'), |
|
140 | - $container->query('OCA\Files_External\Lib\Auth\OpenStack\Rackspace'), |
|
141 | - |
|
142 | - // Specialized mechanisms |
|
143 | - $container->query('OCA\Files_External\Lib\Auth\AmazonS3\AccessKey'), |
|
144 | - ]; |
|
145 | - } |
|
44 | + public function __construct(array $urlParams = array()) { |
|
45 | + parent::__construct('files_external', $urlParams); |
|
46 | + |
|
47 | + $container = $this->getContainer(); |
|
48 | + |
|
49 | + $container->registerService('OCP\Files\Config\IUserMountCache', function (IAppContainer $c) { |
|
50 | + return $c->getServer()->query('UserMountCache'); |
|
51 | + }); |
|
52 | + |
|
53 | + $backendService = $container->query('OCA\\Files_External\\Service\\BackendService'); |
|
54 | + $backendService->registerBackendProvider($this); |
|
55 | + $backendService->registerAuthMechanismProvider($this); |
|
56 | + |
|
57 | + // force-load auth mechanisms since some will register hooks |
|
58 | + // TODO: obsolete these and use the TokenProvider to get the user's password from the session |
|
59 | + $this->getAuthMechanisms(); |
|
60 | + |
|
61 | + // app developers: do NOT depend on this! it will disappear with oC 9.0! |
|
62 | + \OC::$server->getEventDispatcher()->dispatch( |
|
63 | + 'OCA\\Files_External::loadAdditionalBackends' |
|
64 | + ); |
|
65 | + } |
|
66 | + |
|
67 | + /** |
|
68 | + * Register settings templates |
|
69 | + */ |
|
70 | + public function registerSettings() { |
|
71 | + $container = $this->getContainer(); |
|
72 | + $userSession = $container->getServer()->getUserSession(); |
|
73 | + if (!$userSession->isLoggedIn()) { |
|
74 | + return; |
|
75 | + } |
|
76 | + $backendService = $container->query('OCA\\Files_External\\Service\\BackendService'); |
|
77 | + |
|
78 | + /** @var \OCA\Files_External\Service\UserGlobalStoragesService $userGlobalStoragesService */ |
|
79 | + $userGlobalStoragesService = $container->query('OCA\Files_External\Service\UserGlobalStoragesService'); |
|
80 | + if (count($userGlobalStoragesService->getStorages()) > 0 || $backendService->isUserMountingAllowed()) { |
|
81 | + \OCP\App::registerPersonal('files_external', 'personal'); |
|
82 | + } |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * @{inheritdoc} |
|
87 | + */ |
|
88 | + public function getBackends() { |
|
89 | + $container = $this->getContainer(); |
|
90 | + |
|
91 | + $backends = [ |
|
92 | + $container->query('OCA\Files_External\Lib\Backend\Local'), |
|
93 | + $container->query('OCA\Files_External\Lib\Backend\FTP'), |
|
94 | + $container->query('OCA\Files_External\Lib\Backend\DAV'), |
|
95 | + $container->query('OCA\Files_External\Lib\Backend\OwnCloud'), |
|
96 | + $container->query('OCA\Files_External\Lib\Backend\SFTP'), |
|
97 | + $container->query('OCA\Files_External\Lib\Backend\AmazonS3'), |
|
98 | + $container->query('OCA\Files_External\Lib\Backend\Dropbox'), |
|
99 | + $container->query('OCA\Files_External\Lib\Backend\Google'), |
|
100 | + $container->query('OCA\Files_External\Lib\Backend\Swift'), |
|
101 | + $container->query('OCA\Files_External\Lib\Backend\SFTP_Key'), |
|
102 | + $container->query('OCA\Files_External\Lib\Backend\SMB'), |
|
103 | + $container->query('OCA\Files_External\Lib\Backend\SMB_OC'), |
|
104 | + ]; |
|
105 | + |
|
106 | + return $backends; |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * @{inheritdoc} |
|
111 | + */ |
|
112 | + public function getAuthMechanisms() { |
|
113 | + $container = $this->getContainer(); |
|
114 | + |
|
115 | + return [ |
|
116 | + // AuthMechanism::SCHEME_NULL mechanism |
|
117 | + $container->query('OCA\Files_External\Lib\Auth\NullMechanism'), |
|
118 | + |
|
119 | + // AuthMechanism::SCHEME_BUILTIN mechanism |
|
120 | + $container->query('OCA\Files_External\Lib\Auth\Builtin'), |
|
121 | + |
|
122 | + // AuthMechanism::SCHEME_PASSWORD mechanisms |
|
123 | + $container->query('OCA\Files_External\Lib\Auth\Password\Password'), |
|
124 | + $container->query('OCA\Files_External\Lib\Auth\Password\SessionCredentials'), |
|
125 | + $container->query('OCA\Files_External\Lib\Auth\Password\LoginCredentials'), |
|
126 | + $container->query('OCA\Files_External\Lib\Auth\Password\UserProvided'), |
|
127 | + $container->query('OCA\Files_External\Lib\Auth\Password\GlobalAuth'), |
|
128 | + |
|
129 | + // AuthMechanism::SCHEME_OAUTH1 mechanisms |
|
130 | + $container->query('OCA\Files_External\Lib\Auth\OAuth1\OAuth1'), |
|
131 | + |
|
132 | + // AuthMechanism::SCHEME_OAUTH2 mechanisms |
|
133 | + $container->query('OCA\Files_External\Lib\Auth\OAuth2\OAuth2'), |
|
134 | + |
|
135 | + // AuthMechanism::SCHEME_PUBLICKEY mechanisms |
|
136 | + $container->query('OCA\Files_External\Lib\Auth\PublicKey\RSA'), |
|
137 | + |
|
138 | + // AuthMechanism::SCHEME_OPENSTACK mechanisms |
|
139 | + $container->query('OCA\Files_External\Lib\Auth\OpenStack\OpenStack'), |
|
140 | + $container->query('OCA\Files_External\Lib\Auth\OpenStack\Rackspace'), |
|
141 | + |
|
142 | + // Specialized mechanisms |
|
143 | + $container->query('OCA\Files_External\Lib\Auth\AmazonS3\AccessKey'), |
|
144 | + ]; |
|
145 | + } |
|
146 | 146 | |
147 | 147 | } |
@@ -45,143 +45,143 @@ |
||
45 | 45 | */ |
46 | 46 | class ConfigAdapter implements IMountProvider { |
47 | 47 | |
48 | - /** @var UserStoragesService */ |
|
49 | - private $userStoragesService; |
|
50 | - |
|
51 | - /** @var UserGlobalStoragesService */ |
|
52 | - private $userGlobalStoragesService; |
|
53 | - /** @var StorageMigrator */ |
|
54 | - private $migrator; |
|
55 | - |
|
56 | - /** |
|
57 | - * @param UserStoragesService $userStoragesService |
|
58 | - * @param UserGlobalStoragesService $userGlobalStoragesService |
|
59 | - * @param StorageMigrator $migrator |
|
60 | - */ |
|
61 | - public function __construct( |
|
62 | - UserStoragesService $userStoragesService, |
|
63 | - UserGlobalStoragesService $userGlobalStoragesService, |
|
64 | - StorageMigrator $migrator |
|
65 | - ) { |
|
66 | - $this->userStoragesService = $userStoragesService; |
|
67 | - $this->userGlobalStoragesService = $userGlobalStoragesService; |
|
68 | - $this->migrator = $migrator; |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * Process storage ready for mounting |
|
73 | - * |
|
74 | - * @param StorageConfig $storage |
|
75 | - * @param IUser $user |
|
76 | - */ |
|
77 | - private function prepareStorageConfig(StorageConfig &$storage, IUser $user) { |
|
78 | - foreach ($storage->getBackendOptions() as $option => $value) { |
|
79 | - $storage->setBackendOption($option, \OC_Mount_Config::setUserVars( |
|
80 | - $user->getUID(), $value |
|
81 | - )); |
|
82 | - } |
|
83 | - |
|
84 | - $objectStore = $storage->getBackendOption('objectstore'); |
|
85 | - if ($objectStore) { |
|
86 | - $objectClass = $objectStore['class']; |
|
87 | - if (!is_subclass_of($objectClass, '\OCP\Files\ObjectStore\IObjectStore')) { |
|
88 | - throw new \InvalidArgumentException('Invalid object store'); |
|
89 | - } |
|
90 | - $storage->setBackendOption('objectstore', new $objectClass($objectStore)); |
|
91 | - } |
|
92 | - |
|
93 | - $storage->getAuthMechanism()->manipulateStorageConfig($storage, $user); |
|
94 | - $storage->getBackend()->manipulateStorageConfig($storage, $user); |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * Construct the storage implementation |
|
99 | - * |
|
100 | - * @param StorageConfig $storageConfig |
|
101 | - * @return Storage |
|
102 | - */ |
|
103 | - private function constructStorage(StorageConfig $storageConfig) { |
|
104 | - $class = $storageConfig->getBackend()->getStorageClass(); |
|
105 | - $storage = new $class($storageConfig->getBackendOptions()); |
|
106 | - |
|
107 | - // auth mechanism should fire first |
|
108 | - $storage = $storageConfig->getBackend()->wrapStorage($storage); |
|
109 | - $storage = $storageConfig->getAuthMechanism()->wrapStorage($storage); |
|
110 | - |
|
111 | - return $storage; |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * Get all mountpoints applicable for the user |
|
116 | - * |
|
117 | - * @param \OCP\IUser $user |
|
118 | - * @param \OCP\Files\Storage\IStorageFactory $loader |
|
119 | - * @return \OCP\Files\Mount\IMountPoint[] |
|
120 | - */ |
|
121 | - public function getMountsForUser(IUser $user, IStorageFactory $loader) { |
|
122 | - $this->migrator->migrateUser($user); |
|
123 | - |
|
124 | - $this->userStoragesService->setUser($user); |
|
125 | - $this->userGlobalStoragesService->setUser($user); |
|
126 | - |
|
127 | - $storageConfigs = $this->userGlobalStoragesService->getAllStoragesForUser(); |
|
128 | - |
|
129 | - $storages = array_map(function(StorageConfig $storageConfig) use ($user) { |
|
130 | - try { |
|
131 | - $this->prepareStorageConfig($storageConfig, $user); |
|
132 | - return $this->constructStorage($storageConfig); |
|
133 | - } catch (\Exception $e) { |
|
134 | - // propagate exception into filesystem |
|
135 | - return new FailedStorage(['exception' => $e]); |
|
136 | - } |
|
137 | - }, $storageConfigs); |
|
138 | - |
|
139 | - |
|
140 | - \OC\Files\Cache\Storage::getGlobalCache()->loadForStorageIds(array_map(function(Storage\IStorage $storage) { |
|
141 | - return $storage->getId(); |
|
142 | - }, $storages)); |
|
143 | - |
|
144 | - $availableStorages = array_map(function (Storage\IStorage $storage, StorageConfig $storageConfig) { |
|
145 | - try { |
|
146 | - $availability = $storage->getAvailability(); |
|
147 | - if (!$availability['available'] && !Availability::shouldRecheck($availability)) { |
|
148 | - $storage = new FailedStorage([ |
|
149 | - 'exception' => new StorageNotAvailableException('Storage with mount id ' . $storageConfig->getId() . ' is not available') |
|
150 | - ]); |
|
151 | - } |
|
152 | - } catch (\Exception $e) { |
|
153 | - // propagate exception into filesystem |
|
154 | - $storage = new FailedStorage(['exception' => $e]); |
|
155 | - } |
|
156 | - return $storage; |
|
157 | - }, $storages, $storageConfigs); |
|
158 | - |
|
159 | - $mounts = array_map(function(StorageConfig $storageConfig, Storage\IStorage $storage) use ($user, $loader) { |
|
160 | - if ($storageConfig->getType() === StorageConfig::MOUNT_TYPE_PERSONAl) { |
|
161 | - return new PersonalMount( |
|
162 | - $this->userStoragesService, |
|
163 | - $storageConfig->getId(), |
|
164 | - $storage, |
|
165 | - '/' . $user->getUID() . '/files' . $storageConfig->getMountPoint(), |
|
166 | - null, |
|
167 | - $loader, |
|
168 | - $storageConfig->getMountOptions() |
|
169 | - ); |
|
170 | - } else { |
|
171 | - return new MountPoint( |
|
172 | - $storage, |
|
173 | - '/' . $user->getUID() . '/files' . $storageConfig->getMountPoint(), |
|
174 | - null, |
|
175 | - $loader, |
|
176 | - $storageConfig->getMountOptions(), |
|
177 | - $storageConfig->getId() |
|
178 | - ); |
|
179 | - } |
|
180 | - }, $storageConfigs, $availableStorages); |
|
181 | - |
|
182 | - $this->userStoragesService->resetUser(); |
|
183 | - $this->userGlobalStoragesService->resetUser(); |
|
184 | - |
|
185 | - return $mounts; |
|
186 | - } |
|
48 | + /** @var UserStoragesService */ |
|
49 | + private $userStoragesService; |
|
50 | + |
|
51 | + /** @var UserGlobalStoragesService */ |
|
52 | + private $userGlobalStoragesService; |
|
53 | + /** @var StorageMigrator */ |
|
54 | + private $migrator; |
|
55 | + |
|
56 | + /** |
|
57 | + * @param UserStoragesService $userStoragesService |
|
58 | + * @param UserGlobalStoragesService $userGlobalStoragesService |
|
59 | + * @param StorageMigrator $migrator |
|
60 | + */ |
|
61 | + public function __construct( |
|
62 | + UserStoragesService $userStoragesService, |
|
63 | + UserGlobalStoragesService $userGlobalStoragesService, |
|
64 | + StorageMigrator $migrator |
|
65 | + ) { |
|
66 | + $this->userStoragesService = $userStoragesService; |
|
67 | + $this->userGlobalStoragesService = $userGlobalStoragesService; |
|
68 | + $this->migrator = $migrator; |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * Process storage ready for mounting |
|
73 | + * |
|
74 | + * @param StorageConfig $storage |
|
75 | + * @param IUser $user |
|
76 | + */ |
|
77 | + private function prepareStorageConfig(StorageConfig &$storage, IUser $user) { |
|
78 | + foreach ($storage->getBackendOptions() as $option => $value) { |
|
79 | + $storage->setBackendOption($option, \OC_Mount_Config::setUserVars( |
|
80 | + $user->getUID(), $value |
|
81 | + )); |
|
82 | + } |
|
83 | + |
|
84 | + $objectStore = $storage->getBackendOption('objectstore'); |
|
85 | + if ($objectStore) { |
|
86 | + $objectClass = $objectStore['class']; |
|
87 | + if (!is_subclass_of($objectClass, '\OCP\Files\ObjectStore\IObjectStore')) { |
|
88 | + throw new \InvalidArgumentException('Invalid object store'); |
|
89 | + } |
|
90 | + $storage->setBackendOption('objectstore', new $objectClass($objectStore)); |
|
91 | + } |
|
92 | + |
|
93 | + $storage->getAuthMechanism()->manipulateStorageConfig($storage, $user); |
|
94 | + $storage->getBackend()->manipulateStorageConfig($storage, $user); |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * Construct the storage implementation |
|
99 | + * |
|
100 | + * @param StorageConfig $storageConfig |
|
101 | + * @return Storage |
|
102 | + */ |
|
103 | + private function constructStorage(StorageConfig $storageConfig) { |
|
104 | + $class = $storageConfig->getBackend()->getStorageClass(); |
|
105 | + $storage = new $class($storageConfig->getBackendOptions()); |
|
106 | + |
|
107 | + // auth mechanism should fire first |
|
108 | + $storage = $storageConfig->getBackend()->wrapStorage($storage); |
|
109 | + $storage = $storageConfig->getAuthMechanism()->wrapStorage($storage); |
|
110 | + |
|
111 | + return $storage; |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * Get all mountpoints applicable for the user |
|
116 | + * |
|
117 | + * @param \OCP\IUser $user |
|
118 | + * @param \OCP\Files\Storage\IStorageFactory $loader |
|
119 | + * @return \OCP\Files\Mount\IMountPoint[] |
|
120 | + */ |
|
121 | + public function getMountsForUser(IUser $user, IStorageFactory $loader) { |
|
122 | + $this->migrator->migrateUser($user); |
|
123 | + |
|
124 | + $this->userStoragesService->setUser($user); |
|
125 | + $this->userGlobalStoragesService->setUser($user); |
|
126 | + |
|
127 | + $storageConfigs = $this->userGlobalStoragesService->getAllStoragesForUser(); |
|
128 | + |
|
129 | + $storages = array_map(function(StorageConfig $storageConfig) use ($user) { |
|
130 | + try { |
|
131 | + $this->prepareStorageConfig($storageConfig, $user); |
|
132 | + return $this->constructStorage($storageConfig); |
|
133 | + } catch (\Exception $e) { |
|
134 | + // propagate exception into filesystem |
|
135 | + return new FailedStorage(['exception' => $e]); |
|
136 | + } |
|
137 | + }, $storageConfigs); |
|
138 | + |
|
139 | + |
|
140 | + \OC\Files\Cache\Storage::getGlobalCache()->loadForStorageIds(array_map(function(Storage\IStorage $storage) { |
|
141 | + return $storage->getId(); |
|
142 | + }, $storages)); |
|
143 | + |
|
144 | + $availableStorages = array_map(function (Storage\IStorage $storage, StorageConfig $storageConfig) { |
|
145 | + try { |
|
146 | + $availability = $storage->getAvailability(); |
|
147 | + if (!$availability['available'] && !Availability::shouldRecheck($availability)) { |
|
148 | + $storage = new FailedStorage([ |
|
149 | + 'exception' => new StorageNotAvailableException('Storage with mount id ' . $storageConfig->getId() . ' is not available') |
|
150 | + ]); |
|
151 | + } |
|
152 | + } catch (\Exception $e) { |
|
153 | + // propagate exception into filesystem |
|
154 | + $storage = new FailedStorage(['exception' => $e]); |
|
155 | + } |
|
156 | + return $storage; |
|
157 | + }, $storages, $storageConfigs); |
|
158 | + |
|
159 | + $mounts = array_map(function(StorageConfig $storageConfig, Storage\IStorage $storage) use ($user, $loader) { |
|
160 | + if ($storageConfig->getType() === StorageConfig::MOUNT_TYPE_PERSONAl) { |
|
161 | + return new PersonalMount( |
|
162 | + $this->userStoragesService, |
|
163 | + $storageConfig->getId(), |
|
164 | + $storage, |
|
165 | + '/' . $user->getUID() . '/files' . $storageConfig->getMountPoint(), |
|
166 | + null, |
|
167 | + $loader, |
|
168 | + $storageConfig->getMountOptions() |
|
169 | + ); |
|
170 | + } else { |
|
171 | + return new MountPoint( |
|
172 | + $storage, |
|
173 | + '/' . $user->getUID() . '/files' . $storageConfig->getMountPoint(), |
|
174 | + null, |
|
175 | + $loader, |
|
176 | + $storageConfig->getMountOptions(), |
|
177 | + $storageConfig->getId() |
|
178 | + ); |
|
179 | + } |
|
180 | + }, $storageConfigs, $availableStorages); |
|
181 | + |
|
182 | + $this->userStoragesService->resetUser(); |
|
183 | + $this->userGlobalStoragesService->resetUser(); |
|
184 | + |
|
185 | + return $mounts; |
|
186 | + } |
|
187 | 187 | } |
@@ -45,371 +45,371 @@ |
||
45 | 45 | * Class to configure mount.json globally and for users |
46 | 46 | */ |
47 | 47 | class OC_Mount_Config { |
48 | - // TODO: make this class non-static and give it a proper namespace |
|
49 | - |
|
50 | - const MOUNT_TYPE_GLOBAL = 'global'; |
|
51 | - const MOUNT_TYPE_GROUP = 'group'; |
|
52 | - const MOUNT_TYPE_USER = 'user'; |
|
53 | - const MOUNT_TYPE_PERSONAL = 'personal'; |
|
54 | - |
|
55 | - // whether to skip backend test (for unit tests, as this static class is not mockable) |
|
56 | - public static $skipTest = false; |
|
57 | - |
|
58 | - /** @var Application */ |
|
59 | - public static $app; |
|
60 | - |
|
61 | - /** |
|
62 | - * @param string $class |
|
63 | - * @param array $definition |
|
64 | - * @return bool |
|
65 | - * @deprecated 8.2.0 use \OCA\Files_External\Service\BackendService::registerBackend() |
|
66 | - */ |
|
67 | - public static function registerBackend($class, $definition) { |
|
68 | - $backendService = self::$app->getContainer()->query('OCA\Files_External\Service\BackendService'); |
|
69 | - $auth = self::$app->getContainer()->query('OCA\Files_External\Lib\Auth\Builtin'); |
|
70 | - |
|
71 | - $backendService->registerBackend(new LegacyBackend($class, $definition, $auth)); |
|
72 | - |
|
73 | - return true; |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * Returns the mount points for the given user. |
|
78 | - * The mount point is relative to the data directory. |
|
79 | - * |
|
80 | - * @param string $uid user |
|
81 | - * @return array of mount point string as key, mountpoint config as value |
|
82 | - * |
|
83 | - * @deprecated 8.2.0 use UserGlobalStoragesService::getStorages() and UserStoragesService::getStorages() |
|
84 | - */ |
|
85 | - public static function getAbsoluteMountPoints($uid) { |
|
86 | - $mountPoints = array(); |
|
87 | - |
|
88 | - $userGlobalStoragesService = self::$app->getContainer()->query('OCA\Files_External\Service\UserGlobalStoragesService'); |
|
89 | - $userStoragesService = self::$app->getContainer()->query('OCA\Files_External\Service\UserStoragesService'); |
|
90 | - $user = self::$app->getContainer()->query('OCP\IUserManager')->get($uid); |
|
91 | - |
|
92 | - $userGlobalStoragesService->setUser($user); |
|
93 | - $userStoragesService->setUser($user); |
|
94 | - |
|
95 | - foreach ($userGlobalStoragesService->getStorages() as $storage) { |
|
96 | - /** @var \OCA\Files_External\Lib\StorageConfig $storage */ |
|
97 | - $mountPoint = '/'.$uid.'/files'.$storage->getMountPoint(); |
|
98 | - $mountEntry = self::prepareMountPointEntry($storage, false); |
|
99 | - foreach ($mountEntry['options'] as &$option) { |
|
100 | - $option = self::setUserVars($uid, $option); |
|
101 | - } |
|
102 | - $mountPoints[$mountPoint] = $mountEntry; |
|
103 | - } |
|
104 | - |
|
105 | - foreach ($userStoragesService->getStorages() as $storage) { |
|
106 | - $mountPoint = '/'.$uid.'/files'.$storage->getMountPoint(); |
|
107 | - $mountEntry = self::prepareMountPointEntry($storage, true); |
|
108 | - foreach ($mountEntry['options'] as &$option) { |
|
109 | - $option = self::setUserVars($uid, $option); |
|
110 | - } |
|
111 | - $mountPoints[$mountPoint] = $mountEntry; |
|
112 | - } |
|
113 | - |
|
114 | - $userGlobalStoragesService->resetUser(); |
|
115 | - $userStoragesService->resetUser(); |
|
116 | - |
|
117 | - return $mountPoints; |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * Get the system mount points |
|
122 | - * |
|
123 | - * @return array |
|
124 | - * |
|
125 | - * @deprecated 8.2.0 use GlobalStoragesService::getStorages() |
|
126 | - */ |
|
127 | - public static function getSystemMountPoints() { |
|
128 | - $mountPoints = []; |
|
129 | - $service = self::$app->getContainer()->query('OCA\Files_External\Service\GlobalStoragesService'); |
|
130 | - |
|
131 | - foreach ($service->getStorages() as $storage) { |
|
132 | - $mountPoints[] = self::prepareMountPointEntry($storage, false); |
|
133 | - } |
|
134 | - |
|
135 | - return $mountPoints; |
|
136 | - } |
|
137 | - |
|
138 | - /** |
|
139 | - * Get the personal mount points of the current user |
|
140 | - * |
|
141 | - * @return array |
|
142 | - * |
|
143 | - * @deprecated 8.2.0 use UserStoragesService::getStorages() |
|
144 | - */ |
|
145 | - public static function getPersonalMountPoints() { |
|
146 | - $mountPoints = []; |
|
147 | - $service = self::$app->getContainer()->query('OCA\Files_External\Service\UserStoragesService'); |
|
148 | - |
|
149 | - foreach ($service->getStorages() as $storage) { |
|
150 | - $mountPoints[] = self::prepareMountPointEntry($storage, true); |
|
151 | - } |
|
152 | - |
|
153 | - return $mountPoints; |
|
154 | - } |
|
155 | - |
|
156 | - /** |
|
157 | - * Convert a StorageConfig to the legacy mountPoints array format |
|
158 | - * There's a lot of extra information in here, to satisfy all of the legacy functions |
|
159 | - * |
|
160 | - * @param StorageConfig $storage |
|
161 | - * @param bool $isPersonal |
|
162 | - * @return array |
|
163 | - */ |
|
164 | - private static function prepareMountPointEntry(StorageConfig $storage, $isPersonal) { |
|
165 | - $mountEntry = []; |
|
166 | - |
|
167 | - $mountEntry['mountpoint'] = substr($storage->getMountPoint(), 1); // remove leading slash |
|
168 | - $mountEntry['class'] = $storage->getBackend()->getIdentifier(); |
|
169 | - $mountEntry['backend'] = $storage->getBackend()->getText(); |
|
170 | - $mountEntry['authMechanism'] = $storage->getAuthMechanism()->getIdentifier(); |
|
171 | - $mountEntry['personal'] = $isPersonal; |
|
172 | - $mountEntry['options'] = self::decryptPasswords($storage->getBackendOptions()); |
|
173 | - $mountEntry['mountOptions'] = $storage->getMountOptions(); |
|
174 | - $mountEntry['priority'] = $storage->getPriority(); |
|
175 | - $mountEntry['applicable'] = [ |
|
176 | - 'groups' => $storage->getApplicableGroups(), |
|
177 | - 'users' => $storage->getApplicableUsers(), |
|
178 | - ]; |
|
179 | - // if mountpoint is applicable to all users the old API expects ['all'] |
|
180 | - if (empty($mountEntry['applicable']['groups']) && empty($mountEntry['applicable']['users'])) { |
|
181 | - $mountEntry['applicable']['users'] = ['all']; |
|
182 | - } |
|
183 | - |
|
184 | - $mountEntry['id'] = $storage->getId(); |
|
185 | - |
|
186 | - return $mountEntry; |
|
187 | - } |
|
188 | - |
|
189 | - /** |
|
190 | - * fill in the correct values for $user |
|
191 | - * |
|
192 | - * @param string $user user value |
|
193 | - * @param string|array $input |
|
194 | - * @return string |
|
195 | - */ |
|
196 | - public static function setUserVars($user, $input) { |
|
197 | - if (is_array($input)) { |
|
198 | - foreach ($input as &$value) { |
|
199 | - if (is_string($value)) { |
|
200 | - $value = str_replace('$user', $user, $value); |
|
201 | - } |
|
202 | - } |
|
203 | - } else { |
|
204 | - if (is_string($input)) { |
|
205 | - $input = str_replace('$user', $user, $input); |
|
206 | - } |
|
207 | - } |
|
208 | - return $input; |
|
209 | - } |
|
210 | - |
|
211 | - /** |
|
212 | - * Test connecting using the given backend configuration |
|
213 | - * |
|
214 | - * @param string $class backend class name |
|
215 | - * @param array $options backend configuration options |
|
216 | - * @param boolean $isPersonal |
|
217 | - * @return int see self::STATUS_* |
|
218 | - * @throws Exception |
|
219 | - */ |
|
220 | - public static function getBackendStatus($class, $options, $isPersonal, $testOnly = true) { |
|
221 | - if (self::$skipTest) { |
|
222 | - return StorageNotAvailableException::STATUS_SUCCESS; |
|
223 | - } |
|
224 | - foreach ($options as &$option) { |
|
225 | - $option = self::setUserVars(OCP\User::getUser(), $option); |
|
226 | - } |
|
227 | - if (class_exists($class)) { |
|
228 | - try { |
|
229 | - /** @var \OC\Files\Storage\Common $storage */ |
|
230 | - $storage = new $class($options); |
|
231 | - |
|
232 | - try { |
|
233 | - $result = $storage->test($isPersonal, $testOnly); |
|
234 | - $storage->setAvailability($result); |
|
235 | - if ($result) { |
|
236 | - return StorageNotAvailableException::STATUS_SUCCESS; |
|
237 | - } |
|
238 | - } catch (\Exception $e) { |
|
239 | - $storage->setAvailability(false); |
|
240 | - throw $e; |
|
241 | - } |
|
242 | - } catch (Exception $exception) { |
|
243 | - \OCP\Util::logException('files_external', $exception); |
|
244 | - throw $exception; |
|
245 | - } |
|
246 | - } |
|
247 | - return StorageNotAvailableException::STATUS_ERROR; |
|
248 | - } |
|
249 | - |
|
250 | - /** |
|
251 | - * Read the mount points in the config file into an array |
|
252 | - * |
|
253 | - * @param string|null $user If not null, personal for $user, otherwise system |
|
254 | - * @return array |
|
255 | - */ |
|
256 | - public static function readData($user = null) { |
|
257 | - if (isset($user)) { |
|
258 | - $jsonFile = \OC::$server->getUserManager()->get($user)->getHome() . '/mount.json'; |
|
259 | - } else { |
|
260 | - $config = \OC::$server->getConfig(); |
|
261 | - $datadir = $config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data/'); |
|
262 | - $jsonFile = $config->getSystemValue('mount_file', $datadir . '/mount.json'); |
|
263 | - } |
|
264 | - if (is_file($jsonFile)) { |
|
265 | - $mountPoints = json_decode(file_get_contents($jsonFile), true); |
|
266 | - if (is_array($mountPoints)) { |
|
267 | - return $mountPoints; |
|
268 | - } |
|
269 | - } |
|
270 | - return array(); |
|
271 | - } |
|
272 | - |
|
273 | - /** |
|
274 | - * Get backend dependency message |
|
275 | - * TODO: move into AppFramework along with templates |
|
276 | - * |
|
277 | - * @param Backend[] $backends |
|
278 | - * @return string |
|
279 | - */ |
|
280 | - public static function dependencyMessage($backends) { |
|
281 | - $l = \OC::$server->getL10N('files_external'); |
|
282 | - $message = ''; |
|
283 | - $dependencyGroups = []; |
|
284 | - |
|
285 | - foreach ($backends as $backend) { |
|
286 | - foreach ($backend->checkDependencies() as $dependency) { |
|
287 | - if ($message = $dependency->getMessage()) { |
|
288 | - $message .= '<p>' . $message . '</p>'; |
|
289 | - } else { |
|
290 | - $dependencyGroups[$dependency->getDependency()][] = $backend; |
|
291 | - } |
|
292 | - } |
|
293 | - } |
|
294 | - |
|
295 | - foreach ($dependencyGroups as $module => $dependants) { |
|
296 | - $backends = implode(', ', array_map(function($backend) { |
|
297 | - return '"' . $backend->getText() . '"'; |
|
298 | - }, $dependants)); |
|
299 | - $message .= '<p>' . OC_Mount_Config::getSingleDependencyMessage($l, $module, $backends) . '</p>'; |
|
300 | - } |
|
301 | - |
|
302 | - return $message; |
|
303 | - } |
|
304 | - |
|
305 | - /** |
|
306 | - * Returns a dependency missing message |
|
307 | - * |
|
308 | - * @param \OCP\IL10N $l |
|
309 | - * @param string $module |
|
310 | - * @param string $backend |
|
311 | - * @return string |
|
312 | - */ |
|
313 | - private static function getSingleDependencyMessage(\OCP\IL10N $l, $module, $backend) { |
|
314 | - switch (strtolower($module)) { |
|
315 | - case 'curl': |
|
316 | - return (string)$l->t('The cURL support in PHP is not enabled or installed. Mounting of %s is not possible. Please ask your system administrator to install it.', $backend); |
|
317 | - case 'ftp': |
|
318 | - return (string)$l->t('The FTP support in PHP is not enabled or installed. Mounting of %s is not possible. Please ask your system administrator to install it.', $backend); |
|
319 | - default: |
|
320 | - return (string)$l->t('"%s" is not installed. Mounting of %s is not possible. Please ask your system administrator to install it.', array($module, $backend)); |
|
321 | - } |
|
322 | - } |
|
323 | - |
|
324 | - /** |
|
325 | - * Encrypt passwords in the given config options |
|
326 | - * |
|
327 | - * @param array $options mount options |
|
328 | - * @return array updated options |
|
329 | - */ |
|
330 | - public static function encryptPasswords($options) { |
|
331 | - if (isset($options['password'])) { |
|
332 | - $options['password_encrypted'] = self::encryptPassword($options['password']); |
|
333 | - // do not unset the password, we want to keep the keys order |
|
334 | - // on load... because that's how the UI currently works |
|
335 | - $options['password'] = ''; |
|
336 | - } |
|
337 | - return $options; |
|
338 | - } |
|
339 | - |
|
340 | - /** |
|
341 | - * Decrypt passwords in the given config options |
|
342 | - * |
|
343 | - * @param array $options mount options |
|
344 | - * @return array updated options |
|
345 | - */ |
|
346 | - public static function decryptPasswords($options) { |
|
347 | - // note: legacy options might still have the unencrypted password in the "password" field |
|
348 | - if (isset($options['password_encrypted'])) { |
|
349 | - $options['password'] = self::decryptPassword($options['password_encrypted']); |
|
350 | - unset($options['password_encrypted']); |
|
351 | - } |
|
352 | - return $options; |
|
353 | - } |
|
354 | - |
|
355 | - /** |
|
356 | - * Encrypt a single password |
|
357 | - * |
|
358 | - * @param string $password plain text password |
|
359 | - * @return string encrypted password |
|
360 | - */ |
|
361 | - private static function encryptPassword($password) { |
|
362 | - $cipher = self::getCipher(); |
|
363 | - $iv = \OCP\Util::generateRandomBytes(16); |
|
364 | - $cipher->setIV($iv); |
|
365 | - return base64_encode($iv . $cipher->encrypt($password)); |
|
366 | - } |
|
367 | - |
|
368 | - /** |
|
369 | - * Decrypts a single password |
|
370 | - * |
|
371 | - * @param string $encryptedPassword encrypted password |
|
372 | - * @return string plain text password |
|
373 | - */ |
|
374 | - private static function decryptPassword($encryptedPassword) { |
|
375 | - $cipher = self::getCipher(); |
|
376 | - $binaryPassword = base64_decode($encryptedPassword); |
|
377 | - $iv = substr($binaryPassword, 0, 16); |
|
378 | - $cipher->setIV($iv); |
|
379 | - $binaryPassword = substr($binaryPassword, 16); |
|
380 | - return $cipher->decrypt($binaryPassword); |
|
381 | - } |
|
382 | - |
|
383 | - /** |
|
384 | - * Returns the encryption cipher |
|
385 | - * |
|
386 | - * @return AES |
|
387 | - */ |
|
388 | - private static function getCipher() { |
|
389 | - $cipher = new AES(AES::MODE_CBC); |
|
390 | - $cipher->setKey(\OC::$server->getConfig()->getSystemValue('passwordsalt', null)); |
|
391 | - return $cipher; |
|
392 | - } |
|
393 | - |
|
394 | - /** |
|
395 | - * Computes a hash based on the given configuration. |
|
396 | - * This is mostly used to find out whether configurations |
|
397 | - * are the same. |
|
398 | - * |
|
399 | - * @param array $config |
|
400 | - * @return string |
|
401 | - */ |
|
402 | - public static function makeConfigHash($config) { |
|
403 | - $data = json_encode( |
|
404 | - array( |
|
405 | - 'c' => $config['backend'], |
|
406 | - 'a' => $config['authMechanism'], |
|
407 | - 'm' => $config['mountpoint'], |
|
408 | - 'o' => $config['options'], |
|
409 | - 'p' => isset($config['priority']) ? $config['priority'] : -1, |
|
410 | - 'mo' => isset($config['mountOptions']) ? $config['mountOptions'] : [], |
|
411 | - ) |
|
412 | - ); |
|
413 | - return hash('md5', $data); |
|
414 | - } |
|
48 | + // TODO: make this class non-static and give it a proper namespace |
|
49 | + |
|
50 | + const MOUNT_TYPE_GLOBAL = 'global'; |
|
51 | + const MOUNT_TYPE_GROUP = 'group'; |
|
52 | + const MOUNT_TYPE_USER = 'user'; |
|
53 | + const MOUNT_TYPE_PERSONAL = 'personal'; |
|
54 | + |
|
55 | + // whether to skip backend test (for unit tests, as this static class is not mockable) |
|
56 | + public static $skipTest = false; |
|
57 | + |
|
58 | + /** @var Application */ |
|
59 | + public static $app; |
|
60 | + |
|
61 | + /** |
|
62 | + * @param string $class |
|
63 | + * @param array $definition |
|
64 | + * @return bool |
|
65 | + * @deprecated 8.2.0 use \OCA\Files_External\Service\BackendService::registerBackend() |
|
66 | + */ |
|
67 | + public static function registerBackend($class, $definition) { |
|
68 | + $backendService = self::$app->getContainer()->query('OCA\Files_External\Service\BackendService'); |
|
69 | + $auth = self::$app->getContainer()->query('OCA\Files_External\Lib\Auth\Builtin'); |
|
70 | + |
|
71 | + $backendService->registerBackend(new LegacyBackend($class, $definition, $auth)); |
|
72 | + |
|
73 | + return true; |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * Returns the mount points for the given user. |
|
78 | + * The mount point is relative to the data directory. |
|
79 | + * |
|
80 | + * @param string $uid user |
|
81 | + * @return array of mount point string as key, mountpoint config as value |
|
82 | + * |
|
83 | + * @deprecated 8.2.0 use UserGlobalStoragesService::getStorages() and UserStoragesService::getStorages() |
|
84 | + */ |
|
85 | + public static function getAbsoluteMountPoints($uid) { |
|
86 | + $mountPoints = array(); |
|
87 | + |
|
88 | + $userGlobalStoragesService = self::$app->getContainer()->query('OCA\Files_External\Service\UserGlobalStoragesService'); |
|
89 | + $userStoragesService = self::$app->getContainer()->query('OCA\Files_External\Service\UserStoragesService'); |
|
90 | + $user = self::$app->getContainer()->query('OCP\IUserManager')->get($uid); |
|
91 | + |
|
92 | + $userGlobalStoragesService->setUser($user); |
|
93 | + $userStoragesService->setUser($user); |
|
94 | + |
|
95 | + foreach ($userGlobalStoragesService->getStorages() as $storage) { |
|
96 | + /** @var \OCA\Files_External\Lib\StorageConfig $storage */ |
|
97 | + $mountPoint = '/'.$uid.'/files'.$storage->getMountPoint(); |
|
98 | + $mountEntry = self::prepareMountPointEntry($storage, false); |
|
99 | + foreach ($mountEntry['options'] as &$option) { |
|
100 | + $option = self::setUserVars($uid, $option); |
|
101 | + } |
|
102 | + $mountPoints[$mountPoint] = $mountEntry; |
|
103 | + } |
|
104 | + |
|
105 | + foreach ($userStoragesService->getStorages() as $storage) { |
|
106 | + $mountPoint = '/'.$uid.'/files'.$storage->getMountPoint(); |
|
107 | + $mountEntry = self::prepareMountPointEntry($storage, true); |
|
108 | + foreach ($mountEntry['options'] as &$option) { |
|
109 | + $option = self::setUserVars($uid, $option); |
|
110 | + } |
|
111 | + $mountPoints[$mountPoint] = $mountEntry; |
|
112 | + } |
|
113 | + |
|
114 | + $userGlobalStoragesService->resetUser(); |
|
115 | + $userStoragesService->resetUser(); |
|
116 | + |
|
117 | + return $mountPoints; |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * Get the system mount points |
|
122 | + * |
|
123 | + * @return array |
|
124 | + * |
|
125 | + * @deprecated 8.2.0 use GlobalStoragesService::getStorages() |
|
126 | + */ |
|
127 | + public static function getSystemMountPoints() { |
|
128 | + $mountPoints = []; |
|
129 | + $service = self::$app->getContainer()->query('OCA\Files_External\Service\GlobalStoragesService'); |
|
130 | + |
|
131 | + foreach ($service->getStorages() as $storage) { |
|
132 | + $mountPoints[] = self::prepareMountPointEntry($storage, false); |
|
133 | + } |
|
134 | + |
|
135 | + return $mountPoints; |
|
136 | + } |
|
137 | + |
|
138 | + /** |
|
139 | + * Get the personal mount points of the current user |
|
140 | + * |
|
141 | + * @return array |
|
142 | + * |
|
143 | + * @deprecated 8.2.0 use UserStoragesService::getStorages() |
|
144 | + */ |
|
145 | + public static function getPersonalMountPoints() { |
|
146 | + $mountPoints = []; |
|
147 | + $service = self::$app->getContainer()->query('OCA\Files_External\Service\UserStoragesService'); |
|
148 | + |
|
149 | + foreach ($service->getStorages() as $storage) { |
|
150 | + $mountPoints[] = self::prepareMountPointEntry($storage, true); |
|
151 | + } |
|
152 | + |
|
153 | + return $mountPoints; |
|
154 | + } |
|
155 | + |
|
156 | + /** |
|
157 | + * Convert a StorageConfig to the legacy mountPoints array format |
|
158 | + * There's a lot of extra information in here, to satisfy all of the legacy functions |
|
159 | + * |
|
160 | + * @param StorageConfig $storage |
|
161 | + * @param bool $isPersonal |
|
162 | + * @return array |
|
163 | + */ |
|
164 | + private static function prepareMountPointEntry(StorageConfig $storage, $isPersonal) { |
|
165 | + $mountEntry = []; |
|
166 | + |
|
167 | + $mountEntry['mountpoint'] = substr($storage->getMountPoint(), 1); // remove leading slash |
|
168 | + $mountEntry['class'] = $storage->getBackend()->getIdentifier(); |
|
169 | + $mountEntry['backend'] = $storage->getBackend()->getText(); |
|
170 | + $mountEntry['authMechanism'] = $storage->getAuthMechanism()->getIdentifier(); |
|
171 | + $mountEntry['personal'] = $isPersonal; |
|
172 | + $mountEntry['options'] = self::decryptPasswords($storage->getBackendOptions()); |
|
173 | + $mountEntry['mountOptions'] = $storage->getMountOptions(); |
|
174 | + $mountEntry['priority'] = $storage->getPriority(); |
|
175 | + $mountEntry['applicable'] = [ |
|
176 | + 'groups' => $storage->getApplicableGroups(), |
|
177 | + 'users' => $storage->getApplicableUsers(), |
|
178 | + ]; |
|
179 | + // if mountpoint is applicable to all users the old API expects ['all'] |
|
180 | + if (empty($mountEntry['applicable']['groups']) && empty($mountEntry['applicable']['users'])) { |
|
181 | + $mountEntry['applicable']['users'] = ['all']; |
|
182 | + } |
|
183 | + |
|
184 | + $mountEntry['id'] = $storage->getId(); |
|
185 | + |
|
186 | + return $mountEntry; |
|
187 | + } |
|
188 | + |
|
189 | + /** |
|
190 | + * fill in the correct values for $user |
|
191 | + * |
|
192 | + * @param string $user user value |
|
193 | + * @param string|array $input |
|
194 | + * @return string |
|
195 | + */ |
|
196 | + public static function setUserVars($user, $input) { |
|
197 | + if (is_array($input)) { |
|
198 | + foreach ($input as &$value) { |
|
199 | + if (is_string($value)) { |
|
200 | + $value = str_replace('$user', $user, $value); |
|
201 | + } |
|
202 | + } |
|
203 | + } else { |
|
204 | + if (is_string($input)) { |
|
205 | + $input = str_replace('$user', $user, $input); |
|
206 | + } |
|
207 | + } |
|
208 | + return $input; |
|
209 | + } |
|
210 | + |
|
211 | + /** |
|
212 | + * Test connecting using the given backend configuration |
|
213 | + * |
|
214 | + * @param string $class backend class name |
|
215 | + * @param array $options backend configuration options |
|
216 | + * @param boolean $isPersonal |
|
217 | + * @return int see self::STATUS_* |
|
218 | + * @throws Exception |
|
219 | + */ |
|
220 | + public static function getBackendStatus($class, $options, $isPersonal, $testOnly = true) { |
|
221 | + if (self::$skipTest) { |
|
222 | + return StorageNotAvailableException::STATUS_SUCCESS; |
|
223 | + } |
|
224 | + foreach ($options as &$option) { |
|
225 | + $option = self::setUserVars(OCP\User::getUser(), $option); |
|
226 | + } |
|
227 | + if (class_exists($class)) { |
|
228 | + try { |
|
229 | + /** @var \OC\Files\Storage\Common $storage */ |
|
230 | + $storage = new $class($options); |
|
231 | + |
|
232 | + try { |
|
233 | + $result = $storage->test($isPersonal, $testOnly); |
|
234 | + $storage->setAvailability($result); |
|
235 | + if ($result) { |
|
236 | + return StorageNotAvailableException::STATUS_SUCCESS; |
|
237 | + } |
|
238 | + } catch (\Exception $e) { |
|
239 | + $storage->setAvailability(false); |
|
240 | + throw $e; |
|
241 | + } |
|
242 | + } catch (Exception $exception) { |
|
243 | + \OCP\Util::logException('files_external', $exception); |
|
244 | + throw $exception; |
|
245 | + } |
|
246 | + } |
|
247 | + return StorageNotAvailableException::STATUS_ERROR; |
|
248 | + } |
|
249 | + |
|
250 | + /** |
|
251 | + * Read the mount points in the config file into an array |
|
252 | + * |
|
253 | + * @param string|null $user If not null, personal for $user, otherwise system |
|
254 | + * @return array |
|
255 | + */ |
|
256 | + public static function readData($user = null) { |
|
257 | + if (isset($user)) { |
|
258 | + $jsonFile = \OC::$server->getUserManager()->get($user)->getHome() . '/mount.json'; |
|
259 | + } else { |
|
260 | + $config = \OC::$server->getConfig(); |
|
261 | + $datadir = $config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data/'); |
|
262 | + $jsonFile = $config->getSystemValue('mount_file', $datadir . '/mount.json'); |
|
263 | + } |
|
264 | + if (is_file($jsonFile)) { |
|
265 | + $mountPoints = json_decode(file_get_contents($jsonFile), true); |
|
266 | + if (is_array($mountPoints)) { |
|
267 | + return $mountPoints; |
|
268 | + } |
|
269 | + } |
|
270 | + return array(); |
|
271 | + } |
|
272 | + |
|
273 | + /** |
|
274 | + * Get backend dependency message |
|
275 | + * TODO: move into AppFramework along with templates |
|
276 | + * |
|
277 | + * @param Backend[] $backends |
|
278 | + * @return string |
|
279 | + */ |
|
280 | + public static function dependencyMessage($backends) { |
|
281 | + $l = \OC::$server->getL10N('files_external'); |
|
282 | + $message = ''; |
|
283 | + $dependencyGroups = []; |
|
284 | + |
|
285 | + foreach ($backends as $backend) { |
|
286 | + foreach ($backend->checkDependencies() as $dependency) { |
|
287 | + if ($message = $dependency->getMessage()) { |
|
288 | + $message .= '<p>' . $message . '</p>'; |
|
289 | + } else { |
|
290 | + $dependencyGroups[$dependency->getDependency()][] = $backend; |
|
291 | + } |
|
292 | + } |
|
293 | + } |
|
294 | + |
|
295 | + foreach ($dependencyGroups as $module => $dependants) { |
|
296 | + $backends = implode(', ', array_map(function($backend) { |
|
297 | + return '"' . $backend->getText() . '"'; |
|
298 | + }, $dependants)); |
|
299 | + $message .= '<p>' . OC_Mount_Config::getSingleDependencyMessage($l, $module, $backends) . '</p>'; |
|
300 | + } |
|
301 | + |
|
302 | + return $message; |
|
303 | + } |
|
304 | + |
|
305 | + /** |
|
306 | + * Returns a dependency missing message |
|
307 | + * |
|
308 | + * @param \OCP\IL10N $l |
|
309 | + * @param string $module |
|
310 | + * @param string $backend |
|
311 | + * @return string |
|
312 | + */ |
|
313 | + private static function getSingleDependencyMessage(\OCP\IL10N $l, $module, $backend) { |
|
314 | + switch (strtolower($module)) { |
|
315 | + case 'curl': |
|
316 | + return (string)$l->t('The cURL support in PHP is not enabled or installed. Mounting of %s is not possible. Please ask your system administrator to install it.', $backend); |
|
317 | + case 'ftp': |
|
318 | + return (string)$l->t('The FTP support in PHP is not enabled or installed. Mounting of %s is not possible. Please ask your system administrator to install it.', $backend); |
|
319 | + default: |
|
320 | + return (string)$l->t('"%s" is not installed. Mounting of %s is not possible. Please ask your system administrator to install it.', array($module, $backend)); |
|
321 | + } |
|
322 | + } |
|
323 | + |
|
324 | + /** |
|
325 | + * Encrypt passwords in the given config options |
|
326 | + * |
|
327 | + * @param array $options mount options |
|
328 | + * @return array updated options |
|
329 | + */ |
|
330 | + public static function encryptPasswords($options) { |
|
331 | + if (isset($options['password'])) { |
|
332 | + $options['password_encrypted'] = self::encryptPassword($options['password']); |
|
333 | + // do not unset the password, we want to keep the keys order |
|
334 | + // on load... because that's how the UI currently works |
|
335 | + $options['password'] = ''; |
|
336 | + } |
|
337 | + return $options; |
|
338 | + } |
|
339 | + |
|
340 | + /** |
|
341 | + * Decrypt passwords in the given config options |
|
342 | + * |
|
343 | + * @param array $options mount options |
|
344 | + * @return array updated options |
|
345 | + */ |
|
346 | + public static function decryptPasswords($options) { |
|
347 | + // note: legacy options might still have the unencrypted password in the "password" field |
|
348 | + if (isset($options['password_encrypted'])) { |
|
349 | + $options['password'] = self::decryptPassword($options['password_encrypted']); |
|
350 | + unset($options['password_encrypted']); |
|
351 | + } |
|
352 | + return $options; |
|
353 | + } |
|
354 | + |
|
355 | + /** |
|
356 | + * Encrypt a single password |
|
357 | + * |
|
358 | + * @param string $password plain text password |
|
359 | + * @return string encrypted password |
|
360 | + */ |
|
361 | + private static function encryptPassword($password) { |
|
362 | + $cipher = self::getCipher(); |
|
363 | + $iv = \OCP\Util::generateRandomBytes(16); |
|
364 | + $cipher->setIV($iv); |
|
365 | + return base64_encode($iv . $cipher->encrypt($password)); |
|
366 | + } |
|
367 | + |
|
368 | + /** |
|
369 | + * Decrypts a single password |
|
370 | + * |
|
371 | + * @param string $encryptedPassword encrypted password |
|
372 | + * @return string plain text password |
|
373 | + */ |
|
374 | + private static function decryptPassword($encryptedPassword) { |
|
375 | + $cipher = self::getCipher(); |
|
376 | + $binaryPassword = base64_decode($encryptedPassword); |
|
377 | + $iv = substr($binaryPassword, 0, 16); |
|
378 | + $cipher->setIV($iv); |
|
379 | + $binaryPassword = substr($binaryPassword, 16); |
|
380 | + return $cipher->decrypt($binaryPassword); |
|
381 | + } |
|
382 | + |
|
383 | + /** |
|
384 | + * Returns the encryption cipher |
|
385 | + * |
|
386 | + * @return AES |
|
387 | + */ |
|
388 | + private static function getCipher() { |
|
389 | + $cipher = new AES(AES::MODE_CBC); |
|
390 | + $cipher->setKey(\OC::$server->getConfig()->getSystemValue('passwordsalt', null)); |
|
391 | + return $cipher; |
|
392 | + } |
|
393 | + |
|
394 | + /** |
|
395 | + * Computes a hash based on the given configuration. |
|
396 | + * This is mostly used to find out whether configurations |
|
397 | + * are the same. |
|
398 | + * |
|
399 | + * @param array $config |
|
400 | + * @return string |
|
401 | + */ |
|
402 | + public static function makeConfigHash($config) { |
|
403 | + $data = json_encode( |
|
404 | + array( |
|
405 | + 'c' => $config['backend'], |
|
406 | + 'a' => $config['authMechanism'], |
|
407 | + 'm' => $config['mountpoint'], |
|
408 | + 'o' => $config['options'], |
|
409 | + 'p' => isset($config['priority']) ? $config['priority'] : -1, |
|
410 | + 'mo' => isset($config['mountOptions']) ? $config['mountOptions'] : [], |
|
411 | + ) |
|
412 | + ); |
|
413 | + return hash('md5', $data); |
|
414 | + } |
|
415 | 415 | } |
@@ -28,26 +28,26 @@ |
||
28 | 28 | |
29 | 29 | class DummyUserSession implements IUserSession { |
30 | 30 | |
31 | - /** |
|
32 | - * @var IUser |
|
33 | - */ |
|
34 | - private $user; |
|
31 | + /** |
|
32 | + * @var IUser |
|
33 | + */ |
|
34 | + private $user; |
|
35 | 35 | |
36 | - public function login($user, $password) { |
|
37 | - } |
|
36 | + public function login($user, $password) { |
|
37 | + } |
|
38 | 38 | |
39 | - public function logout() { |
|
40 | - } |
|
39 | + public function logout() { |
|
40 | + } |
|
41 | 41 | |
42 | - public function setUser($user) { |
|
43 | - $this->user = $user; |
|
44 | - } |
|
42 | + public function setUser($user) { |
|
43 | + $this->user = $user; |
|
44 | + } |
|
45 | 45 | |
46 | - public function getUser() { |
|
47 | - return $this->user; |
|
48 | - } |
|
46 | + public function getUser() { |
|
47 | + return $this->user; |
|
48 | + } |
|
49 | 49 | |
50 | - public function isLoggedIn() { |
|
51 | - return !is_null($this->user); |
|
52 | - } |
|
50 | + public function isLoggedIn() { |
|
51 | + return !is_null($this->user); |
|
52 | + } |
|
53 | 53 | } |