Merge pull request #3788 from nextcloud/fed-share-modify

Add api to change the remote of an incoming federated share
This commit is contained in:
Roeland Jago Douma 2017-03-15 17:32:35 +01:00 committed by GitHub
commit 93c9a06761
2 changed files with 59 additions and 21 deletions

View File

@ -35,5 +35,6 @@ return [
['root' => '/cloud', 'name' => 'RequestHandler#declineShare', 'url' => '/shares/{id}/decline', 'verb' => 'POST'], ['root' => '/cloud', 'name' => 'RequestHandler#declineShare', 'url' => '/shares/{id}/decline', 'verb' => 'POST'],
['root' => '/cloud', 'name' => 'RequestHandler#unshare', 'url' => '/shares/{id}/unshare', 'verb' => 'POST'], ['root' => '/cloud', 'name' => 'RequestHandler#unshare', 'url' => '/shares/{id}/unshare', 'verb' => 'POST'],
['root' => '/cloud', 'name' => 'RequestHandler#revoke', 'url' => '/shares/{id}/revoke', 'verb' => 'POST'], ['root' => '/cloud', 'name' => 'RequestHandler#revoke', 'url' => '/shares/{id}/revoke', 'verb' => 'POST'],
['root' => '/cloud', 'name' => 'RequestHandler#move', 'url' => '/shares/{id}/move', 'verb' => 'POST'],
], ],
]; ];

View File

@ -69,7 +69,7 @@ class RequestHandlerController extends OCSController {
/** @var string */ /** @var string */
private $shareTable = 'share'; private $shareTable = 'share';
/** @var ICloudIdManager */ /** @var ICloudIdManager */
private $cloudIdManager; private $cloudIdManager;
/** /**
@ -133,7 +133,7 @@ class RequestHandlerController extends OCSController {
if ($remote && $token && $name && $owner && $remoteId && $shareWith) { if ($remote && $token && $name && $owner && $remoteId && $shareWith) {
if(!\OCP\Util::isValidFileName($name)) { if (!\OCP\Util::isValidFileName($name)) {
throw new OCSException('The mountpoint name contains invalid characters.', 400); throw new OCSException('The mountpoint name contains invalid characters.', 400);
} }
@ -157,14 +157,14 @@ class RequestHandlerController extends OCSController {
\OC::$server->getHTTPClientService() \OC::$server->getHTTPClientService()
); );
$externalManager = new \OCA\Files_Sharing\External\Manager( $externalManager = new \OCA\Files_Sharing\External\Manager(
\OC::$server->getDatabaseConnection(), \OC::$server->getDatabaseConnection(),
\OC\Files\Filesystem::getMountManager(), \OC\Files\Filesystem::getMountManager(),
\OC\Files\Filesystem::getLoader(), \OC\Files\Filesystem::getLoader(),
\OC::$server->getHTTPClientService(), \OC::$server->getHTTPClientService(),
\OC::$server->getNotificationManager(), \OC::$server->getNotificationManager(),
$discoveryManager, $discoveryManager,
$shareWith $shareWith
); );
try { try {
$externalManager->addShare($remote, $token, '', $name, $owner, false, $shareWith, $remoteId); $externalManager->addShare($remote, $token, '', $name, $owner, false, $shareWith, $remoteId);
@ -184,7 +184,7 @@ class RequestHandlerController extends OCSController {
->setType('remote_share') ->setType('remote_share')
->setSubject(RemoteShares::SUBJECT_REMOTE_SHARE_RECEIVED, [$ownerFederatedId, trim($name, '/')]) ->setSubject(RemoteShares::SUBJECT_REMOTE_SHARE_RECEIVED, [$ownerFederatedId, trim($name, '/')])
->setAffectedUser($shareWith) ->setAffectedUser($shareWith)
->setObject('remote_share', (int) $shareId, $name); ->setObject('remote_share', (int)$shareId, $name);
\OC::$server->getActivityManager()->publish($event); \OC::$server->getActivityManager()->publish($event);
$urlGenerator = \OC::$server->getURLGenerator(); $urlGenerator = \OC::$server->getURLGenerator();
@ -257,7 +257,7 @@ class RequestHandlerController extends OCSController {
list($user, $remote) = $this->addressHandler->splitUserRemote($shareWith); list($user, $remote) = $this->addressHandler->splitUserRemote($shareWith);
$owner = $share->getShareOwner(); $owner = $share->getShareOwner();
$currentServer = $this->addressHandler->generateRemoteURL(); $currentServer = $this->addressHandler->generateRemoteURL();
if ($this->addressHandler->compareAddresses($user, $remote,$owner , $currentServer)) { if ($this->addressHandler->compareAddresses($user, $remote, $owner, $currentServer)) {
throw new OCSForbiddenException(); throw new OCSForbiddenException();
} }
@ -330,7 +330,7 @@ class RequestHandlerController extends OCSController {
->setType('remote_share') ->setType('remote_share')
->setAffectedUser($this->getCorrectUid($share)) ->setAffectedUser($this->getCorrectUid($share))
->setSubject(RemoteShares::SUBJECT_REMOTE_SHARE_ACCEPTED, [$share->getSharedWith(), $file]) ->setSubject(RemoteShares::SUBJECT_REMOTE_SHARE_ACCEPTED, [$share->getSharedWith(), $file])
->setObject('files', (int) $share->getNode()->getId(), $file) ->setObject('files', (int)$share->getNode()->getId(), $file)
->setLink($link); ->setLink($link);
\OC::$server->getActivityManager()->publish($event); \OC::$server->getActivityManager()->publish($event);
} }
@ -359,7 +359,7 @@ class RequestHandlerController extends OCSController {
return new Http\DataResponse(); return new Http\DataResponse();
} }
if($this->verifyShare($share, $token)) { if ($this->verifyShare($share, $token)) {
if ($share->getShareOwner() !== $share->getSharedBy()) { if ($share->getShareOwner() !== $share->getSharedBy()) {
list(, $remote) = $this->addressHandler->splitUserRemote($share->getSharedBy()); list(, $remote) = $this->addressHandler->splitUserRemote($share->getSharedBy());
$remoteId = $this->federatedShareProvider->getRemoteId($share); $remoteId = $this->federatedShareProvider->getRemoteId($share);
@ -385,7 +385,7 @@ class RequestHandlerController extends OCSController {
->setType('remote_share') ->setType('remote_share')
->setAffectedUser($this->getCorrectUid($share)) ->setAffectedUser($this->getCorrectUid($share))
->setSubject(RemoteShares::SUBJECT_REMOTE_SHARE_DECLINED, [$share->getSharedWith(), $file]) ->setSubject(RemoteShares::SUBJECT_REMOTE_SHARE_DECLINED, [$share->getSharedWith(), $file])
->setObject('files', (int) $share->getNode()->getId(), $file) ->setObject('files', (int)$share->getNode()->getId(), $file)
->setLink($link); ->setLink($link);
\OC::$server->getActivityManager()->publish($event); \OC::$server->getActivityManager()->publish($event);
@ -398,7 +398,7 @@ class RequestHandlerController extends OCSController {
* @return string * @return string
*/ */
protected function getCorrectUid(Share\IShare $share) { protected function getCorrectUid(Share\IShare $share) {
if($this->userManager->userExists($share->getShareOwner())) { if ($this->userManager->userExists($share->getShareOwner())) {
return $share->getShareOwner(); return $share->getShareOwner();
} }
@ -448,7 +448,7 @@ class RequestHandlerController extends OCSController {
$notification = $notificationManager->createNotification(); $notification = $notificationManager->createNotification();
$notification->setApp('files_sharing') $notification->setApp('files_sharing')
->setUser($share['user']) ->setUser($share['user'])
->setObject('remote_share', (int) $share['id']); ->setObject('remote_share', (int)$share['id']);
$notificationManager->markProcessed($notification); $notificationManager->markProcessed($notification);
$event = \OC::$server->getActivityManager()->generateEvent(); $event = \OC::$server->getActivityManager()->generateEvent();
@ -456,7 +456,7 @@ class RequestHandlerController extends OCSController {
->setType('remote_share') ->setType('remote_share')
->setSubject(RemoteShares::SUBJECT_REMOTE_SHARE_UNSHARED, [$owner, $path]) ->setSubject(RemoteShares::SUBJECT_REMOTE_SHARE_UNSHARED, [$owner, $path])
->setAffectedUser($user) ->setAffectedUser($user)
->setObject('remote_share', (int) $share['id'], $path); ->setObject('remote_share', (int)$share['id'], $path);
\OC::$server->getActivityManager()->publish($event); \OC::$server->getActivityManager()->publish($event);
} }
@ -482,9 +482,9 @@ class RequestHandlerController extends OCSController {
*/ */
public function revoke($id) { public function revoke($id) {
$token = $this->request->getParam('token'); $token = $this->request->getParam('token');
$share = $this->federatedShareProvider->getShareById($id); $share = $this->federatedShareProvider->getShareById($id);
if ($this->verifyShare($share, $token)) { if ($this->verifyShare($share, $token)) {
$this->federatedShareProvider->removeShareFromTable($share); $this->federatedShareProvider->removeShareFromTable($share);
return new Http\DataResponse(); return new Http\DataResponse();
@ -492,7 +492,7 @@ class RequestHandlerController extends OCSController {
throw new OCSBadRequestException(); throw new OCSBadRequestException();
} }
/** /**
* get share * get share
* *
@ -620,4 +620,41 @@ class RequestHandlerController extends OCSController {
->execute(); ->execute();
} }
/**
* @NoCSRFRequired
* @PublicPage
*
* change the owner of a server-to-server share
*
* @param int $id
* @return Http\DataResponse
* @throws \InvalidArgumentException
* @throws OCSException
*/
public function move($id) {
if (!$this->isS2SEnabled()) {
throw new OCSException('Server does not support federated cloud sharing', 503);
}
$token = $this->request->getParam('token');
$remote = $this->request->getParam('remote');
$newRemoteId = $this->request->getParam('remote_id', $id);
$cloudId = $this->cloudIdManager->resolveCloudId($remote);
$qb = $this->connection->getQueryBuilder();
$query = $qb->update('share_external')
->set('remote', $qb->createNamedParameter($cloudId->getRemote()))
->set('owner', $qb->createNamedParameter($cloudId->getUser()))
->set('remote_id', $qb->createNamedParameter($newRemoteId))
->where($qb->expr()->eq('remote_id', $qb->createNamedParameter($id)))
->andWhere($qb->expr()->eq('share_token', $qb->createNamedParameter($token)));
$affected = $query->execute();
if ($affected > 0) {
return new Http\DataResponse(['remote' => $cloudId->getRemote(), 'owner' => $cloudId->getUser()]);
} else {
throw new OCSBadRequestException('Share not found or token invalid');
}
}
} }