diff options
Diffstat (limited to 'apps/files_external/lib/Service')
9 files changed, 77 insertions, 108 deletions
diff --git a/apps/files_external/lib/Service/BackendService.php b/apps/files_external/lib/Service/BackendService.php index 5eb0276be65..3a688ee66e6 100644 --- a/apps/files_external/lib/Service/BackendService.php +++ b/apps/files_external/lib/Service/BackendService.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2018-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -7,14 +8,16 @@ namespace OCA\Files_External\Service; use OCA\Files_External\Config\IConfigHandler; +use OCA\Files_External\ConfigLexicon; use OCA\Files_External\Lib\Auth\AuthMechanism; - use OCA\Files_External\Lib\Backend\Backend; use OCA\Files_External\Lib\Config\IAuthMechanismProvider; use OCA\Files_External\Lib\Config\IBackendProvider; +use OCA\Files_External\Lib\MissingDependency; use OCP\EventDispatcher\GenericEvent; use OCP\EventDispatcher\IEventDispatcher; -use OCP\IConfig; +use OCP\IAppConfig; +use OCP\Server; /** * Service class to manage backend definitions @@ -32,9 +35,6 @@ class BackendService { /** Priority constants for PriorityTrait */ public const PRIORITY_DEFAULT = 100; - /** @var IConfig */ - protected $config; - /** @var bool */ private $userMountingAllowed = true; @@ -58,21 +58,12 @@ class BackendService { private $configHandlers = []; - /** - * @param IConfig $config - */ public function __construct( - IConfig $config + protected IAppConfig $appConfig, ) { - $this->config = $config; - // Load config values - if ($this->config->getAppValue('files_external', 'allow_user_mounting', 'yes') !== 'yes') { - $this->userMountingAllowed = false; - } - $this->userMountingBackends = explode(',', - $this->config->getAppValue('files_external', 'user_mounting_backends', '') - ); + $this->userMountingAllowed = $appConfig->getValueBool('files_external', ConfigLexicon::ALLOW_USER_MOUNTING); + $this->userMountingBackends = explode(',', $appConfig->getValueString('files_external', ConfigLexicon::USER_MOUNTING_BACKENDS)); // if no backend is in the list an empty string is in the array and user mounting is disabled if ($this->userMountingBackends === ['']) { @@ -93,7 +84,7 @@ class BackendService { private function callForRegistrations() { static $eventSent = false; if (!$eventSent) { - \OC::$server->get(IEventDispatcher::class)->dispatch( + Server::get(IEventDispatcher::class)->dispatch( 'OCA\\Files_External::loadAdditionalBackends', new GenericEvent() ); @@ -198,7 +189,8 @@ class BackendService { */ public function getAvailableBackends() { return array_filter($this->getBackends(), function ($backend) { - return !$backend->checkDependencies(); + $missing = array_filter($backend->checkDependencies(), fn (MissingDependency $dependency) => !$dependency->isOptional()); + return count($missing) === 0; }); } @@ -267,8 +259,8 @@ class BackendService { * @return bool */ protected function isAllowedUserBackend(Backend $backend) { - if ($this->userMountingAllowed && - array_intersect($backend->getIdentifierAliases(), $this->userMountingBackends) + if ($this->userMountingAllowed + && array_intersect($backend->getIdentifierAliases(), $this->userMountingBackends) ) { return true; } diff --git a/apps/files_external/lib/Service/DBConfigService.php b/apps/files_external/lib/Service/DBConfigService.php index 6fb7e01271e..41ec4512d70 100644 --- a/apps/files_external/lib/Service/DBConfigService.php +++ b/apps/files_external/lib/Service/DBConfigService.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2018-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -24,25 +25,10 @@ class DBConfigService { public const APPLICABLE_TYPE_GROUP = 2; public const APPLICABLE_TYPE_USER = 3; - /** - * @var IDBConnection - */ - private $connection; - - /** - * @var ICrypto - */ - private $crypto; - - /** - * DBConfigService constructor. - * - * @param IDBConnection $connection - * @param ICrypto $crypto - */ - public function __construct(IDBConnection $connection, ICrypto $crypto) { - $this->connection = $connection; - $this->crypto = $crypto; + public function __construct( + private IDBConnection $connection, + private ICrypto $crypto, + ) { } public function getMountById(int $mountId): ?array { @@ -112,7 +98,7 @@ class DBConfigService { ) ) ->groupBy(['a.mount_id']); - $stmt = $query->execute(); + $stmt = $query->executeQuery(); $result = $stmt->fetchAll(); $stmt->closeCursor(); @@ -243,7 +229,7 @@ class DBConfigService { 'priority' => $builder->createNamedParameter($priority, IQueryBuilder::PARAM_INT), 'type' => $builder->createNamedParameter($type, IQueryBuilder::PARAM_INT) ]); - $query->execute(); + $query->executeStatement(); return $query->getLastInsertId(); } @@ -256,22 +242,22 @@ class DBConfigService { $builder = $this->connection->getQueryBuilder(); $query = $builder->delete('external_mounts') ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); - $query->execute(); + $query->executeStatement(); $builder = $this->connection->getQueryBuilder(); $query = $builder->delete('external_applicable') ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); - $query->execute(); + $query->executeStatement(); $builder = $this->connection->getQueryBuilder(); $query = $builder->delete('external_config') ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); - $query->execute(); + $query->executeStatement(); $builder = $this->connection->getQueryBuilder(); $query = $builder->delete('external_options') ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); - $query->execute(); + $query->executeStatement(); } /** @@ -285,7 +271,7 @@ class DBConfigService { ->set('mount_point', $builder->createNamedParameter($newMountPoint)) ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); - $query->execute(); + $query->executeStatement(); } /** @@ -299,7 +285,7 @@ class DBConfigService { ->set('auth_backend', $builder->createNamedParameter($newAuthBackend)) ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); - $query->execute(); + $query->executeStatement(); } /** @@ -325,7 +311,7 @@ class DBConfigService { ->set('value', $builder->createNamedParameter($value, IQueryBuilder::PARAM_STR)) ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))) ->andWhere($builder->expr()->eq('key', $builder->createNamedParameter($key, IQueryBuilder::PARAM_STR))); - $query->execute(); + $query->executeStatement(); } } @@ -348,7 +334,7 @@ class DBConfigService { ->set('value', $builder->createNamedParameter(json_encode($value), IQueryBuilder::PARAM_STR)) ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))) ->andWhere($builder->expr()->eq('key', $builder->createNamedParameter($key, IQueryBuilder::PARAM_STR))); - $query->execute(); + $query->executeStatement(); } } @@ -377,11 +363,11 @@ class DBConfigService { $query = $query->andWhere($builder->expr()->eq('value', $builder->createNamedParameter($value, IQueryBuilder::PARAM_STR))); } - $query->execute(); + $query->executeStatement(); } private function getMountsFromQuery(IQueryBuilder $query) { - $result = $query->execute(); + $result = $query->executeQuery(); $mounts = $result->fetchAll(); $uniqueMounts = []; foreach ($mounts as $mount) { @@ -432,7 +418,7 @@ class DBConfigService { ->from($table) ->where($builder->expr()->in('mount_id', $placeHolders)); - $result = $query->execute(); + $result = $query->executeQuery(); $rows = $result->fetchAll(); $result->closeCursor(); diff --git a/apps/files_external/lib/Service/GlobalStoragesService.php b/apps/files_external/lib/Service/GlobalStoragesService.php index c799007cc6d..5b1a9f41e48 100644 --- a/apps/files_external/lib/Service/GlobalStoragesService.php +++ b/apps/files_external/lib/Service/GlobalStoragesService.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -8,6 +9,7 @@ namespace OCA\Files_External\Service; use OC\Files\Filesystem; use OCA\Files_External\Lib\StorageConfig; +use OCA\Files_External\MountConfig; /** * Service class to manage global external storage @@ -29,7 +31,7 @@ class GlobalStoragesService extends StoragesService { $this->triggerApplicableHooks( $signal, $storage->getMountPoint(), - \OCA\Files_External\MountConfig::MOUNT_TYPE_USER, + MountConfig::MOUNT_TYPE_USER, ['all'] ); return; @@ -38,13 +40,13 @@ class GlobalStoragesService extends StoragesService { $this->triggerApplicableHooks( $signal, $storage->getMountPoint(), - \OCA\Files_External\MountConfig::MOUNT_TYPE_USER, + MountConfig::MOUNT_TYPE_USER, $applicableUsers ); $this->triggerApplicableHooks( $signal, $storage->getMountPoint(), - \OCA\Files_External\MountConfig::MOUNT_TYPE_GROUP, + MountConfig::MOUNT_TYPE_GROUP, $applicableGroups ); } @@ -78,7 +80,7 @@ class GlobalStoragesService extends StoragesService { $this->triggerApplicableHooks( Filesystem::signal_delete_mount, $oldStorage->getMountPoint(), - \OCA\Files_External\MountConfig::MOUNT_TYPE_USER, + MountConfig::MOUNT_TYPE_USER, ['all'] ); } @@ -87,7 +89,7 @@ class GlobalStoragesService extends StoragesService { $this->triggerApplicableHooks( Filesystem::signal_delete_mount, $oldStorage->getMountPoint(), - \OCA\Files_External\MountConfig::MOUNT_TYPE_USER, + MountConfig::MOUNT_TYPE_USER, $userDeletions ); @@ -95,7 +97,7 @@ class GlobalStoragesService extends StoragesService { $this->triggerApplicableHooks( Filesystem::signal_delete_mount, $oldStorage->getMountPoint(), - \OCA\Files_External\MountConfig::MOUNT_TYPE_GROUP, + MountConfig::MOUNT_TYPE_GROUP, $groupDeletions ); @@ -103,7 +105,7 @@ class GlobalStoragesService extends StoragesService { $this->triggerApplicableHooks( Filesystem::signal_create_mount, $newStorage->getMountPoint(), - \OCA\Files_External\MountConfig::MOUNT_TYPE_USER, + MountConfig::MOUNT_TYPE_USER, $userAdditions ); @@ -111,7 +113,7 @@ class GlobalStoragesService extends StoragesService { $this->triggerApplicableHooks( Filesystem::signal_create_mount, $newStorage->getMountPoint(), - \OCA\Files_External\MountConfig::MOUNT_TYPE_GROUP, + MountConfig::MOUNT_TYPE_GROUP, $groupAdditions ); @@ -123,7 +125,7 @@ class GlobalStoragesService extends StoragesService { $this->triggerApplicableHooks( Filesystem::signal_create_mount, $newStorage->getMountPoint(), - \OCA\Files_External\MountConfig::MOUNT_TYPE_USER, + MountConfig::MOUNT_TYPE_USER, ['all'] ); } diff --git a/apps/files_external/lib/Service/ImportLegacyStoragesService.php b/apps/files_external/lib/Service/ImportLegacyStoragesService.php index fe5e71c1a47..7d9840e9f5e 100644 --- a/apps/files_external/lib/Service/ImportLegacyStoragesService.php +++ b/apps/files_external/lib/Service/ImportLegacyStoragesService.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2016 ownCloud, Inc. * SPDX-License-Identifier: AGPL-3.0-only diff --git a/apps/files_external/lib/Service/LegacyStoragesService.php b/apps/files_external/lib/Service/LegacyStoragesService.php index ac9273f2afc..9f199a89b3f 100644 --- a/apps/files_external/lib/Service/LegacyStoragesService.php +++ b/apps/files_external/lib/Service/LegacyStoragesService.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2018-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -7,6 +8,8 @@ namespace OCA\Files_External\Service; use OCA\Files_External\Lib\StorageConfig; +use OCA\Files_External\MountConfig; +use OCP\Server; use Psr\Log\LoggerInterface; /** @@ -37,7 +40,7 @@ abstract class LegacyStoragesService { &$storageConfig, $mountType, $applicable, - $storageOptions + $storageOptions, ) { $backend = $this->backendService->getBackend($storageOptions['backend']); if (!$backend) { @@ -62,13 +65,13 @@ abstract class LegacyStoragesService { $storageOptions['priority'] = $backend->getPriority(); } $storageConfig->setPriority($storageOptions['priority']); - if ($mountType === \OCA\Files_External\MountConfig::MOUNT_TYPE_USER) { + if ($mountType === MountConfig::MOUNT_TYPE_USER) { $applicableUsers = $storageConfig->getApplicableUsers(); if ($applicable !== 'all') { $applicableUsers[] = $applicable; $storageConfig->setApplicableUsers($applicableUsers); } - } elseif ($mountType === \OCA\Files_External\MountConfig::MOUNT_TYPE_GROUP) { + } elseif ($mountType === MountConfig::MOUNT_TYPE_GROUP) { $applicableGroups = $storageConfig->getApplicableGroups(); $applicableGroups[] = $applicable; $storageConfig->setApplicableGroups($applicableGroups); @@ -123,13 +126,13 @@ abstract class LegacyStoragesService { $parts = explode('/', ltrim($rootMountPath, '/'), 3); if (count($parts) < 3) { // something went wrong, skip - \OC::$server->get(LoggerInterface::class)->error('Could not parse mount point "' . $rootMountPath . '"', ['app' => 'files_external']); + Server::get(LoggerInterface::class)->error('Could not parse mount point "' . $rootMountPath . '"', ['app' => 'files_external']); continue; } $relativeMountPath = rtrim($parts[2], '/'); // note: we cannot do this after the loop because the decrypted config // options might be needed for the config hash - $storageOptions['options'] = \OCA\Files_External\MountConfig::decryptPasswords($storageOptions['options']); + $storageOptions['options'] = MountConfig::decryptPasswords($storageOptions['options']); if (!isset($storageOptions['backend'])) { $storageOptions['backend'] = $storageOptions['class']; // legacy compat } @@ -147,7 +150,7 @@ abstract class LegacyStoragesService { // but at this point we don't know the max-id, so use // first group it by config hash $storageOptions['mountpoint'] = $rootMountPath; - $configId = \OCA\Files_External\MountConfig::makeConfigHash($storageOptions); + $configId = MountConfig::makeConfigHash($storageOptions); if (isset($storagesWithConfigHash[$configId])) { $currentStorage = $storagesWithConfigHash[$configId]; } @@ -171,7 +174,7 @@ abstract class LegacyStoragesService { } } catch (\UnexpectedValueException $e) { // don't die if a storage backend doesn't exist - \OC::$server->get(LoggerInterface::class)->error('Could not load storage.', [ + Server::get(LoggerInterface::class)->error('Could not load storage.', [ 'app' => 'files_external', 'exception' => $e, ]); diff --git a/apps/files_external/lib/Service/StoragesService.php b/apps/files_external/lib/Service/StoragesService.php index 09f7ea954b4..a12a8fc245a 100644 --- a/apps/files_external/lib/Service/StoragesService.php +++ b/apps/files_external/lib/Service/StoragesService.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -6,6 +7,7 @@ */ namespace OCA\Files_External\Service; +use OC\Files\Cache\Storage; use OC\Files\Filesystem; use OCA\Files_External\Lib\Auth\AuthMechanism; use OCA\Files_External\Lib\Auth\InvalidAuth; @@ -18,6 +20,8 @@ use OCP\EventDispatcher\IEventDispatcher; use OCP\Files\Config\IUserMountCache; use OCP\Files\Events\InvalidateMountCacheEvent; use OCP\Files\StorageNotAvailableException; +use OCP\Server; +use OCP\Util; use Psr\Log\LoggerInterface; /** @@ -25,37 +29,18 @@ use Psr\Log\LoggerInterface; */ abstract class StoragesService { - /** @var BackendService */ - protected $backendService; - - /** - * @var DBConfigService - */ - protected $dbConfig; - - /** - * @var IUserMountCache - */ - protected $userMountCache; - - protected IEventDispatcher $eventDispatcher; - /** * @param BackendService $backendService - * @param DBConfigService $dbConfigService + * @param DBConfigService $dbConfig * @param IUserMountCache $userMountCache * @param IEventDispatcher $eventDispatcher */ public function __construct( - BackendService $backendService, - DBConfigService $dbConfigService, - IUserMountCache $userMountCache, - IEventDispatcher $eventDispatcher + protected BackendService $backendService, + protected DBConfigService $dbConfig, + protected IUserMountCache $userMountCache, + protected IEventDispatcher $eventDispatcher, ) { - $this->backendService = $backendService; - $this->dbConfig = $dbConfigService; - $this->userMountCache = $userMountCache; - $this->eventDispatcher = $eventDispatcher; } protected function readDBConfig() { @@ -93,13 +78,13 @@ abstract class StoragesService { return $config; } catch (\UnexpectedValueException $e) { // don't die if a storage backend doesn't exist - \OC::$server->get(LoggerInterface::class)->error('Could not load storage.', [ + Server::get(LoggerInterface::class)->error('Could not load storage.', [ 'app' => 'files_external', 'exception' => $e, ]); return null; } catch (\InvalidArgumentException $e) { - \OC::$server->get(LoggerInterface::class)->error('Could not load storage.', [ + Server::get(LoggerInterface::class)->error('Could not load storage.', [ 'app' => 'files_external', 'exception' => $e, ]); @@ -134,7 +119,7 @@ abstract class StoragesService { * @return StorageConfig * @throws NotFoundException if the storage with the given id was not found */ - public function getStorage($id) { + public function getStorage(int $id) { $mount = $this->dbConfig->getMountById($id); if (!is_array($mount)) { @@ -282,7 +267,7 @@ abstract class StoragesService { $mountOptions = null, $applicableUsers = null, $applicableGroups = null, - $priority = null + $priority = null, ) { $backend = $this->backendService->getBackend($backendIdentifier); if (!$backend) { @@ -324,7 +309,7 @@ abstract class StoragesService { protected function triggerApplicableHooks($signal, $mountPoint, $mountType, $applicableArray): void { $this->eventDispatcher->dispatchTyped(new InvalidateMountCacheEvent(null)); foreach ($applicableArray as $applicable) { - \OCP\Util::emitHook( + Util::emitHook( Filesystem::CLASSNAME, $signal, [ @@ -450,7 +435,7 @@ abstract class StoragesService { * * @throws NotFoundException if no storage was found with the given id */ - public function removeStorage($id) { + public function removeStorage(int $id) { $existingMount = $this->dbConfig->getMountById($id); if (!is_array($existingMount)) { @@ -463,7 +448,7 @@ abstract class StoragesService { $this->triggerHooks($deletedStorage, Filesystem::signal_delete_mount); // delete oc_storages entries and oc_filecache - \OC\Files\Cache\Storage::cleanByMountId($id); + Storage::cleanByMountId($id); } /** diff --git a/apps/files_external/lib/Service/UserGlobalStoragesService.php b/apps/files_external/lib/Service/UserGlobalStoragesService.php index 58590b8d682..aaa59c85d62 100644 --- a/apps/files_external/lib/Service/UserGlobalStoragesService.php +++ b/apps/files_external/lib/Service/UserGlobalStoragesService.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -20,9 +21,6 @@ use OCP\IUserSession; class UserGlobalStoragesService extends GlobalStoragesService { use UserTrait; - /** @var IGroupManager */ - protected $groupManager; - /** * @param BackendService $backendService * @param DBConfigService $dbConfig @@ -35,13 +33,12 @@ class UserGlobalStoragesService extends GlobalStoragesService { BackendService $backendService, DBConfigService $dbConfig, IUserSession $userSession, - IGroupManager $groupManager, + protected IGroupManager $groupManager, IUserMountCache $userMountCache, - IEventDispatcher $eventDispatcher + IEventDispatcher $eventDispatcher, ) { parent::__construct($backendService, $dbConfig, $userMountCache, $eventDispatcher); $this->userSession = $userSession; - $this->groupManager = $groupManager; } /** diff --git a/apps/files_external/lib/Service/UserStoragesService.php b/apps/files_external/lib/Service/UserStoragesService.php index ba678156368..9d4192734b6 100644 --- a/apps/files_external/lib/Service/UserStoragesService.php +++ b/apps/files_external/lib/Service/UserStoragesService.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2018-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -8,6 +9,7 @@ namespace OCA\Files_External\Service; use OC\Files\Filesystem; use OCA\Files_External\Lib\StorageConfig; +use OCA\Files_External\MountConfig; use OCA\Files_External\NotFoundException; use OCP\EventDispatcher\IEventDispatcher; use OCP\Files\Config\IUserMountCache; @@ -34,7 +36,7 @@ class UserStoragesService extends StoragesService { DBConfigService $dbConfig, IUserSession $userSession, IUserMountCache $userMountCache, - IEventDispatcher $eventDispatcher + IEventDispatcher $eventDispatcher, ) { $this->userSession = $userSession; parent::__construct($backendService, $dbConfig, $userMountCache, $eventDispatcher); @@ -58,7 +60,7 @@ class UserStoragesService extends StoragesService { $this->triggerApplicableHooks( $signal, $storage->getMountPoint(), - \OCA\Files_External\MountConfig::MOUNT_TYPE_USER, + MountConfig::MOUNT_TYPE_USER, [$user] ); } diff --git a/apps/files_external/lib/Service/UserTrait.php b/apps/files_external/lib/Service/UserTrait.php index 25713894e1f..679066283a5 100644 --- a/apps/files_external/lib/Service/UserTrait.php +++ b/apps/files_external/lib/Service/UserTrait.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. |