Make the external share manager a mount provider

This commit is contained in:
Robin Appelman 2015-10-05 16:18:14 +02:00
parent cab0be6eed
commit 76a528e511
3 changed files with 44 additions and 46 deletions

View File

@ -28,9 +28,12 @@ namespace OCA\Files_Sharing\External;
use OC\Files\Filesystem;
use OCP\Files;
use OCP\Files\Config\IMountProvider;
use OCP\Files\Storage\IStorageFactory;
use OC\Notification\IManager;
use OCP\IUser;
class Manager {
class Manager implements IMountProvider {
const STORAGE = '\OCA\Files_Sharing\External\Storage';
/**
@ -152,26 +155,20 @@ 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('
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(array($this->uid, 1));
while ($row = $query->fetch()) {
$row['manager'] = $this;
$row['token'] = $row['share_token'];
$this->mountShare($row);
}
$query->execute([$user->getUID(), 1]);
$mounts = [];
while ($row = $query->fetch()) {
$row['manager'] = $this;
$row['token'] = $row['share_token'];
$mounts[] = $this->getMount($row, $loader);
}
return $mounts;
}
/**
@ -275,24 +272,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
*
@ -304,16 +283,20 @@ class Manager {
return rtrim(substr($path, strlen($prefix)), '/');
}
protected function getMount($data, IStorageFactory $storageFactory) {
$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, $storageFactory);
}
/**
* @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->storageLoader);
$this->mountManager->addMount($mount);
return $mount;
}

View File

@ -30,7 +30,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');

View File

@ -24,8 +24,10 @@ namespace OCA\Files_Sharing\Tests\External;
use OC\Files\Storage\StorageFactory;
use OCA\Files_Sharing\External\Manager;
use OCA\Files_Sharing\Tests\TestCase;
use Test\Traits\UserTrait;
class ManagerTest extends TestCase {
use UserTrait;
/** @var Manager **/
private $manager;
@ -38,10 +40,17 @@ class ManagerTest extends TestCase {
private $uid;
/**
* @var \OCP\IUser
*/
private $user;
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 */
@ -55,6 +64,13 @@ class ManagerTest extends TestCase {
);
}
private function setupMounts() {
$mounts = $this->manager->getMountsForUser($this->user, new StorageFactory());
foreach ($mounts as $mount) {
$this->mountManager->addMount($mount);
}
}
public function testAddShare() {
$shareData1 = [
@ -77,7 +93,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 +105,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 +127,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 +140,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 +152,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 +167,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');