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