* @return Share20OCS
*/
private function getShare20OCS() {
- return new Share20OCS(
- new \OC\Share20\Manager(
- \OC::$server->getLogger(),
- \OC::$server->getConfig(),
- new \OC\Share20\DefaultShareProvider(
+ $manager =new \OC\Share20\Manager(
+ \OC::$server->getLogger(),
+ \OC::$server->getConfig(),
+ \OC::$server->getSecureRandom(),
+ \OC::$server->getHasher(),
+ \OC::$server->getMountManager(),
+ \OC::$server->getGroupManager(),
+ \OC::$server->getL10N('core')
+ );
+
+ $manager->registerProvider('ocdefault',
+ [
+ \OCP\Share::SHARE_TYPE_USER,
+ \OCP\SHARE::SHARE_TYPE_GROUP,
+ \OCP\SHARE::SHARE_TYPE_LINK
+ ],
+ function() {
+ return new \OC\Share20\DefaultShareProvider(
\OC::$server->getDatabaseConnection(),
\OC::$server->getUserManager(),
\OC::$server->getGroupManager(),
\OC::$server->getRootFolder()
- ),
- \OC::$server->getSecureRandom(),
- \OC::$server->getHasher(),
- \OC::$server->getMountManager(),
- \OC::$server->getGroupManager(),
- \OC::$server->getL10N('core')
- ),
+ );
+ }
+ );
+
+ return new Share20OCS(
+ $manager,
\OC::$server->getGroupManager(),
\OC::$server->getUserManager(),
\OC::$server->getRequest(),
namespace OC\Share20;
+use OC\Share20\Exception\ProviderException;
use OCP\IConfig;
use OCP\IL10N;
use OCP\ILogger;
*/
class Manager {
- /** @var IShareProvider[] */
- private $defaultProvider;
+ /** @var array */
+ private $providers;
+
+ /** @var array */
+ private $type2provider;
/** @var ILogger */
private $logger;
*
* @param ILogger $logger
* @param IConfig $config
- * @param IShareProvider $defaultProvider
* @param ISecureRandom $secureRandom
* @param IHasher $hasher
* @param IMountManager $mountManager
public function __construct(
ILogger $logger,
IConfig $config,
- IShareProvider $defaultProvider,
ISecureRandom $secureRandom,
IHasher $hasher,
IMountManager $mountManager,
IGroupManager $groupManager,
IL10N $l
) {
+ $this->providers = [];
+ $this->type2provider = [];
+
$this->logger = $logger;
$this->config = $config;
$this->secureRandom = $secureRandom;
$this->mountManager = $mountManager;
$this->groupManager = $groupManager;
$this->l = $l;
+ }
+
+ /**
+ * Register a share provider
+ *
+ * @param string $id The id of the share provider
+ * @param int[] $shareTypes Array containing the share types this provider handles
+ * @param callable $callback Callback that must return an IShareProvider instance
+ * @throws ProviderException
+ */
+ public function registerProvider($id, $shareTypes, callable $callback) {
+ // Providers must have an unique id
+ if (isset($this->providers[$id])) {
+ throw new ProviderException('A share provider with the id \''. $id . '\' is already registered');
+ }
+
+ if ($shareTypes === []) {
+ throw new ProviderException('shareTypes can\'t be an empty array');
+ }
+
+ foreach($shareTypes as $shareType) {
+ // We only have 1 provder per share type
+ if (isset($this->type2provider[$shareType])) {
+ throw new ProviderException('The share provider ' . $this->type2provider[$shareType] . ' is already registered for share type ' . $shareType);
+ }
+
+ /*
+ * We only allow providers that provider
+ * user- / group- / link- / federated-shares
+ */
+ if ($shareType !== \OCP\Share::SHARE_TYPE_USER &&
+ $shareType !== \OCP\Share::SHARE_TYPE_GROUP &&
+ $shareType !== \OCP\Share::SHARE_TYPE_LINK &&
+ $shareType !== \OCP\Share::SHARE_TYPE_REMOTE) {
+ throw new ProviderException('Cannot register provider for share type ' . $shareType);
+ //Throw exception
+ }
+ }
+
+ // Add the provider
+ $this->providers[$id] = [
+ 'id' => $id,
+ 'callback' => $callback,
+ 'provider' => null,
+ 'shareTypes' => $shareTypes,
+ ];
+
+ // Update the type mapping
+ foreach ($shareTypes as $shareType) {
+ $this->type2provider[$shareType] = $id;
+ }
+ }
+
+ /**
+ * @param string $id
+ * @return IShareProvider
+ * @throws ProviderException
+ */
+ private function getProvider($id) {
+ if (!isset($this->providers[$id])) {
+ throw new ProviderException('No provider with id ' . $id . ' found');
+ }
+
+ if ($this->providers[$id]['provider'] === null) {
+ // First time using this provider
+ $provider = call_user_func($this->providers[$id]['callback']);
+
+ // Make sure a proper provider is returned
+ if (!($provider instanceof IShareProvider)) {
+ throw new ProviderException('Callback does not return an IShareProvider instance for provider with id ' . $id);
+ }
+
+ $this->providers[$id]['provider'] = $provider;
+ }
- // TEMP SOLUTION JUST TO GET STARTED
- $this->defaultProvider = $defaultProvider;
+ return $this->providers[$id]['provider'];
+ }
+
+ /**
+ * @param int $shareType
+ * @return IShareProvider
+ * @throws ProviderException
+ */
+ private function getProviderForType($shareType) {
+ if (!isset($this->type2provider[$shareType])) {
+ throw new ProviderException('No share provider registered for share type ' . $shareType);
+ }
+
+ return $this->getProvider($this->type2provider[$shareType]);
}
/**
/**
- * Check for pre share requirements for use shares
+ * Check for pre share requirements for user shares
*
* @param IShare $share
* @throws \Exception
*
* Also this is not what we want in the future.. then we want to squash identical shares.
*/
- $existingShares = $this->defaultProvider->getSharesByPath($share->getPath());
+ $provider = $this->getProviderForType(\OCP\Share::SHARE_TYPE_USER);
+ $existingShares = $provider->getSharesByPath($share->getPath());
foreach($existingShares as $existingShare) {
// Identical share already existst
if ($existingShare->getSharedWith() === $share->getSharedWith()) {
*
* Also this is not what we want in the future.. then we want to squash identical shares.
*/
- $existingShares = $this->defaultProvider->getSharesByPath($share->getPath());
+ $provider = $this->getProviderForType(\OCP\Share::SHARE_TYPE_GROUP);
+ $existingShares = $provider->getSharesByPath($share->getPath());
foreach($existingShares as $existingShare) {
if ($existingShare->getSharedWith() === $share->getSharedWith()) {
throw new \Exception('Path already shared with this group');
throw new \Exception($error);
}
- $share = $this->defaultProvider->create($share);
+ $provider = $this->getProviderForType($share->getShareType());
+ $share = $provider->create($share);
// Post share hook
$postHookData = [
*/
protected function deleteChildren(IShare $share) {
$deletedShares = [];
- foreach($this->defaultProvider->getChildren($share) as $child) {
- $deletedChildren = $this->deleteChildren($child);
- $deletedShares = array_merge($deletedShares, $deletedChildren);
- $this->defaultProvider->delete($child);
- $deletedShares[] = $child;
+ $providerIds = array_keys($this->providers);
+
+ foreach($providerIds as $providerId) {
+ $provider = $this->getProvider($providerId);
+ foreach ($provider->getChildren($share) as $child) {
+ $deletedChildren = $this->deleteChildren($child);
+ $deletedShares = array_merge($deletedShares, $deletedChildren);
+
+ $provider->delete($child);
+ $deletedShares[] = $child;
+ }
}
return $deletedShares;
$deletedShares = $this->deleteChildren($share);
// Do the actual delete
- $this->defaultProvider->delete($share);
+ $provider = $this->getProviderForType($share->getShareType());
+ $provider->delete($share);
// All the deleted shares caused by this delete
$deletedShares[] = $share;
throw new ShareNotFound();
}
- $share = $this->defaultProvider->getShareById($id);
+ //FIXME ids need to become proper providerid:shareid eventually
+
+ $providerIds = array_keys($this->providers);
+
+ $share = null;
+ foreach ($providerIds as $providerId) {
+ $provider = $this->getProvider($providerId);
+
+ try {
+ $share = $provider->getShareById($id);
+ $found = true;
+ break;
+ } catch (ShareNotFound $e) {
+ // Ignore
+ }
+ }
+
+ if ($share === null) {
+ throw new ShareNotFound();
+ }
return $share;
}
$this->manager = new Manager(
$this->logger,
$this->config,
- $this->defaultProvider,
$this->secureRandom,
$this->hasher,
$this->mountManager,
$this->groupManager,
$this->l
);
+ $this->manager->registerProvider('tmp',
+ [
+ \OCP\SHARE::SHARE_TYPE_USER,
+ \OCP\Share::SHARE_TYPE_GROUP,
+ \OCP\Share::SHARE_TYPE_LINK
+ ],
+ function() {
+ return $this->defaultProvider;
+ }
+ );
}
/**
->setConstructorArgs([
$this->logger,
$this->config,
- $this->defaultProvider,
$this->secureRandom,
$this->hasher,
$this->mountManager,
->setMethods(['getShareById', 'deleteChildren'])
->getMock();
+ $manager->registerProvider('tmp',
+ [$shareType],
+ function() {
+ return $this->defaultProvider;
+ }
+ );
+
$sharedBy = $this->getMock('\OCP\IUser');
$sharedBy->method('getUID')->willReturn('sharedBy');
->setConstructorArgs([
$this->logger,
$this->config,
- $this->defaultProvider,
$this->secureRandom,
$this->hasher,
$this->mountManager,
->setMethods(['getShareById'])
->getMock();
+ $manager->registerProvider('tmp',
+ [
+ \OCP\Share::SHARE_TYPE_USER
+ ],
+ function() {
+ return $this->defaultProvider;
+ }
+ );
+
$sharedBy1 = $this->getMock('\OCP\IUser');
$sharedBy1->method('getUID')->willReturn('sharedBy1');
$sharedBy2 = $this->getMock('\OCP\IUser');
->setConstructorArgs([
$this->logger,
$this->config,
- $this->defaultProvider,
$this->secureRandom,
$this->hasher,
$this->mountManager,
->setMethods(['deleteShare'])
->getMock();
+ $manager->registerProvider('tmp',
+ [\OCP\Share::SHARE_TYPE_USER],
+ function() {
+ return $this->defaultProvider;
+ }
+ );
+
$share = $this->getMock('\OC\Share20\IShare');
$child1 = $this->getMock('\OC\Share20\IShare');
->setConstructorArgs([
$this->logger,
$this->config,
- $this->defaultProvider,
$this->secureRandom,
$this->hasher,
$this->mountManager,
->setConstructorArgs([
$this->logger,
$this->config,
- $this->defaultProvider,
$this->secureRandom,
$this->hasher,
$this->mountManager,
->setConstructorArgs([
$this->logger,
$this->config,
- $this->defaultProvider,
$this->secureRandom,
$this->hasher,
$this->mountManager,
->setMethods(['canShare', 'generalCreateChecks', 'userCreateChecks', 'pathCreateChecks'])
->getMock();
+ $manager->registerProvider('tmp',
+ [\OCP\Share::SHARE_TYPE_USER],
+ function() {
+ return $this->defaultProvider;
+ }
+ );
+
$sharedWith = $this->getMock('\OCP\IUser');
$sharedBy = $this->getMock('\OCP\IUser');
$shareOwner = $this->getMock('\OCP\IUser');
->setConstructorArgs([
$this->logger,
$this->config,
- $this->defaultProvider,
$this->secureRandom,
$this->hasher,
$this->mountManager,
->setMethods(['canShare', 'generalCreateChecks', 'groupCreateChecks', 'pathCreateChecks'])
->getMock();
+ $manager->registerProvider('tmp',
+ [\OCP\Share::SHARE_TYPE_GROUP],
+ function() {
+ return $this->defaultProvider;
+ }
+ );
+
$sharedWith = $this->getMock('\OCP\IGroup');
$sharedBy = $this->getMock('\OCP\IUser');
$shareOwner = $this->getMock('\OCP\IUser');
->setConstructorArgs([
$this->logger,
$this->config,
- $this->defaultProvider,
+
$this->secureRandom,
$this->hasher,
$this->mountManager,
])
->getMock();
+ $manager->registerProvider('tmp',
+ [\OCP\Share::SHARE_TYPE_LINK],
+ function() {
+ return $this->defaultProvider;
+ }
+ );
+
$sharedBy = $this->getMock('\OCP\IUser');
$sharedBy->method('getUID')->willReturn('sharedBy');
$shareOwner = $this->getMock('\OCP\IUser');
->setConstructorArgs([
$this->logger,
$this->config,
- $this->defaultProvider,
$this->secureRandom,
$this->hasher,
$this->mountManager,
])
->getMock();
+ $manager->registerProvider('tmp',
+ [\OCP\Share::SHARE_TYPE_USER],
+ function() {
+ return $this->defaultProvider;
+ }
+ );
+
$sharedWith = $this->getMock('\OCP\IUser');
$sharedBy = $this->getMock('\OCP\IUser');
$shareOwner = $this->getMock('\OCP\IUser');
$manager->createShare($share);
}
+
+ public function dataRegisterProvider() {
+ return [
+ [[ \OCP\Share::SHARE_TYPE_REMOTE,]],
+ [[ \OCP\Share::SHARE_TYPE_LINK, ]],
+ [[ \OCP\Share::SHARE_TYPE_LINK, \OCP\Share::SHARE_TYPE_REMOTE,]],
+ [[ \OCP\Share::SHARE_TYPE_GROUP, ]],
+ [[ \OCP\Share::SHARE_TYPE_GROUP, \OCP\Share::SHARE_TYPE_REMOTE,]],
+ [[ \OCP\Share::SHARE_TYPE_GROUP, \OCP\Share::SHARE_TYPE_LINK, ]],
+ [[ \OCP\Share::SHARE_TYPE_GROUP, \OCP\Share::SHARE_TYPE_LINK, \OCP\Share::SHARE_TYPE_REMOTE,]],
+ [[ \OCP\Share::SHARE_TYPE_GROUP, ]],
+ [[\OCP\Share::SHARE_TYPE_USER, ]],
+ [[\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_REMOTE,]],
+ [[\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_LINK, ]],
+ [[\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_LINK, \OCP\Share::SHARE_TYPE_REMOTE,]],
+ [[\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_GROUP, ]],
+ [[\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_GROUP, \OCP\Share::SHARE_TYPE_REMOTE,]],
+ [[\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_GROUP, \OCP\Share::SHARE_TYPE_LINK, ]],
+ [[\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_GROUP, \OCP\Share::SHARE_TYPE_LINK, \OCP\Share::SHARE_TYPE_REMOTE,]],
+ ];
+ }
+
+ /**
+ * @dataProvider dataRegisterProvider
+ * @param int[] $shareTypes
+ */
+ public function testRegisterProvider($shareTypes) {
+ $manager = new Manager(
+ $this->logger,
+ $this->config,
+ $this->secureRandom,
+ $this->hasher,
+ $this->mountManager,
+ $this->groupManager,
+ $this->l
+ );
+
+ $provider = $this->getMock('OC\Share20\IShareProvider');
+ $manager->registerProvider('foo', $shareTypes, function() use ($provider) {
+ return $provider;
+ });
+
+ $this->assertEquals($provider, $this->invokePrivate($manager, 'getProvider', ['foo']));
+ foreach ($shareTypes as $shareType) {
+ $this->assertEquals($provider, $this->invokePrivate($manager, 'getProviderForType', [$shareType]));
+ }
+ }
+
+ /**
+ * @expectedException \OC\Share20\Exception\ProviderException
+ * @expectedExceptionMessage A share provider with the id 'foo' is already registered
+ */
+ public function testRegisterProviderDuplicateId() {
+ $manager = new Manager(
+ $this->logger,
+ $this->config,
+ $this->secureRandom,
+ $this->hasher,
+ $this->mountManager,
+ $this->groupManager,
+ $this->l
+ );
+
+ $provider = $this->getMock('OC\Share20\IShareProvider');
+ $manager->registerProvider('foo', [\OCP\Share::SHARE_TYPE_USER], function() use ($provider) {
+ return $provider;
+ });
+ $manager->registerProvider('foo', [\OCP\Share::SHARE_TYPE_USER], function() use ($provider) {
+ return $provider;
+ });
+ }
+
+ /**
+ * @expectedException \OC\Share20\Exception\ProviderException
+ * @expectedExceptionMessage shareTypes can't be an empty array
+ */
+ public function testRegisterProviderEmptyShareTypes() {
+ $manager = new Manager(
+ $this->logger,
+ $this->config,
+ $this->secureRandom,
+ $this->hasher,
+ $this->mountManager,
+ $this->groupManager,
+ $this->l
+ );
+
+ $provider = $this->getMock('OC\Share20\IShareProvider');
+ $manager->registerProvider('foo', [], function() use ($provider) {
+ return $provider;
+ });
+ }
+
+ /**
+ * @expectedException \OC\Share20\Exception\ProviderException
+ * @expectedExceptionMessage The share provider foo is already registered for share type 0
+ */
+ public function testRegisterProviderSameType() {
+ $manager = new Manager(
+ $this->logger,
+ $this->config,
+ $this->secureRandom,
+ $this->hasher,
+ $this->mountManager,
+ $this->groupManager,
+ $this->l
+ );
+
+ $provider = $this->getMock('OC\Share20\IShareProvider');
+ $manager->registerProvider('foo', [\OCP\Share::SHARE_TYPE_USER], function() use ($provider) {
+ return $provider;
+ });
+ $manager->registerProvider('bar', [\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_GROUP], function() use ($provider) {
+ return $provider;
+ });
+ }
+
+ /**
+ * @expectedException \OC\Share20\Exception\ProviderException
+ * @expectedExceptionMessage No provider with id foo found
+ */
+ public function testGetProviderNoProviderWithId() {
+ $manager = new Manager(
+ $this->logger,
+ $this->config,
+ $this->secureRandom,
+ $this->hasher,
+ $this->mountManager,
+ $this->groupManager,
+ $this->l
+ );
+
+ $this->invokePrivate($manager, 'getProvider', ['foo']);
+ }
+
+ /**
+ * @expectedException \OC\Share20\Exception\ProviderException
+ * @expectedExceptionMessage Callback does not return an IShareProvider instance for provider with id foo
+ */
+ public function testGetProviderNoIShareProvider() {
+ $manager = new Manager(
+ $this->logger,
+ $this->config,
+ $this->secureRandom,
+ $this->hasher,
+ $this->mountManager,
+ $this->groupManager,
+ $this->l
+ );
+
+ $provider = $this->getMock('OC\Share20\IShare');
+ $manager->registerProvider('foo', [\OCP\Share::SHARE_TYPE_USER], function() use ($provider) {
+ return $provider;
+ });
+ $this->invokePrivate($manager, 'getProvider', ['foo']);
+ }
+
+ /**
+ * @expectedException \OC\Share20\Exception\ProviderException
+ * @expectedExceptionMessage No share provider registered for share type 1
+ */
+ public function testGetProviderForTypeUnkownType() {
+ $manager = new Manager(
+ $this->logger,
+ $this->config,
+ $this->secureRandom,
+ $this->hasher,
+ $this->mountManager,
+ $this->groupManager,
+ $this->l
+ );
+
+ $provider = $this->getMock('OC\Share20\IShareProvider');
+ $manager->registerProvider('foo', [\OCP\Share::SHARE_TYPE_USER], function() use ($provider) {
+ return $provider;
+ });
+ $this->invokePrivate($manager, 'getProviderForType', [\OCP\SHARE::SHARE_TYPE_GROUP]);
+ }
}
class DummyPassword {