diff options
author | Vincent Petry <pvince81@owncloud.com> | 2015-03-16 12:18:01 +0100 |
---|---|---|
committer | Vincent Petry <pvince81@owncloud.com> | 2015-03-16 12:18:01 +0100 |
commit | 72632ad402bd905107db836ed0f9bfae825d6c52 (patch) | |
tree | 509bb82f2db24bb5d27bd4dea2b926f68ff8dddc /apps/files_external/service/storagesservice.php | |
parent | fb4cf532533d99abfd1b4f40ef6319d484f6ef85 (diff) | |
download | nextcloud-server-72632ad402bd905107db836ed0f9bfae825d6c52.tar.gz nextcloud-server-72632ad402bd905107db836ed0f9bfae825d6c52.zip |
Generate storage config ids when missing
When reading in old mount.json files, they do not contain config ids.
Since these are needed to be able to use the UI and the new service
classes, these will be generated automatically.
The config grouping is based on a config hash.
Diffstat (limited to 'apps/files_external/service/storagesservice.php')
-rw-r--r-- | apps/files_external/service/storagesservice.php | 130 |
1 files changed, 101 insertions, 29 deletions
diff --git a/apps/files_external/service/storagesservice.php b/apps/files_external/service/storagesservice.php index 46a485a169c..73a0ae76475 100644 --- a/apps/files_external/service/storagesservice.php +++ b/apps/files_external/service/storagesservice.php @@ -30,6 +30,39 @@ abstract class StoragesService { } /** + * Copy legacy storage options into the given storage config object. + * + * @param StorageConfig $storageConfig storage config to populate + * @param string $mountType mount type + * @param string $applicable applicable user or group + * @param array $storageOptions legacy storage options + * @return StorageConfig populated storage config + */ + protected function populateStorageConfigWithLegacyOptions(&$storageConfig, $mountType, $applicable, $storageOptions) { + $storageConfig->setBackendClass($storageOptions['class']); + $storageConfig->setBackendOptions($storageOptions['options']); + if (isset($storageOptions['mountOptions'])) { + $storageConfig->setMountOptions($storageOptions['mountOptions']); + } + if (isset($storageOptions['priority'])) { + $storageConfig->setPriority($storageOptions['priority']); + } + + if ($mountType === \OC_Mount_Config::MOUNT_TYPE_USER) { + $applicableUsers = $storageConfig->getApplicableUsers(); + if ($applicable !== 'all') { + $applicableUsers[] = $applicable; + $storageConfig->setApplicableUsers($applicableUsers); + } + } else if ($mountType === \OC_Mount_Config::MOUNT_TYPE_GROUP) { + $applicableGroups = $storageConfig->getApplicableGroups(); + $applicableGroups[] = $applicable; + $storageConfig->setApplicableGroups($applicableGroups); + } + return $storageConfig; + } + + /** * Read the external storages config * * @return array map of storage id to storage config @@ -55,9 +88,25 @@ abstract class StoragesService { // group by storage id $storages = []; + + // for storages without id (legacy), group by config hash for + // later processing + $storagesWithConfigHash = []; + foreach ($mountPoints as $mountType => $applicables) { foreach ($applicables as $applicable => $mountPaths) { foreach ($mountPaths as $rootMountPath => $storageOptions) { + $currentStorage = null; + + /** + * Flag whether the config that was read already has an id. + * If not, it will use a config hash instead and generate + * a proper id later + * + * @var boolean + */ + $hasId = false; + // the root mount point is in the format "/$user/files/the/mount/point" // we remove the "/$user/files" prefix $parts = explode('/', trim($rootMountPath, '/'), 3); @@ -73,46 +122,60 @@ abstract class StoragesService { $relativeMountPath = $parts[2]; - $configId = (int)$storageOptions['id']; - if (isset($storages[$configId])) { - $currentStorage = $storages[$configId]; + // note: we cannot do this after the loop because the decrypted config + // options might be needed for the config hash + $storageOptions['options'] = \OC_Mount_Config::decryptPasswords($storageOptions['options']); + + if (isset($storageOptions['id'])) { + $configId = (int)$storageOptions['id']; + if (isset($storages[$configId])) { + $currentStorage = $storages[$configId]; + } + $hasId = true; } else { + // missing id in legacy config, need to generate + // but at this point we don't know the max-id, so use + // first group it by config hash + $storageOptions['mountpoint'] = $rootMountPath; + $configId = \OC_Mount_Config::makeConfigHash($storageOptions); + if (isset($storagesWithConfigHash[$configId])) { + $currentStorage = $storagesWithConfigHash[$configId]; + } + } + + if (is_null($currentStorage)) { + // create new $currentStorage = new StorageConfig($configId); $currentStorage->setMountPoint($relativeMountPath); } - $currentStorage->setBackendClass($storageOptions['class']); - $currentStorage->setBackendOptions($storageOptions['options']); - if (isset($storageOptions['mountOptions'])) { - $currentStorage->setMountOptions($storageOptions['mountOptions']); - } - if (isset($storageOptions['priority'])) { - $currentStorage->setPriority($storageOptions['priority']); - } + $this->populateStorageConfigWithLegacyOptions( + $currentStorage, + $mountType, + $applicable, + $storageOptions + ); - if ($mountType === \OC_Mount_Config::MOUNT_TYPE_USER) { - $applicableUsers = $currentStorage->getApplicableUsers(); - if ($applicable !== 'all') { - $applicableUsers[] = $applicable; - $currentStorage->setApplicableUsers($applicableUsers); - } - } else if ($mountType === \OC_Mount_Config::MOUNT_TYPE_GROUP) { - $applicableGroups = $currentStorage->getApplicableGroups(); - $applicableGroups[] = $applicable; - $currentStorage->setApplicableGroups($applicableGroups); + if ($hasId) { + $storages[$configId] = $currentStorage; + } else { + $storagesWithConfigHash[$configId] = $currentStorage; } - $storages[$configId] = $currentStorage; } } } - // decrypt passwords - foreach ($storages as &$storage) { - $storage->setBackendOptions( - \OC_Mount_Config::decryptPasswords( - $storage->getBackendOptions() - ) - ); + // process storages with config hash, they must get a real id + if (!empty($storagesWithConfigHash)) { + $nextId = $this->generateNextId($storages); + foreach ($storagesWithConfigHash as $storage) { + $storage->setId($nextId); + $storages[$nextId] = $storage; + $nextId++; + } + + // re-save the config with the generated ids + $this->writeConfig($storages); } return $storages; @@ -177,6 +240,15 @@ abstract class StoragesService { } /** + * Gets all storages + * + * @return array array of storage configs + */ + public function getAllStorages() { + return $this->readConfig(); + } + + /** * Add new storage to the configuration * * @param array $newStorage storage attributes |