nextcloud/apps/files_external/lib/Config/ConfigAdapter.php

184 lines
5.5 KiB
PHP
Raw Normal View History

<?php
/**
2015-03-26 13:44:34 +03:00
* @author Morris Jobke <hey@morrisjobke.de>
* @author Robin Appelman <icewind@owncloud.com>
2016-01-12 17:02:16 +03:00
* @author Robin McCorkell <robin@mccorkell.me.uk>
2015-03-26 13:44:34 +03:00
*
2016-01-12 17:02:16 +03:00
* @copyright Copyright (c) 2016, ownCloud, Inc.
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/>
*
*/
namespace OCA\Files_External\Config;
use OC\Files\Storage\Wrapper\Availability;
2016-05-13 12:22:28 +03:00
use OCA\Files_External\Migration\StorageMigrator;
use OCP\Files\Storage;
use OC\Files\Mount\MountPoint;
use OCP\Files\Storage\IStorageFactory;
use OCA\Files_External\Lib\PersonalMount;
use OCP\Files\Config\IMountProvider;
use OCP\IUser;
2016-05-13 12:22:28 +03:00
use OCA\Files_External\Service\UserStoragesService;
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;
use OCP\Files\StorageNotAvailableException;
/**
* Make the old files_external config work with the new public mount config api
*/
class ConfigAdapter implements IMountProvider {
/** @var UserStoragesService */
private $userStoragesService;
/** @var UserGlobalStoragesService */
private $userGlobalStoragesService;
2015-11-02 15:13:06 +03:00
/** @var StorageMigrator */
private $migrator;
/**
* @param UserStoragesService $userStoragesService
* @param UserGlobalStoragesService $userGlobalStoragesService
2015-11-02 15:13:06 +03:00
* @param StorageMigrator $migrator
*/
public function __construct(
UserStoragesService $userStoragesService,
2015-11-02 15:13:06 +03:00
UserGlobalStoragesService $userGlobalStoragesService,
StorageMigrator $migrator
) {
$this->userStoragesService = $userStoragesService;
$this->userGlobalStoragesService = $userGlobalStoragesService;
2015-11-02 15:13:06 +03:00
$this->migrator = $migrator;
}
/**
* Process storage ready for mounting
*
* @param StorageConfig $storage
* @param IUser $user
*/
private function prepareStorageConfig(StorageConfig &$storage, IUser $user) {
foreach ($storage->getBackendOptions() as $option => $value) {
$storage->setBackendOption($option, \OC_Mount_Config::setUserVars(
$user->getUID(), $value
));
}
$objectStore = $storage->getBackendOption('objectstore');
if ($objectStore) {
$objectClass = $objectStore['class'];
if (!is_subclass_of($objectClass, '\OCP\Files\ObjectStore\IObjectStore')) {
throw new \InvalidArgumentException('Invalid object store');
}
$storage->setBackendOption('objectstore', new $objectClass($objectStore));
}
$storage->getAuthMechanism()->manipulateStorageConfig($storage, $user);
$storage->getBackend()->manipulateStorageConfig($storage, $user);
}
/**
* 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
$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);
return $storage;
}
/**
* 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) {
$this->migrator->migrateUser($user);
2015-11-02 15:13:06 +03:00
$mounts = [];
$this->userStoragesService->setUser($user);
$this->userGlobalStoragesService->setUser($user);
2015-09-14 22:31:24 +03:00
foreach ($this->userGlobalStoragesService->getUniqueStorages() as $storage) {
try {
$this->prepareStorageConfig($storage, $user);
$impl = $this->constructStorage($storage);
} catch (\Exception $e) {
// propagate exception into filesystem
$impl = new FailedStorage(['exception' => $e]);
}
2016-02-04 18:24:41 +03:00
try {
$availability = $impl->getAvailability();
if (!$availability['available'] && !Availability::shouldRecheck($availability)) {
$impl = new FailedStorage([
'exception' => new StorageNotAvailableException('Storage with mount id ' . $storage->getId() . ' is not available')
]);
2016-02-04 18:24:41 +03:00
}
} catch (\Exception $e) {
// propagate exception into filesystem
$impl = new FailedStorage(['exception' => $e]);
2016-02-01 16:45:14 +03:00
}
$mount = new MountPoint(
$impl,
2015-11-02 15:13:06 +03:00
'/' . $user->getUID() . '/files' . $storage->getMountPoint(),
null,
$loader,
$storage->getMountOptions()
);
$mounts[$storage->getMountPoint()] = $mount;
}
foreach ($this->userStoragesService->getStorages() as $storage) {
try {
$this->prepareStorageConfig($storage, $user);
$impl = $this->constructStorage($storage);
} catch (\Exception $e) {
// propagate exception into filesystem
$impl = new FailedStorage(['exception' => $e]);
}
$mount = new PersonalMount(
$this->userStoragesService,
$storage->getId(),
$impl,
2015-11-02 15:13:06 +03:00
'/' . $user->getUID() . '/files' . $storage->getMountPoint(),
null,
$loader,
$storage->getMountOptions()
);
$mounts[$storage->getMountPoint()] = $mount;
}
$this->userStoragesService->resetUser();
$this->userGlobalStoragesService->resetUser();
return $mounts;
}
}