diff options
author | Robin McCorkell <rmccorkell@owncloud.com> | 2015-09-14 20:31:24 +0100 |
---|---|---|
committer | Robin McCorkell <rmccorkell@owncloud.com> | 2015-09-16 13:12:47 +0100 |
commit | ca7b4a42f97728d41e143c2fc8534265ab59523d (patch) | |
tree | fce2a41cf061e81a220af526c338d5807dad43dd /apps/files_external | |
parent | 4d77fac19194639a8150d24ee103043ad3dc9fda (diff) | |
download | nextcloud-server-ca7b4a42f97728d41e143c2fc8534265ab59523d.tar.gz nextcloud-server-ca7b4a42f97728d41e143c2fc8534265ab59523d.zip |
Fix external storage priority logic
Diffstat (limited to 'apps/files_external')
-rw-r--r-- | apps/files_external/lib/config/configadapter.php | 2 | ||||
-rw-r--r-- | apps/files_external/service/userglobalstoragesservice.php | 57 |
2 files changed, 58 insertions, 1 deletions
diff --git a/apps/files_external/lib/config/configadapter.php b/apps/files_external/lib/config/configadapter.php index a255a7b3d25..cb8c2f24caa 100644 --- a/apps/files_external/lib/config/configadapter.php +++ b/apps/files_external/lib/config/configadapter.php @@ -114,7 +114,7 @@ class ConfigAdapter implements IMountProvider { $this->userStoragesService->setUser($user); $this->userGlobalStoragesService->setUser($user); - foreach ($this->userGlobalStoragesService->getAllStorages() as $storage) { + foreach ($this->userGlobalStoragesService->getUniqueStorages() as $storage) { try { $this->prepareStorageConfig($storage, $user); $impl = $this->constructStorage($storage); diff --git a/apps/files_external/service/userglobalstoragesservice.php b/apps/files_external/service/userglobalstoragesservice.php index c59652d057f..b60473f131e 100644 --- a/apps/files_external/service/userglobalstoragesservice.php +++ b/apps/files_external/service/userglobalstoragesservice.php @@ -26,6 +26,7 @@ use \OCA\Files_External\Service\BackendService; use \OCP\IUserSession; use \OCP\IGroupManager; use \OCA\Files_External\Service\UserTrait; +use \OCA\Files_External\Lib\StorageConfig; /** * Service class to read global storages applicable to the user @@ -109,4 +110,60 @@ class UserGlobalStoragesService extends GlobalStoragesService { throw new \DomainException('UserGlobalStoragesService writing disallowed'); } + /** + * Get unique storages, in case two are defined with the same mountpoint + * Higher priority storages take precedence + * + * @return StorageConfig[] + */ + public function getUniqueStorages() { + $storages = $this->getAllStorages(); + + $storagesByMountpoint = []; + foreach ($storages as $storage) { + $storagesByMountpoint[$storage->getMountPoint()][] = $storage; + } + + $result = []; + foreach ($storagesByMountpoint as $storageList) { + $storage = array_reduce($storageList, function($carry, $item) { + if (isset($carry)) { + $carryPriorityType = $this->getPriorityType($carry); + $itemPriorityType = $this->getPriorityType($item); + if ($carryPriorityType > $itemPriorityType) { + return $carry; + } elseif ($carryPriorityType === $itemPriorityType) { + if ($carry->getPriority() > $item->getPriority()) { + return $carry; + } + } + } + return $item; + }); + $result[$storage->getID()] = $storage; + } + + return $result; + } + + /** + * Get a priority 'type', where a bigger number means higher priority + * user applicable > group applicable > 'all' + * + * @param StorageConfig $storage + * @return int + */ + protected function getPriorityType(StorageConfig $storage) { + $applicableUsers = $storage->getApplicableUsers(); + $applicableGroups = $storage->getApplicableGroups(); + + if ($applicableUsers && $applicableUsers[0] !== 'all') { + return 2; + } + if ($applicableGroups) { + return 1; + } + return 0; + } + } |