2014-12-03 08:14:31 -05:00
|
|
|
<?php
|
|
|
|
|
/**
|
2024-06-06 03:55:47 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2014-12-03 08:14:31 -05:00
|
|
|
*/
|
|
|
|
|
namespace OCA\Files_External\Config;
|
|
|
|
|
|
2019-11-22 14:52:10 -05:00
|
|
|
use OC\Files\Storage\FailedStorage;
|
2016-03-03 08:19:34 -05:00
|
|
|
use OC\Files\Storage\Wrapper\Availability;
|
2023-09-19 06:42:53 -04:00
|
|
|
use OC\Files\Storage\Wrapper\KnownMtime;
|
2019-11-22 14:52:10 -05:00
|
|
|
use OCA\Files_External\Lib\PersonalMount;
|
|
|
|
|
use OCA\Files_External\Lib\StorageConfig;
|
|
|
|
|
use OCA\Files_External\Service\UserGlobalStoragesService;
|
|
|
|
|
use OCA\Files_External\Service\UserStoragesService;
|
|
|
|
|
use OCP\Files\Config\IMountProvider;
|
2024-09-15 08:13:29 -04:00
|
|
|
use OCP\Files\ObjectStore\IObjectStore;
|
|
|
|
|
use OCP\Files\Storage\IStorage;
|
2014-12-03 08:14:31 -05:00
|
|
|
use OCP\Files\Storage\IStorageFactory;
|
2016-03-02 06:17:14 -05:00
|
|
|
use OCP\Files\StorageNotAvailableException;
|
2019-11-22 14:52:10 -05:00
|
|
|
use OCP\IUser;
|
2023-09-19 06:42:53 -04:00
|
|
|
use Psr\Clock\ClockInterface;
|
2014-12-03 08:14:31 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Make the old files_external config work with the new public mount config api
|
|
|
|
|
*/
|
|
|
|
|
class ConfigAdapter implements IMountProvider {
|
2015-08-12 09:22:27 -04:00
|
|
|
public function __construct(
|
2023-09-19 06:42:53 -04:00
|
|
|
private UserStoragesService $userStoragesService,
|
|
|
|
|
private UserGlobalStoragesService $userGlobalStoragesService,
|
|
|
|
|
private ClockInterface $clock,
|
|
|
|
|
) {
|
|
|
|
|
}
|
2015-08-12 09:22:27 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Process storage ready for mounting
|
|
|
|
|
*
|
2019-02-11 17:18:08 -05:00
|
|
|
* @throws \OCP\AppFramework\QueryException
|
2015-08-12 09:22:27 -04:00
|
|
|
*/
|
2024-09-15 08:13:29 -04:00
|
|
|
private function prepareStorageConfig(StorageConfig &$storage, IUser $user): void {
|
2015-08-12 14:51:09 -04:00
|
|
|
foreach ($storage->getBackendOptions() as $option => $value) {
|
2020-07-09 17:39:58 -04:00
|
|
|
$storage->setBackendOption($option, \OCA\Files_External\MountConfig::substitutePlaceholdersInConfig($value, $user->getUID()));
|
2015-08-12 14:51:09 -04:00
|
|
|
}
|
|
|
|
|
|
2015-08-12 09:22:27 -04:00
|
|
|
$objectStore = $storage->getBackendOption('objectstore');
|
|
|
|
|
if ($objectStore) {
|
|
|
|
|
$objectClass = $objectStore['class'];
|
2024-09-15 08:13:29 -04:00
|
|
|
if (!is_subclass_of($objectClass, IObjectStore::class)) {
|
2015-08-28 10:13:19 -04:00
|
|
|
throw new \InvalidArgumentException('Invalid object store');
|
|
|
|
|
}
|
2015-08-12 09:22:27 -04:00
|
|
|
$storage->setBackendOption('objectstore', new $objectClass($objectStore));
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-24 11:32:44 -04:00
|
|
|
$storage->getAuthMechanism()->manipulateStorageConfig($storage, $user);
|
|
|
|
|
$storage->getBackend()->manipulateStorageConfig($storage, $user);
|
2015-08-12 09:22:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Construct the storage implementation
|
|
|
|
|
*
|
|
|
|
|
* @param StorageConfig $storageConfig
|
|
|
|
|
*/
|
2024-09-15 08:13:29 -04:00
|
|
|
private function constructStorage(StorageConfig $storageConfig): IStorage {
|
2015-08-12 09:22:27 -04:00
|
|
|
$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 05:54:03 -04:00
|
|
|
// auth mechanism should fire first
|
2015-08-12 09:22:27 -04: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 05:54:03 -04:00
|
|
|
$storage = $storageConfig->getAuthMechanism()->wrapStorage($storage);
|
2015-08-12 09:22:27 -04:00
|
|
|
|
|
|
|
|
return $storage;
|
2015-08-11 13:45:07 -04:00
|
|
|
}
|
|
|
|
|
|
2014-12-03 08:14:31 -05:00
|
|
|
/**
|
|
|
|
|
* Get all mountpoints applicable for the user
|
|
|
|
|
*
|
|
|
|
|
* @return \OCP\Files\Mount\IMountPoint[]
|
|
|
|
|
*/
|
|
|
|
|
public function getMountsForUser(IUser $user, IStorageFactory $loader) {
|
2015-08-12 09:22:27 -04:00
|
|
|
$this->userStoragesService->setUser($user);
|
|
|
|
|
$this->userGlobalStoragesService->setUser($user);
|
|
|
|
|
|
2016-09-02 09:41:59 -04:00
|
|
|
$storageConfigs = $this->userGlobalStoragesService->getAllStoragesForUser();
|
|
|
|
|
|
2020-04-09 07:53:40 -04:00
|
|
|
$storages = array_map(function (StorageConfig $storageConfig) use ($user) {
|
2015-08-12 14:51:09 -04:00
|
|
|
try {
|
2016-09-02 09:41:59 -04:00
|
|
|
$this->prepareStorageConfig($storageConfig, $user);
|
|
|
|
|
return $this->constructStorage($storageConfig);
|
2015-08-12 14:51:09 -04:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
// propagate exception into filesystem
|
2016-09-02 09:41:59 -04:00
|
|
|
return new FailedStorage(['exception' => $e]);
|
2015-08-12 14:51:09 -04:00
|
|
|
}
|
2016-09-02 09:41:59 -04:00
|
|
|
}, $storageConfigs);
|
|
|
|
|
|
2015-08-12 09:22:27 -04:00
|
|
|
|
2024-09-15 08:13:29 -04:00
|
|
|
\OC\Files\Cache\Storage::getGlobalCache()->loadForStorageIds(array_map(function (IStorage $storage) {
|
2016-09-02 09:41:59 -04:00
|
|
|
return $storage->getId();
|
|
|
|
|
}, $storages));
|
|
|
|
|
|
2024-09-15 08:13:29 -04:00
|
|
|
$availableStorages = array_map(function (IStorage $storage, StorageConfig $storageConfig): IStorage {
|
2016-02-04 10:24:41 -05:00
|
|
|
try {
|
2016-09-02 09:41:59 -04:00
|
|
|
$availability = $storage->getAvailability();
|
2016-03-03 08:19:34 -05:00
|
|
|
if (!$availability['available'] && !Availability::shouldRecheck($availability)) {
|
2016-09-02 09:41:59 -04:00
|
|
|
$storage = new FailedStorage([
|
|
|
|
|
'exception' => new StorageNotAvailableException('Storage with mount id ' . $storageConfig->getId() . ' is not available')
|
2016-03-02 06:17:14 -05:00
|
|
|
]);
|
2016-02-04 10:24:41 -05:00
|
|
|
}
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
// propagate exception into filesystem
|
2016-09-02 09:41:59 -04:00
|
|
|
$storage = new FailedStorage(['exception' => $e]);
|
2016-02-01 08:45:14 -05:00
|
|
|
}
|
2016-09-02 09:41:59 -04:00
|
|
|
return $storage;
|
|
|
|
|
}, $storages, $storageConfigs);
|
2016-02-01 08:45:14 -05:00
|
|
|
|
2024-09-15 08:13:29 -04:00
|
|
|
$mounts = array_map(function (StorageConfig $storageConfig, IStorage $storage) use ($user, $loader) {
|
2024-03-19 04:33:16 -04:00
|
|
|
$storage->setOwner($user->getUID());
|
2023-07-10 09:31:47 -04:00
|
|
|
if ($storageConfig->getType() === StorageConfig::MOUNT_TYPE_PERSONAL) {
|
2016-09-02 09:41:59 -04:00
|
|
|
return new PersonalMount(
|
2016-09-02 08:55:45 -04:00
|
|
|
$this->userStoragesService,
|
2020-10-07 09:31:03 -04:00
|
|
|
$storageConfig,
|
2016-09-02 09:41:59 -04:00
|
|
|
$storageConfig->getId(),
|
2023-09-19 06:42:53 -04:00
|
|
|
new KnownMtime([
|
|
|
|
|
'storage' => $storage,
|
|
|
|
|
'clock' => $this->clock,
|
|
|
|
|
]),
|
2016-09-02 09:41:59 -04:00
|
|
|
'/' . $user->getUID() . '/files' . $storageConfig->getMountPoint(),
|
2016-09-02 08:55:45 -04:00
|
|
|
null,
|
|
|
|
|
$loader,
|
2020-12-02 08:25:48 -05:00
|
|
|
$storageConfig->getMountOptions(),
|
|
|
|
|
$storageConfig->getId()
|
2016-09-02 08:55:45 -04:00
|
|
|
);
|
|
|
|
|
} else {
|
2022-08-10 07:58:09 -04:00
|
|
|
return new SystemMountPoint(
|
2020-10-07 09:31:03 -04:00
|
|
|
$storageConfig,
|
2016-09-02 09:41:59 -04:00
|
|
|
$storage,
|
|
|
|
|
'/' . $user->getUID() . '/files' . $storageConfig->getMountPoint(),
|
2016-09-02 08:55:45 -04:00
|
|
|
null,
|
|
|
|
|
$loader,
|
2016-09-02 09:41:59 -04:00
|
|
|
$storageConfig->getMountOptions(),
|
|
|
|
|
$storageConfig->getId()
|
2016-09-02 08:55:45 -04:00
|
|
|
);
|
2015-08-12 14:51:09 -04:00
|
|
|
}
|
2016-09-02 09:41:59 -04:00
|
|
|
}, $storageConfigs, $availableStorages);
|
2015-08-12 09:22:27 -04:00
|
|
|
|
|
|
|
|
$this->userStoragesService->resetUser();
|
|
|
|
|
$this->userGlobalStoragesService->resetUser();
|
|
|
|
|
|
2014-12-03 08:14:31 -05:00
|
|
|
return $mounts;
|
|
|
|
|
}
|
|
|
|
|
}
|