2014-10-31 13:41:07 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2015-06-25 12:43:55 +03:00
|
|
|
* @author Lukas Reschke <lukas@owncloud.com>
|
2015-10-06 10:52:19 +03:00
|
|
|
* @author Robin McCorkell <rmccorkell@karoshi.org.uk>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @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/>
|
|
|
|
*
|
2014-10-31 13:41:07 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Files_external\Service;
|
|
|
|
|
|
|
|
use \OCP\IUserSession;
|
|
|
|
use \OC\Files\Filesystem;
|
|
|
|
|
|
|
|
use \OCA\Files_external\Lib\StorageConfig;
|
|
|
|
use \OCA\Files_external\NotFoundException;
|
2015-08-11 20:45:07 +03:00
|
|
|
use \OCA\Files_External\Service\BackendService;
|
2015-09-23 17:35:17 +03:00
|
|
|
use \OCA\Files_External\Lib\Backend\Backend;
|
|
|
|
use \OCA\Files_External\Lib\Auth\AuthMechanism;
|
2015-11-26 19:51:47 +03:00
|
|
|
use \OCP\Files\StorageNotAvailableException;
|
2014-10-31 13:41:07 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Service class to manage external storages
|
|
|
|
*/
|
|
|
|
abstract class StoragesService {
|
|
|
|
|
2015-08-11 20:45:07 +03:00
|
|
|
/** @var BackendService */
|
|
|
|
protected $backendService;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param BackendService $backendService
|
|
|
|
*/
|
|
|
|
public function __construct(BackendService $backendService) {
|
|
|
|
$this->backendService = $backendService;
|
|
|
|
}
|
|
|
|
|
2014-10-31 13:41:07 +03:00
|
|
|
/**
|
|
|
|
* Read legacy config data
|
|
|
|
*
|
|
|
|
* @return array list of mount configs
|
|
|
|
*/
|
|
|
|
protected function readLegacyConfig() {
|
|
|
|
// read global config
|
|
|
|
return \OC_Mount_Config::readData();
|
|
|
|
}
|
|
|
|
|
2015-08-12 16:22:27 +03:00
|
|
|
/**
|
|
|
|
* Write legacy config data
|
|
|
|
*
|
|
|
|
* @param array $mountPoints
|
|
|
|
*/
|
|
|
|
protected function writeLegacyConfig(array $mountPoints) {
|
|
|
|
// write global config
|
|
|
|
\OC_Mount_Config::writeData(null, $mountPoints);
|
|
|
|
}
|
|
|
|
|
2015-03-16 14:18:01 +03:00
|
|
|
/**
|
|
|
|
* 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
|
2015-03-17 13:42:52 +03:00
|
|
|
*
|
2015-03-16 14:18:01 +03:00
|
|
|
* @return StorageConfig populated storage config
|
|
|
|
*/
|
2015-03-17 13:42:52 +03:00
|
|
|
protected function populateStorageConfigWithLegacyOptions(
|
|
|
|
&$storageConfig,
|
|
|
|
$mountType,
|
|
|
|
$applicable,
|
|
|
|
$storageOptions
|
|
|
|
) {
|
2015-08-12 22:03:11 +03:00
|
|
|
$backend = $this->backendService->getBackend($storageOptions['backend']);
|
Authentication mechanisms for external storage backends
A backend can now specify generic authentication schemes that it
supports, instead of specifying the parameters for its authentication
method directly. This allows multiple authentication mechanisms to be
implemented for a single scheme, providing altered functionality.
This commit introduces the backend framework for this feature, and so at
this point the UI will be broken as the frontend does not specify the
required information.
Terminology:
- authentication scheme
Parameter interface for the authentication method. A backend
supporting the 'password' scheme accepts two parameters, 'user' and
'password'.
- authentication mechanism
Specific mechanism implementing a scheme. Basic mechanisms may
forward configuration options directly to the backend, more advanced
ones may lookup parameters or retrieve them from the session
New dropdown selector for external storage configurations to select the
authentication mechanism to be used.
Authentication mechanisms can have visibilities, just like backends.
The API was extended too to make it easier to add/remove visibilities.
In addition, the concept of 'allowed visibility' has been introduced, so
a backend/auth mechanism can force a maximum visibility level (e.g.
Local storage type) that cannot be overridden by configuration in the
web UI.
An authentication mechanism is a fully instantiated implementation. This
allows an implementation to have dependencies injected into it, e.g. an
\OCP\IDB for database operations.
When a StorageConfig is being prepared for mounting, the authentication
mechanism implementation has manipulateStorage() called,
which inserts the relevant authentication method options into the
storage ready for mounting.
2015-08-12 12:54:03 +03:00
|
|
|
if (!$backend) {
|
2015-08-12 22:03:11 +03:00
|
|
|
throw new \UnexpectedValueException('Invalid backend '.$storageOptions['backend']);
|
Authentication mechanisms for external storage backends
A backend can now specify generic authentication schemes that it
supports, instead of specifying the parameters for its authentication
method directly. This allows multiple authentication mechanisms to be
implemented for a single scheme, providing altered functionality.
This commit introduces the backend framework for this feature, and so at
this point the UI will be broken as the frontend does not specify the
required information.
Terminology:
- authentication scheme
Parameter interface for the authentication method. A backend
supporting the 'password' scheme accepts two parameters, 'user' and
'password'.
- authentication mechanism
Specific mechanism implementing a scheme. Basic mechanisms may
forward configuration options directly to the backend, more advanced
ones may lookup parameters or retrieve them from the session
New dropdown selector for external storage configurations to select the
authentication mechanism to be used.
Authentication mechanisms can have visibilities, just like backends.
The API was extended too to make it easier to add/remove visibilities.
In addition, the concept of 'allowed visibility' has been introduced, so
a backend/auth mechanism can force a maximum visibility level (e.g.
Local storage type) that cannot be overridden by configuration in the
web UI.
An authentication mechanism is a fully instantiated implementation. This
allows an implementation to have dependencies injected into it, e.g. an
\OCP\IDB for database operations.
When a StorageConfig is being prepared for mounting, the authentication
mechanism implementation has manipulateStorage() called,
which inserts the relevant authentication method options into the
storage ready for mounting.
2015-08-12 12:54:03 +03:00
|
|
|
}
|
2015-08-11 20:45:07 +03:00
|
|
|
$storageConfig->setBackend($backend);
|
|
|
|
|
2015-08-19 16:40:20 +03:00
|
|
|
if (isset($storageOptions['authMechanism']) && $storageOptions['authMechanism'] !== 'builtin::builtin') {
|
Authentication mechanisms for external storage backends
A backend can now specify generic authentication schemes that it
supports, instead of specifying the parameters for its authentication
method directly. This allows multiple authentication mechanisms to be
implemented for a single scheme, providing altered functionality.
This commit introduces the backend framework for this feature, and so at
this point the UI will be broken as the frontend does not specify the
required information.
Terminology:
- authentication scheme
Parameter interface for the authentication method. A backend
supporting the 'password' scheme accepts two parameters, 'user' and
'password'.
- authentication mechanism
Specific mechanism implementing a scheme. Basic mechanisms may
forward configuration options directly to the backend, more advanced
ones may lookup parameters or retrieve them from the session
New dropdown selector for external storage configurations to select the
authentication mechanism to be used.
Authentication mechanisms can have visibilities, just like backends.
The API was extended too to make it easier to add/remove visibilities.
In addition, the concept of 'allowed visibility' has been introduced, so
a backend/auth mechanism can force a maximum visibility level (e.g.
Local storage type) that cannot be overridden by configuration in the
web UI.
An authentication mechanism is a fully instantiated implementation. This
allows an implementation to have dependencies injected into it, e.g. an
\OCP\IDB for database operations.
When a StorageConfig is being prepared for mounting, the authentication
mechanism implementation has manipulateStorage() called,
which inserts the relevant authentication method options into the
storage ready for mounting.
2015-08-12 12:54:03 +03:00
|
|
|
$authMechanism = $this->backendService->getAuthMechanism($storageOptions['authMechanism']);
|
|
|
|
} else {
|
|
|
|
$authMechanism = $backend->getLegacyAuthMechanism($storageOptions);
|
|
|
|
$storageOptions['authMechanism'] = 'null'; // to make error handling easier
|
|
|
|
}
|
|
|
|
if (!$authMechanism) {
|
2015-08-12 22:03:11 +03:00
|
|
|
throw new \UnexpectedValueException('Invalid authentication mechanism '.$storageOptions['authMechanism']);
|
Authentication mechanisms for external storage backends
A backend can now specify generic authentication schemes that it
supports, instead of specifying the parameters for its authentication
method directly. This allows multiple authentication mechanisms to be
implemented for a single scheme, providing altered functionality.
This commit introduces the backend framework for this feature, and so at
this point the UI will be broken as the frontend does not specify the
required information.
Terminology:
- authentication scheme
Parameter interface for the authentication method. A backend
supporting the 'password' scheme accepts two parameters, 'user' and
'password'.
- authentication mechanism
Specific mechanism implementing a scheme. Basic mechanisms may
forward configuration options directly to the backend, more advanced
ones may lookup parameters or retrieve them from the session
New dropdown selector for external storage configurations to select the
authentication mechanism to be used.
Authentication mechanisms can have visibilities, just like backends.
The API was extended too to make it easier to add/remove visibilities.
In addition, the concept of 'allowed visibility' has been introduced, so
a backend/auth mechanism can force a maximum visibility level (e.g.
Local storage type) that cannot be overridden by configuration in the
web UI.
An authentication mechanism is a fully instantiated implementation. This
allows an implementation to have dependencies injected into it, e.g. an
\OCP\IDB for database operations.
When a StorageConfig is being prepared for mounting, the authentication
mechanism implementation has manipulateStorage() called,
which inserts the relevant authentication method options into the
storage ready for mounting.
2015-08-12 12:54:03 +03:00
|
|
|
}
|
|
|
|
$storageConfig->setAuthMechanism($authMechanism);
|
|
|
|
|
2015-03-16 14:18:01 +03:00
|
|
|
$storageConfig->setBackendOptions($storageOptions['options']);
|
|
|
|
if (isset($storageOptions['mountOptions'])) {
|
|
|
|
$storageConfig->setMountOptions($storageOptions['mountOptions']);
|
|
|
|
}
|
2015-08-11 20:45:07 +03:00
|
|
|
if (!isset($storageOptions['priority'])) {
|
|
|
|
$storageOptions['priority'] = $backend->getPriority();
|
2015-03-16 14:18:01 +03:00
|
|
|
}
|
2015-08-11 20:45:07 +03:00
|
|
|
$storageConfig->setPriority($storageOptions['priority']);
|
2015-03-16 14:18:01 +03:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2015-08-20 14:23:12 +03:00
|
|
|
|
|
|
|
|
2015-03-16 14:18:01 +03:00
|
|
|
return $storageConfig;
|
|
|
|
}
|
|
|
|
|
2014-10-31 13:41:07 +03:00
|
|
|
/**
|
|
|
|
* Read the external storages config
|
|
|
|
*
|
|
|
|
* @return array map of storage id to storage config
|
|
|
|
*/
|
|
|
|
protected function readConfig() {
|
|
|
|
$mountPoints = $this->readLegacyConfig();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Here is the how the horribly messy mount point array looks like
|
|
|
|
* from the mount.json file:
|
|
|
|
*
|
|
|
|
* $storageOptions = $mountPoints[$mountType][$applicable][$mountPath]
|
|
|
|
*
|
|
|
|
* - $mountType is either "user" or "group"
|
|
|
|
* - $applicable is the name of a user or group (or the current user for personal mounts)
|
|
|
|
* - $mountPath is the mount point path (where the storage must be mounted)
|
|
|
|
* - $storageOptions is a map of storage options:
|
|
|
|
* - "priority": storage priority
|
2015-08-12 22:03:11 +03:00
|
|
|
* - "backend": backend identifier
|
|
|
|
* - "class": LEGACY backend class name
|
2014-10-31 13:41:07 +03:00
|
|
|
* - "options": backend-specific options
|
2015-08-12 22:03:11 +03:00
|
|
|
* - "authMechanism": authentication mechanism identifier
|
2015-03-13 14:49:11 +03:00
|
|
|
* - "mountOptions": mount-specific options (ex: disable previews, scanner, etc)
|
2014-10-31 13:41:07 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
// group by storage id
|
|
|
|
$storages = [];
|
2015-03-16 14:18:01 +03:00
|
|
|
|
|
|
|
// for storages without id (legacy), group by config hash for
|
|
|
|
// later processing
|
|
|
|
$storagesWithConfigHash = [];
|
|
|
|
|
2014-10-31 13:41:07 +03:00
|
|
|
foreach ($mountPoints as $mountType => $applicables) {
|
|
|
|
foreach ($applicables as $applicable => $mountPaths) {
|
|
|
|
foreach ($mountPaths as $rootMountPath => $storageOptions) {
|
2015-03-16 14:18:01 +03:00
|
|
|
$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;
|
|
|
|
|
2014-10-31 13:41:07 +03:00
|
|
|
// the root mount point is in the format "/$user/files/the/mount/point"
|
|
|
|
// we remove the "/$user/files" prefix
|
2015-08-31 15:23:23 +03:00
|
|
|
$parts = explode('/', ltrim($rootMountPath, '/'), 3);
|
2014-10-31 13:41:07 +03:00
|
|
|
if (count($parts) < 3) {
|
|
|
|
// something went wrong, skip
|
|
|
|
\OCP\Util::writeLog(
|
|
|
|
'files_external',
|
|
|
|
'Could not parse mount point "' . $rootMountPath . '"',
|
|
|
|
\OCP\Util::ERROR
|
|
|
|
);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-08-31 15:23:23 +03:00
|
|
|
$relativeMountPath = rtrim($parts[2], '/');
|
2014-10-31 13:41:07 +03:00
|
|
|
|
2015-03-16 14:18:01 +03:00
|
|
|
// 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']);
|
|
|
|
|
2015-08-12 22:03:11 +03:00
|
|
|
if (!isset($storageOptions['backend'])) {
|
|
|
|
$storageOptions['backend'] = $storageOptions['class']; // legacy compat
|
|
|
|
}
|
|
|
|
if (!isset($storageOptions['authMechanism'])) {
|
|
|
|
$storageOptions['authMechanism'] = null; // ensure config hash works
|
|
|
|
}
|
|
|
|
|
2015-03-16 14:18:01 +03:00
|
|
|
if (isset($storageOptions['id'])) {
|
|
|
|
$configId = (int)$storageOptions['id'];
|
|
|
|
if (isset($storages[$configId])) {
|
|
|
|
$currentStorage = $storages[$configId];
|
|
|
|
}
|
|
|
|
$hasId = true;
|
2014-10-31 13:41:07 +03:00
|
|
|
} else {
|
2015-03-16 14:18:01 +03:00
|
|
|
// 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
|
2014-10-31 13:41:07 +03:00
|
|
|
$currentStorage = new StorageConfig($configId);
|
|
|
|
$currentStorage->setMountPoint($relativeMountPath);
|
|
|
|
}
|
|
|
|
|
2015-11-20 17:23:09 +03:00
|
|
|
try {
|
|
|
|
$this->populateStorageConfigWithLegacyOptions(
|
|
|
|
$currentStorage,
|
|
|
|
$mountType,
|
|
|
|
$applicable,
|
|
|
|
$storageOptions
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($hasId) {
|
|
|
|
$storages[$configId] = $currentStorage;
|
|
|
|
} else {
|
|
|
|
$storagesWithConfigHash[$configId] = $currentStorage;
|
|
|
|
}
|
|
|
|
} catch (\UnexpectedValueException $e) {
|
|
|
|
// dont die if a storage backend doesn't exist
|
|
|
|
\OCP\Util::writeLog(
|
|
|
|
'files_external',
|
|
|
|
'Could not load storage: "' . $e->getMessage() . '"',
|
|
|
|
\OCP\Util::ERROR
|
|
|
|
);
|
2014-10-31 13:41:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-16 14:18:01 +03:00
|
|
|
// process storages with config hash, they must get a real id
|
|
|
|
if (!empty($storagesWithConfigHash)) {
|
2015-08-12 16:22:27 +03:00
|
|
|
$this->setRealStorageIds($storages, $storagesWithConfigHash);
|
2014-10-31 13:41:07 +03:00
|
|
|
}
|
|
|
|
|
2015-08-20 14:23:12 +03:00
|
|
|
// convert parameter values
|
|
|
|
foreach ($storages as $storage) {
|
|
|
|
$storage->getBackend()->validateStorageDefinition($storage);
|
|
|
|
$storage->getAuthMechanism()->validateStorageDefinition($storage);
|
|
|
|
}
|
|
|
|
|
2014-10-31 13:41:07 +03:00
|
|
|
return $storages;
|
|
|
|
}
|
|
|
|
|
2015-08-12 16:22:27 +03:00
|
|
|
/**
|
|
|
|
* Replace config hash ID with real IDs, for migrating legacy storages
|
|
|
|
*
|
|
|
|
* @param StorageConfig[] $storages Storages with real IDs
|
|
|
|
* @param StorageConfig[] $storagesWithConfigHash Storages with config hash IDs
|
|
|
|
*/
|
|
|
|
protected function setRealStorageIds(array &$storages, array $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);
|
|
|
|
}
|
|
|
|
|
2014-10-31 13:41:07 +03:00
|
|
|
/**
|
|
|
|
* Add mount point into the messy mount point structure
|
|
|
|
*
|
|
|
|
* @param array $mountPoints messy array of mount points
|
|
|
|
* @param string $mountType mount type
|
|
|
|
* @param string $applicable single applicable user or group
|
|
|
|
* @param string $rootMountPoint root mount point to use
|
|
|
|
* @param array $storageConfig storage config to set to the mount point
|
|
|
|
*/
|
|
|
|
protected function addMountPoint(&$mountPoints, $mountType, $applicable, $rootMountPoint, $storageConfig) {
|
|
|
|
if (!isset($mountPoints[$mountType])) {
|
|
|
|
$mountPoints[$mountType] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($mountPoints[$mountType][$applicable])) {
|
|
|
|
$mountPoints[$mountType][$applicable] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$options = [
|
|
|
|
'id' => $storageConfig->getId(),
|
2015-08-12 22:03:11 +03:00
|
|
|
'backend' => $storageConfig->getBackend()->getIdentifier(),
|
|
|
|
//'class' => $storageConfig->getBackend()->getClass(),
|
|
|
|
'authMechanism' => $storageConfig->getAuthMechanism()->getIdentifier(),
|
2014-10-31 13:41:07 +03:00
|
|
|
'options' => $storageConfig->getBackendOptions(),
|
|
|
|
];
|
|
|
|
|
|
|
|
if (!is_null($storageConfig->getPriority())) {
|
|
|
|
$options['priority'] = $storageConfig->getPriority();
|
|
|
|
}
|
2015-04-08 14:55:10 +03:00
|
|
|
|
|
|
|
$mountOptions = $storageConfig->getMountOptions();
|
|
|
|
if (!empty($mountOptions)) {
|
|
|
|
$options['mountOptions'] = $mountOptions;
|
2015-03-13 14:49:11 +03:00
|
|
|
}
|
2014-10-31 13:41:07 +03:00
|
|
|
|
|
|
|
$mountPoints[$mountType][$applicable][$rootMountPoint] = $options;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write the storages to the configuration.
|
|
|
|
*
|
|
|
|
* @param array $storages map of storage id to storage config
|
|
|
|
*/
|
|
|
|
abstract protected function writeConfig($storages);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a storage with status
|
|
|
|
*
|
2015-03-17 13:42:52 +03:00
|
|
|
* @param int $id storage id
|
2014-10-31 13:41:07 +03:00
|
|
|
*
|
|
|
|
* @return StorageConfig
|
2015-03-17 13:42:52 +03:00
|
|
|
* @throws NotFoundException if the storage with the given id was not found
|
2014-10-31 13:41:07 +03:00
|
|
|
*/
|
|
|
|
public function getStorage($id) {
|
|
|
|
$allStorages = $this->readConfig();
|
|
|
|
|
|
|
|
if (!isset($allStorages[$id])) {
|
|
|
|
throw new NotFoundException('Storage with id "' . $id . '" not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $allStorages[$id];
|
|
|
|
}
|
|
|
|
|
2015-03-16 14:18:01 +03:00
|
|
|
/**
|
2015-09-23 17:35:17 +03:00
|
|
|
* Gets all storages, valid or not
|
2015-03-16 14:18:01 +03:00
|
|
|
*
|
|
|
|
* @return array array of storage configs
|
|
|
|
*/
|
|
|
|
public function getAllStorages() {
|
|
|
|
return $this->readConfig();
|
|
|
|
}
|
|
|
|
|
2015-09-23 17:35:17 +03:00
|
|
|
/**
|
|
|
|
* Gets all valid storages
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getStorages() {
|
|
|
|
return array_filter($this->getAllStorages(), [$this, 'validateStorage']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate storage
|
|
|
|
* FIXME: De-duplicate with StoragesController::validate()
|
|
|
|
*
|
|
|
|
* @param StorageConfig $storage
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function validateStorage(StorageConfig $storage) {
|
|
|
|
/** @var Backend */
|
|
|
|
$backend = $storage->getBackend();
|
|
|
|
/** @var AuthMechanism */
|
|
|
|
$authMechanism = $storage->getAuthMechanism();
|
|
|
|
|
|
|
|
if (!$backend->isVisibleFor($this->getVisibilityType())) {
|
|
|
|
// not permitted to use backend
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!$authMechanism->isVisibleFor($this->getVisibilityType())) {
|
|
|
|
// not permitted to use auth mechanism
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the visibility type for this controller, used in validation
|
|
|
|
*
|
|
|
|
* @return string BackendService::VISIBILITY_* constants
|
|
|
|
*/
|
|
|
|
abstract public function getVisibilityType();
|
|
|
|
|
2014-10-31 13:41:07 +03:00
|
|
|
/**
|
|
|
|
* Add new storage to the configuration
|
|
|
|
*
|
|
|
|
* @param array $newStorage storage attributes
|
|
|
|
*
|
|
|
|
* @return StorageConfig storage config, with added id
|
|
|
|
*/
|
|
|
|
public function addStorage(StorageConfig $newStorage) {
|
|
|
|
$allStorages = $this->readConfig();
|
|
|
|
|
|
|
|
$configId = $this->generateNextId($allStorages);
|
|
|
|
$newStorage->setId($configId);
|
|
|
|
|
|
|
|
// add new storage
|
|
|
|
$allStorages[$configId] = $newStorage;
|
|
|
|
|
|
|
|
$this->writeConfig($allStorages);
|
|
|
|
|
|
|
|
$this->triggerHooks($newStorage, Filesystem::signal_create_mount);
|
|
|
|
|
2015-11-26 19:51:47 +03:00
|
|
|
$newStorage->setStatus(StorageNotAvailableException::STATUS_SUCCESS);
|
2014-10-31 13:41:07 +03:00
|
|
|
return $newStorage;
|
2015-08-11 20:45:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a storage from its parameters
|
|
|
|
*
|
|
|
|
* @param string $mountPoint storage mount point
|
2015-08-12 22:03:11 +03:00
|
|
|
* @param string $backendIdentifier backend identifier
|
|
|
|
* @param string $authMechanismIdentifier authentication mechanism identifier
|
2015-08-11 20:45:07 +03:00
|
|
|
* @param array $backendOptions backend-specific options
|
|
|
|
* @param array|null $mountOptions mount-specific options
|
|
|
|
* @param array|null $applicableUsers users for which to mount the storage
|
|
|
|
* @param array|null $applicableGroups groups for which to mount the storage
|
|
|
|
* @param int|null $priority priority
|
|
|
|
*
|
|
|
|
* @return StorageConfig
|
|
|
|
*/
|
|
|
|
public function createStorage(
|
|
|
|
$mountPoint,
|
2015-08-12 22:03:11 +03:00
|
|
|
$backendIdentifier,
|
|
|
|
$authMechanismIdentifier,
|
2015-08-11 20:45:07 +03:00
|
|
|
$backendOptions,
|
|
|
|
$mountOptions = null,
|
|
|
|
$applicableUsers = null,
|
|
|
|
$applicableGroups = null,
|
|
|
|
$priority = null
|
|
|
|
) {
|
2015-08-12 22:03:11 +03:00
|
|
|
$backend = $this->backendService->getBackend($backendIdentifier);
|
2015-08-11 20:45:07 +03:00
|
|
|
if (!$backend) {
|
2015-08-12 22:03:11 +03:00
|
|
|
throw new \InvalidArgumentException('Unable to get backend for '.$backendIdentifier);
|
2015-08-11 20:45:07 +03:00
|
|
|
}
|
2015-08-12 22:03:11 +03:00
|
|
|
$authMechanism = $this->backendService->getAuthMechanism($authMechanismIdentifier);
|
Authentication mechanisms for external storage backends
A backend can now specify generic authentication schemes that it
supports, instead of specifying the parameters for its authentication
method directly. This allows multiple authentication mechanisms to be
implemented for a single scheme, providing altered functionality.
This commit introduces the backend framework for this feature, and so at
this point the UI will be broken as the frontend does not specify the
required information.
Terminology:
- authentication scheme
Parameter interface for the authentication method. A backend
supporting the 'password' scheme accepts two parameters, 'user' and
'password'.
- authentication mechanism
Specific mechanism implementing a scheme. Basic mechanisms may
forward configuration options directly to the backend, more advanced
ones may lookup parameters or retrieve them from the session
New dropdown selector for external storage configurations to select the
authentication mechanism to be used.
Authentication mechanisms can have visibilities, just like backends.
The API was extended too to make it easier to add/remove visibilities.
In addition, the concept of 'allowed visibility' has been introduced, so
a backend/auth mechanism can force a maximum visibility level (e.g.
Local storage type) that cannot be overridden by configuration in the
web UI.
An authentication mechanism is a fully instantiated implementation. This
allows an implementation to have dependencies injected into it, e.g. an
\OCP\IDB for database operations.
When a StorageConfig is being prepared for mounting, the authentication
mechanism implementation has manipulateStorage() called,
which inserts the relevant authentication method options into the
storage ready for mounting.
2015-08-12 12:54:03 +03:00
|
|
|
if (!$authMechanism) {
|
2015-08-12 22:03:11 +03:00
|
|
|
throw new \InvalidArgumentException('Unable to get authentication mechanism for '.$authMechanismIdentifier);
|
Authentication mechanisms for external storage backends
A backend can now specify generic authentication schemes that it
supports, instead of specifying the parameters for its authentication
method directly. This allows multiple authentication mechanisms to be
implemented for a single scheme, providing altered functionality.
This commit introduces the backend framework for this feature, and so at
this point the UI will be broken as the frontend does not specify the
required information.
Terminology:
- authentication scheme
Parameter interface for the authentication method. A backend
supporting the 'password' scheme accepts two parameters, 'user' and
'password'.
- authentication mechanism
Specific mechanism implementing a scheme. Basic mechanisms may
forward configuration options directly to the backend, more advanced
ones may lookup parameters or retrieve them from the session
New dropdown selector for external storage configurations to select the
authentication mechanism to be used.
Authentication mechanisms can have visibilities, just like backends.
The API was extended too to make it easier to add/remove visibilities.
In addition, the concept of 'allowed visibility' has been introduced, so
a backend/auth mechanism can force a maximum visibility level (e.g.
Local storage type) that cannot be overridden by configuration in the
web UI.
An authentication mechanism is a fully instantiated implementation. This
allows an implementation to have dependencies injected into it, e.g. an
\OCP\IDB for database operations.
When a StorageConfig is being prepared for mounting, the authentication
mechanism implementation has manipulateStorage() called,
which inserts the relevant authentication method options into the
storage ready for mounting.
2015-08-12 12:54:03 +03:00
|
|
|
}
|
2015-08-11 20:45:07 +03:00
|
|
|
$newStorage = new StorageConfig();
|
|
|
|
$newStorage->setMountPoint($mountPoint);
|
|
|
|
$newStorage->setBackend($backend);
|
Authentication mechanisms for external storage backends
A backend can now specify generic authentication schemes that it
supports, instead of specifying the parameters for its authentication
method directly. This allows multiple authentication mechanisms to be
implemented for a single scheme, providing altered functionality.
This commit introduces the backend framework for this feature, and so at
this point the UI will be broken as the frontend does not specify the
required information.
Terminology:
- authentication scheme
Parameter interface for the authentication method. A backend
supporting the 'password' scheme accepts two parameters, 'user' and
'password'.
- authentication mechanism
Specific mechanism implementing a scheme. Basic mechanisms may
forward configuration options directly to the backend, more advanced
ones may lookup parameters or retrieve them from the session
New dropdown selector for external storage configurations to select the
authentication mechanism to be used.
Authentication mechanisms can have visibilities, just like backends.
The API was extended too to make it easier to add/remove visibilities.
In addition, the concept of 'allowed visibility' has been introduced, so
a backend/auth mechanism can force a maximum visibility level (e.g.
Local storage type) that cannot be overridden by configuration in the
web UI.
An authentication mechanism is a fully instantiated implementation. This
allows an implementation to have dependencies injected into it, e.g. an
\OCP\IDB for database operations.
When a StorageConfig is being prepared for mounting, the authentication
mechanism implementation has manipulateStorage() called,
which inserts the relevant authentication method options into the
storage ready for mounting.
2015-08-12 12:54:03 +03:00
|
|
|
$newStorage->setAuthMechanism($authMechanism);
|
2015-08-11 20:45:07 +03:00
|
|
|
$newStorage->setBackendOptions($backendOptions);
|
|
|
|
if (isset($mountOptions)) {
|
|
|
|
$newStorage->setMountOptions($mountOptions);
|
|
|
|
}
|
|
|
|
if (isset($applicableUsers)) {
|
|
|
|
$newStorage->setApplicableUsers($applicableUsers);
|
|
|
|
}
|
|
|
|
if (isset($applicableGroups)) {
|
|
|
|
$newStorage->setApplicableGroups($applicableGroups);
|
|
|
|
}
|
|
|
|
if (isset($priority)) {
|
|
|
|
$newStorage->setPriority($priority);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $newStorage;
|
2014-10-31 13:41:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Triggers the given hook signal for all the applicables given
|
|
|
|
*
|
|
|
|
* @param string $signal signal
|
|
|
|
* @param string $mountPoint hook mount pount param
|
|
|
|
* @param string $mountType hook mount type param
|
|
|
|
* @param array $applicableArray array of applicable users/groups for which to trigger the hook
|
|
|
|
*/
|
|
|
|
protected function triggerApplicableHooks($signal, $mountPoint, $mountType, $applicableArray) {
|
|
|
|
foreach ($applicableArray as $applicable) {
|
2015-08-19 00:49:29 +03:00
|
|
|
\OCP\Util::emitHook(
|
2014-10-31 13:41:07 +03:00
|
|
|
Filesystem::CLASSNAME,
|
|
|
|
$signal,
|
|
|
|
[
|
|
|
|
Filesystem::signal_param_path => $mountPoint,
|
|
|
|
Filesystem::signal_param_mount_type => $mountType,
|
|
|
|
Filesystem::signal_param_users => $applicable,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Triggers $signal for all applicable users of the given
|
|
|
|
* storage
|
|
|
|
*
|
|
|
|
* @param StorageConfig $storage storage data
|
|
|
|
* @param string $signal signal to trigger
|
|
|
|
*/
|
|
|
|
abstract protected function triggerHooks(StorageConfig $storage, $signal);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Triggers signal_create_mount or signal_delete_mount to
|
|
|
|
* accomodate for additions/deletions in applicableUsers
|
|
|
|
* and applicableGroups fields.
|
|
|
|
*
|
|
|
|
* @param StorageConfig $oldStorage old storage data
|
|
|
|
* @param StorageConfig $newStorage new storage data
|
|
|
|
*/
|
|
|
|
abstract protected function triggerChangeHooks(StorageConfig $oldStorage, StorageConfig $newStorage);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update storage to the configuration
|
|
|
|
*
|
|
|
|
* @param StorageConfig $updatedStorage storage attributes
|
|
|
|
*
|
|
|
|
* @return StorageConfig storage config
|
2015-03-17 13:42:52 +03:00
|
|
|
* @throws NotFoundException if the given storage does not exist in the config
|
2014-10-31 13:41:07 +03:00
|
|
|
*/
|
|
|
|
public function updateStorage(StorageConfig $updatedStorage) {
|
|
|
|
$allStorages = $this->readConfig();
|
|
|
|
|
|
|
|
$id = $updatedStorage->getId();
|
|
|
|
if (!isset($allStorages[$id])) {
|
|
|
|
throw new NotFoundException('Storage with id "' . $id . '" not found');
|
|
|
|
}
|
|
|
|
$oldStorage = $allStorages[$id];
|
|
|
|
|
2015-08-25 16:51:47 +03:00
|
|
|
// ensure objectstore is persistent
|
|
|
|
if ($objectstore = $oldStorage->getBackendOption('objectstore')) {
|
|
|
|
$updatedStorage->setBackendOption('objectstore', $objectstore);
|
|
|
|
}
|
|
|
|
|
|
|
|
$allStorages[$id] = $updatedStorage;
|
2014-10-31 13:41:07 +03:00
|
|
|
$this->writeConfig($allStorages);
|
|
|
|
|
|
|
|
$this->triggerChangeHooks($oldStorage, $updatedStorage);
|
|
|
|
|
|
|
|
return $this->getStorage($id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete the storage with the given id.
|
|
|
|
*
|
|
|
|
* @param int $id storage id
|
|
|
|
*
|
2015-03-17 13:42:52 +03:00
|
|
|
* @throws NotFoundException if no storage was found with the given id
|
2014-10-31 13:41:07 +03:00
|
|
|
*/
|
|
|
|
public function removeStorage($id) {
|
|
|
|
$allStorages = $this->readConfig();
|
|
|
|
|
|
|
|
if (!isset($allStorages[$id])) {
|
|
|
|
throw new NotFoundException('Storage with id "' . $id . '" not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
$deletedStorage = $allStorages[$id];
|
|
|
|
unset($allStorages[$id]);
|
|
|
|
|
|
|
|
$this->writeConfig($allStorages);
|
|
|
|
|
|
|
|
$this->triggerHooks($deletedStorage, Filesystem::signal_delete_mount);
|
2015-10-06 17:47:59 +03:00
|
|
|
|
|
|
|
// delete oc_storages entries and oc_filecache
|
|
|
|
try {
|
|
|
|
$rustyStorageId = $this->getRustyStorageIdFromConfig($deletedStorage);
|
|
|
|
\OC\Files\Cache\Storage::remove($rustyStorageId);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
// can happen either for invalid configs where the storage could not
|
|
|
|
// be instantiated or whenever $user vars where used, in which case
|
|
|
|
// the storage id could not be computed
|
|
|
|
\OCP\Util::writeLog(
|
|
|
|
'files_external',
|
|
|
|
'Exception: "' . $e->getMessage() . '"',
|
|
|
|
\OCP\Util::ERROR
|
|
|
|
);
|
|
|
|
}
|
2014-10-31 13:41:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a configuration id to use for a new configuration entry.
|
|
|
|
*
|
|
|
|
* @param array $allStorages array of all storage configs
|
|
|
|
*
|
|
|
|
* @return int id
|
|
|
|
*/
|
|
|
|
protected function generateNextId($allStorages) {
|
|
|
|
if (empty($allStorages)) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
// note: this will mess up with with concurrency,
|
|
|
|
// but so did the mount.json. This horribly hack
|
|
|
|
// will disappear once we move to DB tables to
|
|
|
|
// store the config
|
2015-03-17 13:42:52 +03:00
|
|
|
return (max(array_keys($allStorages)) + 1);
|
2014-10-31 13:41:07 +03:00
|
|
|
}
|
|
|
|
|
2015-10-06 17:47:59 +03:00
|
|
|
/**
|
|
|
|
* Returns the rusty storage id from oc_storages from the given storage config.
|
|
|
|
*
|
|
|
|
* @param StorageConfig $storageConfig
|
|
|
|
* @return string rusty storage id
|
|
|
|
*/
|
|
|
|
private function getRustyStorageIdFromConfig(StorageConfig $storageConfig) {
|
|
|
|
// if any of the storage options contains $user, it is not possible
|
|
|
|
// to compute the possible storage id as we don't know which users
|
|
|
|
// mounted it already (and we certainly don't want to iterate over ALL users)
|
|
|
|
foreach ($storageConfig->getBackendOptions() as $value) {
|
|
|
|
if (strpos($value, '$user') !== false) {
|
|
|
|
throw new \Exception('Cannot compute storage id for deletion due to $user vars in the configuration');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// note: similar to ConfigAdapter->prepateStorageConfig()
|
|
|
|
$storageConfig->getAuthMechanism()->manipulateStorageConfig($storageConfig);
|
|
|
|
$storageConfig->getBackend()->manipulateStorageConfig($storageConfig);
|
|
|
|
|
|
|
|
$class = $storageConfig->getBackend()->getStorageClass();
|
|
|
|
$storageImpl = new $class($storageConfig->getBackendOptions());
|
|
|
|
|
|
|
|
return $storageImpl->getId();
|
|
|
|
}
|
|
|
|
|
2014-10-31 13:41:07 +03:00
|
|
|
}
|