summaryrefslogtreecommitdiffstats
path: root/lib/private
diff options
context:
space:
mode:
Diffstat (limited to 'lib/private')
-rw-r--r--lib/private/server.php28
-rw-r--r--lib/private/share20/defaultshareprovider.php13
-rw-r--r--lib/private/share20/iproviderfactory.php38
-rw-r--r--lib/private/share20/ishareprovider.php7
-rw-r--r--lib/private/share20/manager.php115
-rw-r--r--lib/private/share20/providerfactory.php54
6 files changed, 194 insertions, 61 deletions
diff --git a/lib/private/server.php b/lib/private/server.php
index fe71b748e08..79bd96652ed 100644
--- a/lib/private/server.php
+++ b/lib/private/server.php
@@ -564,6 +564,25 @@ class Server extends ServerContainer implements IServerContainer {
$request
);
});
+ $this->registerService('ShareManager', function(Server $c) {
+ $config = $c->getConfig();
+ $factoryClass = $config->getSystemValue('sharing.managerFactory', '\OC\Share20\ProviderFactory');
+ /** @var \OC\Share20\IProviderFactory $factory */
+ $factory = new $factoryClass();
+
+ $manager = new \OC\Share20\Manager(
+ $c->getLogger(),
+ $c->getConfig(),
+ $c->getSecureRandom(),
+ $c->getHasher(),
+ $c->getMountManager(),
+ $c->getGroupManager(),
+ $c->getL10N('core'),
+ $factory
+ );
+
+ return $manager;
+ });
}
/**
@@ -1181,5 +1200,12 @@ class Server extends ServerContainer implements IServerContainer {
public function getUserStoragesService() {
return \OC_Mount_Config::$app->getContainer()->query('OCA\\Files_External\\Service\\UserStoragesService');
}
-
+
+
+ /**
+ * @return \OC\Share20\Manager
+ */
+ public function getShareManager() {
+ return $this->query('ShareManager');
+ }
}
diff --git a/lib/private/share20/defaultshareprovider.php b/lib/private/share20/defaultshareprovider.php
index adeaf2c44c9..f961bc5202f 100644
--- a/lib/private/share20/defaultshareprovider.php
+++ b/lib/private/share20/defaultshareprovider.php
@@ -63,6 +63,19 @@ class DefaultShareProvider implements IShareProvider {
}
/**
+ * Return the share types this provider handles
+ *
+ * @return int[]
+ */
+ public function shareTypes() {
+ return [
+ \OCP\Share::SHARE_TYPE_USER,
+ \OCP\Share::SHARE_TYPE_GROUP,
+ \OCP\Share::SHARE_TYPE_LINK,
+ ];
+ }
+
+ /**
* Share a path
*
* @param IShare $share
diff --git a/lib/private/share20/iproviderfactory.php b/lib/private/share20/iproviderfactory.php
new file mode 100644
index 00000000000..0bbd304e6b8
--- /dev/null
+++ b/lib/private/share20/iproviderfactory.php
@@ -0,0 +1,38 @@
+<?php
+/**
+ * @author Roeland Jago Douma <rullzer@owncloud.com>
+ *
+ * @copyright Copyright (c) 2016, 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 <http://www.gnu.org/licenses/>
+ *
+ */
+namespace OC\Share20;
+
+/**
+ * Interface IProviderFactory
+ *
+ * @package OC\Share20
+ * @since 9.0.0
+ */
+interface IProviderFactory {
+
+ /**
+ * Creates and returns the shareProviders
+ *
+ * @return IShareProvider[]
+ * @since 9.0.0
+ */
+ public function getProviders();
+}
diff --git a/lib/private/share20/ishareprovider.php b/lib/private/share20/ishareprovider.php
index ad3c22f43e3..a7e9c59a972 100644
--- a/lib/private/share20/ishareprovider.php
+++ b/lib/private/share20/ishareprovider.php
@@ -27,6 +27,13 @@ use OCP\IUser;
interface IShareProvider {
/**
+ * Return the share types this provider handles
+ *
+ * @return int[]
+ */
+ public function shareTypes();
+
+ /**
* Share a path
*
* @param IShare $share
diff --git a/lib/private/share20/manager.php b/lib/private/share20/manager.php
index d9014e64dcf..e35b69f0031 100644
--- a/lib/private/share20/manager.php
+++ b/lib/private/share20/manager.php
@@ -41,7 +41,10 @@ use OC\HintException;
*/
class Manager {
- /** @var array */
+ /** @var IProviderFactory */
+ private $factory;
+
+ /** @var IShareProvider[] */
private $providers;
/** @var array */
@@ -78,6 +81,7 @@ class Manager {
* @param IMountManager $mountManager
* @param IGroupManager $groupManager
* @param IL10N $l
+ * @param IProviderFactory $factory
*/
public function __construct(
ILogger $logger,
@@ -86,7 +90,8 @@ class Manager {
IHasher $hasher,
IMountManager $mountManager,
IGroupManager $groupManager,
- IL10N $l
+ IL10N $l,
+ IProviderFactory $factory
) {
$this->providers = [];
$this->type2provider = [];
@@ -98,82 +103,64 @@ class Manager {
$this->mountManager = $mountManager;
$this->groupManager = $groupManager;
$this->l = $l;
+ $this->factory = $factory;
}
/**
- * 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
+ * @param IShareProvider[] $providers
* @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');
- }
+ private function addProviders(array $providers) {
+ foreach ($providers as $provider) {
+ $name = get_class($provider);
- if ($shareTypes === []) {
- throw new ProviderException('shareTypes can\'t be an empty array');
- }
+ if (!($provider instanceof IShareProvider)) {
+ throw new ProviderException($name . ' is not an instance of IShareProvider');
+ }
- 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);
+ if (isset($this->providers[$name])) {
+ throw new ProviderException($name . ' is already registered');
}
- /*
- * 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
+ $this->providers[$name] = $provider;
+ $types = $provider->shareTypes();
+
+ if ($types === []) {
+ throw new ProviderException('Provider ' . $name . ' must supply share types it can handle');
}
- }
- // Add the provider
- $this->providers[$id] = [
- 'id' => $id,
- 'callback' => $callback,
- 'provider' => null,
- 'shareTypes' => $shareTypes,
- ];
+ foreach ($types as $type) {
+ if (isset($this->type2provider[$type])) {
+ throw new ProviderException($name . ' tried to register for share type ' . $type . ' but this type is already handled by ' . $this->type2provider[$type]);
+ }
- // Update the type mapping
- foreach ($shareTypes as $shareType) {
- $this->type2provider[$shareType] = $id;
+ $this->type2provider[$type] = $name;
+ }
}
}
+ private function runFactory() {
+ if (!empty($this->providers)) {
+ return;
+ }
+
+ $this->addProviders($this->factory->getProviders());
+ $this->factoryDone = true;
+ }
+
/**
* @param string $id
* @return IShareProvider
* @throws ProviderException
*/
private function getProvider($id) {
+ $this->runFactory();
+
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;
- }
-
- return $this->providers[$id]['provider'];
+ return $this->providers[$id];
}
/**
@@ -182,6 +169,8 @@ class Manager {
* @throws ProviderException
*/
private function getProviderForType($shareType) {
+ $this->runFactory();
+
if (!isset($this->type2provider[$shareType])) {
throw new ProviderException('No share provider registered for share type ' . $shareType);
}
@@ -190,6 +179,15 @@ class Manager {
}
/**
+ * @return IShareProvider[]
+ */
+ private function getProviders() {
+ $this->runFactory();
+
+ return $this->providers;
+ }
+
+ /**
* Verify if a password meets all requirements
*
* @param string $password
@@ -587,10 +585,9 @@ class Manager {
protected function deleteChildren(IShare $share) {
$deletedShares = [];
- $providerIds = array_keys($this->providers);
+ $providers = $this->getProviders();
- foreach($providerIds as $providerId) {
- $provider = $this->getProvider($providerId);
+ foreach($providers as $provider) {
foreach ($provider->getChildren($share) as $child) {
$deletedChildren = $this->deleteChildren($child);
$deletedShares = array_merge($deletedShares, $deletedChildren);
@@ -691,12 +688,10 @@ class Manager {
//FIXME ids need to become proper providerid:shareid eventually
- $providerIds = array_keys($this->providers);
+ $providers = $this->getProviders();
$share = null;
- foreach ($providerIds as $providerId) {
- $provider = $this->getProvider($providerId);
-
+ foreach ($providers as $provider) {
try {
$share = $provider->getShareById($id);
$found = true;
diff --git a/lib/private/share20/providerfactory.php b/lib/private/share20/providerfactory.php
new file mode 100644
index 00000000000..55ba2aadf0a
--- /dev/null
+++ b/lib/private/share20/providerfactory.php
@@ -0,0 +1,54 @@
+<?php
+/**
+ * @author Roeland Jago Douma <rullzer@owncloud.com>
+ *
+ * @copyright Copyright (c) 2016, 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 <http://www.gnu.org/licenses/>
+ *
+ */
+namespace OC\Share20;
+
+/**
+ * Class ProviderFactory
+ *
+ * @package OC\Share20
+ */
+class ProviderFactory implements IProviderFactory {
+
+ /**
+ * Create the default share provider.
+ *
+ * @return DefaultShareProvider
+ */
+ protected function defaultShareProvider() {
+ return new DefaultShareProvider(
+ \OC::$server->getDatabaseConnection(),
+ \OC::$server->getUserManager(),
+ \OC::$server->getGroupManager(),
+ \OC::$server->getRootFolder()
+ );
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function getProviders() {
+ $providers = [];
+
+ $providers[] = $this->defaultShareProvider();
+
+ return $providers;
+ }
+}