From ab275c5e38de80b6cbc1a60890d8076f5075cace Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Wed, 4 Aug 2021 18:59:47 +0200 Subject: [PATCH 1/5] move files_sharing to IBootStrap Signed-off-by: Robin Appelman --- apps/files_sharing/appinfo/app.php | 34 -------- .../files_sharing/lib/AppInfo/Application.php | 84 +++++++------------ apps/files_sharing/lib/External/Manager.php | 31 +++---- 3 files changed, 47 insertions(+), 102 deletions(-) delete mode 100644 apps/files_sharing/appinfo/app.php diff --git a/apps/files_sharing/appinfo/app.php b/apps/files_sharing/appinfo/app.php deleted file mode 100644 index 79be3732773d9..0000000000000 --- a/apps/files_sharing/appinfo/app.php +++ /dev/null @@ -1,34 +0,0 @@ - - * @author John Molakvoæ - * @author Morris Jobke - * @author Robin Appelman - * - * @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 - * - */ -use OCA\Files_Sharing\ShareBackend\File; -use OCA\Files_Sharing\ShareBackend\Folder; -use OCA\Files_Sharing\AppInfo\Application; - -\OCA\Files_Sharing\Helper::registerHooks(); - -\OC\Share\Share::registerBackend('file', File::class); -\OC\Share\Share::registerBackend('folder', Folder::class, 'file'); - -\OC::$server->query(Application::class); diff --git a/apps/files_sharing/lib/AppInfo/Application.php b/apps/files_sharing/lib/AppInfo/Application.php index 2dfbe4d86a591..9d2ddec544a35 100644 --- a/apps/files_sharing/lib/AppInfo/Application.php +++ b/apps/files_sharing/lib/AppInfo/Application.php @@ -29,10 +29,11 @@ */ namespace OCA\Files_Sharing\AppInfo; -use OC\AppFramework\Utility\SimpleContainer; +use OC\Share\Share; use OCA\Files_Sharing\Capabilities; use OCA\Files_Sharing\Event\BeforeTemplateRenderedEvent; use OCA\Files_Sharing\External\Manager; +use OCA\Files_Sharing\Helper; use OCA\Files_Sharing\Listener\LegacyBeforeTemplateRenderedListener; use OCA\Files_Sharing\Listener\LoadAdditionalListener; use OCA\Files_Sharing\Listener\LoadSidebarListener; @@ -47,71 +48,35 @@ use OCA\Files_Sharing\Notification\Notifier; use OCA\Files\Event\LoadAdditionalScriptsEvent; use OCA\Files\Event\LoadSidebar; +use OCA\Files_Sharing\ShareBackend\File; +use OCA\Files_Sharing\ShareBackend\Folder; use OCP\AppFramework\App; +use OCP\AppFramework\Bootstrap\IBootContext; +use OCP\AppFramework\Bootstrap\IBootstrap; +use OCP\AppFramework\Bootstrap\IRegistrationContext; use OCP\EventDispatcher\IEventDispatcher; use OCP\Federation\ICloudIdManager; use OCP\Files\Config\IMountProviderCollection; use OCP\Group\Events\UserAddedEvent; use OCP\IDBConnection; use OCP\IGroup; -use OCP\IServerContainer; use OCP\IUserSession; use OCP\Share\Events\ShareCreatedEvent; use OCP\Share\IManager; use OCP\Util; use Psr\Container\ContainerInterface; -use Psr\Log\LoggerInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\GenericEvent; -class Application extends App { +class Application extends App implements IBootstrap { public const APP_ID = 'files_sharing'; public function __construct(array $urlParams = []) { parent::__construct(self::APP_ID, $urlParams); + } - $container = $this->getContainer(); - - /** @var IServerContainer $server */ - $server = $container->getServer(); - - /** @var IEventDispatcher $dispatcher */ - $dispatcher = $container->query(IEventDispatcher::class); - $oldDispatcher = $container->getServer()->getEventDispatcher(); - $mountProviderCollection = $server->getMountProviderCollection(); - $notifications = $server->getNotificationManager(); - - /** - * Core class wrappers - */ - $container->registerService(Manager::class, function (SimpleContainer $c) use ($server) { - $user = $server->getUserSession()->getUser(); - $uid = $user ? $user->getUID() : null; - return new \OCA\Files_Sharing\External\Manager( - $server->getDatabaseConnection(), - \OC\Files\Filesystem::getMountManager(), - \OC\Files\Filesystem::getLoader(), - $server->getHTTPClientService(), - $server->getNotificationManager(), - $server->query(\OCP\OCS\IDiscoveryService::class), - $server->getCloudFederationProviderManager(), - $server->getCloudFederationFactory(), - $server->getGroupManager(), - $server->getUserManager(), - $uid, - $server->query(IEventDispatcher::class), - $server->get(LoggerInterface::class) - ); - }); - - /** - * Middleware - */ - $container->registerMiddleWare(SharingCheckMiddleware::class); - $container->registerMiddleWare(OCSShareAPIMiddleware::class); - $container->registerMiddleWare(ShareInfoMiddleware::class); - - $container->registerService('ExternalMountProvider', function (ContainerInterface $c) { + public function register(IRegistrationContext $context): void { + $context->registerService('ExternalMountProvider', function (ContainerInterface $c) { return new \OCA\Files_Sharing\External\MountProvider( $c->get(IDBConnection::class), function () use ($c) { @@ -122,15 +87,25 @@ function () use ($c) { }); /** - * Register capabilities + * Middleware */ - $container->registerCapability(Capabilities::class); + $context->registerMiddleWare(SharingCheckMiddleware::class); + $context->registerMiddleWare(OCSShareAPIMiddleware::class); + $context->registerMiddleWare(ShareInfoMiddleware::class); - $notifications->registerNotifierService(Notifier::class); + $context->registerCapability(Capabilities::class); - $this->registerMountProviders($mountProviderCollection); - $this->registerEventsScripts($dispatcher, $oldDispatcher); - $this->setupSharingMenus(); + $context->registerNotifierService(Notifier::class); + } + + public function boot(IBootContext $context): void { + $context->injectFn([$this, 'registerMountProviders']); + $context->injectFn([$this, 'registerEventsScripts']); + + Helper::registerHooks(); + + Share::registerBackend('file', File::class); + Share::registerBackend('folder', Folder::class, 'file'); /** * Always add main sharing script @@ -138,12 +113,13 @@ function () use ($c) { Util::addScript(self::APP_ID, 'dist/main'); } - protected function registerMountProviders(IMountProviderCollection $mountProviderCollection) { + + public function registerMountProviders(IMountProviderCollection $mountProviderCollection) { $mountProviderCollection->registerProvider($this->getContainer()->query(MountProvider::class)); $mountProviderCollection->registerProvider($this->getContainer()->query('ExternalMountProvider')); } - protected function registerEventsScripts(IEventDispatcher $dispatcher, EventDispatcherInterface $oldDispatcher) { + public function registerEventsScripts(IEventDispatcher $dispatcher, EventDispatcherInterface $oldDispatcher) { // sidebar and files scripts $dispatcher->addServiceListener(LoadAdditionalScriptsEvent::class, LoadAdditionalListener::class); $dispatcher->addServiceListener(BeforeTemplateRenderedEvent::class, LegacyBeforeTemplateRenderedListener::class); diff --git a/apps/files_sharing/lib/External/Manager.php b/apps/files_sharing/lib/External/Manager.php index d967f40cc3223..a48e2a63ae451 100644 --- a/apps/files_sharing/lib/External/Manager.php +++ b/apps/files_sharing/lib/External/Manager.php @@ -31,6 +31,7 @@ * along with this program. If not, see * */ + namespace OCA\Files_Sharing\External; use Doctrine\DBAL\Driver\Exception; @@ -47,6 +48,7 @@ use OCP\IDBConnection; use OCP\IGroupManager; use OCP\IUserManager; +use OCP\IUserSession; use OCP\Notification\IManager; use OCP\OCS\IDiscoveryService; use OCP\Share; @@ -83,7 +85,7 @@ class Manager { /** @var ICloudFederationFactory */ private $cloudFederationFactory; - /** @var IGroupManager */ + /** @var IGroupManager */ private $groupManager; /** @var IUserManager */ @@ -96,25 +98,26 @@ class Manager { private $logger; public function __construct( - IDBConnection $connection, - \OC\Files\Mount\Manager $mountManager, - IStorageFactory $storageLoader, - IClientService $clientService, - IManager $notificationManager, - IDiscoveryService $discoveryService, + IDBConnection $connection, + \OC\Files\Mount\Manager $mountManager, + IStorageFactory $storageLoader, + IClientService $clientService, + IManager $notificationManager, + IDiscoveryService $discoveryService, ICloudFederationProviderManager $cloudFederationProviderManager, - ICloudFederationFactory $cloudFederationFactory, - IGroupManager $groupManager, - IUserManager $userManager, - ?string $uid, - IEventDispatcher $eventDispatcher, - LoggerInterface $logger + ICloudFederationFactory $cloudFederationFactory, + IGroupManager $groupManager, + IUserManager $userManager, + IUserSession $userSession, + IEventDispatcher $eventDispatcher, + LoggerInterface $logger ) { + $user = $userSession->getUser(); $this->connection = $connection; $this->mountManager = $mountManager; $this->storageLoader = $storageLoader; $this->clientService = $clientService; - $this->uid = $uid; + $this->uid = $user ? $user->getUID() : null; $this->notificationManager = $notificationManager; $this->discoveryService = $discoveryService; $this->cloudFederationProviderManager = $cloudFederationProviderManager; From 4b82d8d20c5222d0f36b792f3a252c81cbacbb59 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Mon, 9 Aug 2021 16:06:23 +0200 Subject: [PATCH 2/5] call setupSharingMenus again Signed-off-by: Robin Appelman --- .../files_sharing/lib/AppInfo/Application.php | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/apps/files_sharing/lib/AppInfo/Application.php b/apps/files_sharing/lib/AppInfo/Application.php index 9d2ddec544a35..7a432650f1222 100644 --- a/apps/files_sharing/lib/AppInfo/Application.php +++ b/apps/files_sharing/lib/AppInfo/Application.php @@ -33,6 +33,7 @@ use OCA\Files_Sharing\Capabilities; use OCA\Files_Sharing\Event\BeforeTemplateRenderedEvent; use OCA\Files_Sharing\External\Manager; +use OCA\Files_Sharing\External\MountProvider as ExternalMountProvider; use OCA\Files_Sharing\Helper; use OCA\Files_Sharing\Listener\LegacyBeforeTemplateRenderedListener; use OCA\Files_Sharing\Listener\LoadAdditionalListener; @@ -60,7 +61,9 @@ use OCP\Group\Events\UserAddedEvent; use OCP\IDBConnection; use OCP\IGroup; +use OCP\INavigationManager; use OCP\IUserSession; +use OCP\L10N\IFactory; use OCP\Share\Events\ShareCreatedEvent; use OCP\Share\IManager; use OCP\Util; @@ -76,8 +79,8 @@ public function __construct(array $urlParams = []) { } public function register(IRegistrationContext $context): void { - $context->registerService('ExternalMountProvider', function (ContainerInterface $c) { - return new \OCA\Files_Sharing\External\MountProvider( + $context->registerService(ExternalMountProvider::class, function (ContainerInterface $c) { + return new ExternalMountProvider( $c->get(IDBConnection::class), function () use ($c) { return $c->get(Manager::class); @@ -101,6 +104,7 @@ function () use ($c) { public function boot(IBootContext $context): void { $context->injectFn([$this, 'registerMountProviders']); $context->injectFn([$this, 'registerEventsScripts']); + $context->injectFn([$this, 'setupSharingMenus']); Helper::registerHooks(); @@ -114,9 +118,9 @@ public function boot(IBootContext $context): void { } - public function registerMountProviders(IMountProviderCollection $mountProviderCollection) { - $mountProviderCollection->registerProvider($this->getContainer()->query(MountProvider::class)); - $mountProviderCollection->registerProvider($this->getContainer()->query('ExternalMountProvider')); + public function registerMountProviders(IMountProviderCollection $mountProviderCollection, MountProvider $mountProvider, ExternalMountProvider $externalMountProvider) { + $mountProviderCollection->registerProvider($mountProvider); + $mountProviderCollection->registerProvider($externalMountProvider); } public function registerEventsScripts(IEventDispatcher $dispatcher, EventDispatcherInterface $oldDispatcher) { @@ -144,19 +148,14 @@ public function registerEventsScripts(IEventDispatcher $dispatcher, EventDispatc }); } - protected function setupSharingMenus() { - /** @var IManager $shareManager */ - $shareManager = \OC::$server->get(IManager::class); - + public function setupSharingMenus(INavigationManager $navigationManager, IManager $shareManager, IFactory $l10nFactory, IUserSession $userSession) { if (!$shareManager->shareApiEnabled() || !class_exists('\OCA\Files\App')) { return; } // show_Quick_Access stored as string - \OCA\Files\App::getNavigationManager()->add(function () use ($shareManager) { - $l = \OC::$server->getL10N('files_sharing'); - /** @var IUserSession $userSession */ - $userSession = \OC::$server->get(IUserSession::class); + $navigationManager->add(function () use ($shareManager, $l10nFactory, $userSession) { + $l = $l10nFactory->get('files_sharing'); $user = $userSession->getUser(); $userId = $user ? $user->getUID() : null; From 7345de78c582ffcb01c56e2a0c38788eb913d414 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Wed, 11 Aug 2021 17:24:37 +0200 Subject: [PATCH 3/5] use the correct navigation manager for the shares Signed-off-by: Robin Appelman --- apps/files_sharing/lib/AppInfo/Application.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/files_sharing/lib/AppInfo/Application.php b/apps/files_sharing/lib/AppInfo/Application.php index 7a432650f1222..056d246296f91 100644 --- a/apps/files_sharing/lib/AppInfo/Application.php +++ b/apps/files_sharing/lib/AppInfo/Application.php @@ -61,7 +61,6 @@ use OCP\Group\Events\UserAddedEvent; use OCP\IDBConnection; use OCP\IGroup; -use OCP\INavigationManager; use OCP\IUserSession; use OCP\L10N\IFactory; use OCP\Share\Events\ShareCreatedEvent; @@ -148,11 +147,12 @@ public function registerEventsScripts(IEventDispatcher $dispatcher, EventDispatc }); } - public function setupSharingMenus(INavigationManager $navigationManager, IManager $shareManager, IFactory $l10nFactory, IUserSession $userSession) { + public function setupSharingMenus(IManager $shareManager, IFactory $l10nFactory, IUserSession $userSession) { if (!$shareManager->shareApiEnabled() || !class_exists('\OCA\Files\App')) { return; } + $navigationManager = \OCA\Files\App::getNavigationManager(); // show_Quick_Access stored as string $navigationManager->add(function () use ($shareManager, $l10nFactory, $userSession) { $l = $l10nFactory->get('files_sharing'); From ef93d2ea1c089d65660591857e5f6a1608210d3c Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Thu, 12 Aug 2021 13:31:29 +0200 Subject: [PATCH 4/5] update tests Signed-off-by: Robin Appelman --- apps/files_sharing/lib/Hooks.php | 18 +----------------- .../tests/External/ManagerTest.php | 11 ++++++++++- apps/files_sharing/tests/TestCase.php | 10 +++++++++- 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/apps/files_sharing/lib/Hooks.php b/apps/files_sharing/lib/Hooks.php index f28f6910abd6a..1c93d913eaf6b 100644 --- a/apps/files_sharing/lib/Hooks.php +++ b/apps/files_sharing/lib/Hooks.php @@ -27,26 +27,10 @@ namespace OCA\Files_Sharing; use OC\Files\Filesystem; -use OCP\EventDispatcher\IEventDispatcher; -use Psr\Log\LoggerInterface; class Hooks { public static function deleteUser($params) { - $manager = new External\Manager( - \OC::$server->getDatabaseConnection(), - \OC\Files\Filesystem::getMountManager(), - \OC\Files\Filesystem::getLoader(), - \OC::$server->getHTTPClientService(), - \OC::$server->getNotificationManager(), - \OC::$server->query(\OCP\OCS\IDiscoveryService::class), - \OC::$server->getCloudFederationProviderManager(), - \OC::$server->getCloudFederationFactory(), - \OC::$server->getGroupManager(), - \OC::$server->getUserManager(), - $params['uid'], - \OC::$server->query(IEventDispatcher::class), - \OC::$server->get(LoggerInterface::class) - ); + $manager = \OC::$server->get(External\Manager::class); $manager->removeUserShares($params['uid']); } diff --git a/apps/files_sharing/tests/External/ManagerTest.php b/apps/files_sharing/tests/External/ManagerTest.php index 335425b7a122b..ab7c682c3a668 100644 --- a/apps/files_sharing/tests/External/ManagerTest.php +++ b/apps/files_sharing/tests/External/ManagerTest.php @@ -44,7 +44,9 @@ use OCP\IGroup; use OCP\IGroupManager; use OCP\IURLGenerator; +use OCP\IUser; use OCP\IUserManager; +use OCP\IUserSession; use OCP\Share\IShare; use Psr\Log\LoggerInterface; use Test\Traits\UserTrait; @@ -153,6 +155,13 @@ protected function tearDown(): void { } private function createManagerForUser($userId) { + $user = $this->createMock(IUser::class); + $user->method('getUID') + ->willReturn($userId); + $userSession = $this->createMock(IUserSession::class); + $userSession->method('getUser') + ->willReturn($user); + return $this->getMockBuilder(Manager::class) ->setConstructorArgs( [ @@ -166,7 +175,7 @@ private function createManagerForUser($userId) { $this->cloudFederationFactory, $this->groupManager, $this->userManager, - $userId, + $userSession, $this->eventDispatcher, $this->logger, ] diff --git a/apps/files_sharing/tests/TestCase.php b/apps/files_sharing/tests/TestCase.php index 54c8704df81e8..bb1e3125ab2c9 100644 --- a/apps/files_sharing/tests/TestCase.php +++ b/apps/files_sharing/tests/TestCase.php @@ -34,6 +34,9 @@ use OC\Files\Filesystem; use OCA\Files_Sharing\AppInfo\Application; +use OCA\Files_Sharing\External\MountProvider as ExternalMountProvider; +use OCA\Files_Sharing\MountProvider; +use OCP\Files\Config\IMountProviderCollection; use OCP\Share\IShare; use Test\Traits\MountProviderTrait; @@ -71,7 +74,12 @@ abstract class TestCase extends \Test\TestCase { public static function setUpBeforeClass(): void { parent::setUpBeforeClass(); - new Application(); + $app = new Application(); + $app->registerMountProviders( + \OC::$server->get(IMountProviderCollection::class), + \OC::$server->get(MountProvider::class), + \OC::$server->get(ExternalMountProvider::class), + ); // reset backend \OC_User::clearBackends(); From 033a83b0ace2dd12be7329c458a93e89da0ca207 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Fri, 13 Aug 2021 17:02:27 +0200 Subject: [PATCH 5/5] fix federated sharing Signed-off-by: Robin Appelman --- .../lib/OCM/CloudFederationProviderFiles.php | 59 ++++++++----------- 1 file changed, 25 insertions(+), 34 deletions(-) diff --git a/apps/federatedfilesharing/lib/OCM/CloudFederationProviderFiles.php b/apps/federatedfilesharing/lib/OCM/CloudFederationProviderFiles.php index 916a8538c6e20..370ef8dc32a56 100644 --- a/apps/federatedfilesharing/lib/OCM/CloudFederationProviderFiles.php +++ b/apps/federatedfilesharing/lib/OCM/CloudFederationProviderFiles.php @@ -33,10 +33,10 @@ use OCA\FederatedFileSharing\AddressHandler; use OCA\FederatedFileSharing\FederatedShareProvider; use OCA\Files_Sharing\Activity\Providers\RemoteShares; +use OCA\Files_Sharing\External\Manager; use OCP\Activity\IManager as IActivityManager; use OCP\App\IAppManager; use OCP\Constants; -use OCP\EventDispatcher\IEventDispatcher; use OCP\Federation\Exceptions\ActionNotSupportedException; use OCP\Federation\Exceptions\AuthenticationFailedException; use OCP\Federation\Exceptions\BadRequestException; @@ -59,7 +59,6 @@ use OCP\Share\IManager; use OCP\Share\IShare; use OCP\Util; -use Psr\Log\LoggerInterface; class CloudFederationProviderFiles implements ICloudFederationProvider { @@ -108,6 +107,9 @@ class CloudFederationProviderFiles implements ICloudFederationProvider { /** @var IConfig */ private $config; + /** @var Manager */ + private $externalShareManager; + /** * CloudFederationProvider constructor. * @@ -125,22 +127,26 @@ class CloudFederationProviderFiles implements ICloudFederationProvider { * @param ICloudFederationProviderManager $cloudFederationProviderManager * @param IDBConnection $connection * @param IGroupManager $groupManager + * @param IConfig $config + * @param Manager $externalShareManager */ - public function __construct(IAppManager $appManager, - FederatedShareProvider $federatedShareProvider, - AddressHandler $addressHandler, - ILogger $logger, - IUserManager $userManager, - IManager $shareManager, - ICloudIdManager $cloudIdManager, - IActivityManager $activityManager, - INotificationManager $notificationManager, - IURLGenerator $urlGenerator, - ICloudFederationFactory $cloudFederationFactory, - ICloudFederationProviderManager $cloudFederationProviderManager, - IDBConnection $connection, - IGroupManager $groupManager, - IConfig $config + public function __construct( + IAppManager $appManager, + FederatedShareProvider $federatedShareProvider, + AddressHandler $addressHandler, + ILogger $logger, + IUserManager $userManager, + IManager $shareManager, + ICloudIdManager $cloudIdManager, + IActivityManager $activityManager, + INotificationManager $notificationManager, + IURLGenerator $urlGenerator, + ICloudFederationFactory $cloudFederationFactory, + ICloudFederationProviderManager $cloudFederationProviderManager, + IDBConnection $connection, + IGroupManager $groupManager, + IConfig $config, + Manager $externalShareManager ) { $this->appManager = $appManager; $this->federatedShareProvider = $federatedShareProvider; @@ -157,6 +163,7 @@ public function __construct(IAppManager $appManager, $this->connection = $connection; $this->groupManager = $groupManager; $this->config = $config; + $this->externalShareManager = $externalShareManager; } @@ -239,24 +246,8 @@ public function shareReceived(ICloudFederationShare $share) { throw new ProviderCouldNotAddShareException('Group does not exists', '',Http::STATUS_BAD_REQUEST); } - $externalManager = new \OCA\Files_Sharing\External\Manager( - \OC::$server->getDatabaseConnection(), - Filesystem::getMountManager(), - Filesystem::getLoader(), - \OC::$server->getHTTPClientService(), - \OC::$server->getNotificationManager(), - \OC::$server->query(\OCP\OCS\IDiscoveryService::class), - \OC::$server->getCloudFederationProviderManager(), - \OC::$server->getCloudFederationFactory(), - \OC::$server->getGroupManager(), - \OC::$server->getUserManager(), - $shareWith, - \OC::$server->query(IEventDispatcher::class), - \OC::$server->get(LoggerInterface::class) - ); - try { - $externalManager->addShare($remote, $token, '', $name, $owner, $shareType,false, $shareWith, $remoteId); + $this->externalShareManager->addShare($remote, $token, '', $name, $owner, $shareType,false, $shareWith, $remoteId); $shareId = \OC::$server->getDatabaseConnection()->lastInsertId('*PREFIX*share_external'); if ($shareType === IShare::TYPE_USER) {