diff --git a/apps/files_sharing/appinfo/application.php b/apps/files_sharing/appinfo/application.php index 9587d74f16..9dc0e0618b 100644 --- a/apps/files_sharing/appinfo/application.php +++ b/apps/files_sharing/appinfo/application.php @@ -120,6 +120,17 @@ class Application extends App { ); }); + $container->registerService('ExternalMountProvider', function (IContainer $c) { + /** @var \OCP\IServerContainer $server */ + $server = $c->query('ServerContainer'); + return new \OCA\Files_Sharing\External\MountProvider( + $server->getDatabaseConnection(), + function() use ($c) { + return $c->query('ExternalManager'); + } + ); + }); + $container->registerService('PropagationManager', function (IContainer $c) { /** @var \OCP\IServerContainer $server */ $server = $c->query('ServerContainer'); @@ -150,6 +161,7 @@ class Application extends App { $server = $this->getContainer()->query('ServerContainer'); $mountProviderCollection = $server->getMountProviderCollection(); $mountProviderCollection->registerProvider($this->getContainer()->query('MountProvider')); + $mountProviderCollection->registerProvider($this->getContainer()->query('ExternalMountProvider')); } public function setupPropagation() { diff --git a/apps/files_sharing/lib/external/manager.php b/apps/files_sharing/lib/external/manager.php index ab6a02f94a..86b8904cc9 100644 --- a/apps/files_sharing/lib/external/manager.php +++ b/apps/files_sharing/lib/external/manager.php @@ -153,28 +153,6 @@ class Manager { return $this->mountShare($options); } - private function setupMounts() { - // don't setup server-to-server shares if the admin disabled it - if (\OCA\Files_Sharing\Helper::isIncomingServer2serverShareEnabled() === false) { - return false; - } - - if (!is_null($this->uid)) { - $query = $this->connection->prepare(' - SELECT `remote`, `share_token`, `password`, `mountpoint`, `owner` - FROM `*PREFIX*share_external` - WHERE `user` = ? AND `accepted` = ? - '); - $query->execute(array($this->uid, 1)); - - while ($row = $query->fetch()) { - $row['manager'] = $this; - $row['token'] = $row['share_token']; - $this->mountShare($row); - } - } - } - /** * get share * @@ -276,24 +254,6 @@ class Manager { return ($result['success'] && $status['ocs']['meta']['statuscode'] === 100); } - /** - * setup the server-to-server mounts - * - * @param array $params - */ - public static function setup(array $params) { - $externalManager = new \OCA\Files_Sharing\External\Manager( - \OC::$server->getDatabaseConnection(), - \OC\Files\Filesystem::getMountManager(), - \OC\Files\Filesystem::getLoader(), - \OC::$server->getHTTPHelper(), - \OC::$server->getNotificationManager(), - $params['user'] - ); - - $externalManager->setupMounts(); - } - /** * remove '/user/files' from the path and trailing slashes * @@ -305,16 +265,20 @@ class Manager { return rtrim(substr($path, strlen($prefix)), '/'); } + public function getMount($data) { + $data['manager'] = $this; + $mountPoint = '/' . $this->uid . '/files' . $data['mountpoint']; + $data['mountpoint'] = $mountPoint; + $data['certificateManager'] = \OC::$server->getCertificateManager($this->uid); + return new Mount(self::STORAGE, $mountPoint, $data, $this, $this->storageLoader); + } + /** * @param array $data * @return Mount */ protected function mountShare($data) { - $data['manager'] = $this; - $mountPoint = '/' . $this->uid . '/files' . $data['mountpoint']; - $data['mountpoint'] = $mountPoint; - $data['certificateManager'] = \OC::$server->getCertificateManager($this->uid); - $mount = new Mount(self::STORAGE, $mountPoint, $data, $this, $this->storageLoader); + $mount = $this->getMount($data); $this->mountManager->addMount($mount); return $mount; } diff --git a/apps/files_sharing/lib/external/mountprovider.php b/apps/files_sharing/lib/external/mountprovider.php new file mode 100644 index 0000000000..1739cec543 --- /dev/null +++ b/apps/files_sharing/lib/external/mountprovider.php @@ -0,0 +1,75 @@ + + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @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 + * + */ + +namespace OCA\Files_Sharing\External; + +use OCP\Files\Config\IMountProvider; +use OCP\Files\Storage\IStorageFactory; +use OCP\IDBConnection; +use OCP\IUser; + +class MountProvider implements IMountProvider { + const STORAGE = '\OCA\Files_Sharing\External\Storage'; + + /** + * @var \OCP\IDBConnection + */ + private $connection; + + /** + * @var callable + */ + private $managerProvider; + + /** + * @param \OCP\IDBConnection $connection + * @param callable $managerProvider due to setup order we need a callable that return the manager instead of the manager itself + */ + public function __construct(IDBConnection $connection, callable $managerProvider) { + $this->connection = $connection; + $this->managerProvider = $managerProvider; + } + + public function getMount(IUser $user, $data, IStorageFactory $storageFactory) { + $data['manager'] = $this; + $mountPoint = '/' . $user->getUID() . '/files/' . ltrim($data['mountpoint'], '/'); + $data['mountpoint'] = $mountPoint; + $data['certificateManager'] = \OC::$server->getCertificateManager($user->getUID()); + $managerProvider = $this->managerProvider; + return new Mount(self::STORAGE, $mountPoint, $data, $managerProvider(), $storageFactory); + } + + public function getMountsForUser(IUser $user, IStorageFactory $loader) { + $query = $this->connection->prepare(' + SELECT `remote`, `share_token`, `password`, `mountpoint`, `owner` + FROM `*PREFIX*share_external` + WHERE `user` = ? AND `accepted` = ? + '); + $query->execute([$user->getUID(), 1]); + $mounts = []; + while ($row = $query->fetch()) { + $row['manager'] = $this; + $row['token'] = $row['share_token']; + $mounts[] = $this->getMount($user, $row, $loader); + } + return $mounts; + } +} diff --git a/apps/files_sharing/lib/helper.php b/apps/files_sharing/lib/helper.php index 63648b9f06..b15f70fcde 100644 --- a/apps/files_sharing/lib/helper.php +++ b/apps/files_sharing/lib/helper.php @@ -31,7 +31,6 @@ namespace OCA\Files_Sharing; class Helper { public static function registerHooks() { - \OCP\Util::connectHook('OC_Filesystem', 'setup', '\OCA\Files_Sharing\External\Manager', 'setup'); \OCP\Util::connectHook('OC_Filesystem', 'delete', '\OC\Files\Cache\Shared_Updater', 'deleteHook'); \OCP\Util::connectHook('OC_Filesystem', 'post_rename', '\OC\Files\Cache\Shared_Updater', 'renameHook'); \OCP\Util::connectHook('OC_Filesystem', 'post_delete', '\OCA\Files_Sharing\Hooks', 'unshareChildren'); diff --git a/apps/files_sharing/lib/sharedstorage.php b/apps/files_sharing/lib/sharedstorage.php index 27dd2f1e48..b0e56f5f05 100644 --- a/apps/files_sharing/lib/sharedstorage.php +++ b/apps/files_sharing/lib/sharedstorage.php @@ -583,9 +583,6 @@ class Shared extends \OC\Files\Storage\Common implements ISharedStorage { } public function getETag($path) { - if ($path == '') { - $path = $this->getMountPoint(); - } if ($source = $this->getSourcePath($path)) { list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source); return $storage->getETag($internalPath); diff --git a/apps/files_sharing/tests/external/managertest.php b/apps/files_sharing/tests/external/managertest.php index 8e03c67a9a..7242779d45 100644 --- a/apps/files_sharing/tests/external/managertest.php +++ b/apps/files_sharing/tests/external/managertest.php @@ -23,9 +23,12 @@ namespace OCA\Files_Sharing\Tests\External; use OC\Files\Storage\StorageFactory; use OCA\Files_Sharing\External\Manager; +use OCA\Files_Sharing\External\MountProvider; use OCA\Files_Sharing\Tests\TestCase; +use Test\Traits\UserTrait; class ManagerTest extends TestCase { + use UserTrait; /** @var Manager **/ private $manager; @@ -38,10 +41,18 @@ class ManagerTest extends TestCase { private $uid; + /** + * @var \OCP\IUser + */ + private $user; + private $mountProvider; + protected function setUp() { parent::setUp(); $this->uid = $this->getUniqueID('user'); + $this->createUser($this->uid, ''); + $this->user = \OC::$server->getUserManager()->get($this->uid); $this->mountManager = new \OC\Files\Mount\Manager(); $this->httpHelper = $httpHelper = $this->getMockBuilder('\OC\HTTPHelper')->disableOriginalConstructor()->getMock(); /** @var \OC\HTTPHelper $httpHelper */ @@ -53,6 +64,16 @@ class ManagerTest extends TestCase { \OC::$server->getNotificationManager(), $this->uid ); + $this->mountProvider = new MountProvider(\OC::$server->getDatabaseConnection(), function() { + return $this->manager; + }); + } + + private function setupMounts() { + $mounts = $this->mountProvider->getMountsForUser($this->user, new StorageFactory()); + foreach ($mounts as $mount) { + $this->mountManager->addMount($mount); + } } public function testAddShare() { @@ -77,7 +98,7 @@ class ManagerTest extends TestCase { $this->assertCount(1, $openShares); $this->assertExternalShareEntry($shareData1, $openShares[0], 1, '{{TemporaryMountPointName#' . $shareData1['name'] . '}}'); - self::invokePrivate($this->manager, 'setupMounts'); + $this->setupMounts(); $this->assertNotMount('SharedFolder'); $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}'); @@ -89,7 +110,7 @@ class ManagerTest extends TestCase { // New share falls back to "-1" appendix, because the name is already taken $this->assertExternalShareEntry($shareData2, $openShares[1], 2, '{{TemporaryMountPointName#' . $shareData2['name'] . '}}-1'); - self::invokePrivate($this->manager, 'setupMounts'); + $this->setupMounts(); $this->assertNotMount('SharedFolder'); $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}'); $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1'); @@ -111,7 +132,7 @@ class ManagerTest extends TestCase { $this->assertCount(1, $openShares); $this->assertExternalShareEntry($shareData2, $openShares[0], 2, '{{TemporaryMountPointName#' . $shareData2['name'] . '}}-1'); - self::invokePrivate($this->manager, 'setupMounts'); + $this->setupMounts(); $this->assertMount($shareData1['name']); $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}'); $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1'); @@ -124,7 +145,7 @@ class ManagerTest extends TestCase { // New share falls back to the original name (no "-\d", because the name is not taken) $this->assertExternalShareEntry($shareData3, $openShares[1], 3, '{{TemporaryMountPointName#' . $shareData3['name'] . '}}'); - self::invokePrivate($this->manager, 'setupMounts'); + $this->setupMounts(); $this->assertMount($shareData1['name']); $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}'); $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1'); @@ -136,7 +157,7 @@ class ManagerTest extends TestCase { // Decline the third share $this->manager->declineShare($openShares[1]['id']); - self::invokePrivate($this->manager, 'setupMounts'); + $this->setupMounts(); $this->assertMount($shareData1['name']); $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}'); $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1'); @@ -151,7 +172,7 @@ class ManagerTest extends TestCase { $this->assertCount(1, $openShares); $this->assertExternalShareEntry($shareData2, $openShares[0], 2, '{{TemporaryMountPointName#' . $shareData2['name'] . '}}-1'); - self::invokePrivate($this->manager, 'setupMounts'); + $this->setupMounts(); $this->assertMount($shareData1['name']); $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}'); $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');