summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/private/Federation/CloudFederationProviderManager.php73
-rw-r--r--lib/private/Federation/CloudFederationShare.php6
-rw-r--r--lib/private/Server.php2
-rw-r--r--lib/private/Share20/ProviderFactory.php4
-rw-r--r--lib/public/Federation/ICloudFederationProvider.php7
-rw-r--r--lib/public/Federation/ICloudFederationShare.php4
6 files changed, 80 insertions, 16 deletions
diff --git a/lib/private/Federation/CloudFederationProviderManager.php b/lib/private/Federation/CloudFederationProviderManager.php
index 73e1dd99c61..dcf666ca266 100644
--- a/lib/private/Federation/CloudFederationProviderManager.php
+++ b/lib/private/Federation/CloudFederationProviderManager.php
@@ -29,6 +29,8 @@ use OCP\Federation\ICloudFederationNotification;
use OCP\Federation\ICloudFederationProvider;
use OCP\Federation\ICloudFederationProviderManager;
use OCP\Federation\ICloudFederationShare;
+use OCP\Federation\ICloudIdManager;
+use OCP\Http\Client\IClientService;
/**
* Class Manager
@@ -45,9 +47,26 @@ class CloudFederationProviderManager implements ICloudFederationProviderManager
/** @var IAppManager */
private $appManager;
- public function __construct(IAppManager $appManager) {
+ /** @var IClientService */
+ private $httpClientService;
+
+ /** @var ICloudIdManager */
+ private $cloudIdManager;
+
+ /**
+ * CloudFederationProviderManager constructor.
+ *
+ * @param IAppManager $appManager
+ * @param IClientService $httpClientService
+ * @param ICloudIdManager $cloudIdManager
+ */
+ public function __construct(IAppManager $appManager,
+ IClientService $httpClientService,
+ ICloudIdManager $cloudIdManager) {
$this->cloudFederationProvider= [];
$this->appManager = $appManager;
+ $this->httpClientService = $httpClientService;
+ $this->cloudIdManager = $cloudIdManager;
}
@@ -103,7 +122,32 @@ class CloudFederationProviderManager implements ICloudFederationProviderManager
}
public function sendShare(ICloudFederationShare $share) {
- // TODO: Implement sendShare() method.
+ $ocmEndPoint = $this->getOCMEndPoint($share->getShareWith());
+
+ if (empty($ocmEndPoint)) {
+ return false;
+ }
+
+ $client = $this->httpClientService->newClient();
+ try {
+ $response = $client->post($ocmEndPoint . '/shares', [
+ 'body' => $share->getShare(),
+ 'timeout' => 10,
+ 'connect_timeout' => 10,
+ ]);
+ $result['result'] = $response->getBody();
+ $result['success'] = true;
+ } catch (\Exception $e) {
+ // if flat re-sharing is not supported by the remote server
+ // we re-throw the exception and fall back to the old behaviour.
+ // (flat re-shares has been introduced in Nextcloud 9.1)
+ if ($e->getCode() === Http::STATUS_INTERNAL_SERVER_ERROR) {
+ throw $e;
+ }
+ }
+
+ return true;
+
}
public function sendNotification(ICloudFederationNotification $notification) {
@@ -118,5 +162,30 @@ class CloudFederationProviderManager implements ICloudFederationProviderManager
public function isReady() {
return $this->appManager->isEnabledForUser('cloud_federation_api', false);
}
+ /**
+ * check if server supports the new OCM api and ask for the correct end-point
+ *
+ * @param string $recipient full federated cloud ID of the recipient of a share
+ * @return string
+ */
+ protected function getOCMEndPoint($recipient) {
+ $cloudId = $this->cloudIdManager->resolveCloudId($recipient);
+ $client = $this->httpClientService->newClient();
+ try {
+ $response = $client->get($cloudId->getRemote() . '/ocm-provider/', ['timeout' => 10, 'connect_timeout' => 10]);
+ } catch (\Exception $e) {
+ return '';
+ }
+
+ $result = $response->getBody();
+ $result = json_decode($result, true);
+
+ if (isset($result['end-point'])) {
+ return $result['end-point'];
+ }
+
+ return '';
+ }
+
}
diff --git a/lib/private/Federation/CloudFederationShare.php b/lib/private/Federation/CloudFederationShare.php
index 4622dc096d7..5bc172ab6b6 100644
--- a/lib/private/Federation/CloudFederationShare.php
+++ b/lib/private/Federation/CloudFederationShare.php
@@ -203,14 +203,14 @@ class CloudFederationShare implements ICloudFederationShare {
}
/**
- * get JSON encoded share, ready to send out
+ * get the whole share, ready to send out
*
- * @return string
+ * @return array
*
* @since 14.0.0
*/
public function getShare() {
- return json_encode($this->share);
+ return $this->share;
}
/**
diff --git a/lib/private/Server.php b/lib/private/Server.php
index 7156c8a2c22..8f1b24eb11d 100644
--- a/lib/private/Server.php
+++ b/lib/private/Server.php
@@ -1117,7 +1117,7 @@ class Server extends ServerContainer implements IServerContainer {
});
$this->registerService(ICloudFederationProviderManager::class, function (Server $c) {
- return new CloudFederationProviderManager($c->getAppManager());
+ return new CloudFederationProviderManager($c->getAppManager(), $c->getHTTPClientService(), $c->getCloudIdManager());
});
$this->registerService(ICloudFederationFactory::class, function (Server $c) {
diff --git a/lib/private/Share20/ProviderFactory.php b/lib/private/Share20/ProviderFactory.php
index 456b6dbc596..7d866db24fa 100644
--- a/lib/private/Share20/ProviderFactory.php
+++ b/lib/private/Share20/ProviderFactory.php
@@ -116,7 +116,9 @@ class ProviderFactory implements IProviderFactory {
$addressHandler,
$this->serverContainer->getHTTPClientService(),
$this->serverContainer->query(\OCP\OCS\IDiscoveryService::class),
- $this->serverContainer->getJobList()
+ $this->serverContainer->getJobList(),
+ \OC::$server->getCloudFederationProviderManager(),
+ \OC::$server->getCloudFederationFactory()
);
$tokenHandler = new TokenHandler(
$this->serverContainer->getSecureRandom()
diff --git a/lib/public/Federation/ICloudFederationProvider.php b/lib/public/Federation/ICloudFederationProvider.php
index 535f61aae89..38a551000f0 100644
--- a/lib/public/Federation/ICloudFederationProvider.php
+++ b/lib/public/Federation/ICloudFederationProvider.php
@@ -46,13 +46,6 @@ interface ICloudFederationProvider {
public function getShareType();
/**
- * send new share to another server
- *
- * @since 14.0.0
- */
- public function sendShare();
-
- /**
* share received from another server
*
* @param ICloudFederationShare $share
diff --git a/lib/public/Federation/ICloudFederationShare.php b/lib/public/Federation/ICloudFederationShare.php
index b116da0fb54..ce5065c70b8 100644
--- a/lib/public/Federation/ICloudFederationShare.php
+++ b/lib/public/Federation/ICloudFederationShare.php
@@ -123,9 +123,9 @@ interface ICloudFederationShare {
public function setShareType($shareType);
/**
- * get JSON encoded share, ready to send out
+ * get the whole share, ready to send out
*
- * @return string
+ * @return array
*
* @since 14.0.0
*/