Skip to content

Commit c21ad0a

Browse files
author
Björn Schießle
committed
update share permissions
1 parent 519be62 commit c21ad0a

File tree

4 files changed

+57
-13
lines changed

4 files changed

+57
-13
lines changed

apps/federatedfilesharing/lib/RequestHandler.php

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -529,28 +529,44 @@ protected function verifyShare(Share\IShare $share, $token) {
529529

530530
/**
531531
* update share information to keep federated re-shares in sync
532+
*
533+
* @param array $params
534+
* @return \OC_OCS_Result
532535
*/
533-
public function update() {
536+
public function updatePermissions($params) {
537+
$id = (int)$params['id'];
534538
$token = $this->request->getParam('token', null);
535-
$data = $this->request->getParam('data', []);
536-
537-
$dataArray = json_decode($data, true);
539+
$permissions = $this->request->getParam('permissions', null);
538540

539541
try {
540-
$share = $this->federatedShareProvider->getShareByToken($token);
542+
$share = $this->federatedShareProvider->getShareById($id);
541543
} catch (Share\Exceptions\ShareNotFound $e) {
542544
return new \OC_OCS_Result(null, Http::STATUS_BAD_REQUEST);
543545
}
544546

545-
if (isset($dataArray['decline'])) {
546-
$this->executeDeclineShare($share);
547-
}
548-
549-
if (isset($dataArray['accept'])) {
550-
$this->executeAcceptShare($share);
547+
$validPermission = ctype_digit($permissions);
548+
$validToken = $this->verifyShare($share, $token);
549+
if ($validPermission && $validToken) {
550+
$this->updatePermissionsInDatabase($share, (int)$permissions);
551+
} else {
552+
return new \OC_OCS_Result(null, Http::STATUS_BAD_REQUEST);
551553
}
552554

553555
return new \OC_OCS_Result();
554556
}
555557

558+
/**
559+
* update permissions in database
560+
*
561+
* @param IShare $share
562+
* @param int $permissions
563+
*/
564+
protected function updatePermissionsInDatabase(IShare $share, $permissions) {
565+
$query = $this->connection->getQueryBuilder();
566+
$query->update('share')
567+
->where($query->expr()->eq('id', $query->createNamedParameter($share->getId())))
568+
->set('permissions', $query->createNamedParameter($permissions))
569+
->execute();
570+
}
571+
556572
}

apps/federatedfilesharing/lib/federatedshareprovider.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,33 @@ public function update(IShare $share) {
313313
->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
314314
->execute();
315315

316+
// send the updated permission to the owner/initiator, if they are not the same
317+
if ($share->getShareOwner() !== $share->getSharedBy()) {
318+
$this->sendPermissionUpdate($share);
319+
}
320+
316321
return $share;
317322
}
318323

324+
/**
325+
* send the updated permission to the owner/initiator, if they are not the same
326+
*
327+
* @param IShare $share
328+
* @throws ShareNotFound
329+
* @throws \OC\HintException
330+
*/
331+
protected function sendPermissionUpdate(IShare $share) {
332+
$remoteId = $this->getRemoteId($share);
333+
// if the local user is the owner we send the permission change to the initiator
334+
if ($this->userManager->userExists($share->getShareOwner())) {
335+
list(, $remote) = $this->addressHandler->splitUserRemote($share->getSharedBy());
336+
} else { // ... if not we send the permission change to the owner
337+
list(, $remote) = $this->addressHandler->splitUserRemote($share->getShareOwner());
338+
}
339+
$this->notifications->sendPermissionChange($remote, $remoteId, $share->getToken(), $share->getPermissions());
340+
}
341+
342+
319343
/**
320344
* update successful reShare with the correct token
321345
*

apps/federatedfilesharing/lib/notifications.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public function sendRevokeShare($remote, $id, $token) {
183183
* @return bool
184184
*/
185185
public function sendPermissionChange($remote, $remoteId, $token, $permissions) {
186-
$this->sendUpdateToRemote($remote, $remoteId, $token, ['permissions' => $permissions]);
186+
$this->sendUpdateToRemote($remote, $remoteId, $token, 'permissions', ['permissions' => $permissions]);
187187
}
188188

189189
/**
@@ -222,6 +222,10 @@ public function sendDeclineShare($remote, $remoteId, $token) {
222222
public function sendUpdateToRemote($remote, $remoteId, $token, $action, $data = [], $try = 0) {
223223

224224
$fields = array('token' => $token);
225+
foreach ($data as $key => $value) {
226+
$fields[$key] = $value;
227+
}
228+
225229
$url = $this->addressHandler->removeProtocolFromUrl($remote);
226230
$result = $this->tryHttpPostToShareEndpoint(rtrim($url, '/'), '/' . $remoteId . '/' . $action, $fields);
227231
$status = json_decode($result['result'], true);

ocs/routes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@
135135

136136
API::register('post',
137137
'/cloud/shares/{id}/permissions',
138-
array($s2s, 'update'),
138+
array($s2s, 'updatePermissions'),
139139
'files_sharing',
140140
API::GUEST_AUTH
141141
);

0 commit comments

Comments
 (0)