2014-12-03 08:14:31 -05:00
< ? php
2025-06-30 09:04:05 -04:00
2014-12-03 08:14:31 -05:00
/**
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 ;
2024-10-10 06:40:31 -04:00
use OC\Files\Cache\Storage ;
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 ;
2024-10-10 06:40:31 -04:00
use OCA\Files_External\MountConfig ;
2019-11-22 14:52:10 -05:00
use OCA\Files_External\Service\UserGlobalStoragesService ;
use OCA\Files_External\Service\UserStoragesService ;
2025-11-17 12:10:48 -05:00
use OCP\Files\Config\IAuthoritativeMountProvider ;
2019-11-22 14:52:10 -05:00
use OCP\Files\Config\IMountProvider ;
2026-01-12 09:13:13 -05:00
use OCP\Files\Config\IPartialMountProvider ;
2024-10-18 06:04:22 -04:00
use OCP\Files\Mount\IMountPoint ;
2024-09-15 08:13:29 -04:00
use OCP\Files\ObjectStore\IObjectStore ;
2024-09-16 15:48:18 -04:00
use OCP\Files\Storage\IConstructableStorage ;
2024-09-15 08:13:29 -04:00
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 ;
2024-10-10 06:40:31 -04:00
use OCP\Server ;
2026-01-12 09:13:13 -05:00
use Override ;
2023-09-19 06:42:53 -04:00
use Psr\Clock\ClockInterface ;
2026-01-16 05:52:35 -05:00
use Psr\Container\ContainerExceptionInterface ;
2024-09-16 15:48:18 -04:00
use Psr\Log\LoggerInterface ;
2014-12-03 08:14:31 -05:00
/**
* Make the old files_external config work with the new public mount config api
*/
2026-01-12 09:13:13 -05:00
class ConfigAdapter implements IMountProvider , IAuthoritativeMountProvider , IPartialMountProvider {
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 ,
2023-11-23 04:22:34 -05:00
) {
}
2015-08-12 09:22:27 -04:00
2025-02-17 12:06:45 -05:00
/**
* @ param class - string $class
* @ return class - string < IObjectStore >
* @ throws \InvalidArgumentException
* @ psalm - taint - escape callable
*/
private function validateObjectStoreClassString ( string $class ) : string {
if ( ! \is_subclass_of ( $class , IObjectStore :: class )) {
throw new \InvalidArgumentException ( 'Invalid object store' );
}
return $class ;
}
2015-08-12 09:22:27 -04:00
/**
* Process storage ready for mounting
*
2026-01-16 05:52:35 -05:00
* @ throws ContainerExceptionInterface
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 ) {
2024-10-10 06:40:31 -04:00
$storage -> setBackendOption ( $option , 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 ) {
2025-02-17 12:06:45 -05:00
$objectClass = $this -> validateObjectStoreClassString ( $objectStore [ 'class' ]);
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
}
2025-11-17 12:10:48 -05:00
public function constructStorageForUser ( IUser $user , StorageConfig $storage ) {
$this -> prepareStorageConfig ( $storage , $user );
return $this -> constructStorage ( $storage );
}
2015-08-12 09:22:27 -04:00
/**
* Construct the storage implementation
*/
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 ();
2024-09-30 05:19:53 -04:00
if ( ! is_a ( $class , IConstructableStorage :: class , true )) {
2024-10-10 06:40:31 -04:00
Server :: get ( LoggerInterface :: class ) -> warning ( 'Building a storage not implementing IConstructableStorage is deprecated since 31.0.0' , [ 'class' => $class ]);
2024-09-16 15:48:18 -04:00
}
2015-08-12 09:22:27 -04:00
$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
/**
2026-01-12 09:13:13 -05:00
* @ param list < StorageConfig > $storageConfigs
* @ return array
* @ throws ContainerExceptionInterface
2014-12-03 08:14:31 -05:00
*/
2026-01-12 09:13:13 -05:00
private function getAvailableStorages ( array $storageConfigs , IUser $user ) : array {
$storages = array_map ( function ( StorageConfig $storageConfig ) use ( $user ) : IStorage {
2015-08-12 14:51:09 -04:00
try {
2025-11-17 12:10:48 -05:00
return $this -> constructStorageForUser ( $user , $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-10-10 06:40:31 -04:00
Storage :: getGlobalCache () -> loadForStorageIds ( array_map ( function ( IStorage $storage ) {
2016-09-02 09:41:59 -04:00
return $storage -> getId ();
}, $storages ));
2026-01-12 09:13:13 -05:00
return 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 ([
2025-11-17 12:10:48 -05:00
'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 );
2026-01-12 09:13:13 -05:00
}
/**
* Get all mountpoints applicable for the user
*
* @ return IMountPoint []
*/
public function getMountsForUser ( IUser $user , IStorageFactory $loader ) : array {
$this -> userStoragesService -> setUser ( $user );
$this -> userGlobalStoragesService -> setUser ( $user );
$storageConfigs = $this -> userGlobalStoragesService -> getAllStoragesForUser ();
$availableStorages = $this -> getAvailableStorages ( $storageConfigs , $user );
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 ) {
2026-01-12 09:13:13 -05:00
$mountpoint = '/' . $user -> getUID () . '/files' . $storageConfig -> getMountPoint ();
return $this -> storageConfigToMount ( $user , $mountpoint , $loader , $storage , $storageConfig );
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 ;
}
2026-01-12 09:13:13 -05:00
#[Override]
public function getMountsForPath ( string $setupPathHint , bool $forChildren , array $mountProviderArgs , IStorageFactory $loader ) : array {
$user = $mountProviderArgs [ 0 ] -> mountInfo -> getUser ();
if ( ! $forChildren ) {
// override path with mount point when fetching without children
$setupPathHint = $mountProviderArgs [ 0 ] -> mountInfo -> getMountPoint ();
}
$this -> userStoragesService -> setUser ( $user );
$this -> userGlobalStoragesService -> setUser ( $user );
$storageConfigs = $this -> userGlobalStoragesService -> getAllStoragesForUserWithPath ( $setupPathHint , $forChildren );
$availableStorages = $this -> getAvailableStorages ( $storageConfigs , $user );
$mounts = [];
$i = 0 ;
foreach ( $storageConfigs as $storageConfig ) {
$storage = $availableStorages [ $i ];
$i ++ ;
$mountPoint = '/' . $user -> getUID () . '/files' . $storageConfig -> getMountPoint ();
$mounts [ $mountPoint ] = $this -> storageConfigToMount ( $user , $mountPoint , $loader , $storage , $storageConfig );
}
$this -> userStoragesService -> resetUser ();
$this -> userGlobalStoragesService -> resetUser ();
return $mounts ;
}
private function storageConfigToMount ( IUser $user , string $mountPoint , IStorageFactory $loader , IStorage $storage , StorageConfig $storageConfig ) : IMountPoint {
$storage -> setOwner ( $user -> getUID ());
if ( $storageConfig -> getType () === StorageConfig :: MOUNT_TYPE_PERSONAL ) {
return new PersonalMount (
$this -> userStoragesService ,
$storageConfig ,
$storageConfig -> getId (),
new KnownMtime ([
'storage' => $storage ,
'clock' => $this -> clock ,
]),
$mountPoint ,
null ,
$loader ,
$storageConfig -> getMountOptions (),
$storageConfig -> getId ()
);
} else {
return new SystemMountPoint (
$storageConfig ,
$storage ,
$mountPoint ,
null ,
$loader ,
$storageConfig -> getMountOptions (),
$storageConfig -> getId ()
);
}
}
2014-12-03 08:14:31 -05:00
}