diff options
author | Robin McCorkell <rmccorkell@owncloud.com> | 2015-08-12 09:21:09 +0100 |
---|---|---|
committer | Robin McCorkell <rmccorkell@owncloud.com> | 2015-08-19 10:08:14 +0100 |
commit | 97dbc79c16ba9d4d6c361d6f397908ef7893954d (patch) | |
tree | 50f5c1bff61dd5985158f9ffcbba81b85e669605 /apps | |
parent | c572631087e2b56aa48c87ac753447e709248234 (diff) | |
download | nextcloud-server-97dbc79c16ba9d4d6c361d6f397908ef7893954d.tar.gz nextcloud-server-97dbc79c16ba9d4d6c361d6f397908ef7893954d.zip |
Compatibility shims for OC_Mount_Config
The following functions have been removed:
- addMountPoint()
- removeMountPoint()
- movePersonalMountPoint()
registerBackend() has been rewritten as a shim around BackendService,
allowing legacy code to interact with the new API seamlessly
addMountPoint() was already disconnected from all production code, so
this commit completes the job and removes the function itself, along
with disconnecting and removing related functions. Unit tests have
likewise been removed.
getAbsoluteMountPoints(), getSystemMountPoints() and
getPersonalMountPoints() have been rewritten to use the StoragesServices
Diffstat (limited to 'apps')
-rw-r--r-- | apps/files_external/appinfo/application.php | 3 | ||||
-rw-r--r-- | apps/files_external/lib/auth/authmechanism.php | 1 | ||||
-rw-r--r-- | apps/files_external/lib/auth/builtin.php | 41 | ||||
-rw-r--r-- | apps/files_external/lib/backend/legacybackend.php | 85 | ||||
-rw-r--r-- | apps/files_external/lib/config.php | 539 | ||||
-rw-r--r-- | apps/files_external/tests/backend/legacybackendtest.php | 4 | ||||
-rw-r--r-- | apps/files_external/tests/mountconfig.php | 1157 |
7 files changed, 231 insertions, 1599 deletions
diff --git a/apps/files_external/appinfo/application.php b/apps/files_external/appinfo/application.php index 19da1f724ba..38b9e9b7c36 100644 --- a/apps/files_external/appinfo/application.php +++ b/apps/files_external/appinfo/application.php @@ -72,6 +72,9 @@ class Application extends App { $service->registerAuthMechanisms([ // AuthMechanism::SCHEME_NULL mechanism $container->query('OCA\Files_External\Lib\Auth\NullMechanism'), + + // AuthMechanism::SCHEME_BUILTIN mechanism + $container->query('OCA\Files_External\Lib\Auth\Builtin'), ]); } diff --git a/apps/files_external/lib/auth/authmechanism.php b/apps/files_external/lib/auth/authmechanism.php index a89ee823d51..11d99bb330d 100644 --- a/apps/files_external/lib/auth/authmechanism.php +++ b/apps/files_external/lib/auth/authmechanism.php @@ -51,6 +51,7 @@ class AuthMechanism implements \JsonSerializable { /** Standard authentication schemes */ const SCHEME_NULL = 'null'; + const SCHEME_BUILTIN = 'builtin'; const SCHEME_PASSWORD = 'password'; const SCHEME_OAUTH1 = 'oauth1'; const SCHEME_OAUTH2 = 'oauth2'; diff --git a/apps/files_external/lib/auth/builtin.php b/apps/files_external/lib/auth/builtin.php new file mode 100644 index 00000000000..ee28a4e8a5c --- /dev/null +++ b/apps/files_external/lib/auth/builtin.php @@ -0,0 +1,41 @@ +<?php +/** + * @author Robin McCorkell <rmccorkell@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OCA\Files_External\Lib\Auth; + +use \OCP\IL10N; +use \OCA\Files_External\Lib\Auth\AuthMechanism; +use \OCA\Files_external\Lib\StorageConfig; + +/** + * Builtin authentication mechanism, for legacy backends + */ +class Builtin extends AuthMechanism { + + public function __construct(IL10N $l) { + $this + ->setIdentifier('builtin::builtin') + ->setScheme(self::SCHEME_BUILTIN) + ->setText($l->t('Builtin')) + ; + } + +} diff --git a/apps/files_external/lib/backend/legacybackend.php b/apps/files_external/lib/backend/legacybackend.php new file mode 100644 index 00000000000..0f60c2caa47 --- /dev/null +++ b/apps/files_external/lib/backend/legacybackend.php @@ -0,0 +1,85 @@ +<?php +/** + * @author Robin McCorkell <rmccorkell@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OCA\Files_External\Lib\Backend; + +use \OCA\Files_External\Lib\DefinitionParameter; +use \OCA\Files_External\Lib\Backend\Backend; +use \OCA\Files_External\Lib\Auth\Builtin; + +/** + * Legacy compatibility for OC_Mount_Config::registerBackend() + */ +class LegacyBackend extends Backend { + + /** + * @param string $class + * @param array $definition + * @param Builtin $authMechanism + */ + public function __construct($class, array $definition, Builtin $authMechanism) { + $this + ->setIdentifier($class) + ->setStorageClass($class) + ->setText($definition['backend']) + ->addAuthScheme(Builtin::SCHEME_BUILTIN) + ->setLegacyAuthMechanism($authMechanism) + ; + + foreach ($definition['configuration'] as $name => $placeholder) { + $flags = DefinitionParameter::FLAG_NONE; + $type = DefinitionParameter::VALUE_TEXT; + if ($placeholder[0] === '&') { + $flags = DefinitionParameter::FLAG_OPTIONAL; + $placeholder = substr($placeholder, 1); + } + switch ($placeholder[0]) { + case '!': + $type = DefinitionParameter::VALUE_BOOLEAN; + $placeholder = substr($placeholder, 1); + break; + case '*': + $type = DefinitionParameter::VALUE_PASSWORD; + $placeholder = substr($placeholder, 1); + break; + case '#': + $type = DefinitionParameter::VALUE_HIDDEN; + $placeholder = substr($placeholder, 1); + break; + } + $this->addParameter((new DefinitionParameter($name, $placeholder)) + ->setType($type) + ->setFlags($flags) + ); + } + + if (isset($definition['priority'])) { + $this->setPriority($definition['priority']); + } + if (isset($definition['custom'])) { + $this->setCustomJs($definition['custom']); + } + if (isset($definition['has_dependencies']) && $definition['has_dependencies']) { + $this->setDependencyCheck($class . '::checkDependencies'); + } + } + +} diff --git a/apps/files_external/lib/config.php b/apps/files_external/lib/config.php index c0ded522de0..6fdf050fb60 100644 --- a/apps/files_external/lib/config.php +++ b/apps/files_external/lib/config.php @@ -35,6 +35,8 @@ use phpseclib\Crypt\AES; use \OCP\AppFramework\IAppContainer; use \OCA\Files_External\Lib\BackendConfig; use \OCA\Files_External\Service\BackendService; +use \OCA\Files_External\Lib\Backend\LegacyBackend; +use \OCA\Files_External\Lib\StorageConfig; /** * Class to configure mount.json globally and for users @@ -66,6 +68,21 @@ class OC_Mount_Config { self::$appContainer = $appContainer; } + /** + * @param string $class + * @param array $definition + * @return bool + * @deprecated 8.2.0 use \OCA\Files_External\Service\BackendService::registerBackend() + */ + public static function registerBackend($class, $definition) { + $backendService = self::$appContainer->query('OCA\Files_External\Service\BackendService'); + $auth = self::$appContainer->query('OCA\Files_External\Lib\Auth\Builtin'); + + $backendService->registerBackend(new LegacyBackend($class, $definition, $auth)); + + return true; + } + /* * Hook that mounts the given user's visible mount points * @@ -102,331 +119,125 @@ class OC_Mount_Config { /** * Returns the mount points for the given user. * The mount point is relative to the data directory. - * TODO: Move me into StoragesService * - * @param string $user user + * @param string $uid user * @return array of mount point string as key, mountpoint config as value + * + * @deprecated 8.2.0 use UserGlobalStoragesService::getAllStorages() and UserStoragesService::getAllStorages() */ - public static function getAbsoluteMountPoints($user) { + public static function getAbsoluteMountPoints($uid) { $mountPoints = array(); - $backendService = self::$appContainer->query('OCA\Files_External\Service\BackendService'); - // Load system mount points - $mountConfig = self::readData(); + $userGlobalStoragesService = self::$appContainer->query('OCA\Files_External\Service\UserGlobalStoragesService'); + $userStoragesService = self::$appContainer->query('OCA\Files_External\Service\UserStoragesService'); + $user = self::$appContainer->query('OCP\IUserManager')->get($uid); - // Global mount points (is this redundant?) - if (isset($mountConfig[self::MOUNT_TYPE_GLOBAL])) { - foreach ($mountConfig[self::MOUNT_TYPE_GLOBAL] as $mountPoint => $options) { - if (!isset($options['backend'])) { - $options['backend'] = $options['class']; - } - $backend = $backendService->getBackend($options['backend']); - $options['personal'] = false; - $options['options'] = self::decryptPasswords($options['options']); - if (!isset($options['priority'])) { - $options['priority'] = $backend->getPriority(); - } - if (!isset($options['authMechanism'])) { - $options['authMechanism'] = $backend->getLegacyAuthMechanism($options['options'])->getIdentifier(); - } + $userGlobalStoragesService->setUser($user); + $userStoragesService->setUser($user); - // Override if priority greater - if ((!isset($mountPoints[$mountPoint])) - || ($options['priority'] >= $mountPoints[$mountPoint]['priority']) - ) { - $options['priority_type'] = self::MOUNT_TYPE_GLOBAL; - $options['backend'] = $backend->getText(); - $mountPoints[$mountPoint] = $options; - } + foreach ($userGlobalStoragesService->getAllStorages() as $storage) { + $mountPoint = '/'.$uid.'/files'.$storage->getMountPoint(); + $mountEntry = self::prepareMountPointEntry($storage, false); + foreach ($mountEntry['options'] as &$option) { + $option = self::setUserVars($uid, $option); } + $mountPoints[$mountPoint] = $mountEntry; } - // All user mount points - if (isset($mountConfig[self::MOUNT_TYPE_USER]) && isset($mountConfig[self::MOUNT_TYPE_USER]['all'])) { - $mounts = $mountConfig[self::MOUNT_TYPE_USER]['all']; - foreach ($mounts as $mountPoint => $options) { - $mountPoint = self::setUserVars($user, $mountPoint); - foreach ($options as &$option) { - $option = self::setUserVars($user, $option); - } - if (!isset($options['backend'])) { - $options['backend'] = $options['class']; - } - $backend = $backendService->getBackend($options['backend']); - $options['personal'] = false; - $options['options'] = self::decryptPasswords($options['options']); - if (!isset($options['priority'])) { - $options['priority'] = $backend->getPriority(); - } - if (!isset($options['authMechanism'])) { - $options['authMechanism'] = $backend->getLegacyAuthMechanism($options['options'])->getIdentifier(); - } - // Override if priority greater - if ((!isset($mountPoints[$mountPoint])) - || ($options['priority'] >= $mountPoints[$mountPoint]['priority']) - ) { - $options['priority_type'] = self::MOUNT_TYPE_GLOBAL; - $options['backend'] = $backend->getText(); - $mountPoints[$mountPoint] = $options; - } - } - } - // Group mount points - if (isset($mountConfig[self::MOUNT_TYPE_GROUP])) { - foreach ($mountConfig[self::MOUNT_TYPE_GROUP] as $group => $mounts) { - if (\OC_Group::inGroup($user, $group)) { - foreach ($mounts as $mountPoint => $options) { - $mountPoint = self::setUserVars($user, $mountPoint); - foreach ($options as &$option) { - $option = self::setUserVars($user, $option); - } - if (!isset($options['backend'])) { - $options['backend'] = $options['class']; - } - $backend = $backendService->getBackend($options['backend']); - $options['personal'] = false; - $options['options'] = self::decryptPasswords($options['options']); - if (!isset($options['priority'])) { - $options['priority'] = $backend->getPriority(); - } - if (!isset($options['authMechanism'])) { - $options['authMechanism'] = $backend->getLegacyAuthMechanism($options['options'])->getIdentifier(); - } - - // Override if priority greater or if priority type different - if ((!isset($mountPoints[$mountPoint])) - || ($options['priority'] >= $mountPoints[$mountPoint]['priority']) - || ($mountPoints[$mountPoint]['priority_type'] !== self::MOUNT_TYPE_GROUP) - ) { - $options['priority_type'] = self::MOUNT_TYPE_GROUP; - $options['backend'] = $backend->getText(); - $mountPoints[$mountPoint] = $options; - } - } - } - } - } - // User mount points - if (isset($mountConfig[self::MOUNT_TYPE_USER])) { - foreach ($mountConfig[self::MOUNT_TYPE_USER] as $mountUser => $mounts) { - if (strtolower($mountUser) === strtolower($user)) { - foreach ($mounts as $mountPoint => $options) { - $mountPoint = self::setUserVars($user, $mountPoint); - foreach ($options as &$option) { - $option = self::setUserVars($user, $option); - } - if (!isset($options['backend'])) { - $options['backend'] = $options['class']; - } - $backend = $backendService->getBackend($options['backend']); - $options['personal'] = false; - $options['options'] = self::decryptPasswords($options['options']); - if (!isset($options['priority'])) { - $options['priority'] = $backend->getPriority(); - } - if (!isset($options['authMechanism'])) { - $options['authMechanism'] = $backend->getLegacyAuthMechanism($options['options'])->getIdentifier(); - } - - // Override if priority greater or if priority type different - if ((!isset($mountPoints[$mountPoint])) - || ($options['priority'] >= $mountPoints[$mountPoint]['priority']) - || ($mountPoints[$mountPoint]['priority_type'] !== self::MOUNT_TYPE_USER) - ) { - $options['priority_type'] = self::MOUNT_TYPE_USER; - $options['backend'] = $backend->getText(); - $mountPoints[$mountPoint] = $options; - } - } - } + foreach ($userStoragesService->getAllStorages() as $storage) { + $mountPoint = '/'.$uid.'/files'.$storage->getMountPoint(); + $mountEntry = self::prepareMountPointEntry($storage, true); + foreach ($mountEntry['options'] as &$option) { + $option = self::setUserVars($uid, $option); } + $mountPoints[$mountPoint] = $mountEntry; } - // Load personal mount points - $mountConfig = self::readData($user); - if (isset($mountConfig[self::MOUNT_TYPE_USER][$user])) { - foreach ($mountConfig[self::MOUNT_TYPE_USER][$user] as $mountPoint => $options) { - if (!isset($options['backend'])) { - $options['backend'] = $options['class']; - } - $backend = $backendService->getBackend($options['backend']); - if ($backend->isVisibleFor(BackendService::VISIBILITY_PERSONAL)) { - $options['personal'] = true; - $options['options'] = self::decryptPasswords($options['options']); - if (!isset($options['authMechanism'])) { - $options['authMechanism'] = $backend->getLegacyAuthMechanism($options['options'])->getIdentifier(); - } - - // Always override previous config - $options['priority_type'] = self::MOUNT_TYPE_PERSONAL; - $options['backend'] = $backend->getText(); - $mountPoints[$mountPoint] = $options; - } - } - } + $userGlobalStoragesService->resetUser(); + $userStoragesService->resetUser(); return $mountPoints; } /** - * fill in the correct values for $user + * Get the system mount points * - * @param string $user user value - * @param string|array $input - * @return string + * @return array + * + * @deprecated 8.2.0 use GlobalStoragesService::getAllStorages() */ - private static function setUserVars($user, $input) { - if (is_array($input)) { - foreach ($input as &$value) { - if (is_string($value)) { - $value = str_replace('$user', $user, $value); - } - } - } else { - $input = str_replace('$user', $user, $input); + public static function getSystemMountPoints() { + $mountPoints = []; + $service = self::$appContainer->query('OCA\Files_External\Service\GlobalStoragesService'); + + foreach ($service->getAllStorages() as $storage) { + $mountPoints[] = self::prepareMountPointEntry($storage, false); } - return $input; } /** - * Get the system mount points - * The returned array is not in the same format as getUserMountPoints() + * Get the personal mount points of the current user * * @return array + * + * @deprecated 8.2.0 use UserStoragesService::getAllStorages() */ - public static function getSystemMountPoints() { - $mountPoints = self::readData(); - $backendService = self::$appContainer->query('\OCA\Files_External\Service\BackendService'); - $system = array(); - if (isset($mountPoints[self::MOUNT_TYPE_GROUP])) { - foreach ($mountPoints[self::MOUNT_TYPE_GROUP] as $group => $mounts) { - foreach ($mounts as $mountPoint => $mount) { - // Update old classes to new namespace - if (strpos($mount['class'], 'OC_Filestorage_') !== false) { - $mount['class'] = '\OC\Files\Storage\\' . substr($mount['class'], 15); - } - $backend = $backendService->getBackend($mount['class']); - $mount['options'] = self::decryptPasswords($mount['options']); - if (!isset($mount['priority'])) { - $mount['priority'] = $backend->getPriority(); - } - // Remove '/$user/files/' from mount point - $mountPoint = substr($mountPoint, 13); - - $config = array( - 'class' => $mount['class'], - 'mountpoint' => $mountPoint, - 'backend' => $backend->getText(), - 'priority' => $mount['priority'], - 'options' => $mount['options'], - 'applicable' => array('groups' => array($group), 'users' => array()) - ); - if (isset($mount['id'])) { - $config['id'] = (int)$mount['id']; - } - if (isset($mount['storage_id'])) { - $config['storage_id'] = (int)$mount['storage_id']; - } - if (isset($mount['mountOptions'])) { - $config['mountOptions'] = $mount['mountOptions']; - } - $hash = self::makeConfigHash($config); - // If an existing config exists (with same class, mountpoint and options) - if (isset($system[$hash])) { - // add the groups into that config - $system[$hash]['applicable']['groups'] - = array_merge($system[$hash]['applicable']['groups'], array($group)); - } else { - $system[$hash] = $config; - } - } - } - } - if (isset($mountPoints[self::MOUNT_TYPE_USER])) { - foreach ($mountPoints[self::MOUNT_TYPE_USER] as $user => $mounts) { - foreach ($mounts as $mountPoint => $mount) { - // Update old classes to new namespace - if (strpos($mount['class'], 'OC_Filestorage_') !== false) { - $mount['class'] = '\OC\Files\Storage\\' . substr($mount['class'], 15); - } - $backend = $backendService->getBackend($mount['class']); - $mount['options'] = self::decryptPasswords($mount['options']); - if (!isset($mount['priority'])) { - $mount['priority'] = $backend->getPriority(); - } - // Remove '/$user/files/' from mount point - $mountPoint = substr($mountPoint, 13); - $config = array( - 'class' => $mount['class'], - 'mountpoint' => $mountPoint, - 'backend' => $backend->getText(), - 'priority' => $mount['priority'], - 'options' => $mount['options'], - 'applicable' => array('groups' => array(), 'users' => array($user)) - ); - if (isset($mount['id'])) { - $config['id'] = (int)$mount['id']; - } - if (isset($mount['storage_id'])) { - $config['storage_id'] = (int)$mount['storage_id']; - } - if (isset($mount['mountOptions'])) { - $config['mountOptions'] = $mount['mountOptions']; - } - $hash = self::makeConfigHash($config); - // If an existing config exists (with same class, mountpoint and options) - if (isset($system[$hash])) { - // add the users into that config - $system[$hash]['applicable']['users'] - = array_merge($system[$hash]['applicable']['users'], array($user)); - } else { - $system[$hash] = $config; - } - } - } + public static function getPersonalMountPoints() { + $mountPoints = []; + $service = self::$appContainer->query('OCA\Files_External\Service\UserStoragesService'); + + foreach ($service->getAllStorages() as $storage) { + $mountPoints[] = self::prepareMountPointEntry($storage, true); } - return array_values($system); } /** - * Get the personal mount points of the current user - * The returned array is not in the same format as getUserMountPoints() + * Convert a StorageConfig to the legacy mountPoints array format + * There's a lot of extra information in here, to satisfy all of the legacy functions * + * @param StorageConfig $storage + * @param bool $isPersonal * @return array */ - public static function getPersonalMountPoints() { - $mountPoints = self::readData(OCP\User::getUser()); - $backendService = self::$appContainer->query('\OCA\Files_External\Service\BackendService'); - $uid = OCP\User::getUser(); - $personal = array(); - if (isset($mountPoints[self::MOUNT_TYPE_USER][$uid])) { - foreach ($mountPoints[self::MOUNT_TYPE_USER][$uid] as $mountPoint => $mount) { - // Update old classes to new namespace - if (strpos($mount['class'], 'OC_Filestorage_') !== false) { - $mount['class'] = '\OC\Files\Storage\\' . substr($mount['class'], 15); - } - $backend = $backendService->getBackend($mount['class']); - $mount['options'] = self::decryptPasswords($mount['options']); - $config = array( - 'class' => $mount['class'], - // Remove '/uid/files/' from mount point - 'mountpoint' => substr($mountPoint, strlen($uid) + 8), - 'backend' => $backend->getText(), - 'options' => $mount['options'] - ); - if (isset($mount['id'])) { - $config['id'] = (int)$mount['id']; - } - if (isset($mount['storage_id'])) { - $config['storage_id'] = (int)$mount['storage_id']; - } - if (isset($mount['mountOptions'])) { - $config['mountOptions'] = $mount['mountOptions']; + private static function prepareMountPointEntry(StorageConfig $storage, $isPersonal) { + $mountEntry = []; + + $mountEntry['mountpoint'] = substr($storage->getMountPoint(), 1); // remove leading slash + $mountEntry['class'] = $storage->getBackend()->getIdentifier(); + $mountEntry['backend'] = $storage->getBackend()->getText(); + $mountEntry['authMechanism'] = $storage->getAuthMechanism()->getIdentifier(); + $mountEntry['personal'] = $isPersonal; + $mountEntry['options'] = self::decryptPasswords($storage->getBackendOptions()); + $mountEntry['mountOptions'] = $storage->getMountOptions(); + $mountEntry['priority'] = $storage->getPriority(); + $mountEntry['applicable'] = [ + 'groups' => $storage->getApplicableGroups(), + 'users' => $storage->getApplicableUsers(), + ]; + $mountEntry['id'] = $storage->getId(); + // $mountEntry['storage_id'] = null; // we don't store this! + + return $mountEntry; + } + + /** + * fill in the correct values for $user + * + * @param string $user user value + * @param string|array $input + * @return string + */ + private static function setUserVars($user, $input) { + if (is_array($input)) { + foreach ($input as &$value) { + if (is_string($value)) { + $value = str_replace('$user', $user, $value); } - $personal[] = $config; } + } else { + $input = str_replace('$user', $user, $input); } - return $personal; + return $input; } /** @@ -465,158 +276,6 @@ class OC_Mount_Config { } /** - * Add a mount point to the filesystem - * - * @param string $mountPoint Mount point - * @param string $class Backend class - * @param array $classOptions Backend parameters for the class - * @param string $mountType MOUNT_TYPE_GROUP | MOUNT_TYPE_USER - * @param string $applicable User or group to apply mount to - * @param bool $isPersonal Personal or system mount point i.e. is this being called from the personal or admin page - * @param int|null $priority Mount point priority, null for default - * @return boolean - * - * @deprecated use StoragesService#addStorage() instead - */ - public static function addMountPoint($mountPoint, - $class, - $classOptions, - $mountType, - $applicable, - $isPersonal = false, - $priority = null) { - $backendService = self::$appContainer->query('\OCA\Files_External\Service\BackendService'); - $mountPoint = OC\Files\Filesystem::normalizePath($mountPoint); - $relMountPoint = $mountPoint; - if ($mountPoint === '' || $mountPoint === '/') { - // can't mount at root folder - return false; - } - - $backend = $backendService->getBackend($class); - if (!isset($backend)) { - // invalid backend - return false; - } - if ($isPersonal) { - // Verify that the mount point applies for the current user - // Prevent non-admin users from mounting local storage and other disabled backends - if ($applicable != OCP\User::getUser() || !$backend->isVisibleFor(BackendConfig::VISIBILITY_PERSONAL)) { - return false; - } - $mountPoint = '/' . $applicable . '/files/' . ltrim($mountPoint, '/'); - } else { - $mountPoint = '/$user/files/' . ltrim($mountPoint, '/'); - } - - $mount = array($applicable => array( - $mountPoint => array( - 'class' => $class, - 'options' => self::encryptPasswords($classOptions)) - ) - ); - if (!$isPersonal && !is_null($priority)) { - $mount[$applicable][$mountPoint]['priority'] = $priority; - } - - $mountPoints = self::readData($isPersonal ? OCP\User::getUser() : null); - // who else loves multi-dimensional array ? - $isNew = !isset($mountPoints[$mountType]) || - !isset($mountPoints[$mountType][$applicable]) || - !isset($mountPoints[$mountType][$applicable][$mountPoint]); - $mountPoints = self::mergeMountPoints($mountPoints, $mount, $mountType); - - // Set default priority if none set - if (!isset($mountPoints[$mountType][$applicable][$mountPoint]['priority'])) { - $mountPoints[$mountType][$applicable][$mountPoint]['priority'] - = $backend->getPriority(); - } - - self::writeData($isPersonal ? OCP\User::getUser() : null, $mountPoints); - - $result = self::getBackendStatus($class, $classOptions, $isPersonal); - if ($result === self::STATUS_SUCCESS && $isNew) { - \OC_Hook::emit( - \OC\Files\Filesystem::CLASSNAME, - \OC\Files\Filesystem::signal_create_mount, - array( - \OC\Files\Filesystem::signal_param_path => $relMountPoint, - \OC\Files\Filesystem::signal_param_mount_type => $mountType, - \OC\Files\Filesystem::signal_param_users => $applicable, - ) - ); - } - return $result; - } - - /** - * - * @param string $mountPoint Mount point - * @param string $mountType MOUNT_TYPE_GROUP | MOUNT_TYPE_USER - * @param string $applicable User or group to remove mount from - * @param bool $isPersonal Personal or system mount point - * @return bool - * - * @deprecated use StoragesService#removeStorage() instead - */ - public static function removeMountPoint($mountPoint, $mountType, $applicable, $isPersonal = false) { - // Verify that the mount point applies for the current user - $relMountPoints = $mountPoint; - if ($isPersonal) { - if ($applicable != OCP\User::getUser()) { - return false; - } - $mountPoint = '/' . $applicable . '/files/' . ltrim($mountPoint, '/'); - } else { - $mountPoint = '/$user/files/' . ltrim($mountPoint, '/'); - } - $mountPoint = \OC\Files\Filesystem::normalizePath($mountPoint); - $mountPoints = self::readData($isPersonal ? OCP\User::getUser() : null); - // Remove mount point - unset($mountPoints[$mountType][$applicable][$mountPoint]); - // Unset parent arrays if empty - if (empty($mountPoints[$mountType][$applicable])) { - unset($mountPoints[$mountType][$applicable]); - if (empty($mountPoints[$mountType])) { - unset($mountPoints[$mountType]); - } - } - self::writeData($isPersonal ? OCP\User::getUser() : null, $mountPoints); - \OC_Hook::emit( - \OC\Files\Filesystem::CLASSNAME, - \OC\Files\Filesystem::signal_delete_mount, - array( - \OC\Files\Filesystem::signal_param_path => $relMountPoints, - \OC\Files\Filesystem::signal_param_mount_type => $mountType, - \OC\Files\Filesystem::signal_param_users => $applicable, - ) - ); - return true; - } - - /** - * - * @param string $mountPoint Mount point - * @param string $target The new mount point - * @param string $mountType MOUNT_TYPE_GROUP | MOUNT_TYPE_USER - * @return bool - */ - public static function movePersonalMountPoint($mountPoint, $target, $mountType) { - $mountPoint = rtrim($mountPoint, '/'); - $user = OCP\User::getUser(); - $mountPoints = self::readData($user); - if (!isset($mountPoints[$mountType][$user][$mountPoint])) { - return false; - } - $mountPoints[$mountType][$user][$target] = $mountPoints[$mountType][$user][$mountPoint]; - // Remove old mount point - unset($mountPoints[$mountType][$user][$mountPoint]); - - self::writeData($user, $mountPoints); - return true; - } - - /** * Read the mount points in the config file into an array * * @param string|null $user If not null, personal for $user, otherwise system diff --git a/apps/files_external/tests/backend/legacybackendtest.php b/apps/files_external/tests/backend/legacybackendtest.php index ceedede1302..44cb16a4986 100644 --- a/apps/files_external/tests/backend/legacybackendtest.php +++ b/apps/files_external/tests/backend/legacybackendtest.php @@ -27,7 +27,7 @@ use \OCA\Files_External\Lib\DefinitionParameter; class LegacyBackendTest extends \Test\TestCase { public function testConstructor() { - $auth = $this->getMockBuilder('\OCA\Files_External\Lib\Auth\NullMechanism') + $auth = $this->getMockBuilder('\OCA\Files_External\Lib\Auth\Builtin') ->disableOriginalConstructor() ->getMock(); @@ -54,7 +54,7 @@ class LegacyBackendTest extends \Test\TestCase { $this->assertEquals(123, $backend->getPriority()); $this->assertEquals('foo/bar.js', $backend->getCustomJs()); $this->assertEquals(true, $backend->hasDependencies()); - $this->assertArrayHasKey('null', $backend->getAuthSchemes()); + $this->assertArrayHasKey('builtin', $backend->getAuthSchemes()); $this->assertEquals($auth, $backend->getLegacyAuthMechanism()); $parameters = $backend->getParameters(); diff --git a/apps/files_external/tests/mountconfig.php b/apps/files_external/tests/mountconfig.php deleted file mode 100644 index b76ba0a39a6..00000000000 --- a/apps/files_external/tests/mountconfig.php +++ /dev/null @@ -1,1157 +0,0 @@ -<?php -/** - * @author Björn Schießle <schiessle@owncloud.com> - * @author Joas Schilling <nickvergessen@owncloud.com> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Robin Appelman <icewind@owncloud.com> - * @author Robin McCorkell <rmccorkell@karoshi.org.uk> - * @author Vincent Petry <pvince81@owncloud.com> - * - * @copyright Copyright (c) 2015, ownCloud, Inc. - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * - */ - -class Test_Mount_Config_Dummy_Storage extends \OC\Files\Storage\Common { - public function __construct($params) { - if (isset($params['simulateFail']) && $params['simulateFail'] == true) { - throw new \Exception('Simulated config validation fail'); - } - } - - public function getId() { - return 'dummy_storage'; - } - - public function mkdir($path) { - return false; - } - - public function rmdir($path) { - return false; - } - - public function opendir($path) { - return false; - } - - public function filetype($path) { - return false; - } - - public function file_exists($path) { - return false; - } - - public function unlink($path) { - return false; - } - - public function fopen($path, $mode) { - return false; - } - - public function touch($path, $mtime = null) { - return false; - } - - public function stat($path) { - return false; - } - - public function test() { - return true; - } -} - -class Test_Mount_Config_Storage_No_Personal extends Test_Mount_Config_Dummy_Storage { -} - -class Test_Mount_Config_Hook_Test { - static $signal; - static $params; - - public static function setUpHooks() { - self::clear(); - \OCP\Util::connectHook( - \OC\Files\Filesystem::CLASSNAME, - \OC\Files\Filesystem::signal_create_mount, - '\Test_Mount_Config_Hook_Test', 'createHookCallback'); - \OCP\Util::connectHook( - \OC\Files\Filesystem::CLASSNAME, - \OC\Files\Filesystem::signal_delete_mount, - '\Test_Mount_Config_Hook_Test', 'deleteHookCallback'); - } - - public static function clear() { - self::$signal = null; - self::$params = null; - } - - public static function createHookCallback($params) { - self::$signal = \OC\Files\Filesystem::signal_create_mount; - self::$params = $params; - } - - public static function deleteHookCallback($params) { - self::$signal = \OC\Files\Filesystem::signal_delete_mount; - self::$params = $params; - } - - public static function getLastCall() { - return array(self::$signal, self::$params); - } -} - -/** - * Class Test_Mount_Config - */ -class Test_Mount_Config extends \Test\TestCase { - - private $dataDir; - private $userHome; - private $oldAllowedBackends; - - const TEST_USER1 = 'user1'; - const TEST_USER2 = 'user2'; - const TEST_GROUP1 = 'group1'; - const TEST_GROUP1B = 'group1b'; - const TEST_GROUP2 = 'group2'; - const TEST_GROUP2B = 'group2b'; - - protected function setUp() { - parent::setUp(); - - OC_Mount_Config::registerBackend('Test_Mount_Config_Dummy_Storage', array( - 'backend' => 'dummy', - 'priority' => 150, - 'configuration' => array() - ) - ); - OC_Mount_Config::registerBackend('Test_Mount_Config_Storage_No_Personal', array( - 'backend' => 'dummy no personal', - 'priority' => 150, - 'configuration' => array() - ) - ); - - \OC_User::createUser(self::TEST_USER1, self::TEST_USER1); - \OC_User::createUser(self::TEST_USER2, self::TEST_USER2); - - \OC_Group::createGroup(self::TEST_GROUP1); - \OC_Group::createGroup(self::TEST_GROUP1B); - \OC_Group::addToGroup(self::TEST_USER1, self::TEST_GROUP1); - \OC_Group::addToGroup(self::TEST_USER1, self::TEST_GROUP1B); - \OC_Group::createGroup(self::TEST_GROUP2); - \OC_Group::createGroup(self::TEST_GROUP2B); - \OC_Group::addToGroup(self::TEST_USER2, self::TEST_GROUP2); - \OC_Group::addToGroup(self::TEST_USER2, self::TEST_GROUP2B); - - \OC_User::setUserId(self::TEST_USER1); - $this->userHome = \OC_User::getHome(self::TEST_USER1); - @mkdir($this->userHome); - - $this->dataDir = \OC_Config::getValue( - 'datadirectory', - \OC::$SERVERROOT . '/data/' - ); - $this->oldAllowedBackends = OCP\Config::getAppValue( - 'files_external', - 'user_mounting_backends', - '' - ); - OCP\Config::setAppValue( - 'files_external', - 'user_mounting_backends', - 'Test_Mount_Config_Dummy_Storage' - ); - - OC_Mount_Config::$skipTest = true; - Test_Mount_Config_Hook_Test::setupHooks(); - } - - protected function tearDown() { - Test_Mount_Config_Hook_Test::clear(); - OC_Mount_Config::$skipTest = false; - - \OC_User::deleteUser(self::TEST_USER2); - \OC_User::deleteUser(self::TEST_USER1); - \OC_Group::deleteGroup(self::TEST_GROUP1); - \OC_Group::deleteGroup(self::TEST_GROUP1B); - \OC_Group::deleteGroup(self::TEST_GROUP2); - \OC_Group::deleteGroup(self::TEST_GROUP2B); - - @unlink($this->dataDir . '/mount.json'); - - OCP\Config::setAppValue( - 'files_external', - 'user_mounting_backends', - $this->oldAllowedBackends - ); - - parent::tearDown(); - } - - /** - * Reads the global config, for checking - */ - private function readGlobalConfig() { - $configFile = $this->dataDir . '/mount.json'; - return json_decode(file_get_contents($configFile), true); - } - - private function writeGlobalConfig($config) { - $configFile = $this->dataDir . '/mount.json'; - file_put_contents($configFile, json_encode($config)); - } - - /** - * Reads the user config, for checking - */ - private function readUserConfig() { - $configFile = $this->userHome . '/mount.json'; - return json_decode(file_get_contents($configFile), true); - } - - /** - * Write the user config, to simulate existing files - */ - private function writeUserConfig($config) { - $configFile = $this->userHome . '/mount.json'; - file_put_contents($configFile, json_encode($config)); - } - - /** - * Test mount point validation - */ - public function testAddMountPointValidation() { - $storageClass = 'Test_Mount_Config_Dummy_Storage'; - $mountType = 'user'; - $applicable = 'all'; - $isPersonal = false; - $this->assertFalse(OC_Mount_Config::addMountPoint('', $storageClass, array(), $mountType, $applicable, $isPersonal)); - $this->assertFalse(OC_Mount_Config::addMountPoint('/', $storageClass, array(), $mountType, $applicable, $isPersonal)); - } - - /** - * Test adding a global mount point - */ - public function testAddGlobalMountPoint() { - $mountType = OC_Mount_Config::MOUNT_TYPE_USER; - $applicable = 'all'; - $isPersonal = false; - - $storageOptions = array( - 'host' => 'localhost', - 'user' => 'testuser', - 'password' => '12345', - ); - - $this->assertEquals(0, OC_Mount_Config::addMountPoint('/ext', 'Test_Mount_Config_Dummy_Storage', $storageOptions, $mountType, $applicable, $isPersonal)); - - $config = $this->readGlobalConfig(); - $this->assertEquals(1, count($config)); - $this->assertTrue(isset($config[$mountType])); - $this->assertTrue(isset($config[$mountType][$applicable])); - $this->assertTrue(isset($config[$mountType][$applicable]['/$user/files/ext'])); - $this->assertEquals( - 'Test_Mount_Config_Dummy_Storage', - $config[$mountType][$applicable]['/$user/files/ext']['class'] - ); - } - - /** - * Test adding a personal mount point - */ - public function testAddMountPointSingleUser() { - $mountType = OC_Mount_Config::MOUNT_TYPE_USER; - $applicable = self::TEST_USER1; - $isPersonal = true; - - $storageOptions = array( - 'host' => 'localhost', - 'user' => 'testuser', - 'password' => '12345', - ); - - $this->assertEquals(0, OC_Mount_Config::addMountPoint('/ext', 'Test_Mount_Config_Dummy_Storage', $storageOptions, $mountType, $applicable, $isPersonal)); - - $config = $this->readUserConfig(); - $this->assertEquals(1, count($config)); - $this->assertTrue(isset($config[$mountType])); - $this->assertTrue(isset($config[$mountType][$applicable])); - $this->assertTrue(isset($config[$mountType][$applicable]['/' . self::TEST_USER1 . '/files/ext'])); - $this->assertEquals( - 'Test_Mount_Config_Dummy_Storage', - $config[$mountType][$applicable]['/' . self::TEST_USER1 . '/files/ext']['class'] - ); - } - - /** - * Test adding a personal mount point using disallowed backend - */ - public function testAddDisallowedBackendMountPointSingleUser() { - $mountType = OC_Mount_Config::MOUNT_TYPE_USER; - $applicable = self::TEST_USER1; - $isPersonal = true; - - // local - $this->assertFalse(OC_Mount_Config::addMountPoint('/ext', '\OC\Files\Storage\Local', array(), $mountType, $applicable, $isPersonal)); - - $storageOptions = array( - 'host' => 'localhost', - 'user' => 'testuser', - 'password' => '12345', - ); - - // non-local but forbidden - $this->assertFalse(OC_Mount_Config::addMountPoint('/ext', 'Test_Mount_Config_Storage_No_Personal', $storageOptions, $mountType, $applicable, $isPersonal)); - - $this->assertFalse(file_exists($this->userHome . '/mount.json')); - } - - /** - * Test adding a mount point with an non-existant backend - */ - public function testAddMountPointUnexistClass() { - $storageClass = 'Unexist_Storage'; - $mountType = OC_Mount_Config::MOUNT_TYPE_USER; - $applicable = self::TEST_USER1; - $isPersonal = false; - $this->assertFalse(OC_Mount_Config::addMountPoint('/ext', $storageClass, array(), $mountType, $applicable, $isPersonal)); - - } - - /** - * Provider for testing configurations with different - * "applicable" values (all, user, groups) - */ - public function applicableConfigProvider() { - return array( - // applicable to "all" - array( - OC_Mount_Config::MOUNT_TYPE_USER, - 'all', - array( - 'users' => array('all'), - 'groups' => array() - ) - ), - // applicable to single user - array( - OC_Mount_Config::MOUNT_TYPE_USER, - self::TEST_USER1, - array( - 'users' => array(self::TEST_USER1), - 'groups' => array() - ) - ), - // applicable to single group - array( - OC_Mount_Config::MOUNT_TYPE_GROUP, - self::TEST_GROUP1, - array( - 'users' => array(), - 'groups' => array(self::TEST_GROUP1) - ) - ), - ); - } - - /** - * Test reading and writing global config - * - * @dataProvider applicableConfigProvider - */ - public function testReadWriteGlobalConfig($mountType, $applicable, $expectApplicableArray) { - - $mountType = $mountType; - $applicable = $applicable; - $isPersonal = false; - $options = array( - 'host' => 'smbhost', - 'user' => 'smbuser', - 'password' => 'smbpassword', - 'share' => 'smbshare', - 'root' => 'smbroot' - ); - - // write config - $this->assertEquals( - 0, - OC_Mount_Config::addMountPoint( - '/ext', - 'Test_Mount_Config_Dummy_Storage', - $options, - $mountType, - $applicable, - $isPersonal - ) - ); - - // re-read config - $config = OC_Mount_Config::getSystemMountPoints(); - $this->assertEquals(1, count($config)); - $this->assertEquals('Test_Mount_Config_Dummy_Storage', $config[0]['class']); - $this->assertEquals('ext', $config[0]['mountpoint']); - $this->assertEquals($expectApplicableArray, $config[0]['applicable']); - $savedOptions = $config[0]['options']; - $this->assertEquals($options, $savedOptions); - // key order needs to be preserved for the UI... - $this->assertEquals(array_keys($options), array_keys($savedOptions)); - } - - /** - * Test reading and writing config - */ - public function testReadWritePersonalConfig() { - - $mountType = OC_Mount_Config::MOUNT_TYPE_USER; - $applicable = self::TEST_USER1; - $isPersonal = true; - $options = array( - 'host' => 'smbhost', - 'user' => 'smbuser', - 'password' => 'smbpassword', - 'share' => 'smbshare', - 'root' => 'smbroot' - ); - - // write config - $this->assertEquals( - 0, - OC_Mount_Config::addMountPoint( - '/ext', - 'Test_Mount_Config_Dummy_Storage', - $options, - $mountType, - $applicable, - $isPersonal - ) - ); - - // re-read config - $config = OC_Mount_Config::getPersonalMountPoints(); - $this->assertEquals(1, count($config)); - $this->assertEquals('Test_Mount_Config_Dummy_Storage', $config[0]['class']); - $this->assertEquals('ext', $config[0]['mountpoint']); - $savedOptions = $config[0]['options']; - $this->assertEquals($options, $savedOptions); - // key order needs to be preserved for the UI... - $this->assertEquals(array_keys($options), array_keys($savedOptions)); - } - - public function testHooks() { - $mountPoint = '/test'; - $mountType = 'user'; - $applicable = 'all'; - $isPersonal = false; - - $mountConfig = array( - 'host' => 'smbhost', - 'user' => 'smbuser', - 'password' => 'smbpassword', - 'share' => 'smbshare', - 'root' => 'smbroot' - ); - - // write config - $this->assertEquals( - 0, - OC_Mount_Config::addMountPoint( - $mountPoint, - 'Test_Mount_Config_Dummy_Storage', - $mountConfig, - $mountType, - $applicable, - $isPersonal - ) - ); - - list($hookName, $params) = Test_Mount_Config_Hook_Test::getLastCall(); - $this->assertEquals( - \OC\Files\Filesystem::signal_create_mount, - $hookName - ); - $this->assertEquals( - $mountPoint, - $params[\OC\Files\Filesystem::signal_param_path] - ); - $this->assertEquals( - $mountType, - $params[\OC\Files\Filesystem::signal_param_mount_type] - ); - $this->assertEquals( - $applicable, - $params[\OC\Files\Filesystem::signal_param_users] - ); - - Test_Mount_Config_Hook_Test::clear(); - - // edit - $mountConfig['host'] = 'anothersmbhost'; - $this->assertEquals( - 0, - OC_Mount_Config::addMountPoint( - $mountPoint, - 'Test_Mount_Config_Dummy_Storage', - $mountConfig, - $mountType, - $applicable, - $isPersonal - ) - ); - - // hook must not be called on edit - list($hookName, $params) = Test_Mount_Config_Hook_Test::getLastCall(); - $this->assertEquals( - null, - $hookName - ); - - Test_Mount_Config_Hook_Test::clear(); - - $this->assertTrue( - OC_Mount_Config::removeMountPoint( - $mountPoint, - $mountType, - $applicable, - $isPersonal - ) - ); - - list($hookName, $params) = Test_Mount_Config_Hook_Test::getLastCall(); - $this->assertEquals( - \OC\Files\Filesystem::signal_delete_mount, - $hookName - ); - $this->assertEquals( - $mountPoint, - $params[\OC\Files\Filesystem::signal_param_path] - ); - $this->assertEquals( - $mountType, - $params[\OC\Files\Filesystem::signal_param_mount_type] - ); - $this->assertEquals( - $applicable, - $params[\OC\Files\Filesystem::signal_param_users] - ); - } - - /** - * Test password obfuscation - */ - public function testPasswordObfuscation() { - - $mountType = OC_Mount_Config::MOUNT_TYPE_USER; - $applicable = self::TEST_USER1; - $isPersonal = true; - $mountConfig = array( - 'host' => 'smbhost', - 'user' => 'smbuser', - 'password' => 'smbpassword', - 'share' => 'smbshare', - 'root' => 'smbroot' - ); - - // write config - $this->assertEquals( - 0, - OC_Mount_Config::addMountPoint( - '/ext', - 'Test_Mount_Config_Dummy_Storage', - $mountConfig, - $mountType, - $applicable, - $isPersonal - ) - ); - - // note: password re-reading is covered by testReadWritePersonalConfig - - // check that password inside the file is NOT in plain text - $config = $this->readUserConfig(); - $savedConfig = $config[$mountType][$applicable]['/' . self::TEST_USER1 . '/files/ext']['options']; - - // no more clear text password in file (kept because of key order) - $this->assertEquals('', $savedConfig['password']); - - // encrypted password is present - $this->assertNotEquals($mountConfig['password'], $savedConfig['password_encrypted']); - } - - /** - * Test read legacy passwords - */ - public function testReadLegacyPassword() { - - $mountType = OC_Mount_Config::MOUNT_TYPE_USER; - $applicable = self::TEST_USER1; - $isPersonal = true; - $mountConfig = array( - 'host' => 'smbhost', - 'user' => 'smbuser', - 'password' => 'smbpassword', - 'share' => 'smbshare', - 'root' => 'smbroot' - ); - - // write config - $this->assertEquals( - 0, - OC_Mount_Config::addMountPoint( - '/ext', - 'Test_Mount_Config_Dummy_Storage', - $mountConfig, - $mountType, - $applicable, - $isPersonal - ) - ); - - $config = $this->readUserConfig(); - // simulate non-encrypted password situation - $config[$mountType][$applicable]['/' . self::TEST_USER1 . '/files/ext']['options']['password'] = 'smbpasswd'; - - $this->writeUserConfig($config); - - // re-read config, password was read correctly - $config = OC_Mount_Config::getPersonalMountPoints(); - $savedMountConfig = $config[0]['options']; - $this->assertEquals($mountConfig, $savedMountConfig); - } - - public function testVariableSubstitution() { - $legacyBackendOptions = [ - 'user' => 'someuser', - 'password' => 'somepassword', - 'replacethis' => '$user', - ]; - $legacyBackendOptions = \OC_Mount_Config::encryptPasswords($legacyBackendOptions); - - $legacyConfig = [ - 'class' => '\OC\Files\Storage\SMB', - 'options' => $legacyBackendOptions, - 'mountOptions' => ['preview' => false, 'int' => 1], - ]; - // different mount options - $legacyConfig2 = [ - 'class' => '\OC\Files\Storage\SMB', - 'options' => $legacyBackendOptions, - 'mountOptions' => ['preview' => true, 'string' => 'abc'], - ]; - - $json = [ - 'user' => [ - self::TEST_USER1 => [ - '/$user/files/somemount' => $legacyConfig, - '/$user/files/anothermount' => $legacyConfig2, - ], - ], - ]; - - $this->writeGlobalConfig($json); - - // re-read config, password was read correctly - $config = OC_Mount_Config::getAbsoluteMountPoints(self::TEST_USER1); - - $config1 = $config['/' . self::TEST_USER1 . '/files/somemount']; - $config2 = $config['/' . self::TEST_USER1 . '/files/anothermount']; - - $this->assertSame(self::TEST_USER1, $config1['options']['replacethis']); - $this->assertSame(self::TEST_USER1, $config1['options']['replacethis']); - $this->assertSame(1, $config1['mountOptions']['int']); - $this->assertSame(true, $config2['mountOptions']['preview']); - $this->assertSame('abc', $config2['mountOptions']['string']); - } - - - public function mountDataProvider() { - return array( - // Tests for visible mount points - // system mount point for all users - array( - false, - OC_Mount_Config::MOUNT_TYPE_USER, - 'all', - self::TEST_USER1, - true, - ), - // system mount point for a specific user - array( - false, - OC_Mount_Config::MOUNT_TYPE_USER, - self::TEST_USER1, - self::TEST_USER1, - true, - ), - // system mount point for a specific group - array( - false, - OC_Mount_Config::MOUNT_TYPE_GROUP, - self::TEST_GROUP1, - self::TEST_USER1, - true, - ), - // user mount point - array( - true, - OC_Mount_Config::MOUNT_TYPE_USER, - self::TEST_USER1, - self::TEST_USER1, - true, - ), - - // Tests for non-visible mount points - // system mount point for another user - array( - false, - OC_Mount_Config::MOUNT_TYPE_USER, - self::TEST_USER2, - self::TEST_USER1, - false, - ), - // system mount point for a specific group - array( - false, - OC_Mount_Config::MOUNT_TYPE_GROUP, - self::TEST_GROUP2, - self::TEST_USER1, - false, - ), - // user mount point - array( - true, - OC_Mount_Config::MOUNT_TYPE_USER, - self::TEST_USER1, - self::TEST_USER2, - false, - ), - ); - } - - /** - * Test mount points used at mount time, making sure - * the configuration is prepared properly. - * - * @dataProvider mountDataProvider - * @param bool $isPersonal true for personal mount point, false for system mount point - * @param string $mountType mount type - * @param string $applicable target user/group or "all" - * @param string $testUser user for which to retrieve the mount points - * @param bool $expectVisible whether to expect the mount point to be visible for $testUser - */ - public function testMount($isPersonal, $mountType, $applicable, $testUser, $expectVisible) { - - $mountConfig = array( - 'host' => 'someost', - 'user' => 'someuser', - 'password' => 'somepassword', - 'root' => 'someroot', - 'share' => '', - ); - - // add mount point as "test" user - $this->assertEquals( - 0, - OC_Mount_Config::addMountPoint( - '/ext', - 'Test_Mount_Config_Dummy_Storage', - $mountConfig, - $mountType, - $applicable, - $isPersonal - ) - ); - - // check mount points in the perspective of user $testUser - \OC_User::setUserId($testUser); - - $mountPoints = OC_Mount_Config::getAbsoluteMountPoints($testUser); - if ($expectVisible) { - $this->assertEquals(1, count($mountPoints)); - $this->assertTrue(isset($mountPoints['/' . self::TEST_USER1 . '/files/ext'])); - $this->assertEquals('Test_Mount_Config_Dummy_Storage', $mountPoints['/' . self::TEST_USER1 . '/files/ext']['class']); - $this->assertEquals($mountConfig, $mountPoints['/' . self::TEST_USER1 . '/files/ext']['options']); - } - else { - $this->assertEquals(0, count($mountPoints)); - } - } - - /** - * Test the same config for multiple users. - * The config will be merged by getSystemMountPoints(). - */ - public function testConfigMerging() { - - $mountType = OC_Mount_Config::MOUNT_TYPE_USER; - $isPersonal = false; - $options = array( - 'host' => 'smbhost', - 'user' => 'smbuser', - 'password' => 'smbpassword', - 'share' => 'smbshare', - 'root' => 'smbroot' - ); - - // write config - $this->assertEquals( - 0, - OC_Mount_Config::addMountPoint( - '/ext', - 'Test_Mount_Config_Dummy_Storage', - $options, - OC_Mount_Config::MOUNT_TYPE_USER, - self::TEST_USER1, - $isPersonal - ) - ); - - $this->assertEquals( - 0, - OC_Mount_Config::addMountPoint( - '/ext', - 'Test_Mount_Config_Dummy_Storage', - $options, - OC_Mount_Config::MOUNT_TYPE_USER, - self::TEST_USER2, - $isPersonal - ) - ); - - $this->assertEquals( - 0, - OC_Mount_Config::addMountPoint( - '/ext', - 'Test_Mount_Config_Dummy_Storage', - $options, - OC_Mount_Config::MOUNT_TYPE_GROUP, - self::TEST_GROUP2, - $isPersonal - ) - ); - - $this->assertEquals( - 0, - OC_Mount_Config::addMountPoint( - '/ext', - 'Test_Mount_Config_Dummy_Storage', - $options, - OC_Mount_Config::MOUNT_TYPE_GROUP, - self::TEST_GROUP1, - $isPersonal - ) - ); - - // re-read config - $config = OC_Mount_Config::getSystemMountPoints(); - $this->assertEquals(1, count($config)); - $this->assertEquals('Test_Mount_Config_Dummy_Storage', $config[0]['class']); - $this->assertEquals('ext', $config[0]['mountpoint']); - $this->assertEquals($options, $config[0]['options']); - $this->assertEquals(array(self::TEST_USER1, self::TEST_USER2), $config[0]['applicable']['users']); - $this->assertEquals(array(self::TEST_GROUP2, self::TEST_GROUP1), $config[0]['applicable']['groups']); - } - - /** - * Create then re-read mount points configs where the mount points - * have the same path, the config must NOT be merged. - */ - public function testRereadMountpointWithSamePath() { - - $mountType = OC_Mount_Config::MOUNT_TYPE_USER; - $isPersonal = false; - $options1 = array( - 'host' => 'smbhost', - 'user' => 'smbuser', - 'password' => 'smbpassword', - 'share' => 'smbshare', - 'root' => 'smbroot' - ); - - // write config - $this->assertEquals( - 0, - OC_Mount_Config::addMountPoint( - '/ext', - 'Test_Mount_Config_Dummy_Storage', - $options1, - $mountType, - self::TEST_USER1, - $isPersonal - ) - ); - - $options2 = array( - 'host' => 'anothersmbhost', - 'user' => 'anothersmbuser', - 'password' => 'anothersmbpassword', - 'share' => 'anothersmbshare', - 'root' => 'anothersmbroot' - ); - $this->assertEquals( - 0, - OC_Mount_Config::addMountPoint( - '/ext', - 'Test_Mount_Config_Dummy_Storage', - $options2, - $mountType, - self::TEST_USER2, - $isPersonal - ) - ); - - // re-read config - $config = OC_Mount_Config::getSystemMountPoints(); - $this->assertEquals(2, count($config)); - $this->assertEquals('Test_Mount_Config_Dummy_Storage', $config[0]['class']); - $this->assertEquals('ext', $config[0]['mountpoint']); - $this->assertEquals($options1, $config[0]['options']); - $this->assertEquals('Test_Mount_Config_Dummy_Storage', $config[1]['class']); - $this->assertEquals('ext', $config[1]['mountpoint']); - $this->assertEquals($options2, $config[1]['options']); - } - - public function priorityDataProvider() { - return array( - - // test 1 - group vs group - array( - array( - array( - 'isPersonal' => false, - 'mountType' => OC_Mount_Config::MOUNT_TYPE_GROUP, - 'applicable' => self::TEST_GROUP1, - 'priority' => 50 - ), - array( - 'isPersonal' => false, - 'mountType' => OC_Mount_Config::MOUNT_TYPE_GROUP, - 'applicable' => self::TEST_GROUP1B, - 'priority' => 60 - ) - ), - 1 - ), - // test 2 - user vs personal - array( - array( - array( - 'isPersonal' => false, - 'mountType' => OC_Mount_Config::MOUNT_TYPE_USER, - 'applicable' => self::TEST_USER1, - 'priority' => 2000 - ), - array( - 'isPersonal' => true, - 'mountType' => OC_Mount_Config::MOUNT_TYPE_USER, - 'applicable' => self::TEST_USER1, - 'priority' => null - ) - ), - 1 - ), - // test 3 - all vs group vs user - array( - array( - array( - 'isPersonal' => false, - 'mountType' => OC_Mount_Config::MOUNT_TYPE_USER, - 'applicable' => 'all', - 'priority' => 70 - ), - array( - 'isPersonal' => false, - 'mountType' => OC_Mount_Config::MOUNT_TYPE_GROUP, - 'applicable' => self::TEST_GROUP1, - 'priority' => 60 - ), - array( - 'isPersonal' => false, - 'mountType' => OC_Mount_Config::MOUNT_TYPE_USER, - 'applicable' => self::TEST_USER1, - 'priority' => 50 - ) - ), - 2 - ) - - ); - } - - /** - * Ensure priorities are being respected - * Test user is self::TEST_USER1 - * - * @dataProvider priorityDataProvider - * @param array[] $mounts array of associative array of mount parameters: - * bool $isPersonal - * string $mountType - * string $applicable - * int|null $priority null for personal - * @param int $expected index of expected visible mount - */ - public function testPriority($mounts, $expected) { - - $mountConfig = array( - 'host' => 'somehost', - 'user' => 'someuser', - 'password' => 'somepassword', - 'root' => 'someroot', - 'share' => '', - ); - - // Add mount points - foreach($mounts as $i => $mount) { - $this->assertEquals( - 0, - OC_Mount_Config::addMountPoint( - '/ext', - 'Test_Mount_Config_Dummy_Storage', - $mountConfig + array('id' => $i), - $mount['mountType'], - $mount['applicable'], - $mount['isPersonal'], - $mount['priority'] - ) - ); - } - - // Get mount points for user - $mountPoints = OC_Mount_Config::getAbsoluteMountPoints(self::TEST_USER1); - - $this->assertEquals(1, count($mountPoints)); - $this->assertEquals($expected, $mountPoints['/'.self::TEST_USER1.'/files/ext']['options']['id']); - } - - /** - * Test for persistence of priority when changing mount options - */ - public function testPriorityPersistence() { - - $class = 'Test_Mount_Config_Dummy_Storage'; - $priority = 123; - $mountConfig = array( - 'host' => 'somehost', - 'user' => 'someuser', - 'password' => 'somepassword', - 'root' => 'someroot', - 'share' => '', - ); - - $this->assertEquals( - 0, - OC_Mount_Config::addMountPoint( - '/ext', - $class, - $mountConfig, - OC_Mount_Config::MOUNT_TYPE_USER, - self::TEST_USER1, - false, - $priority - ) - ); - - // Check for correct priority - $mountPoints = OC_Mount_Config::getAbsoluteMountPoints(self::TEST_USER1); - $this->assertEquals($priority, - $mountPoints['/'.self::TEST_USER1.'/files/ext']['priority']); - - // Simulate changed mount options (without priority set) - $this->assertEquals( - 0, - OC_Mount_Config::addMountPoint( - '/ext', - $class, - $mountConfig, - OC_Mount_Config::MOUNT_TYPE_USER, - self::TEST_USER1, - false - ) - ); - - // Check for correct priority - $mountPoints = OC_Mount_Config::getAbsoluteMountPoints(self::TEST_USER1); - $this->assertEquals($priority, - $mountPoints['/'.self::TEST_USER1.'/files/ext']['priority']); - } - - /* - * Test for correct personal configuration loading in file sharing scenarios - */ - public function testMultiUserPersonalConfigLoading() { - $mountConfig = array( - 'host' => 'somehost', - 'user' => 'someuser', - 'password' => 'somepassword', - 'root' => 'someroot', - 'share' => '', - ); - - // Create personal mount point - $this->assertEquals( - 0, - OC_Mount_Config::addMountPoint( - '/ext', - 'Test_Mount_Config_Dummy_Storage', - $mountConfig, - OC_Mount_Config::MOUNT_TYPE_USER, - self::TEST_USER1, - true - ) - ); - - // Ensure other user can read mount points - \OC_User::setUserId(self::TEST_USER2); - $mountPointsMe = OC_Mount_Config::getAbsoluteMountPoints(self::TEST_USER2); - $mountPointsOther = OC_Mount_Config::getAbsoluteMountPoints(self::TEST_USER1); - - $this->assertEquals(0, count($mountPointsMe)); - $this->assertEquals(1, count($mountPointsOther)); - $this->assertTrue(isset($mountPointsOther['/'.self::TEST_USER1.'/files/ext'])); - $this->assertEquals('Test_Mount_Config_Dummy_Storage', - $mountPointsOther['/'.self::TEST_USER1.'/files/ext']['class']); - $this->assertEquals($mountConfig, - $mountPointsOther['/'.self::TEST_USER1.'/files/ext']['options']); - } - - public function testAllowWritingIncompleteConfigIfStorageContructorFails() { - $storageClass = 'Test_Mount_Config_Dummy_Storage'; - $mountType = 'user'; - $applicable = 'all'; - $isPersonal = false; - - $this->assertEquals( - 0, - OC_Mount_Config::addMountPoint( - '/ext', - $storageClass, - array('simulateFail' => true), - $mountType, - $applicable, - $isPersonal - ) - ); - - // config can be retrieved afterwards - $mounts = OC_Mount_Config::getSystemMountPoints(); - $this->assertEquals(1, count($mounts)); - - // no storage id was set - $this->assertFalse(isset($mounts[0]['storage_id'])); - } -} |