@@ -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 | |
@@ -41,490 +41,490 @@ |
||
| 41 | 41 | */ |
| 42 | 42 | abstract class StoragesService { |
| 43 | 43 | |
| 44 | - /** @var BackendService */ |
|
| 45 | - protected $backendService; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * @var DBConfigService |
|
| 49 | - */ |
|
| 50 | - protected $dbConfig; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * @var IUserMountCache |
|
| 54 | - */ |
|
| 55 | - protected $userMountCache; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * @param BackendService $backendService |
|
| 59 | - * @param DBConfigService $dbConfigService |
|
| 60 | - * @param IUserMountCache $userMountCache |
|
| 61 | - */ |
|
| 62 | - public function __construct(BackendService $backendService, DBConfigService $dbConfigService, IUserMountCache $userMountCache) { |
|
| 63 | - $this->backendService = $backendService; |
|
| 64 | - $this->dbConfig = $dbConfigService; |
|
| 65 | - $this->userMountCache = $userMountCache; |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - protected function readDBConfig() { |
|
| 69 | - return $this->dbConfig->getAdminMounts(); |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - protected function getStorageConfigFromDBMount(array $mount) { |
|
| 73 | - $applicableUsers = array_filter($mount['applicable'], function ($applicable) { |
|
| 74 | - return $applicable['type'] === DBConfigService::APPLICABLE_TYPE_USER; |
|
| 75 | - }); |
|
| 76 | - $applicableUsers = array_map(function ($applicable) { |
|
| 77 | - return $applicable['value']; |
|
| 78 | - }, $applicableUsers); |
|
| 79 | - |
|
| 80 | - $applicableGroups = array_filter($mount['applicable'], function ($applicable) { |
|
| 81 | - return $applicable['type'] === DBConfigService::APPLICABLE_TYPE_GROUP; |
|
| 82 | - }); |
|
| 83 | - $applicableGroups = array_map(function ($applicable) { |
|
| 84 | - return $applicable['value']; |
|
| 85 | - }, $applicableGroups); |
|
| 86 | - |
|
| 87 | - try { |
|
| 88 | - $config = $this->createStorage( |
|
| 89 | - $mount['mount_point'], |
|
| 90 | - $mount['storage_backend'], |
|
| 91 | - $mount['auth_backend'], |
|
| 92 | - $mount['config'], |
|
| 93 | - $mount['options'], |
|
| 94 | - array_values($applicableUsers), |
|
| 95 | - array_values($applicableGroups), |
|
| 96 | - $mount['priority'] |
|
| 97 | - ); |
|
| 98 | - $config->setType($mount['type']); |
|
| 99 | - $config->setId((int)$mount['mount_id']); |
|
| 100 | - return $config; |
|
| 101 | - } catch (\UnexpectedValueException $e) { |
|
| 102 | - // don't die if a storage backend doesn't exist |
|
| 103 | - \OCP\Util::writeLog( |
|
| 104 | - 'files_external', |
|
| 105 | - 'Could not load storage: "' . $e->getMessage() . '"', |
|
| 106 | - \OCP\Util::ERROR |
|
| 107 | - ); |
|
| 108 | - return null; |
|
| 109 | - } catch (\InvalidArgumentException $e) { |
|
| 110 | - \OCP\Util::writeLog( |
|
| 111 | - 'files_external', |
|
| 112 | - 'Could not load storage: "' . $e->getMessage() . '"', |
|
| 113 | - \OCP\Util::ERROR |
|
| 114 | - ); |
|
| 115 | - return null; |
|
| 116 | - } |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * Read the external storages config |
|
| 121 | - * |
|
| 122 | - * @return array map of storage id to storage config |
|
| 123 | - */ |
|
| 124 | - protected function readConfig() { |
|
| 125 | - $mounts = $this->readDBConfig(); |
|
| 126 | - $configs = array_map([$this, 'getStorageConfigFromDBMount'], $mounts); |
|
| 127 | - $configs = array_filter($configs, function ($config) { |
|
| 128 | - return $config instanceof StorageConfig; |
|
| 129 | - }); |
|
| 130 | - |
|
| 131 | - $keys = array_map(function (StorageConfig $config) { |
|
| 132 | - return $config->getId(); |
|
| 133 | - }, $configs); |
|
| 134 | - |
|
| 135 | - return array_combine($keys, $configs); |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * Get a storage with status |
|
| 140 | - * |
|
| 141 | - * @param int $id storage id |
|
| 142 | - * |
|
| 143 | - * @return StorageConfig |
|
| 144 | - * @throws NotFoundException if the storage with the given id was not found |
|
| 145 | - */ |
|
| 146 | - public function getStorage($id) { |
|
| 147 | - $mount = $this->dbConfig->getMountById($id); |
|
| 148 | - |
|
| 149 | - if (!is_array($mount)) { |
|
| 150 | - throw new NotFoundException('Storage with id "' . $id . '" not found'); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - $config = $this->getStorageConfigFromDBMount($mount); |
|
| 154 | - if ($this->isApplicable($config)) { |
|
| 155 | - return $config; |
|
| 156 | - } else { |
|
| 157 | - throw new NotFoundException('Storage with id "' . $id . '" not found'); |
|
| 158 | - } |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * Check whether this storage service should provide access to a storage |
|
| 163 | - * |
|
| 164 | - * @param StorageConfig $config |
|
| 165 | - * @return bool |
|
| 166 | - */ |
|
| 167 | - abstract protected function isApplicable(StorageConfig $config); |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * Gets all storages, valid or not |
|
| 171 | - * |
|
| 172 | - * @return StorageConfig[] array of storage configs |
|
| 173 | - */ |
|
| 174 | - public function getAllStorages() { |
|
| 175 | - return $this->readConfig(); |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * Gets all valid storages |
|
| 180 | - * |
|
| 181 | - * @return StorageConfig[] |
|
| 182 | - */ |
|
| 183 | - public function getStorages() { |
|
| 184 | - return array_filter($this->getAllStorages(), [$this, 'validateStorage']); |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - /** |
|
| 188 | - * Validate storage |
|
| 189 | - * FIXME: De-duplicate with StoragesController::validate() |
|
| 190 | - * |
|
| 191 | - * @param StorageConfig $storage |
|
| 192 | - * @return bool |
|
| 193 | - */ |
|
| 194 | - protected function validateStorage(StorageConfig $storage) { |
|
| 195 | - /** @var Backend */ |
|
| 196 | - $backend = $storage->getBackend(); |
|
| 197 | - /** @var AuthMechanism */ |
|
| 198 | - $authMechanism = $storage->getAuthMechanism(); |
|
| 199 | - |
|
| 200 | - if (!$backend->isVisibleFor($this->getVisibilityType())) { |
|
| 201 | - // not permitted to use backend |
|
| 202 | - return false; |
|
| 203 | - } |
|
| 204 | - if (!$authMechanism->isVisibleFor($this->getVisibilityType())) { |
|
| 205 | - // not permitted to use auth mechanism |
|
| 206 | - return false; |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - return true; |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - /** |
|
| 213 | - * Get the visibility type for this controller, used in validation |
|
| 214 | - * |
|
| 215 | - * @return string BackendService::VISIBILITY_* constants |
|
| 216 | - */ |
|
| 217 | - abstract public function getVisibilityType(); |
|
| 218 | - |
|
| 219 | - /** |
|
| 220 | - * @return integer |
|
| 221 | - */ |
|
| 222 | - protected function getType() { |
|
| 223 | - return DBConfigService::MOUNT_TYPE_ADMIN; |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * Add new storage to the configuration |
|
| 228 | - * |
|
| 229 | - * @param StorageConfig $newStorage storage attributes |
|
| 230 | - * |
|
| 231 | - * @return StorageConfig storage config, with added id |
|
| 232 | - */ |
|
| 233 | - public function addStorage(StorageConfig $newStorage) { |
|
| 234 | - $allStorages = $this->readConfig(); |
|
| 235 | - |
|
| 236 | - $configId = $this->dbConfig->addMount( |
|
| 237 | - $newStorage->getMountPoint(), |
|
| 238 | - $newStorage->getBackend()->getIdentifier(), |
|
| 239 | - $newStorage->getAuthMechanism()->getIdentifier(), |
|
| 240 | - $newStorage->getPriority(), |
|
| 241 | - $this->getType() |
|
| 242 | - ); |
|
| 243 | - |
|
| 244 | - $newStorage->setId($configId); |
|
| 245 | - |
|
| 246 | - foreach ($newStorage->getApplicableUsers() as $user) { |
|
| 247 | - $this->dbConfig->addApplicable($configId, DBConfigService::APPLICABLE_TYPE_USER, $user); |
|
| 248 | - } |
|
| 249 | - foreach ($newStorage->getApplicableGroups() as $group) { |
|
| 250 | - $this->dbConfig->addApplicable($configId, DBConfigService::APPLICABLE_TYPE_GROUP, $group); |
|
| 251 | - } |
|
| 252 | - foreach ($newStorage->getBackendOptions() as $key => $value) { |
|
| 253 | - $this->dbConfig->setConfig($configId, $key, $value); |
|
| 254 | - } |
|
| 255 | - foreach ($newStorage->getMountOptions() as $key => $value) { |
|
| 256 | - $this->dbConfig->setOption($configId, $key, $value); |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - if (count($newStorage->getApplicableUsers()) === 0 && count($newStorage->getApplicableGroups()) === 0) { |
|
| 260 | - $this->dbConfig->addApplicable($configId, DBConfigService::APPLICABLE_TYPE_GLOBAL, null); |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - // add new storage |
|
| 264 | - $allStorages[$configId] = $newStorage; |
|
| 265 | - |
|
| 266 | - $this->triggerHooks($newStorage, Filesystem::signal_create_mount); |
|
| 267 | - |
|
| 268 | - $newStorage->setStatus(StorageNotAvailableException::STATUS_SUCCESS); |
|
| 269 | - return $newStorage; |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - /** |
|
| 273 | - * Create a storage from its parameters |
|
| 274 | - * |
|
| 275 | - * @param string $mountPoint storage mount point |
|
| 276 | - * @param string $backendIdentifier backend identifier |
|
| 277 | - * @param string $authMechanismIdentifier authentication mechanism identifier |
|
| 278 | - * @param array $backendOptions backend-specific options |
|
| 279 | - * @param array|null $mountOptions mount-specific options |
|
| 280 | - * @param array|null $applicableUsers users for which to mount the storage |
|
| 281 | - * @param array|null $applicableGroups groups for which to mount the storage |
|
| 282 | - * @param int|null $priority priority |
|
| 283 | - * |
|
| 284 | - * @return StorageConfig |
|
| 285 | - */ |
|
| 286 | - public function createStorage( |
|
| 287 | - $mountPoint, |
|
| 288 | - $backendIdentifier, |
|
| 289 | - $authMechanismIdentifier, |
|
| 290 | - $backendOptions, |
|
| 291 | - $mountOptions = null, |
|
| 292 | - $applicableUsers = null, |
|
| 293 | - $applicableGroups = null, |
|
| 294 | - $priority = null |
|
| 295 | - ) { |
|
| 296 | - $backend = $this->backendService->getBackend($backendIdentifier); |
|
| 297 | - if (!$backend) { |
|
| 298 | - throw new \InvalidArgumentException('Unable to get backend for ' . $backendIdentifier); |
|
| 299 | - } |
|
| 300 | - $authMechanism = $this->backendService->getAuthMechanism($authMechanismIdentifier); |
|
| 301 | - if (!$authMechanism) { |
|
| 302 | - throw new \InvalidArgumentException('Unable to get authentication mechanism for ' . $authMechanismIdentifier); |
|
| 303 | - } |
|
| 304 | - $newStorage = new StorageConfig(); |
|
| 305 | - $newStorage->setMountPoint($mountPoint); |
|
| 306 | - $newStorage->setBackend($backend); |
|
| 307 | - $newStorage->setAuthMechanism($authMechanism); |
|
| 308 | - $newStorage->setBackendOptions($backendOptions); |
|
| 309 | - if (isset($mountOptions)) { |
|
| 310 | - $newStorage->setMountOptions($mountOptions); |
|
| 311 | - } |
|
| 312 | - if (isset($applicableUsers)) { |
|
| 313 | - $newStorage->setApplicableUsers($applicableUsers); |
|
| 314 | - } |
|
| 315 | - if (isset($applicableGroups)) { |
|
| 316 | - $newStorage->setApplicableGroups($applicableGroups); |
|
| 317 | - } |
|
| 318 | - if (isset($priority)) { |
|
| 319 | - $newStorage->setPriority($priority); |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - return $newStorage; |
|
| 323 | - } |
|
| 324 | - |
|
| 325 | - /** |
|
| 326 | - * Triggers the given hook signal for all the applicables given |
|
| 327 | - * |
|
| 328 | - * @param string $signal signal |
|
| 329 | - * @param string $mountPoint hook mount pount param |
|
| 330 | - * @param string $mountType hook mount type param |
|
| 331 | - * @param array $applicableArray array of applicable users/groups for which to trigger the hook |
|
| 332 | - */ |
|
| 333 | - protected function triggerApplicableHooks($signal, $mountPoint, $mountType, $applicableArray) { |
|
| 334 | - foreach ($applicableArray as $applicable) { |
|
| 335 | - \OCP\Util::emitHook( |
|
| 336 | - Filesystem::CLASSNAME, |
|
| 337 | - $signal, |
|
| 338 | - [ |
|
| 339 | - Filesystem::signal_param_path => $mountPoint, |
|
| 340 | - Filesystem::signal_param_mount_type => $mountType, |
|
| 341 | - Filesystem::signal_param_users => $applicable, |
|
| 342 | - ] |
|
| 343 | - ); |
|
| 344 | - } |
|
| 345 | - } |
|
| 346 | - |
|
| 347 | - /** |
|
| 348 | - * Triggers $signal for all applicable users of the given |
|
| 349 | - * storage |
|
| 350 | - * |
|
| 351 | - * @param StorageConfig $storage storage data |
|
| 352 | - * @param string $signal signal to trigger |
|
| 353 | - */ |
|
| 354 | - abstract protected function triggerHooks(StorageConfig $storage, $signal); |
|
| 355 | - |
|
| 356 | - /** |
|
| 357 | - * Triggers signal_create_mount or signal_delete_mount to |
|
| 358 | - * accommodate for additions/deletions in applicableUsers |
|
| 359 | - * and applicableGroups fields. |
|
| 360 | - * |
|
| 361 | - * @param StorageConfig $oldStorage old storage data |
|
| 362 | - * @param StorageConfig $newStorage new storage data |
|
| 363 | - */ |
|
| 364 | - abstract protected function triggerChangeHooks(StorageConfig $oldStorage, StorageConfig $newStorage); |
|
| 365 | - |
|
| 366 | - /** |
|
| 367 | - * Update storage to the configuration |
|
| 368 | - * |
|
| 369 | - * @param StorageConfig $updatedStorage storage attributes |
|
| 370 | - * |
|
| 371 | - * @return StorageConfig storage config |
|
| 372 | - * @throws NotFoundException if the given storage does not exist in the config |
|
| 373 | - */ |
|
| 374 | - public function updateStorage(StorageConfig $updatedStorage) { |
|
| 375 | - $id = $updatedStorage->getId(); |
|
| 376 | - |
|
| 377 | - $existingMount = $this->dbConfig->getMountById($id); |
|
| 378 | - |
|
| 379 | - if (!is_array($existingMount)) { |
|
| 380 | - throw new NotFoundException('Storage with id "' . $id . '" not found while updating storage'); |
|
| 381 | - } |
|
| 382 | - |
|
| 383 | - $oldStorage = $this->getStorageConfigFromDBMount($existingMount); |
|
| 384 | - |
|
| 385 | - $removedUsers = array_diff($oldStorage->getApplicableUsers(), $updatedStorage->getApplicableUsers()); |
|
| 386 | - $removedGroups = array_diff($oldStorage->getApplicableGroups(), $updatedStorage->getApplicableGroups()); |
|
| 387 | - $addedUsers = array_diff($updatedStorage->getApplicableUsers(), $oldStorage->getApplicableUsers()); |
|
| 388 | - $addedGroups = array_diff($updatedStorage->getApplicableGroups(), $oldStorage->getApplicableGroups()); |
|
| 389 | - |
|
| 390 | - $oldUserCount = count($oldStorage->getApplicableUsers()); |
|
| 391 | - $oldGroupCount = count($oldStorage->getApplicableGroups()); |
|
| 392 | - $newUserCount = count($updatedStorage->getApplicableUsers()); |
|
| 393 | - $newGroupCount = count($updatedStorage->getApplicableGroups()); |
|
| 394 | - $wasGlobal = ($oldUserCount + $oldGroupCount) === 0; |
|
| 395 | - $isGlobal = ($newUserCount + $newGroupCount) === 0; |
|
| 396 | - |
|
| 397 | - foreach ($removedUsers as $user) { |
|
| 398 | - $this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, $user); |
|
| 399 | - } |
|
| 400 | - foreach ($removedGroups as $group) { |
|
| 401 | - $this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_GROUP, $group); |
|
| 402 | - } |
|
| 403 | - foreach ($addedUsers as $user) { |
|
| 404 | - $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, $user); |
|
| 405 | - } |
|
| 406 | - foreach ($addedGroups as $group) { |
|
| 407 | - $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_GROUP, $group); |
|
| 408 | - } |
|
| 409 | - |
|
| 410 | - if ($wasGlobal && !$isGlobal) { |
|
| 411 | - $this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_GLOBAL, null); |
|
| 412 | - } else if (!$wasGlobal && $isGlobal) { |
|
| 413 | - $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_GLOBAL, null); |
|
| 414 | - } |
|
| 415 | - |
|
| 416 | - $changedConfig = array_diff_assoc($updatedStorage->getBackendOptions(), $oldStorage->getBackendOptions()); |
|
| 417 | - $changedOptions = array_diff_assoc($updatedStorage->getMountOptions(), $oldStorage->getMountOptions()); |
|
| 418 | - |
|
| 419 | - foreach ($changedConfig as $key => $value) { |
|
| 420 | - $this->dbConfig->setConfig($id, $key, $value); |
|
| 421 | - } |
|
| 422 | - foreach ($changedOptions as $key => $value) { |
|
| 423 | - $this->dbConfig->setOption($id, $key, $value); |
|
| 424 | - } |
|
| 425 | - |
|
| 426 | - if ($updatedStorage->getMountPoint() !== $oldStorage->getMountPoint()) { |
|
| 427 | - $this->dbConfig->setMountPoint($id, $updatedStorage->getMountPoint()); |
|
| 428 | - } |
|
| 429 | - |
|
| 430 | - if ($updatedStorage->getAuthMechanism()->getIdentifier() !== $oldStorage->getAuthMechanism()->getIdentifier()) { |
|
| 431 | - $this->dbConfig->setAuthBackend($id, $updatedStorage->getAuthMechanism()->getIdentifier()); |
|
| 432 | - } |
|
| 433 | - |
|
| 434 | - $this->triggerChangeHooks($oldStorage, $updatedStorage); |
|
| 435 | - |
|
| 436 | - if (($wasGlobal && !$isGlobal) || count($removedGroups) > 0) { // to expensive to properly handle these on the fly |
|
| 437 | - $this->userMountCache->remoteStorageMounts($this->getStorageId($updatedStorage)); |
|
| 438 | - } else { |
|
| 439 | - $storageId = $this->getStorageId($updatedStorage); |
|
| 440 | - foreach ($removedUsers as $userId) { |
|
| 441 | - $this->userMountCache->removeUserStorageMount($storageId, $userId); |
|
| 442 | - } |
|
| 443 | - } |
|
| 444 | - |
|
| 445 | - return $this->getStorage($id); |
|
| 446 | - } |
|
| 447 | - |
|
| 448 | - /** |
|
| 449 | - * Delete the storage with the given id. |
|
| 450 | - * |
|
| 451 | - * @param int $id storage id |
|
| 452 | - * |
|
| 453 | - * @throws NotFoundException if no storage was found with the given id |
|
| 454 | - */ |
|
| 455 | - public function removeStorage($id) { |
|
| 456 | - $existingMount = $this->dbConfig->getMountById($id); |
|
| 457 | - |
|
| 458 | - if (!is_array($existingMount)) { |
|
| 459 | - throw new NotFoundException('Storage with id "' . $id . '" not found'); |
|
| 460 | - } |
|
| 461 | - |
|
| 462 | - $this->dbConfig->removeMount($id); |
|
| 463 | - |
|
| 464 | - $deletedStorage = $this->getStorageConfigFromDBMount($existingMount); |
|
| 465 | - $this->triggerHooks($deletedStorage, Filesystem::signal_delete_mount); |
|
| 466 | - |
|
| 467 | - // delete oc_storages entries and oc_filecache |
|
| 468 | - try { |
|
| 469 | - $rustyStorageId = $this->getRustyStorageIdFromConfig($deletedStorage); |
|
| 470 | - \OC\Files\Cache\Storage::remove($rustyStorageId); |
|
| 471 | - } catch (\Exception $e) { |
|
| 472 | - // can happen either for invalid configs where the storage could not |
|
| 473 | - // be instantiated or whenever $user vars where used, in which case |
|
| 474 | - // the storage id could not be computed |
|
| 475 | - \OCP\Util::writeLog( |
|
| 476 | - 'files_external', |
|
| 477 | - 'Exception: "' . $e->getMessage() . '"', |
|
| 478 | - \OCP\Util::ERROR |
|
| 479 | - ); |
|
| 480 | - } |
|
| 481 | - } |
|
| 482 | - |
|
| 483 | - /** |
|
| 484 | - * Returns the rusty storage id from oc_storages from the given storage config. |
|
| 485 | - * |
|
| 486 | - * @param StorageConfig $storageConfig |
|
| 487 | - * @return string rusty storage id |
|
| 488 | - */ |
|
| 489 | - private function getRustyStorageIdFromConfig(StorageConfig $storageConfig) { |
|
| 490 | - // if any of the storage options contains $user, it is not possible |
|
| 491 | - // to compute the possible storage id as we don't know which users |
|
| 492 | - // mounted it already (and we certainly don't want to iterate over ALL users) |
|
| 493 | - foreach ($storageConfig->getBackendOptions() as $value) { |
|
| 494 | - if (strpos($value, '$user') !== false) { |
|
| 495 | - throw new \Exception('Cannot compute storage id for deletion due to $user vars in the configuration'); |
|
| 496 | - } |
|
| 497 | - } |
|
| 498 | - |
|
| 499 | - // note: similar to ConfigAdapter->prepateStorageConfig() |
|
| 500 | - $storageConfig->getAuthMechanism()->manipulateStorageConfig($storageConfig); |
|
| 501 | - $storageConfig->getBackend()->manipulateStorageConfig($storageConfig); |
|
| 502 | - |
|
| 503 | - $class = $storageConfig->getBackend()->getStorageClass(); |
|
| 504 | - $storageImpl = new $class($storageConfig->getBackendOptions()); |
|
| 505 | - |
|
| 506 | - return $storageImpl->getId(); |
|
| 507 | - } |
|
| 508 | - |
|
| 509 | - /** |
|
| 510 | - * Construct the storage implementation |
|
| 511 | - * |
|
| 512 | - * @param StorageConfig $storageConfig |
|
| 513 | - * @return int |
|
| 514 | - */ |
|
| 515 | - private function getStorageId(StorageConfig $storageConfig) { |
|
| 516 | - try { |
|
| 517 | - $class = $storageConfig->getBackend()->getStorageClass(); |
|
| 518 | - /** @var \OC\Files\Storage\Storage $storage */ |
|
| 519 | - $storage = new $class($storageConfig->getBackendOptions()); |
|
| 520 | - |
|
| 521 | - // auth mechanism should fire first |
|
| 522 | - $storage = $storageConfig->getBackend()->wrapStorage($storage); |
|
| 523 | - $storage = $storageConfig->getAuthMechanism()->wrapStorage($storage); |
|
| 524 | - |
|
| 525 | - return $storage->getStorageCache()->getNumericId(); |
|
| 526 | - } catch (\Exception $e) { |
|
| 527 | - return -1; |
|
| 528 | - } |
|
| 529 | - } |
|
| 44 | + /** @var BackendService */ |
|
| 45 | + protected $backendService; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * @var DBConfigService |
|
| 49 | + */ |
|
| 50 | + protected $dbConfig; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * @var IUserMountCache |
|
| 54 | + */ |
|
| 55 | + protected $userMountCache; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * @param BackendService $backendService |
|
| 59 | + * @param DBConfigService $dbConfigService |
|
| 60 | + * @param IUserMountCache $userMountCache |
|
| 61 | + */ |
|
| 62 | + public function __construct(BackendService $backendService, DBConfigService $dbConfigService, IUserMountCache $userMountCache) { |
|
| 63 | + $this->backendService = $backendService; |
|
| 64 | + $this->dbConfig = $dbConfigService; |
|
| 65 | + $this->userMountCache = $userMountCache; |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + protected function readDBConfig() { |
|
| 69 | + return $this->dbConfig->getAdminMounts(); |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + protected function getStorageConfigFromDBMount(array $mount) { |
|
| 73 | + $applicableUsers = array_filter($mount['applicable'], function ($applicable) { |
|
| 74 | + return $applicable['type'] === DBConfigService::APPLICABLE_TYPE_USER; |
|
| 75 | + }); |
|
| 76 | + $applicableUsers = array_map(function ($applicable) { |
|
| 77 | + return $applicable['value']; |
|
| 78 | + }, $applicableUsers); |
|
| 79 | + |
|
| 80 | + $applicableGroups = array_filter($mount['applicable'], function ($applicable) { |
|
| 81 | + return $applicable['type'] === DBConfigService::APPLICABLE_TYPE_GROUP; |
|
| 82 | + }); |
|
| 83 | + $applicableGroups = array_map(function ($applicable) { |
|
| 84 | + return $applicable['value']; |
|
| 85 | + }, $applicableGroups); |
|
| 86 | + |
|
| 87 | + try { |
|
| 88 | + $config = $this->createStorage( |
|
| 89 | + $mount['mount_point'], |
|
| 90 | + $mount['storage_backend'], |
|
| 91 | + $mount['auth_backend'], |
|
| 92 | + $mount['config'], |
|
| 93 | + $mount['options'], |
|
| 94 | + array_values($applicableUsers), |
|
| 95 | + array_values($applicableGroups), |
|
| 96 | + $mount['priority'] |
|
| 97 | + ); |
|
| 98 | + $config->setType($mount['type']); |
|
| 99 | + $config->setId((int)$mount['mount_id']); |
|
| 100 | + return $config; |
|
| 101 | + } catch (\UnexpectedValueException $e) { |
|
| 102 | + // don't die if a storage backend doesn't exist |
|
| 103 | + \OCP\Util::writeLog( |
|
| 104 | + 'files_external', |
|
| 105 | + 'Could not load storage: "' . $e->getMessage() . '"', |
|
| 106 | + \OCP\Util::ERROR |
|
| 107 | + ); |
|
| 108 | + return null; |
|
| 109 | + } catch (\InvalidArgumentException $e) { |
|
| 110 | + \OCP\Util::writeLog( |
|
| 111 | + 'files_external', |
|
| 112 | + 'Could not load storage: "' . $e->getMessage() . '"', |
|
| 113 | + \OCP\Util::ERROR |
|
| 114 | + ); |
|
| 115 | + return null; |
|
| 116 | + } |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * Read the external storages config |
|
| 121 | + * |
|
| 122 | + * @return array map of storage id to storage config |
|
| 123 | + */ |
|
| 124 | + protected function readConfig() { |
|
| 125 | + $mounts = $this->readDBConfig(); |
|
| 126 | + $configs = array_map([$this, 'getStorageConfigFromDBMount'], $mounts); |
|
| 127 | + $configs = array_filter($configs, function ($config) { |
|
| 128 | + return $config instanceof StorageConfig; |
|
| 129 | + }); |
|
| 130 | + |
|
| 131 | + $keys = array_map(function (StorageConfig $config) { |
|
| 132 | + return $config->getId(); |
|
| 133 | + }, $configs); |
|
| 134 | + |
|
| 135 | + return array_combine($keys, $configs); |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * Get a storage with status |
|
| 140 | + * |
|
| 141 | + * @param int $id storage id |
|
| 142 | + * |
|
| 143 | + * @return StorageConfig |
|
| 144 | + * @throws NotFoundException if the storage with the given id was not found |
|
| 145 | + */ |
|
| 146 | + public function getStorage($id) { |
|
| 147 | + $mount = $this->dbConfig->getMountById($id); |
|
| 148 | + |
|
| 149 | + if (!is_array($mount)) { |
|
| 150 | + throw new NotFoundException('Storage with id "' . $id . '" not found'); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + $config = $this->getStorageConfigFromDBMount($mount); |
|
| 154 | + if ($this->isApplicable($config)) { |
|
| 155 | + return $config; |
|
| 156 | + } else { |
|
| 157 | + throw new NotFoundException('Storage with id "' . $id . '" not found'); |
|
| 158 | + } |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * Check whether this storage service should provide access to a storage |
|
| 163 | + * |
|
| 164 | + * @param StorageConfig $config |
|
| 165 | + * @return bool |
|
| 166 | + */ |
|
| 167 | + abstract protected function isApplicable(StorageConfig $config); |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * Gets all storages, valid or not |
|
| 171 | + * |
|
| 172 | + * @return StorageConfig[] array of storage configs |
|
| 173 | + */ |
|
| 174 | + public function getAllStorages() { |
|
| 175 | + return $this->readConfig(); |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * Gets all valid storages |
|
| 180 | + * |
|
| 181 | + * @return StorageConfig[] |
|
| 182 | + */ |
|
| 183 | + public function getStorages() { |
|
| 184 | + return array_filter($this->getAllStorages(), [$this, 'validateStorage']); |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + /** |
|
| 188 | + * Validate storage |
|
| 189 | + * FIXME: De-duplicate with StoragesController::validate() |
|
| 190 | + * |
|
| 191 | + * @param StorageConfig $storage |
|
| 192 | + * @return bool |
|
| 193 | + */ |
|
| 194 | + protected function validateStorage(StorageConfig $storage) { |
|
| 195 | + /** @var Backend */ |
|
| 196 | + $backend = $storage->getBackend(); |
|
| 197 | + /** @var AuthMechanism */ |
|
| 198 | + $authMechanism = $storage->getAuthMechanism(); |
|
| 199 | + |
|
| 200 | + if (!$backend->isVisibleFor($this->getVisibilityType())) { |
|
| 201 | + // not permitted to use backend |
|
| 202 | + return false; |
|
| 203 | + } |
|
| 204 | + if (!$authMechanism->isVisibleFor($this->getVisibilityType())) { |
|
| 205 | + // not permitted to use auth mechanism |
|
| 206 | + return false; |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + return true; |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + /** |
|
| 213 | + * Get the visibility type for this controller, used in validation |
|
| 214 | + * |
|
| 215 | + * @return string BackendService::VISIBILITY_* constants |
|
| 216 | + */ |
|
| 217 | + abstract public function getVisibilityType(); |
|
| 218 | + |
|
| 219 | + /** |
|
| 220 | + * @return integer |
|
| 221 | + */ |
|
| 222 | + protected function getType() { |
|
| 223 | + return DBConfigService::MOUNT_TYPE_ADMIN; |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * Add new storage to the configuration |
|
| 228 | + * |
|
| 229 | + * @param StorageConfig $newStorage storage attributes |
|
| 230 | + * |
|
| 231 | + * @return StorageConfig storage config, with added id |
|
| 232 | + */ |
|
| 233 | + public function addStorage(StorageConfig $newStorage) { |
|
| 234 | + $allStorages = $this->readConfig(); |
|
| 235 | + |
|
| 236 | + $configId = $this->dbConfig->addMount( |
|
| 237 | + $newStorage->getMountPoint(), |
|
| 238 | + $newStorage->getBackend()->getIdentifier(), |
|
| 239 | + $newStorage->getAuthMechanism()->getIdentifier(), |
|
| 240 | + $newStorage->getPriority(), |
|
| 241 | + $this->getType() |
|
| 242 | + ); |
|
| 243 | + |
|
| 244 | + $newStorage->setId($configId); |
|
| 245 | + |
|
| 246 | + foreach ($newStorage->getApplicableUsers() as $user) { |
|
| 247 | + $this->dbConfig->addApplicable($configId, DBConfigService::APPLICABLE_TYPE_USER, $user); |
|
| 248 | + } |
|
| 249 | + foreach ($newStorage->getApplicableGroups() as $group) { |
|
| 250 | + $this->dbConfig->addApplicable($configId, DBConfigService::APPLICABLE_TYPE_GROUP, $group); |
|
| 251 | + } |
|
| 252 | + foreach ($newStorage->getBackendOptions() as $key => $value) { |
|
| 253 | + $this->dbConfig->setConfig($configId, $key, $value); |
|
| 254 | + } |
|
| 255 | + foreach ($newStorage->getMountOptions() as $key => $value) { |
|
| 256 | + $this->dbConfig->setOption($configId, $key, $value); |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + if (count($newStorage->getApplicableUsers()) === 0 && count($newStorage->getApplicableGroups()) === 0) { |
|
| 260 | + $this->dbConfig->addApplicable($configId, DBConfigService::APPLICABLE_TYPE_GLOBAL, null); |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + // add new storage |
|
| 264 | + $allStorages[$configId] = $newStorage; |
|
| 265 | + |
|
| 266 | + $this->triggerHooks($newStorage, Filesystem::signal_create_mount); |
|
| 267 | + |
|
| 268 | + $newStorage->setStatus(StorageNotAvailableException::STATUS_SUCCESS); |
|
| 269 | + return $newStorage; |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + /** |
|
| 273 | + * Create a storage from its parameters |
|
| 274 | + * |
|
| 275 | + * @param string $mountPoint storage mount point |
|
| 276 | + * @param string $backendIdentifier backend identifier |
|
| 277 | + * @param string $authMechanismIdentifier authentication mechanism identifier |
|
| 278 | + * @param array $backendOptions backend-specific options |
|
| 279 | + * @param array|null $mountOptions mount-specific options |
|
| 280 | + * @param array|null $applicableUsers users for which to mount the storage |
|
| 281 | + * @param array|null $applicableGroups groups for which to mount the storage |
|
| 282 | + * @param int|null $priority priority |
|
| 283 | + * |
|
| 284 | + * @return StorageConfig |
|
| 285 | + */ |
|
| 286 | + public function createStorage( |
|
| 287 | + $mountPoint, |
|
| 288 | + $backendIdentifier, |
|
| 289 | + $authMechanismIdentifier, |
|
| 290 | + $backendOptions, |
|
| 291 | + $mountOptions = null, |
|
| 292 | + $applicableUsers = null, |
|
| 293 | + $applicableGroups = null, |
|
| 294 | + $priority = null |
|
| 295 | + ) { |
|
| 296 | + $backend = $this->backendService->getBackend($backendIdentifier); |
|
| 297 | + if (!$backend) { |
|
| 298 | + throw new \InvalidArgumentException('Unable to get backend for ' . $backendIdentifier); |
|
| 299 | + } |
|
| 300 | + $authMechanism = $this->backendService->getAuthMechanism($authMechanismIdentifier); |
|
| 301 | + if (!$authMechanism) { |
|
| 302 | + throw new \InvalidArgumentException('Unable to get authentication mechanism for ' . $authMechanismIdentifier); |
|
| 303 | + } |
|
| 304 | + $newStorage = new StorageConfig(); |
|
| 305 | + $newStorage->setMountPoint($mountPoint); |
|
| 306 | + $newStorage->setBackend($backend); |
|
| 307 | + $newStorage->setAuthMechanism($authMechanism); |
|
| 308 | + $newStorage->setBackendOptions($backendOptions); |
|
| 309 | + if (isset($mountOptions)) { |
|
| 310 | + $newStorage->setMountOptions($mountOptions); |
|
| 311 | + } |
|
| 312 | + if (isset($applicableUsers)) { |
|
| 313 | + $newStorage->setApplicableUsers($applicableUsers); |
|
| 314 | + } |
|
| 315 | + if (isset($applicableGroups)) { |
|
| 316 | + $newStorage->setApplicableGroups($applicableGroups); |
|
| 317 | + } |
|
| 318 | + if (isset($priority)) { |
|
| 319 | + $newStorage->setPriority($priority); |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + return $newStorage; |
|
| 323 | + } |
|
| 324 | + |
|
| 325 | + /** |
|
| 326 | + * Triggers the given hook signal for all the applicables given |
|
| 327 | + * |
|
| 328 | + * @param string $signal signal |
|
| 329 | + * @param string $mountPoint hook mount pount param |
|
| 330 | + * @param string $mountType hook mount type param |
|
| 331 | + * @param array $applicableArray array of applicable users/groups for which to trigger the hook |
|
| 332 | + */ |
|
| 333 | + protected function triggerApplicableHooks($signal, $mountPoint, $mountType, $applicableArray) { |
|
| 334 | + foreach ($applicableArray as $applicable) { |
|
| 335 | + \OCP\Util::emitHook( |
|
| 336 | + Filesystem::CLASSNAME, |
|
| 337 | + $signal, |
|
| 338 | + [ |
|
| 339 | + Filesystem::signal_param_path => $mountPoint, |
|
| 340 | + Filesystem::signal_param_mount_type => $mountType, |
|
| 341 | + Filesystem::signal_param_users => $applicable, |
|
| 342 | + ] |
|
| 343 | + ); |
|
| 344 | + } |
|
| 345 | + } |
|
| 346 | + |
|
| 347 | + /** |
|
| 348 | + * Triggers $signal for all applicable users of the given |
|
| 349 | + * storage |
|
| 350 | + * |
|
| 351 | + * @param StorageConfig $storage storage data |
|
| 352 | + * @param string $signal signal to trigger |
|
| 353 | + */ |
|
| 354 | + abstract protected function triggerHooks(StorageConfig $storage, $signal); |
|
| 355 | + |
|
| 356 | + /** |
|
| 357 | + * Triggers signal_create_mount or signal_delete_mount to |
|
| 358 | + * accommodate for additions/deletions in applicableUsers |
|
| 359 | + * and applicableGroups fields. |
|
| 360 | + * |
|
| 361 | + * @param StorageConfig $oldStorage old storage data |
|
| 362 | + * @param StorageConfig $newStorage new storage data |
|
| 363 | + */ |
|
| 364 | + abstract protected function triggerChangeHooks(StorageConfig $oldStorage, StorageConfig $newStorage); |
|
| 365 | + |
|
| 366 | + /** |
|
| 367 | + * Update storage to the configuration |
|
| 368 | + * |
|
| 369 | + * @param StorageConfig $updatedStorage storage attributes |
|
| 370 | + * |
|
| 371 | + * @return StorageConfig storage config |
|
| 372 | + * @throws NotFoundException if the given storage does not exist in the config |
|
| 373 | + */ |
|
| 374 | + public function updateStorage(StorageConfig $updatedStorage) { |
|
| 375 | + $id = $updatedStorage->getId(); |
|
| 376 | + |
|
| 377 | + $existingMount = $this->dbConfig->getMountById($id); |
|
| 378 | + |
|
| 379 | + if (!is_array($existingMount)) { |
|
| 380 | + throw new NotFoundException('Storage with id "' . $id . '" not found while updating storage'); |
|
| 381 | + } |
|
| 382 | + |
|
| 383 | + $oldStorage = $this->getStorageConfigFromDBMount($existingMount); |
|
| 384 | + |
|
| 385 | + $removedUsers = array_diff($oldStorage->getApplicableUsers(), $updatedStorage->getApplicableUsers()); |
|
| 386 | + $removedGroups = array_diff($oldStorage->getApplicableGroups(), $updatedStorage->getApplicableGroups()); |
|
| 387 | + $addedUsers = array_diff($updatedStorage->getApplicableUsers(), $oldStorage->getApplicableUsers()); |
|
| 388 | + $addedGroups = array_diff($updatedStorage->getApplicableGroups(), $oldStorage->getApplicableGroups()); |
|
| 389 | + |
|
| 390 | + $oldUserCount = count($oldStorage->getApplicableUsers()); |
|
| 391 | + $oldGroupCount = count($oldStorage->getApplicableGroups()); |
|
| 392 | + $newUserCount = count($updatedStorage->getApplicableUsers()); |
|
| 393 | + $newGroupCount = count($updatedStorage->getApplicableGroups()); |
|
| 394 | + $wasGlobal = ($oldUserCount + $oldGroupCount) === 0; |
|
| 395 | + $isGlobal = ($newUserCount + $newGroupCount) === 0; |
|
| 396 | + |
|
| 397 | + foreach ($removedUsers as $user) { |
|
| 398 | + $this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, $user); |
|
| 399 | + } |
|
| 400 | + foreach ($removedGroups as $group) { |
|
| 401 | + $this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_GROUP, $group); |
|
| 402 | + } |
|
| 403 | + foreach ($addedUsers as $user) { |
|
| 404 | + $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, $user); |
|
| 405 | + } |
|
| 406 | + foreach ($addedGroups as $group) { |
|
| 407 | + $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_GROUP, $group); |
|
| 408 | + } |
|
| 409 | + |
|
| 410 | + if ($wasGlobal && !$isGlobal) { |
|
| 411 | + $this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_GLOBAL, null); |
|
| 412 | + } else if (!$wasGlobal && $isGlobal) { |
|
| 413 | + $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_GLOBAL, null); |
|
| 414 | + } |
|
| 415 | + |
|
| 416 | + $changedConfig = array_diff_assoc($updatedStorage->getBackendOptions(), $oldStorage->getBackendOptions()); |
|
| 417 | + $changedOptions = array_diff_assoc($updatedStorage->getMountOptions(), $oldStorage->getMountOptions()); |
|
| 418 | + |
|
| 419 | + foreach ($changedConfig as $key => $value) { |
|
| 420 | + $this->dbConfig->setConfig($id, $key, $value); |
|
| 421 | + } |
|
| 422 | + foreach ($changedOptions as $key => $value) { |
|
| 423 | + $this->dbConfig->setOption($id, $key, $value); |
|
| 424 | + } |
|
| 425 | + |
|
| 426 | + if ($updatedStorage->getMountPoint() !== $oldStorage->getMountPoint()) { |
|
| 427 | + $this->dbConfig->setMountPoint($id, $updatedStorage->getMountPoint()); |
|
| 428 | + } |
|
| 429 | + |
|
| 430 | + if ($updatedStorage->getAuthMechanism()->getIdentifier() !== $oldStorage->getAuthMechanism()->getIdentifier()) { |
|
| 431 | + $this->dbConfig->setAuthBackend($id, $updatedStorage->getAuthMechanism()->getIdentifier()); |
|
| 432 | + } |
|
| 433 | + |
|
| 434 | + $this->triggerChangeHooks($oldStorage, $updatedStorage); |
|
| 435 | + |
|
| 436 | + if (($wasGlobal && !$isGlobal) || count($removedGroups) > 0) { // to expensive to properly handle these on the fly |
|
| 437 | + $this->userMountCache->remoteStorageMounts($this->getStorageId($updatedStorage)); |
|
| 438 | + } else { |
|
| 439 | + $storageId = $this->getStorageId($updatedStorage); |
|
| 440 | + foreach ($removedUsers as $userId) { |
|
| 441 | + $this->userMountCache->removeUserStorageMount($storageId, $userId); |
|
| 442 | + } |
|
| 443 | + } |
|
| 444 | + |
|
| 445 | + return $this->getStorage($id); |
|
| 446 | + } |
|
| 447 | + |
|
| 448 | + /** |
|
| 449 | + * Delete the storage with the given id. |
|
| 450 | + * |
|
| 451 | + * @param int $id storage id |
|
| 452 | + * |
|
| 453 | + * @throws NotFoundException if no storage was found with the given id |
|
| 454 | + */ |
|
| 455 | + public function removeStorage($id) { |
|
| 456 | + $existingMount = $this->dbConfig->getMountById($id); |
|
| 457 | + |
|
| 458 | + if (!is_array($existingMount)) { |
|
| 459 | + throw new NotFoundException('Storage with id "' . $id . '" not found'); |
|
| 460 | + } |
|
| 461 | + |
|
| 462 | + $this->dbConfig->removeMount($id); |
|
| 463 | + |
|
| 464 | + $deletedStorage = $this->getStorageConfigFromDBMount($existingMount); |
|
| 465 | + $this->triggerHooks($deletedStorage, Filesystem::signal_delete_mount); |
|
| 466 | + |
|
| 467 | + // delete oc_storages entries and oc_filecache |
|
| 468 | + try { |
|
| 469 | + $rustyStorageId = $this->getRustyStorageIdFromConfig($deletedStorage); |
|
| 470 | + \OC\Files\Cache\Storage::remove($rustyStorageId); |
|
| 471 | + } catch (\Exception $e) { |
|
| 472 | + // can happen either for invalid configs where the storage could not |
|
| 473 | + // be instantiated or whenever $user vars where used, in which case |
|
| 474 | + // the storage id could not be computed |
|
| 475 | + \OCP\Util::writeLog( |
|
| 476 | + 'files_external', |
|
| 477 | + 'Exception: "' . $e->getMessage() . '"', |
|
| 478 | + \OCP\Util::ERROR |
|
| 479 | + ); |
|
| 480 | + } |
|
| 481 | + } |
|
| 482 | + |
|
| 483 | + /** |
|
| 484 | + * Returns the rusty storage id from oc_storages from the given storage config. |
|
| 485 | + * |
|
| 486 | + * @param StorageConfig $storageConfig |
|
| 487 | + * @return string rusty storage id |
|
| 488 | + */ |
|
| 489 | + private function getRustyStorageIdFromConfig(StorageConfig $storageConfig) { |
|
| 490 | + // if any of the storage options contains $user, it is not possible |
|
| 491 | + // to compute the possible storage id as we don't know which users |
|
| 492 | + // mounted it already (and we certainly don't want to iterate over ALL users) |
|
| 493 | + foreach ($storageConfig->getBackendOptions() as $value) { |
|
| 494 | + if (strpos($value, '$user') !== false) { |
|
| 495 | + throw new \Exception('Cannot compute storage id for deletion due to $user vars in the configuration'); |
|
| 496 | + } |
|
| 497 | + } |
|
| 498 | + |
|
| 499 | + // note: similar to ConfigAdapter->prepateStorageConfig() |
|
| 500 | + $storageConfig->getAuthMechanism()->manipulateStorageConfig($storageConfig); |
|
| 501 | + $storageConfig->getBackend()->manipulateStorageConfig($storageConfig); |
|
| 502 | + |
|
| 503 | + $class = $storageConfig->getBackend()->getStorageClass(); |
|
| 504 | + $storageImpl = new $class($storageConfig->getBackendOptions()); |
|
| 505 | + |
|
| 506 | + return $storageImpl->getId(); |
|
| 507 | + } |
|
| 508 | + |
|
| 509 | + /** |
|
| 510 | + * Construct the storage implementation |
|
| 511 | + * |
|
| 512 | + * @param StorageConfig $storageConfig |
|
| 513 | + * @return int |
|
| 514 | + */ |
|
| 515 | + private function getStorageId(StorageConfig $storageConfig) { |
|
| 516 | + try { |
|
| 517 | + $class = $storageConfig->getBackend()->getStorageClass(); |
|
| 518 | + /** @var \OC\Files\Storage\Storage $storage */ |
|
| 519 | + $storage = new $class($storageConfig->getBackendOptions()); |
|
| 520 | + |
|
| 521 | + // auth mechanism should fire first |
|
| 522 | + $storage = $storageConfig->getBackend()->wrapStorage($storage); |
|
| 523 | + $storage = $storageConfig->getAuthMechanism()->wrapStorage($storage); |
|
| 524 | + |
|
| 525 | + return $storage->getStorageCache()->getNumericId(); |
|
| 526 | + } catch (\Exception $e) { |
|
| 527 | + return -1; |
|
| 528 | + } |
|
| 529 | + } |
|
| 530 | 530 | } |
@@ -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 | } |
@@ -43,185 +43,185 @@ |
||
| 43 | 43 | * User storages controller |
| 44 | 44 | */ |
| 45 | 45 | class UserStoragesController extends StoragesController { |
| 46 | - /** |
|
| 47 | - * @var IUserSession |
|
| 48 | - */ |
|
| 49 | - private $userSession; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * Creates a new user storages controller. |
|
| 53 | - * |
|
| 54 | - * @param string $AppName application name |
|
| 55 | - * @param IRequest $request request object |
|
| 56 | - * @param IL10N $l10n l10n service |
|
| 57 | - * @param UserStoragesService $userStoragesService storage service |
|
| 58 | - * @param IUserSession $userSession |
|
| 59 | - * @param ILogger $logger |
|
| 60 | - */ |
|
| 61 | - public function __construct( |
|
| 62 | - $AppName, |
|
| 63 | - IRequest $request, |
|
| 64 | - IL10N $l10n, |
|
| 65 | - UserStoragesService $userStoragesService, |
|
| 66 | - IUserSession $userSession, |
|
| 67 | - ILogger $logger |
|
| 68 | - ) { |
|
| 69 | - parent::__construct( |
|
| 70 | - $AppName, |
|
| 71 | - $request, |
|
| 72 | - $l10n, |
|
| 73 | - $userStoragesService, |
|
| 74 | - $logger |
|
| 75 | - ); |
|
| 76 | - $this->userSession = $userSession; |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - protected function manipulateStorageConfig(StorageConfig $storage) { |
|
| 80 | - /** @var AuthMechanism */ |
|
| 81 | - $authMechanism = $storage->getAuthMechanism(); |
|
| 82 | - $authMechanism->manipulateStorageConfig($storage, $this->userSession->getUser()); |
|
| 83 | - /** @var Backend */ |
|
| 84 | - $backend = $storage->getBackend(); |
|
| 85 | - $backend->manipulateStorageConfig($storage, $this->userSession->getUser()); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * Get all storage entries |
|
| 90 | - * |
|
| 91 | - * @NoAdminRequired |
|
| 92 | - * |
|
| 93 | - * @return DataResponse |
|
| 94 | - */ |
|
| 95 | - public function index() { |
|
| 96 | - return parent::index(); |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * Return storage |
|
| 101 | - * |
|
| 102 | - * @NoAdminRequired |
|
| 103 | - * |
|
| 104 | - * {@inheritdoc} |
|
| 105 | - */ |
|
| 106 | - public function show($id, $testOnly = true) { |
|
| 107 | - return parent::show($id, $testOnly); |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * Create an external storage entry. |
|
| 112 | - * |
|
| 113 | - * @param string $mountPoint storage mount point |
|
| 114 | - * @param string $backend backend identifier |
|
| 115 | - * @param string $authMechanism authentication mechanism identifier |
|
| 116 | - * @param array $backendOptions backend-specific options |
|
| 117 | - * @param array $mountOptions backend-specific mount options |
|
| 118 | - * |
|
| 119 | - * @return DataResponse |
|
| 120 | - * |
|
| 121 | - * @NoAdminRequired |
|
| 122 | - */ |
|
| 123 | - public function create( |
|
| 124 | - $mountPoint, |
|
| 125 | - $backend, |
|
| 126 | - $authMechanism, |
|
| 127 | - $backendOptions, |
|
| 128 | - $mountOptions |
|
| 129 | - ) { |
|
| 130 | - $newStorage = $this->createStorage( |
|
| 131 | - $mountPoint, |
|
| 132 | - $backend, |
|
| 133 | - $authMechanism, |
|
| 134 | - $backendOptions, |
|
| 135 | - $mountOptions |
|
| 136 | - ); |
|
| 137 | - if ($newStorage instanceOf DataResponse) { |
|
| 138 | - return $newStorage; |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - $response = $this->validate($newStorage); |
|
| 142 | - if (!empty($response)) { |
|
| 143 | - return $response; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - $newStorage = $this->service->addStorage($newStorage); |
|
| 147 | - $this->updateStorageStatus($newStorage); |
|
| 148 | - |
|
| 149 | - return new DataResponse( |
|
| 150 | - $newStorage, |
|
| 151 | - Http::STATUS_CREATED |
|
| 152 | - ); |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * Update an external storage entry. |
|
| 157 | - * |
|
| 158 | - * @param int $id storage id |
|
| 159 | - * @param string $mountPoint storage mount point |
|
| 160 | - * @param string $backend backend identifier |
|
| 161 | - * @param string $authMechanism authentication mechanism identifier |
|
| 162 | - * @param array $backendOptions backend-specific options |
|
| 163 | - * @param array $mountOptions backend-specific mount options |
|
| 164 | - * @param bool $testOnly whether to storage should only test the connection or do more things |
|
| 165 | - * |
|
| 166 | - * @return DataResponse |
|
| 167 | - * |
|
| 168 | - * @NoAdminRequired |
|
| 169 | - */ |
|
| 170 | - public function update( |
|
| 171 | - $id, |
|
| 172 | - $mountPoint, |
|
| 173 | - $backend, |
|
| 174 | - $authMechanism, |
|
| 175 | - $backendOptions, |
|
| 176 | - $mountOptions, |
|
| 177 | - $testOnly = true |
|
| 178 | - ) { |
|
| 179 | - $storage = $this->createStorage( |
|
| 180 | - $mountPoint, |
|
| 181 | - $backend, |
|
| 182 | - $authMechanism, |
|
| 183 | - $backendOptions, |
|
| 184 | - $mountOptions |
|
| 185 | - ); |
|
| 186 | - if ($storage instanceOf DataResponse) { |
|
| 187 | - return $storage; |
|
| 188 | - } |
|
| 189 | - $storage->setId($id); |
|
| 190 | - |
|
| 191 | - $response = $this->validate($storage); |
|
| 192 | - if (!empty($response)) { |
|
| 193 | - return $response; |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - try { |
|
| 197 | - $storage = $this->service->updateStorage($storage); |
|
| 198 | - } catch (NotFoundException $e) { |
|
| 199 | - return new DataResponse( |
|
| 200 | - [ |
|
| 201 | - 'message' => (string)$this->l10n->t('Storage with id "%i" not found', array($id)) |
|
| 202 | - ], |
|
| 203 | - Http::STATUS_NOT_FOUND |
|
| 204 | - ); |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - $this->updateStorageStatus($storage, $testOnly); |
|
| 208 | - |
|
| 209 | - return new DataResponse( |
|
| 210 | - $storage, |
|
| 211 | - Http::STATUS_OK |
|
| 212 | - ); |
|
| 213 | - |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - /** |
|
| 217 | - * Delete storage |
|
| 218 | - * |
|
| 219 | - * @NoAdminRequired |
|
| 220 | - * |
|
| 221 | - * {@inheritdoc} |
|
| 222 | - */ |
|
| 223 | - public function destroy($id) { |
|
| 224 | - return parent::destroy($id); |
|
| 225 | - } |
|
| 46 | + /** |
|
| 47 | + * @var IUserSession |
|
| 48 | + */ |
|
| 49 | + private $userSession; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * Creates a new user storages controller. |
|
| 53 | + * |
|
| 54 | + * @param string $AppName application name |
|
| 55 | + * @param IRequest $request request object |
|
| 56 | + * @param IL10N $l10n l10n service |
|
| 57 | + * @param UserStoragesService $userStoragesService storage service |
|
| 58 | + * @param IUserSession $userSession |
|
| 59 | + * @param ILogger $logger |
|
| 60 | + */ |
|
| 61 | + public function __construct( |
|
| 62 | + $AppName, |
|
| 63 | + IRequest $request, |
|
| 64 | + IL10N $l10n, |
|
| 65 | + UserStoragesService $userStoragesService, |
|
| 66 | + IUserSession $userSession, |
|
| 67 | + ILogger $logger |
|
| 68 | + ) { |
|
| 69 | + parent::__construct( |
|
| 70 | + $AppName, |
|
| 71 | + $request, |
|
| 72 | + $l10n, |
|
| 73 | + $userStoragesService, |
|
| 74 | + $logger |
|
| 75 | + ); |
|
| 76 | + $this->userSession = $userSession; |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + protected function manipulateStorageConfig(StorageConfig $storage) { |
|
| 80 | + /** @var AuthMechanism */ |
|
| 81 | + $authMechanism = $storage->getAuthMechanism(); |
|
| 82 | + $authMechanism->manipulateStorageConfig($storage, $this->userSession->getUser()); |
|
| 83 | + /** @var Backend */ |
|
| 84 | + $backend = $storage->getBackend(); |
|
| 85 | + $backend->manipulateStorageConfig($storage, $this->userSession->getUser()); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * Get all storage entries |
|
| 90 | + * |
|
| 91 | + * @NoAdminRequired |
|
| 92 | + * |
|
| 93 | + * @return DataResponse |
|
| 94 | + */ |
|
| 95 | + public function index() { |
|
| 96 | + return parent::index(); |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * Return storage |
|
| 101 | + * |
|
| 102 | + * @NoAdminRequired |
|
| 103 | + * |
|
| 104 | + * {@inheritdoc} |
|
| 105 | + */ |
|
| 106 | + public function show($id, $testOnly = true) { |
|
| 107 | + return parent::show($id, $testOnly); |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * Create an external storage entry. |
|
| 112 | + * |
|
| 113 | + * @param string $mountPoint storage mount point |
|
| 114 | + * @param string $backend backend identifier |
|
| 115 | + * @param string $authMechanism authentication mechanism identifier |
|
| 116 | + * @param array $backendOptions backend-specific options |
|
| 117 | + * @param array $mountOptions backend-specific mount options |
|
| 118 | + * |
|
| 119 | + * @return DataResponse |
|
| 120 | + * |
|
| 121 | + * @NoAdminRequired |
|
| 122 | + */ |
|
| 123 | + public function create( |
|
| 124 | + $mountPoint, |
|
| 125 | + $backend, |
|
| 126 | + $authMechanism, |
|
| 127 | + $backendOptions, |
|
| 128 | + $mountOptions |
|
| 129 | + ) { |
|
| 130 | + $newStorage = $this->createStorage( |
|
| 131 | + $mountPoint, |
|
| 132 | + $backend, |
|
| 133 | + $authMechanism, |
|
| 134 | + $backendOptions, |
|
| 135 | + $mountOptions |
|
| 136 | + ); |
|
| 137 | + if ($newStorage instanceOf DataResponse) { |
|
| 138 | + return $newStorage; |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + $response = $this->validate($newStorage); |
|
| 142 | + if (!empty($response)) { |
|
| 143 | + return $response; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + $newStorage = $this->service->addStorage($newStorage); |
|
| 147 | + $this->updateStorageStatus($newStorage); |
|
| 148 | + |
|
| 149 | + return new DataResponse( |
|
| 150 | + $newStorage, |
|
| 151 | + Http::STATUS_CREATED |
|
| 152 | + ); |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * Update an external storage entry. |
|
| 157 | + * |
|
| 158 | + * @param int $id storage id |
|
| 159 | + * @param string $mountPoint storage mount point |
|
| 160 | + * @param string $backend backend identifier |
|
| 161 | + * @param string $authMechanism authentication mechanism identifier |
|
| 162 | + * @param array $backendOptions backend-specific options |
|
| 163 | + * @param array $mountOptions backend-specific mount options |
|
| 164 | + * @param bool $testOnly whether to storage should only test the connection or do more things |
|
| 165 | + * |
|
| 166 | + * @return DataResponse |
|
| 167 | + * |
|
| 168 | + * @NoAdminRequired |
|
| 169 | + */ |
|
| 170 | + public function update( |
|
| 171 | + $id, |
|
| 172 | + $mountPoint, |
|
| 173 | + $backend, |
|
| 174 | + $authMechanism, |
|
| 175 | + $backendOptions, |
|
| 176 | + $mountOptions, |
|
| 177 | + $testOnly = true |
|
| 178 | + ) { |
|
| 179 | + $storage = $this->createStorage( |
|
| 180 | + $mountPoint, |
|
| 181 | + $backend, |
|
| 182 | + $authMechanism, |
|
| 183 | + $backendOptions, |
|
| 184 | + $mountOptions |
|
| 185 | + ); |
|
| 186 | + if ($storage instanceOf DataResponse) { |
|
| 187 | + return $storage; |
|
| 188 | + } |
|
| 189 | + $storage->setId($id); |
|
| 190 | + |
|
| 191 | + $response = $this->validate($storage); |
|
| 192 | + if (!empty($response)) { |
|
| 193 | + return $response; |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + try { |
|
| 197 | + $storage = $this->service->updateStorage($storage); |
|
| 198 | + } catch (NotFoundException $e) { |
|
| 199 | + return new DataResponse( |
|
| 200 | + [ |
|
| 201 | + 'message' => (string)$this->l10n->t('Storage with id "%i" not found', array($id)) |
|
| 202 | + ], |
|
| 203 | + Http::STATUS_NOT_FOUND |
|
| 204 | + ); |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + $this->updateStorageStatus($storage, $testOnly); |
|
| 208 | + |
|
| 209 | + return new DataResponse( |
|
| 210 | + $storage, |
|
| 211 | + Http::STATUS_OK |
|
| 212 | + ); |
|
| 213 | + |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + /** |
|
| 217 | + * Delete storage |
|
| 218 | + * |
|
| 219 | + * @NoAdminRequired |
|
| 220 | + * |
|
| 221 | + * {@inheritdoc} |
|
| 222 | + */ |
|
| 223 | + public function destroy($id) { |
|
| 224 | + return parent::destroy($id); |
|
| 225 | + } |
|
| 226 | 226 | |
| 227 | 227 | } |
@@ -39,151 +39,151 @@ |
||
| 39 | 39 | * Global storages controller |
| 40 | 40 | */ |
| 41 | 41 | class GlobalStoragesController extends StoragesController { |
| 42 | - /** |
|
| 43 | - * Creates a new global storages controller. |
|
| 44 | - * |
|
| 45 | - * @param string $AppName application name |
|
| 46 | - * @param IRequest $request request object |
|
| 47 | - * @param IL10N $l10n l10n service |
|
| 48 | - * @param GlobalStoragesService $globalStoragesService storage service |
|
| 49 | - * @param ILogger $logger |
|
| 50 | - */ |
|
| 51 | - public function __construct( |
|
| 52 | - $AppName, |
|
| 53 | - IRequest $request, |
|
| 54 | - IL10N $l10n, |
|
| 55 | - GlobalStoragesService $globalStoragesService, |
|
| 56 | - ILogger $logger |
|
| 57 | - ) { |
|
| 58 | - parent::__construct( |
|
| 59 | - $AppName, |
|
| 60 | - $request, |
|
| 61 | - $l10n, |
|
| 62 | - $globalStoragesService, |
|
| 63 | - $logger |
|
| 64 | - ); |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * Create an external storage entry. |
|
| 69 | - * |
|
| 70 | - * @param string $mountPoint storage mount point |
|
| 71 | - * @param string $backend backend identifier |
|
| 72 | - * @param string $authMechanism authentication mechanism identifier |
|
| 73 | - * @param array $backendOptions backend-specific options |
|
| 74 | - * @param array $mountOptions mount-specific options |
|
| 75 | - * @param array $applicableUsers users for which to mount the storage |
|
| 76 | - * @param array $applicableGroups groups for which to mount the storage |
|
| 77 | - * @param int $priority priority |
|
| 78 | - * |
|
| 79 | - * @return DataResponse |
|
| 80 | - */ |
|
| 81 | - public function create( |
|
| 82 | - $mountPoint, |
|
| 83 | - $backend, |
|
| 84 | - $authMechanism, |
|
| 85 | - $backendOptions, |
|
| 86 | - $mountOptions, |
|
| 87 | - $applicableUsers, |
|
| 88 | - $applicableGroups, |
|
| 89 | - $priority |
|
| 90 | - ) { |
|
| 91 | - $newStorage = $this->createStorage( |
|
| 92 | - $mountPoint, |
|
| 93 | - $backend, |
|
| 94 | - $authMechanism, |
|
| 95 | - $backendOptions, |
|
| 96 | - $mountOptions, |
|
| 97 | - $applicableUsers, |
|
| 98 | - $applicableGroups, |
|
| 99 | - $priority |
|
| 100 | - ); |
|
| 101 | - if ($newStorage instanceof DataResponse) { |
|
| 102 | - return $newStorage; |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - $response = $this->validate($newStorage); |
|
| 106 | - if (!empty($response)) { |
|
| 107 | - return $response; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - $newStorage = $this->service->addStorage($newStorage); |
|
| 111 | - |
|
| 112 | - $this->updateStorageStatus($newStorage); |
|
| 113 | - |
|
| 114 | - return new DataResponse( |
|
| 115 | - $newStorage, |
|
| 116 | - Http::STATUS_CREATED |
|
| 117 | - ); |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * Update an external storage entry. |
|
| 122 | - * |
|
| 123 | - * @param int $id storage id |
|
| 124 | - * @param string $mountPoint storage mount point |
|
| 125 | - * @param string $backend backend identifier |
|
| 126 | - * @param string $authMechanism authentication mechansim identifier |
|
| 127 | - * @param array $backendOptions backend-specific options |
|
| 128 | - * @param array $mountOptions mount-specific options |
|
| 129 | - * @param array $applicableUsers users for which to mount the storage |
|
| 130 | - * @param array $applicableGroups groups for which to mount the storage |
|
| 131 | - * @param int $priority priority |
|
| 132 | - * @param bool $testOnly whether to storage should only test the connection or do more things |
|
| 133 | - * |
|
| 134 | - * @return DataResponse |
|
| 135 | - */ |
|
| 136 | - public function update( |
|
| 137 | - $id, |
|
| 138 | - $mountPoint, |
|
| 139 | - $backend, |
|
| 140 | - $authMechanism, |
|
| 141 | - $backendOptions, |
|
| 142 | - $mountOptions, |
|
| 143 | - $applicableUsers, |
|
| 144 | - $applicableGroups, |
|
| 145 | - $priority, |
|
| 146 | - $testOnly = true |
|
| 147 | - ) { |
|
| 148 | - $storage = $this->createStorage( |
|
| 149 | - $mountPoint, |
|
| 150 | - $backend, |
|
| 151 | - $authMechanism, |
|
| 152 | - $backendOptions, |
|
| 153 | - $mountOptions, |
|
| 154 | - $applicableUsers, |
|
| 155 | - $applicableGroups, |
|
| 156 | - $priority |
|
| 157 | - ); |
|
| 158 | - if ($storage instanceof DataResponse) { |
|
| 159 | - return $storage; |
|
| 160 | - } |
|
| 161 | - $storage->setId($id); |
|
| 162 | - |
|
| 163 | - $response = $this->validate($storage); |
|
| 164 | - if (!empty($response)) { |
|
| 165 | - return $response; |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - try { |
|
| 169 | - $storage = $this->service->updateStorage($storage); |
|
| 170 | - } catch (NotFoundException $e) { |
|
| 171 | - return new DataResponse( |
|
| 172 | - [ |
|
| 173 | - 'message' => (string)$this->l10n->t('Storage with id "%i" not found', array($id)) |
|
| 174 | - ], |
|
| 175 | - Http::STATUS_NOT_FOUND |
|
| 176 | - ); |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - $this->updateStorageStatus($storage, $testOnly); |
|
| 180 | - |
|
| 181 | - return new DataResponse( |
|
| 182 | - $storage, |
|
| 183 | - Http::STATUS_OK |
|
| 184 | - ); |
|
| 185 | - |
|
| 186 | - } |
|
| 42 | + /** |
|
| 43 | + * Creates a new global storages controller. |
|
| 44 | + * |
|
| 45 | + * @param string $AppName application name |
|
| 46 | + * @param IRequest $request request object |
|
| 47 | + * @param IL10N $l10n l10n service |
|
| 48 | + * @param GlobalStoragesService $globalStoragesService storage service |
|
| 49 | + * @param ILogger $logger |
|
| 50 | + */ |
|
| 51 | + public function __construct( |
|
| 52 | + $AppName, |
|
| 53 | + IRequest $request, |
|
| 54 | + IL10N $l10n, |
|
| 55 | + GlobalStoragesService $globalStoragesService, |
|
| 56 | + ILogger $logger |
|
| 57 | + ) { |
|
| 58 | + parent::__construct( |
|
| 59 | + $AppName, |
|
| 60 | + $request, |
|
| 61 | + $l10n, |
|
| 62 | + $globalStoragesService, |
|
| 63 | + $logger |
|
| 64 | + ); |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * Create an external storage entry. |
|
| 69 | + * |
|
| 70 | + * @param string $mountPoint storage mount point |
|
| 71 | + * @param string $backend backend identifier |
|
| 72 | + * @param string $authMechanism authentication mechanism identifier |
|
| 73 | + * @param array $backendOptions backend-specific options |
|
| 74 | + * @param array $mountOptions mount-specific options |
|
| 75 | + * @param array $applicableUsers users for which to mount the storage |
|
| 76 | + * @param array $applicableGroups groups for which to mount the storage |
|
| 77 | + * @param int $priority priority |
|
| 78 | + * |
|
| 79 | + * @return DataResponse |
|
| 80 | + */ |
|
| 81 | + public function create( |
|
| 82 | + $mountPoint, |
|
| 83 | + $backend, |
|
| 84 | + $authMechanism, |
|
| 85 | + $backendOptions, |
|
| 86 | + $mountOptions, |
|
| 87 | + $applicableUsers, |
|
| 88 | + $applicableGroups, |
|
| 89 | + $priority |
|
| 90 | + ) { |
|
| 91 | + $newStorage = $this->createStorage( |
|
| 92 | + $mountPoint, |
|
| 93 | + $backend, |
|
| 94 | + $authMechanism, |
|
| 95 | + $backendOptions, |
|
| 96 | + $mountOptions, |
|
| 97 | + $applicableUsers, |
|
| 98 | + $applicableGroups, |
|
| 99 | + $priority |
|
| 100 | + ); |
|
| 101 | + if ($newStorage instanceof DataResponse) { |
|
| 102 | + return $newStorage; |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + $response = $this->validate($newStorage); |
|
| 106 | + if (!empty($response)) { |
|
| 107 | + return $response; |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + $newStorage = $this->service->addStorage($newStorage); |
|
| 111 | + |
|
| 112 | + $this->updateStorageStatus($newStorage); |
|
| 113 | + |
|
| 114 | + return new DataResponse( |
|
| 115 | + $newStorage, |
|
| 116 | + Http::STATUS_CREATED |
|
| 117 | + ); |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * Update an external storage entry. |
|
| 122 | + * |
|
| 123 | + * @param int $id storage id |
|
| 124 | + * @param string $mountPoint storage mount point |
|
| 125 | + * @param string $backend backend identifier |
|
| 126 | + * @param string $authMechanism authentication mechansim identifier |
|
| 127 | + * @param array $backendOptions backend-specific options |
|
| 128 | + * @param array $mountOptions mount-specific options |
|
| 129 | + * @param array $applicableUsers users for which to mount the storage |
|
| 130 | + * @param array $applicableGroups groups for which to mount the storage |
|
| 131 | + * @param int $priority priority |
|
| 132 | + * @param bool $testOnly whether to storage should only test the connection or do more things |
|
| 133 | + * |
|
| 134 | + * @return DataResponse |
|
| 135 | + */ |
|
| 136 | + public function update( |
|
| 137 | + $id, |
|
| 138 | + $mountPoint, |
|
| 139 | + $backend, |
|
| 140 | + $authMechanism, |
|
| 141 | + $backendOptions, |
|
| 142 | + $mountOptions, |
|
| 143 | + $applicableUsers, |
|
| 144 | + $applicableGroups, |
|
| 145 | + $priority, |
|
| 146 | + $testOnly = true |
|
| 147 | + ) { |
|
| 148 | + $storage = $this->createStorage( |
|
| 149 | + $mountPoint, |
|
| 150 | + $backend, |
|
| 151 | + $authMechanism, |
|
| 152 | + $backendOptions, |
|
| 153 | + $mountOptions, |
|
| 154 | + $applicableUsers, |
|
| 155 | + $applicableGroups, |
|
| 156 | + $priority |
|
| 157 | + ); |
|
| 158 | + if ($storage instanceof DataResponse) { |
|
| 159 | + return $storage; |
|
| 160 | + } |
|
| 161 | + $storage->setId($id); |
|
| 162 | + |
|
| 163 | + $response = $this->validate($storage); |
|
| 164 | + if (!empty($response)) { |
|
| 165 | + return $response; |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + try { |
|
| 169 | + $storage = $this->service->updateStorage($storage); |
|
| 170 | + } catch (NotFoundException $e) { |
|
| 171 | + return new DataResponse( |
|
| 172 | + [ |
|
| 173 | + 'message' => (string)$this->l10n->t('Storage with id "%i" not found', array($id)) |
|
| 174 | + ], |
|
| 175 | + Http::STATUS_NOT_FOUND |
|
| 176 | + ); |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + $this->updateStorageStatus($storage, $testOnly); |
|
| 180 | + |
|
| 181 | + return new DataResponse( |
|
| 182 | + $storage, |
|
| 183 | + Http::STATUS_OK |
|
| 184 | + ); |
|
| 185 | + |
|
| 186 | + } |
|
| 187 | 187 | |
| 188 | 188 | |
| 189 | 189 | } |
@@ -43,165 +43,165 @@ |
||
| 43 | 43 | * User global storages controller |
| 44 | 44 | */ |
| 45 | 45 | class UserGlobalStoragesController extends StoragesController { |
| 46 | - /** |
|
| 47 | - * @var IUserSession |
|
| 48 | - */ |
|
| 49 | - private $userSession; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * Creates a new user global storages controller. |
|
| 53 | - * |
|
| 54 | - * @param string $AppName application name |
|
| 55 | - * @param IRequest $request request object |
|
| 56 | - * @param IL10N $l10n l10n service |
|
| 57 | - * @param UserGlobalStoragesService $userGlobalStoragesService storage service |
|
| 58 | - * @param IUserSession $userSession |
|
| 59 | - */ |
|
| 60 | - public function __construct( |
|
| 61 | - $AppName, |
|
| 62 | - IRequest $request, |
|
| 63 | - IL10N $l10n, |
|
| 64 | - UserGlobalStoragesService $userGlobalStoragesService, |
|
| 65 | - IUserSession $userSession, |
|
| 66 | - ILogger $logger |
|
| 67 | - ) { |
|
| 68 | - parent::__construct( |
|
| 69 | - $AppName, |
|
| 70 | - $request, |
|
| 71 | - $l10n, |
|
| 72 | - $userGlobalStoragesService, |
|
| 73 | - $logger |
|
| 74 | - ); |
|
| 75 | - $this->userSession = $userSession; |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * Get all storage entries |
|
| 80 | - * |
|
| 81 | - * @return DataResponse |
|
| 82 | - * |
|
| 83 | - * @NoAdminRequired |
|
| 84 | - */ |
|
| 85 | - public function index() { |
|
| 86 | - $storages = $this->service->getUniqueStorages(); |
|
| 87 | - |
|
| 88 | - // remove configuration data, this must be kept private |
|
| 89 | - foreach ($storages as $storage) { |
|
| 90 | - $this->sanitizeStorage($storage); |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - return new DataResponse( |
|
| 94 | - $storages, |
|
| 95 | - Http::STATUS_OK |
|
| 96 | - ); |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - protected function manipulateStorageConfig(StorageConfig $storage) { |
|
| 100 | - /** @var AuthMechanism */ |
|
| 101 | - $authMechanism = $storage->getAuthMechanism(); |
|
| 102 | - $authMechanism->manipulateStorageConfig($storage, $this->userSession->getUser()); |
|
| 103 | - /** @var Backend */ |
|
| 104 | - $backend = $storage->getBackend(); |
|
| 105 | - $backend->manipulateStorageConfig($storage, $this->userSession->getUser()); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * Get an external storage entry. |
|
| 110 | - * |
|
| 111 | - * @param int $id storage id |
|
| 112 | - * @param bool $testOnly whether to storage should only test the connection or do more things |
|
| 113 | - * @return DataResponse |
|
| 114 | - * |
|
| 115 | - * @NoAdminRequired |
|
| 116 | - */ |
|
| 117 | - public function show($id, $testOnly = true) { |
|
| 118 | - try { |
|
| 119 | - $storage = $this->service->getStorage($id); |
|
| 120 | - |
|
| 121 | - $this->updateStorageStatus($storage, $testOnly); |
|
| 122 | - } catch (NotFoundException $e) { |
|
| 123 | - return new DataResponse( |
|
| 124 | - [ |
|
| 125 | - 'message' => (string)$this->l10n->t('Storage with id "%i" not found', array($id)) |
|
| 126 | - ], |
|
| 127 | - Http::STATUS_NOT_FOUND |
|
| 128 | - ); |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - $this->sanitizeStorage($storage); |
|
| 132 | - |
|
| 133 | - return new DataResponse( |
|
| 134 | - $storage, |
|
| 135 | - Http::STATUS_OK |
|
| 136 | - ); |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - /** |
|
| 140 | - * Update an external storage entry. |
|
| 141 | - * Only allows setting user provided backend fields |
|
| 142 | - * |
|
| 143 | - * @param int $id storage id |
|
| 144 | - * @param array $backendOptions backend-specific options |
|
| 145 | - * @param bool $testOnly whether to storage should only test the connection or do more things |
|
| 146 | - * |
|
| 147 | - * @return DataResponse |
|
| 148 | - * |
|
| 149 | - * @NoAdminRequired |
|
| 150 | - */ |
|
| 151 | - public function update( |
|
| 152 | - $id, |
|
| 153 | - $backendOptions, |
|
| 154 | - $testOnly = true |
|
| 155 | - ) { |
|
| 156 | - try { |
|
| 157 | - $storage = $this->service->getStorage($id); |
|
| 158 | - $authMechanism = $storage->getAuthMechanism(); |
|
| 159 | - if ($authMechanism instanceof IUserProvided) { |
|
| 160 | - $authMechanism->saveBackendOptions($this->userSession->getUser(), $id, $backendOptions); |
|
| 161 | - $authMechanism->manipulateStorageConfig($storage, $this->userSession->getUser()); |
|
| 162 | - } else { |
|
| 163 | - return new DataResponse( |
|
| 164 | - [ |
|
| 165 | - 'message' => (string)$this->l10n->t('Storage with id "%i" is not user editable', array($id)) |
|
| 166 | - ], |
|
| 167 | - Http::STATUS_FORBIDDEN |
|
| 168 | - ); |
|
| 169 | - } |
|
| 170 | - } catch (NotFoundException $e) { |
|
| 171 | - return new DataResponse( |
|
| 172 | - [ |
|
| 173 | - 'message' => (string)$this->l10n->t('Storage with id "%i" not found', array($id)) |
|
| 174 | - ], |
|
| 175 | - Http::STATUS_NOT_FOUND |
|
| 176 | - ); |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - $this->updateStorageStatus($storage, $testOnly); |
|
| 180 | - $this->sanitizeStorage($storage); |
|
| 181 | - |
|
| 182 | - return new DataResponse( |
|
| 183 | - $storage, |
|
| 184 | - Http::STATUS_OK |
|
| 185 | - ); |
|
| 186 | - |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * Remove sensitive data from a StorageConfig before returning it to the user |
|
| 191 | - * |
|
| 192 | - * @param StorageConfig $storage |
|
| 193 | - */ |
|
| 194 | - protected function sanitizeStorage(StorageConfig $storage) { |
|
| 195 | - $storage->setBackendOptions([]); |
|
| 196 | - $storage->setMountOptions([]); |
|
| 197 | - |
|
| 198 | - if ($storage->getAuthMechanism() instanceof IUserProvided) { |
|
| 199 | - try { |
|
| 200 | - $storage->getAuthMechanism()->manipulateStorageConfig($storage, $this->userSession->getUser()); |
|
| 201 | - } catch (InsufficientDataForMeaningfulAnswerException $e) { |
|
| 202 | - // not configured yet |
|
| 203 | - } |
|
| 204 | - } |
|
| 205 | - } |
|
| 46 | + /** |
|
| 47 | + * @var IUserSession |
|
| 48 | + */ |
|
| 49 | + private $userSession; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * Creates a new user global storages controller. |
|
| 53 | + * |
|
| 54 | + * @param string $AppName application name |
|
| 55 | + * @param IRequest $request request object |
|
| 56 | + * @param IL10N $l10n l10n service |
|
| 57 | + * @param UserGlobalStoragesService $userGlobalStoragesService storage service |
|
| 58 | + * @param IUserSession $userSession |
|
| 59 | + */ |
|
| 60 | + public function __construct( |
|
| 61 | + $AppName, |
|
| 62 | + IRequest $request, |
|
| 63 | + IL10N $l10n, |
|
| 64 | + UserGlobalStoragesService $userGlobalStoragesService, |
|
| 65 | + IUserSession $userSession, |
|
| 66 | + ILogger $logger |
|
| 67 | + ) { |
|
| 68 | + parent::__construct( |
|
| 69 | + $AppName, |
|
| 70 | + $request, |
|
| 71 | + $l10n, |
|
| 72 | + $userGlobalStoragesService, |
|
| 73 | + $logger |
|
| 74 | + ); |
|
| 75 | + $this->userSession = $userSession; |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * Get all storage entries |
|
| 80 | + * |
|
| 81 | + * @return DataResponse |
|
| 82 | + * |
|
| 83 | + * @NoAdminRequired |
|
| 84 | + */ |
|
| 85 | + public function index() { |
|
| 86 | + $storages = $this->service->getUniqueStorages(); |
|
| 87 | + |
|
| 88 | + // remove configuration data, this must be kept private |
|
| 89 | + foreach ($storages as $storage) { |
|
| 90 | + $this->sanitizeStorage($storage); |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + return new DataResponse( |
|
| 94 | + $storages, |
|
| 95 | + Http::STATUS_OK |
|
| 96 | + ); |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + protected function manipulateStorageConfig(StorageConfig $storage) { |
|
| 100 | + /** @var AuthMechanism */ |
|
| 101 | + $authMechanism = $storage->getAuthMechanism(); |
|
| 102 | + $authMechanism->manipulateStorageConfig($storage, $this->userSession->getUser()); |
|
| 103 | + /** @var Backend */ |
|
| 104 | + $backend = $storage->getBackend(); |
|
| 105 | + $backend->manipulateStorageConfig($storage, $this->userSession->getUser()); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * Get an external storage entry. |
|
| 110 | + * |
|
| 111 | + * @param int $id storage id |
|
| 112 | + * @param bool $testOnly whether to storage should only test the connection or do more things |
|
| 113 | + * @return DataResponse |
|
| 114 | + * |
|
| 115 | + * @NoAdminRequired |
|
| 116 | + */ |
|
| 117 | + public function show($id, $testOnly = true) { |
|
| 118 | + try { |
|
| 119 | + $storage = $this->service->getStorage($id); |
|
| 120 | + |
|
| 121 | + $this->updateStorageStatus($storage, $testOnly); |
|
| 122 | + } catch (NotFoundException $e) { |
|
| 123 | + return new DataResponse( |
|
| 124 | + [ |
|
| 125 | + 'message' => (string)$this->l10n->t('Storage with id "%i" not found', array($id)) |
|
| 126 | + ], |
|
| 127 | + Http::STATUS_NOT_FOUND |
|
| 128 | + ); |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + $this->sanitizeStorage($storage); |
|
| 132 | + |
|
| 133 | + return new DataResponse( |
|
| 134 | + $storage, |
|
| 135 | + Http::STATUS_OK |
|
| 136 | + ); |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + /** |
|
| 140 | + * Update an external storage entry. |
|
| 141 | + * Only allows setting user provided backend fields |
|
| 142 | + * |
|
| 143 | + * @param int $id storage id |
|
| 144 | + * @param array $backendOptions backend-specific options |
|
| 145 | + * @param bool $testOnly whether to storage should only test the connection or do more things |
|
| 146 | + * |
|
| 147 | + * @return DataResponse |
|
| 148 | + * |
|
| 149 | + * @NoAdminRequired |
|
| 150 | + */ |
|
| 151 | + public function update( |
|
| 152 | + $id, |
|
| 153 | + $backendOptions, |
|
| 154 | + $testOnly = true |
|
| 155 | + ) { |
|
| 156 | + try { |
|
| 157 | + $storage = $this->service->getStorage($id); |
|
| 158 | + $authMechanism = $storage->getAuthMechanism(); |
|
| 159 | + if ($authMechanism instanceof IUserProvided) { |
|
| 160 | + $authMechanism->saveBackendOptions($this->userSession->getUser(), $id, $backendOptions); |
|
| 161 | + $authMechanism->manipulateStorageConfig($storage, $this->userSession->getUser()); |
|
| 162 | + } else { |
|
| 163 | + return new DataResponse( |
|
| 164 | + [ |
|
| 165 | + 'message' => (string)$this->l10n->t('Storage with id "%i" is not user editable', array($id)) |
|
| 166 | + ], |
|
| 167 | + Http::STATUS_FORBIDDEN |
|
| 168 | + ); |
|
| 169 | + } |
|
| 170 | + } catch (NotFoundException $e) { |
|
| 171 | + return new DataResponse( |
|
| 172 | + [ |
|
| 173 | + 'message' => (string)$this->l10n->t('Storage with id "%i" not found', array($id)) |
|
| 174 | + ], |
|
| 175 | + Http::STATUS_NOT_FOUND |
|
| 176 | + ); |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + $this->updateStorageStatus($storage, $testOnly); |
|
| 180 | + $this->sanitizeStorage($storage); |
|
| 181 | + |
|
| 182 | + return new DataResponse( |
|
| 183 | + $storage, |
|
| 184 | + Http::STATUS_OK |
|
| 185 | + ); |
|
| 186 | + |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * Remove sensitive data from a StorageConfig before returning it to the user |
|
| 191 | + * |
|
| 192 | + * @param StorageConfig $storage |
|
| 193 | + */ |
|
| 194 | + protected function sanitizeStorage(StorageConfig $storage) { |
|
| 195 | + $storage->setBackendOptions([]); |
|
| 196 | + $storage->setMountOptions([]); |
|
| 197 | + |
|
| 198 | + if ($storage->getAuthMechanism() instanceof IUserProvided) { |
|
| 199 | + try { |
|
| 200 | + $storage->getAuthMechanism()->manipulateStorageConfig($storage, $this->userSession->getUser()); |
|
| 201 | + } catch (InsufficientDataForMeaningfulAnswerException $e) { |
|
| 202 | + // not configured yet |
|
| 203 | + } |
|
| 204 | + } |
|
| 205 | + } |
|
| 206 | 206 | |
| 207 | 207 | } |