2014-12-03 16:14:31 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 19:13:36 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2016-01-12 17:02:16 +03:00
|
|
|
* @author Robin McCorkell <robin@mccorkell.me.uk>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
2015-03-26 13:44:34 +03:00
|
|
|
*
|
|
|
|
* @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-12-03 16:14:31 +03:00
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2014-12-03 16:14:31 +03:00
|
|
|
namespace OCA\Files_External\Config;
|
|
|
|
|
2016-03-03 16:19:34 +03:00
|
|
|
use OC\Files\Storage\Wrapper\Availability;
|
2016-05-13 12:22:28 +03:00
|
|
|
use OCA\Files_External\Migration\StorageMigrator;
|
2015-08-12 16:22:27 +03:00
|
|
|
use OCP\Files\Storage;
|
2014-12-03 16:14:31 +03:00
|
|
|
use OC\Files\Mount\MountPoint;
|
|
|
|
use OCP\Files\Storage\IStorageFactory;
|
2015-08-11 20:45:07 +03:00
|
|
|
use OCA\Files_External\Lib\PersonalMount;
|
2014-12-03 16:14:31 +03:00
|
|
|
use OCP\Files\Config\IMountProvider;
|
|
|
|
use OCP\IUser;
|
2016-05-13 12:22:28 +03:00
|
|
|
use OCA\Files_External\Service\UserStoragesService;
|
2015-08-12 16:22:27 +03:00
|
|
|
use OCA\Files_External\Service\UserGlobalStoragesService;
|
|
|
|
use OCA\Files_External\Lib\StorageConfig;
|
2016-03-10 15:38:48 +03:00
|
|
|
use OC\Files\Storage\FailedStorage;
|
2016-03-02 14:17:14 +03:00
|
|
|
use OCP\Files\StorageNotAvailableException;
|
2014-12-03 16:14:31 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Make the old files_external config work with the new public mount config api
|
|
|
|
*/
|
|
|
|
class ConfigAdapter implements IMountProvider {
|
2015-08-11 20:45:07 +03:00
|
|
|
|
2015-08-12 16:22:27 +03:00
|
|
|
/** @var UserStoragesService */
|
|
|
|
private $userStoragesService;
|
|
|
|
|
|
|
|
/** @var UserGlobalStoragesService */
|
|
|
|
private $userGlobalStoragesService;
|
2015-11-02 15:13:06 +03:00
|
|
|
/** @var StorageMigrator */
|
|
|
|
private $migrator;
|
2015-08-12 16:22:27 +03:00
|
|
|
|
2015-08-11 20:45:07 +03:00
|
|
|
/**
|
|
|
|
* @param UserStoragesService $userStoragesService
|
2015-08-12 16:22:27 +03:00
|
|
|
* @param UserGlobalStoragesService $userGlobalStoragesService
|
2015-11-02 15:13:06 +03:00
|
|
|
* @param StorageMigrator $migrator
|
2015-08-11 20:45:07 +03:00
|
|
|
*/
|
2015-08-12 16:22:27 +03:00
|
|
|
public function __construct(
|
|
|
|
UserStoragesService $userStoragesService,
|
2015-11-02 15:13:06 +03:00
|
|
|
UserGlobalStoragesService $userGlobalStoragesService,
|
|
|
|
StorageMigrator $migrator
|
2015-08-12 16:22:27 +03:00
|
|
|
) {
|
2015-08-11 20:45:07 +03:00
|
|
|
$this->userStoragesService = $userStoragesService;
|
2015-08-12 16:22:27 +03:00
|
|
|
$this->userGlobalStoragesService = $userGlobalStoragesService;
|
2015-11-02 15:13:06 +03:00
|
|
|
$this->migrator = $migrator;
|
2015-08-12 16:22:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process storage ready for mounting
|
|
|
|
*
|
|
|
|
* @param StorageConfig $storage
|
2015-08-12 21:51:09 +03:00
|
|
|
* @param IUser $user
|
2015-08-12 16:22:27 +03:00
|
|
|
*/
|
2015-08-12 21:51:09 +03:00
|
|
|
private function prepareStorageConfig(StorageConfig &$storage, IUser $user) {
|
|
|
|
foreach ($storage->getBackendOptions() as $option => $value) {
|
|
|
|
$storage->setBackendOption($option, \OC_Mount_Config::setUserVars(
|
|
|
|
$user->getUID(), $value
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2015-08-12 16:22:27 +03:00
|
|
|
$objectStore = $storage->getBackendOption('objectstore');
|
|
|
|
if ($objectStore) {
|
|
|
|
$objectClass = $objectStore['class'];
|
2015-08-28 17:13:19 +03:00
|
|
|
if (!is_subclass_of($objectClass, '\OCP\Files\ObjectStore\IObjectStore')) {
|
|
|
|
throw new \InvalidArgumentException('Invalid object store');
|
|
|
|
}
|
2015-08-12 16:22:27 +03:00
|
|
|
$storage->setBackendOption('objectstore', new $objectClass($objectStore));
|
|
|
|
}
|
|
|
|
|
2015-08-24 18:32:44 +03:00
|
|
|
$storage->getAuthMechanism()->manipulateStorageConfig($storage, $user);
|
|
|
|
$storage->getBackend()->manipulateStorageConfig($storage, $user);
|
2015-08-12 16:22:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct the storage implementation
|
|
|
|
*
|
|
|
|
* @param StorageConfig $storageConfig
|
|
|
|
* @return Storage
|
|
|
|
*/
|
|
|
|
private function constructStorage(StorageConfig $storageConfig) {
|
|
|
|
$class = $storageConfig->getBackend()->getStorageClass();
|
|
|
|
$storage = new $class($storageConfig->getBackendOptions());
|
|
|
|
|
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
|
|
|
// auth mechanism should fire first
|
2015-08-12 16:22:27 +03:00
|
|
|
$storage = $storageConfig->getBackend()->wrapStorage($storage);
|
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
|
|
|
$storage = $storageConfig->getAuthMechanism()->wrapStorage($storage);
|
2015-08-12 16:22:27 +03:00
|
|
|
|
|
|
|
return $storage;
|
2015-08-11 20:45:07 +03:00
|
|
|
}
|
|
|
|
|
2014-12-03 16:14:31 +03:00
|
|
|
/**
|
|
|
|
* Get all mountpoints applicable for the user
|
|
|
|
*
|
|
|
|
* @param \OCP\IUser $user
|
|
|
|
* @param \OCP\Files\Storage\IStorageFactory $loader
|
|
|
|
* @return \OCP\Files\Mount\IMountPoint[]
|
|
|
|
*/
|
|
|
|
public function getMountsForUser(IUser $user, IStorageFactory $loader) {
|
2015-12-10 18:40:27 +03:00
|
|
|
$this->migrator->migrateUser($user);
|
2015-11-02 15:13:06 +03:00
|
|
|
|
2015-08-12 16:22:27 +03:00
|
|
|
$this->userStoragesService->setUser($user);
|
|
|
|
$this->userGlobalStoragesService->setUser($user);
|
|
|
|
|
2016-09-02 16:41:59 +03:00
|
|
|
$storageConfigs = $this->userGlobalStoragesService->getAllStoragesForUser();
|
|
|
|
|
|
|
|
$storages = array_map(function(StorageConfig $storageConfig) use ($user) {
|
2015-08-12 21:51:09 +03:00
|
|
|
try {
|
2016-09-02 16:41:59 +03:00
|
|
|
$this->prepareStorageConfig($storageConfig, $user);
|
|
|
|
return $this->constructStorage($storageConfig);
|
2015-08-12 21:51:09 +03:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
// propagate exception into filesystem
|
2016-09-02 16:41:59 +03:00
|
|
|
return new FailedStorage(['exception' => $e]);
|
2015-08-12 21:51:09 +03:00
|
|
|
}
|
2016-09-02 16:41:59 +03:00
|
|
|
}, $storageConfigs);
|
|
|
|
|
2015-08-12 16:22:27 +03:00
|
|
|
|
2016-09-02 16:41:59 +03:00
|
|
|
\OC\Files\Cache\Storage::getGlobalCache()->loadForStorageIds(array_map(function(Storage\IStorage $storage) {
|
|
|
|
return $storage->getId();
|
|
|
|
}, $storages));
|
|
|
|
|
|
|
|
$availableStorages = array_map(function (Storage\IStorage $storage, StorageConfig $storageConfig) {
|
2016-02-04 18:24:41 +03:00
|
|
|
try {
|
2016-09-02 16:41:59 +03:00
|
|
|
$availability = $storage->getAvailability();
|
2016-03-03 16:19:34 +03:00
|
|
|
if (!$availability['available'] && !Availability::shouldRecheck($availability)) {
|
2016-09-02 16:41:59 +03:00
|
|
|
$storage = new FailedStorage([
|
|
|
|
'exception' => new StorageNotAvailableException('Storage with mount id ' . $storageConfig->getId() . ' is not available')
|
2016-03-02 14:17:14 +03:00
|
|
|
]);
|
2016-02-04 18:24:41 +03:00
|
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
// propagate exception into filesystem
|
2016-09-02 16:41:59 +03:00
|
|
|
$storage = new FailedStorage(['exception' => $e]);
|
2016-02-01 16:45:14 +03:00
|
|
|
}
|
2016-09-02 16:41:59 +03:00
|
|
|
return $storage;
|
|
|
|
}, $storages, $storageConfigs);
|
2016-02-01 16:45:14 +03:00
|
|
|
|
2016-09-02 16:41:59 +03:00
|
|
|
$mounts = array_map(function(StorageConfig $storageConfig, Storage\IStorage $storage) use ($user, $loader) {
|
|
|
|
if ($storageConfig->getType() === StorageConfig::MOUNT_TYPE_PERSONAl) {
|
|
|
|
return new PersonalMount(
|
2016-09-02 15:55:45 +03:00
|
|
|
$this->userStoragesService,
|
2016-09-02 16:41:59 +03:00
|
|
|
$storageConfig->getId(),
|
|
|
|
$storage,
|
|
|
|
'/' . $user->getUID() . '/files' . $storageConfig->getMountPoint(),
|
2016-09-02 15:55:45 +03:00
|
|
|
null,
|
|
|
|
$loader,
|
2016-09-02 16:41:59 +03:00
|
|
|
$storageConfig->getMountOptions()
|
2016-09-02 15:55:45 +03:00
|
|
|
);
|
|
|
|
} else {
|
2016-09-02 16:41:59 +03:00
|
|
|
return new MountPoint(
|
|
|
|
$storage,
|
|
|
|
'/' . $user->getUID() . '/files' . $storageConfig->getMountPoint(),
|
2016-09-02 15:55:45 +03:00
|
|
|
null,
|
|
|
|
$loader,
|
2016-09-02 16:41:59 +03:00
|
|
|
$storageConfig->getMountOptions(),
|
|
|
|
$storageConfig->getId()
|
2016-09-02 15:55:45 +03:00
|
|
|
);
|
2015-08-12 21:51:09 +03:00
|
|
|
}
|
2016-09-02 16:41:59 +03:00
|
|
|
}, $storageConfigs, $availableStorages);
|
2015-08-12 16:22:27 +03:00
|
|
|
|
|
|
|
$this->userStoragesService->resetUser();
|
|
|
|
$this->userGlobalStoragesService->resetUser();
|
|
|
|
|
2014-12-03 16:14:31 +03:00
|
|
|
return $mounts;
|
|
|
|
}
|
|
|
|
}
|