Skip to content

Commit 0e76f93

Browse files
committed
implement decline share
Signed-off-by: Bjoern Schiessle <bjoern@schiessle.org>
1 parent 6812899 commit 0e76f93

File tree

5 files changed

+131
-47
lines changed

5 files changed

+131
-47
lines changed

apps/federatedfilesharing/lib/AppInfo/Application.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ function() use ($container) {
6262
$server->getCloudIdManager(),
6363
$server->getActivityManager(),
6464
$server->getNotificationManager(),
65-
$server->getURLGenerator()
65+
$server->getURLGenerator(),
66+
$server->getCloudFederationFactory(),
67+
$server->getCloudFederationProviderManager()
6668
);
6769
});
6870

apps/federatedfilesharing/lib/Controller/RequestHandlerController.php

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -304,51 +304,27 @@ public function acceptShare($id) {
304304
*/
305305
public function declineShare($id) {
306306

307-
if (!$this->isS2SEnabled()) {
308-
throw new OCSException('Server does not support federated cloud sharing', 503);
309-
}
310-
311307
$token = isset($_POST['token']) ? $_POST['token'] : null;
312308

313-
try {
314-
$share = $this->federatedShareProvider->getShareById($id);
315-
} catch (Share\Exceptions\ShareNotFound $e) {
316-
return new Http\DataResponse();
317-
}
309+
$notification = [
310+
'sharedSecret' => $token,
311+
'message' => 'Recipient declined the share'
312+
];
318313

319-
if ($this->verifyShare($share, $token)) {
320-
if ($share->getShareOwner() !== $share->getSharedBy()) {
321-
list(, $remote) = $this->addressHandler->splitUserRemote($share->getSharedBy());
322-
$remoteId = $this->federatedShareProvider->getRemoteId($share);
323-
$this->notifications->sendDeclineShare($remote, $remoteId, $share->getToken());
324-
}
325-
$this->executeDeclineShare($share);
314+
try {
315+
$provider = $this->cloudFederationProviderManager->getCloudFederationProvider('file');
316+
$provider->notificationReceived('SHARE_DECLINED', $id, $notification);
317+
} catch (ProviderDoesNotExistsException $e) {
318+
throw new OCSException('Server does not support federated cloud sharing', 503);
319+
} catch (ShareNotFoundException $e) {
320+
$this->logger->debug('Share not found: ' . $e->getMessage());
321+
} catch (\Exception $e) {
322+
$this->logger->debug('internal server error, can not process notification: ' . $e->getMessage());
326323
}
327324

328325
return new Http\DataResponse();
329326
}
330327

331-
/**
332-
* delete declined share and create a activity
333-
*
334-
* @param Share\IShare $share
335-
*/
336-
protected function executeDeclineShare(Share\IShare $share) {
337-
$this->federatedShareProvider->removeShareFromTable($share);
338-
$fileId = (int) $share->getNode()->getId();
339-
list($file, $link) = $this->getFile($this->getCorrectUid($share), $fileId);
340-
341-
$event = \OC::$server->getActivityManager()->generateEvent();
342-
$event->setApp('files_sharing')
343-
->setType('remote_share')
344-
->setAffectedUser($this->getCorrectUid($share))
345-
->setSubject(RemoteShares::SUBJECT_REMOTE_SHARE_DECLINED, [$share->getSharedWith(), [$fileId => $file]])
346-
->setObject('files', $fileId, $file)
347-
->setLink($link);
348-
\OC::$server->getActivityManager()->publish($event);
349-
350-
}
351-
352328
/**
353329
* @NoCSRFRequired
354330
* @PublicPage

apps/federatedfilesharing/lib/FederatedShareProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,7 @@ public function getShareById($id, $recipientId = null) {
709709
$cursor->closeCursor();
710710

711711
if ($data === false) {
712-
throw new ShareNotFoundException();
712+
throw new ShareNotFoundException('Can not find share with ID: ' . $id);
713713
}
714714

715715
try {

apps/federatedfilesharing/lib/ocm/CloudFederationProviderFiles.php

Lines changed: 98 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434
use OCP\Federation\Exceptions\BadRequestException;
3535
use OCP\Federation\Exceptions\ProviderCouldNotAddShareException;
3636
use OCP\Federation\Exceptions\ShareNotFoundException;
37+
use OCP\Federation\ICloudFederationFactory;
3738
use OCP\Federation\ICloudFederationProvider;
39+
use OCP\Federation\ICloudFederationProviderManager;
3840
use OCP\Federation\ICloudFederationShare;
3941
use OCP\Federation\ICloudIdManager;
4042
use OCP\Files\NotFoundException;
@@ -75,6 +77,12 @@ class CloudFederationProviderFiles implements ICloudFederationProvider {
7577
/** @var IURLGenerator */
7678
private $urlGenerator;
7779

80+
/** @var ICloudFederationFactory */
81+
private $cloudFederationFactory;
82+
83+
/** @var ICloudFederationProviderManager */
84+
private $cloudFederationProviderManager;
85+
7886
/**
7987
* CloudFederationProvider constructor.
8088
*
@@ -87,6 +95,8 @@ class CloudFederationProviderFiles implements ICloudFederationProvider {
8795
* @param IActivityManager $activityManager
8896
* @param INotificationManager $notificationManager
8997
* @param IURLGenerator $urlGenerator
98+
* @param ICloudFederationFactory $cloudFederationFactory
99+
* @param ICloudFederationProviderManager $cloudFederationProviderManager
90100
*/
91101
public function __construct(IAppManager $appManager,
92102
FederatedShareProvider $federatedShareProvider,
@@ -96,7 +106,9 @@ public function __construct(IAppManager $appManager,
96106
ICloudIdManager $cloudIdManager,
97107
IActivityManager $activityManager,
98108
INotificationManager $notificationManager,
99-
IURLGenerator $urlGenerator
109+
IURLGenerator $urlGenerator,
110+
ICloudFederationFactory $cloudFederationFactory,
111+
ICloudFederationProviderManager $cloudFederationProviderManager
100112
) {
101113
$this->appManager = $appManager;
102114
$this->federatedShareProvider = $federatedShareProvider;
@@ -107,6 +119,8 @@ public function __construct(IAppManager $appManager,
107119
$this->activityManager = $activityManager;
108120
$this->notificationManager = $notificationManager;
109121
$this->urlGenerator = $urlGenerator;
122+
$this->cloudFederationFactory = $cloudFederationFactory;
123+
$this->cloudFederationProviderManager = $cloudFederationProviderManager;
110124
}
111125

112126

@@ -258,15 +272,18 @@ public function notificationReceived($notificationType, $providerId, array $noti
258272
case 'SHARE_ACCEPTED':
259273
$this->shareAccepted($providerId, $notification);
260274
return;
275+
case 'SHARE_DECLINED':
276+
$this->shareDeclined($providerId, $notification);
277+
return;
261278
}
262279

263280

264-
throw new ActionNotSupportedException($notification);
281+
throw new BadRequestException([$notificationType]);
265282
}
266283

267284
/**
268-
* @param $id
269-
* @param $notification
285+
* @param string $id
286+
* @param array $notification
270287
* @return bool
271288
* @throws ActionNotSupportedException
272289
* @throws AuthenticationFailedException
@@ -276,8 +293,8 @@ public function notificationReceived($notificationType, $providerId, array $noti
276293
*/
277294
private function shareAccepted($id, $notification) {
278295

279-
if (!$this->isS2SEnabled(true)) {
280-
throw new ActionNotSupportedException('Server does not support federated cloud sharing', '', Http::STATUS_SERVICE_UNAVAILABLE);
296+
if (!$this->isS2SEnabled()) {
297+
throw new ActionNotSupportedException('Server does not support federated cloud sharing');
281298
}
282299

283300
if (!isset($notification['sharedSecret'])) {
@@ -311,7 +328,6 @@ private function shareAccepted($id, $notification) {
311328
return true;
312329
}
313330

314-
315331
/**
316332
* @param IShare $share
317333
* @throws ShareNotFoundException
@@ -334,6 +350,81 @@ protected function executeAcceptShare(IShare $share) {
334350
$this->activityManager->publish($event);
335351
}
336352

353+
/**
354+
* @param string $id
355+
* @param array $notification
356+
* @throws ActionNotSupportedException
357+
* @throws AuthenticationFailedException
358+
* @throws BadRequestException
359+
* @throws ShareNotFound
360+
* @throws ShareNotFoundException
361+
* @throws \OC\HintException
362+
*/
363+
protected function shareDeclined($id, $notification) {
364+
365+
if (!$this->isS2SEnabled()) {
366+
throw new ActionNotSupportedException('Server does not support federated cloud sharing');
367+
}
368+
369+
if (!isset($notification['sharedSecret'])) {
370+
throw new BadRequestException(['sharedSecret']);
371+
}
372+
373+
$token = $notification['sharedSecret'];
374+
375+
$share = $this->federatedShareProvider->getShareById($id);
376+
377+
$this->verifyShare($share, $token);
378+
379+
if ($share->getShareOwner() !== $share->getSharedBy()) {
380+
list(, $remote) = $this->addressHandler->splitUserRemote($share->getSharedBy());
381+
$remoteId = $this->federatedShareProvider->getRemoteId($share);
382+
$notification = $this->cloudFederationFactory->getCloudFederationNotification();
383+
$notification->setMessage(
384+
'SHARE_DECLINED',
385+
'file',
386+
$remoteId,
387+
[
388+
'sharedSecret' => $token,
389+
'message' => 'Recipient declined the re-share'
390+
]
391+
392+
);
393+
$this->cloudFederationProviderManager->sendNotification($remote, $notification);
394+
}
395+
396+
$this->executeDeclineShare($share);
397+
398+
}
399+
400+
/**
401+
* delete declined share and create a activity
402+
*
403+
* @param IShare $share
404+
* @throws ShareNotFoundException
405+
*/
406+
protected function executeDeclineShare(IShare $share) {
407+
$this->federatedShareProvider->removeShareFromTable($share);
408+
409+
try {
410+
$fileId = (int)$share->getNode()->getId();
411+
list($file, $link) = $this->getFile($this->getCorrectUid($share), $fileId);
412+
} catch (\Exception $e) {
413+
throw new ShareNotFoundException();
414+
}
415+
416+
$event = $this->activityManager->generateEvent();
417+
$event->setApp('files_sharing')
418+
->setType('remote_share')
419+
->setAffectedUser($this->getCorrectUid($share))
420+
->setSubject(RemoteShares::SUBJECT_REMOTE_SHARE_DECLINED, [$share->getSharedWith(), [$fileId => $file]])
421+
->setObject('files', $fileId, $file)
422+
->setLink($link);
423+
$this->activityManager->publish($event);
424+
425+
}
426+
427+
337428
/**
338429
* get file
339430
*

apps/files_sharing/lib/External/Manager.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ private function sendFeedbackToRemote($remote, $token, $remoteId, $feedback) {
326326
* @param string $token
327327
* @param $remoteId id of the share
328328
* @param string $feedback
329-
* @return mixed
329+
* @return bool
330330
*/
331331
protected function tryOCMEndPoint($remoteDomain, $token, $remoteId, $feedback) {
332332
switch ($feedback) {
@@ -341,10 +341,25 @@ protected function tryOCMEndPoint($remoteDomain, $token, $remoteId, $feedback) {
341341
'message' => 'Recipient accept the share'
342342
]
343343

344+
);
345+
return $this->cloudFederationProviderManager->sendNotification($remoteDomain, $notification);
346+
case 'decline':
347+
$notification = $this->cloudFederationFactory->getCloudFederationNotification();
348+
$notification->setMessage(
349+
'SHARE_DECLINED',
350+
'file',
351+
$remoteId,
352+
[
353+
'sharedSecret' => $token,
354+
'message' => 'Recipient declined the share'
355+
]
356+
344357
);
345358
return $this->cloudFederationProviderManager->sendNotification($remoteDomain, $notification);
346359
}
347360

361+
return false;
362+
348363
}
349364

350365

0 commit comments

Comments
 (0)