fall back to old re-sharing behaviour in case the remote server doesn't support flat-reshares

This commit is contained in:
Björn Schießle 2016-05-17 15:03:37 +02:00
parent 2dc26aada7
commit 92fa0c7dfd
No known key found for this signature in database
GPG Key ID: 2378A753E2BF04F6
2 changed files with 70 additions and 35 deletions

View File

@ -133,7 +133,6 @@ class FederatedShareProvider implements IShareProvider {
$shareWith = $share->getSharedWith(); $shareWith = $share->getSharedWith();
$itemSource = $share->getNodeId(); $itemSource = $share->getNodeId();
$itemType = $share->getNodeType(); $itemType = $share->getNodeType();
$uidOwner = $share->getShareOwner();
$permissions = $share->getPermissions(); $permissions = $share->getPermissions();
$sharedBy = $share->getSharedBy(); $sharedBy = $share->getSharedBy();
@ -160,7 +159,7 @@ class FederatedShareProvider implements IShareProvider {
throw new \Exception($message_t); throw new \Exception($message_t);
} }
$shareWith = $user . '@' . $remote; $share->setSharedWith($user . '@' . $remote);
try { try {
$remoteShare = $this->getShareFromExternalShareTable($share); $remoteShare = $this->getShareFromExternalShareTable($share);
@ -169,6 +168,7 @@ class FederatedShareProvider implements IShareProvider {
} }
if ($remoteShare) { if ($remoteShare) {
try {
$uidOwner = $remoteShare['owner'] . '@' . $remoteShare['remote']; $uidOwner = $remoteShare['owner'] . '@' . $remoteShare['remote'];
$shareId = $this->addShareToDB($itemSource, $itemType, $shareWith, $sharedBy, $uidOwner, $permissions, 'tmp_token_' . time()); $shareId = $this->addShareToDB($itemSource, $itemType, $shareWith, $sharedBy, $uidOwner, $permissions, 'tmp_token_' . time());
list($token, $remoteId) = $this->askOwnerToReShare($shareWith, $share, $shareId); list($token, $remoteId) = $this->askOwnerToReShare($shareWith, $share, $shareId);
@ -178,16 +178,55 @@ class FederatedShareProvider implements IShareProvider {
$this->updateSuccessfulReshare($shareId, $token); $this->updateSuccessfulReshare($shareId, $token);
$this->storeRemoteId($shareId, $remoteId); $this->storeRemoteId($shareId, $remoteId);
} }
} catch (\Exception $e) {
// fall back to old re-share behavior if the remote server
// doesn't support flat re-shares (was introduced with ownCloud 9.1)
$data = $this->getRawShare($shareId);
$brokenShare = $this->createShareObject($data);
$this->removeShareFromTable($brokenShare);
list($shareId, $send) = $this->createFederatedShare($share);
}
} else { } else {
list($shareId, $send) = $this->createFederatedShare($share);
}
$data = $this->getRawShare($shareId);
$share = $this->createShareObject($data);
if ($send === false) {
$this->removeShareFromTable($share);
$message_t = $this->l->t('Sharing %s failed, could not find %s, maybe the server is currently unreachable.',
[$share->getNode()->getName(), $shareWith]);
throw new \Exception($message_t);
}
return $share;
}
/**
* create federated share and inform the recipient
*
* @param IShare $share
* @return array
*/
protected function createFederatedShare(IShare $share) {
$token = $this->tokenHandler->generateToken(); $token = $this->tokenHandler->generateToken();
$shareId = $this->addShareToDB($itemSource, $itemType, $shareWith, $sharedBy, $uidOwner, $permissions, $token); $shareId = $this->addShareToDB(
$share->getNodeId(),
$share->getNodeType(),
$share->getSharedWith(),
$share->getSharedBy(),
$share->getShareOwner(),
$share->getPermissions(),
$token
);
$sharedByFederatedId = $share->getSharedBy(); $sharedByFederatedId = $share->getSharedBy();
if ($this->userManager->userExists($sharedByFederatedId)) { if ($this->userManager->userExists($sharedByFederatedId)) {
$sharedByFederatedId = $sharedByFederatedId . '@' . $this->addressHandler->generateRemoteURL(); $sharedByFederatedId = $sharedByFederatedId . '@' . $this->addressHandler->generateRemoteURL();
} }
$send = $this->notifications->sendRemoteShare( $send = $this->notifications->sendRemoteShare(
$token, $token,
$shareWith, $share->getSharedWith(),
$share->getNode()->getName(), $share->getNode()->getName(),
$shareId, $shareId,
$share->getShareOwner(), $share->getShareOwner(),
@ -195,19 +234,8 @@ class FederatedShareProvider implements IShareProvider {
$share->getSharedBy(), $share->getSharedBy(),
$sharedByFederatedId $sharedByFederatedId
); );
}
$data = $this->getRawShare($shareId); return [$shareId, $send];
$share = $this->createShare($data);
if ($send === false) {
$this->delete($share);
$message_t = $this->l->t('Sharing %s failed, could not find %s, maybe the server is currently unreachable.',
[$share->getNode()->getName(), $shareWith]);
throw new \Exception($message_t);
}
return $share;
} }
/** /**
@ -421,7 +449,7 @@ class FederatedShareProvider implements IShareProvider {
$cursor = $qb->execute(); $cursor = $qb->execute();
while($data = $cursor->fetch()) { while($data = $cursor->fetch()) {
$children[] = $this->createShare($data); $children[] = $this->createShareObject($data);
} }
$cursor->closeCursor(); $cursor->closeCursor();
@ -562,7 +590,7 @@ class FederatedShareProvider implements IShareProvider {
$cursor = $qb->execute(); $cursor = $qb->execute();
$shares = []; $shares = [];
while($data = $cursor->fetch()) { while($data = $cursor->fetch()) {
$shares[] = $this->createShare($data); $shares[] = $this->createShareObject($data);
} }
$cursor->closeCursor(); $cursor->closeCursor();
@ -589,7 +617,7 @@ class FederatedShareProvider implements IShareProvider {
} }
try { try {
$share = $this->createShare($data); $share = $this->createShareObject($data);
} catch (InvalidShare $e) { } catch (InvalidShare $e) {
throw new ShareNotFound(); throw new ShareNotFound();
} }
@ -614,7 +642,7 @@ class FederatedShareProvider implements IShareProvider {
$shares = []; $shares = [];
while($data = $cursor->fetch()) { while($data = $cursor->fetch()) {
$shares[] = $this->createShare($data); $shares[] = $this->createShareObject($data);
} }
$cursor->closeCursor(); $cursor->closeCursor();
@ -653,7 +681,7 @@ class FederatedShareProvider implements IShareProvider {
$cursor = $qb->execute(); $cursor = $qb->execute();
while($data = $cursor->fetch()) { while($data = $cursor->fetch()) {
$shares[] = $this->createShare($data); $shares[] = $this->createShareObject($data);
} }
$cursor->closeCursor(); $cursor->closeCursor();
@ -684,7 +712,7 @@ class FederatedShareProvider implements IShareProvider {
} }
try { try {
$share = $this->createShare($data); $share = $this->createShareObject($data);
} catch (InvalidShare $e) { } catch (InvalidShare $e) {
throw new ShareNotFound(); throw new ShareNotFound();
} }
@ -726,7 +754,7 @@ class FederatedShareProvider implements IShareProvider {
* @throws InvalidShare * @throws InvalidShare
* @throws ShareNotFound * @throws ShareNotFound
*/ */
private function createShare($data) { private function createShareObject($data) {
$share = new Share($this->rootFolder, $this->userManager); $share = new Share($this->rootFolder, $this->userManager);
$share->setId((int)$data['id']) $share->setId((int)$data['id'])

View File

@ -23,6 +23,7 @@
namespace OCA\FederatedFileSharing; namespace OCA\FederatedFileSharing;
use OCP\AppFramework\Http;
use OCP\BackgroundJob\IJobList; use OCP\BackgroundJob\IJobList;
use OCP\Http\Client\IClientService; use OCP\Http\Client\IClientService;
@ -291,6 +292,12 @@ class Notifications {
$result['success'] = true; $result['success'] = true;
break; break;
} catch (\Exception $e) { } 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 ownCloud 9.1)
if ($e->getCode() === Http::STATUS_INTERNAL_SERVER_ERROR) {
throw $e;
}
$try++; $try++;
$protocol = 'http://'; $protocol = 'http://';
} }