Skip to content

Commit 860617b

Browse files
committed
Allow to accept group shares
Signed-off-by: Joas Schilling <coding@schilljs.com>
1 parent 14524c5 commit 860617b

5 files changed

Lines changed: 147 additions & 32 deletions

File tree

apps/files_sharing/lib/Controller/ShareAPIController.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,15 +1005,13 @@ public function acceptShare(string $id): DataResponse {
10051005
throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist'));
10061006
}
10071007

1008-
if ($share->getShareType() !== Share::SHARE_TYPE_USER ||
1009-
$share->getSharedWith() !== $this->currentUser) {
1010-
throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist'));
1008+
if ($share->getShareType() !== IShare::TYPE_USER &&
1009+
$share->getShareType() !== IShare::TYPE_GROUP) {
1010+
throw new OCSNotFoundException($this->l->t('Share type does not support accepting'));
10111011
}
10121012

1013-
$share->setStatus(IShare::STATUS_ACCEPTED);
1014-
10151013
try {
1016-
$this->shareManager->updateShare($share);
1014+
$this->shareManager->acceptShare($share, $this->currentUser);
10171015
} catch (GenericShareException $e) {
10181016
$code = $e->getCode() === 0 ? 403 : $e->getCode();
10191017
throw new OCSException($e->getHint(), $code);

lib/private/Share20/DefaultShareProvider.php

Lines changed: 101 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,72 @@ public function update(\OCP\Share\IShare $share) {
320320
return $share;
321321
}
322322

323+
/**
324+
* Accept a share.
325+
*
326+
* @param IShare $share
327+
* @param string $recipient
328+
* @return IShare The share object
329+
* @since 9.0.0
330+
*/
331+
public function acceptShare(IShare $share, string $recipient): IShare {
332+
if ($share->getShareType() === IShare::TYPE_GROUP) {
333+
$group = $this->groupManager->get($share->getSharedWith());
334+
$user = $this->userManager->get($recipient);
335+
336+
if (is_null($group)) {
337+
throw new ProviderException('Group "' . $share->getSharedWith() . '" does not exist');
338+
}
339+
340+
if (!$group->inGroup($user)) {
341+
throw new ProviderException('Recipient not in receiving group');
342+
}
343+
344+
// Try to fetch user specific share
345+
$qb = $this->dbConn->getQueryBuilder();
346+
$stmt = $qb->select('*')
347+
->from('share')
348+
->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)))
349+
->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($recipient)))
350+
->andWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))
351+
->andWhere($qb->expr()->orX(
352+
$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
353+
$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
354+
))
355+
->execute();
356+
357+
$data = $stmt->fetch();
358+
359+
/*
360+
* Check if there already is a user specific group share.
361+
* If there is update it (if required).
362+
*/
363+
if ($data === false) {
364+
$userShare = $this->createUserSpecificGroupShare($share, $recipient);
365+
$id = $userShare->getId();
366+
} else {
367+
$id = $data['id'];
368+
}
369+
370+
} else if ($share->getShareType() === IShare::TYPE_USER) {
371+
if ($share->getSharedWith() !== $recipient) {
372+
throw new ProviderException('Recipient does not match');
373+
}
374+
375+
$id = $share->getId();
376+
} else {
377+
throw new ProviderException('Invalid shareType');
378+
}
379+
380+
$qb = $this->dbConn->getQueryBuilder();
381+
$qb->update('share')
382+
->set('accepted', $qb->createNamedParameter(IShare::STATUS_ACCEPTED))
383+
->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
384+
->execute();
385+
386+
return $share;
387+
}
388+
323389
/**
324390
* Get all children of this share
325391
* FIXME: remove once https://github.com/owncloud/core/pull/21660 is in
@@ -384,13 +450,13 @@ public function delete(\OCP\Share\IShare $share) {
384450
* Unshare a share from the recipient. If this is a group share
385451
* this means we need a special entry in the share db.
386452
*
387-
* @param \OCP\Share\IShare $share
453+
* @param IShare $share
388454
* @param string $recipient UserId of recipient
389455
* @throws BackendError
390456
* @throws ProviderException
391457
*/
392-
public function deleteFromSelf(\OCP\Share\IShare $share, $recipient) {
393-
if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
458+
public function deleteFromSelf(IShare $share, $recipient) {
459+
if ($share->getShareType() === IShare::TYPE_GROUP) {
394460

395461
$group = $this->groupManager->get($share->getSharedWith());
396462
$user = $this->userManager->get($recipient);
@@ -423,37 +489,24 @@ public function deleteFromSelf(\OCP\Share\IShare $share, $recipient) {
423489
* If there is update it (if required).
424490
*/
425491
if ($data === false) {
426-
$qb = $this->dbConn->getQueryBuilder();
427-
428-
$type = $share->getNodeType();
429-
430-
//Insert new share
431-
$qb->insert('share')
432-
->values([
433-
'share_type' => $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP),
434-
'share_with' => $qb->createNamedParameter($recipient),
435-
'uid_owner' => $qb->createNamedParameter($share->getShareOwner()),
436-
'uid_initiator' => $qb->createNamedParameter($share->getSharedBy()),
437-
'parent' => $qb->createNamedParameter($share->getId()),
438-
'item_type' => $qb->createNamedParameter($type),
439-
'item_source' => $qb->createNamedParameter($share->getNodeId()),
440-
'file_source' => $qb->createNamedParameter($share->getNodeId()),
441-
'file_target' => $qb->createNamedParameter($share->getTarget()),
442-
'permissions' => $qb->createNamedParameter(0),
443-
'stime' => $qb->createNamedParameter($share->getShareTime()->getTimestamp()),
444-
])->execute();
445-
446-
} else if ($data['permissions'] !== 0) {
492+
$userShare = $this->createUserSpecificGroupShare($share, $recipient);
493+
$permissions = $share->getPermissions();
494+
$id = $userShare->getId();
495+
} else {
496+
$permissions = $data['permissions'];
497+
$id = $data['id'];
498+
}
447499

500+
if ($permissions !== 0) {
448501
// Update existing usergroup share
449502
$qb = $this->dbConn->getQueryBuilder();
450503
$qb->update('share')
451504
->set('permissions', $qb->createNamedParameter(0))
452-
->where($qb->expr()->eq('id', $qb->createNamedParameter($data['id'])))
505+
->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
453506
->execute();
454507
}
455508

456-
} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
509+
} else if ($share->getShareType() === IShare::TYPE_USER) {
457510

458511
if ($share->getSharedWith() !== $recipient) {
459512
throw new ProviderException('Recipient does not match');
@@ -466,6 +519,28 @@ public function deleteFromSelf(\OCP\Share\IShare $share, $recipient) {
466519
}
467520
}
468521

522+
protected function createUserSpecificGroupShare(IShare $share, string $recipient): IShare {
523+
$type = $share->getNodeType();
524+
525+
$qb = $this->dbConn->getQueryBuilder();
526+
$qb->insert('share')
527+
->values([
528+
'share_type' => $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP),
529+
'share_with' => $qb->createNamedParameter($recipient),
530+
'uid_owner' => $qb->createNamedParameter($share->getShareOwner()),
531+
'uid_initiator' => $qb->createNamedParameter($share->getSharedBy()),
532+
'parent' => $qb->createNamedParameter($share->getId()),
533+
'item_type' => $qb->createNamedParameter($type),
534+
'item_source' => $qb->createNamedParameter($share->getNodeId()),
535+
'file_source' => $qb->createNamedParameter($share->getNodeId()),
536+
'file_target' => $qb->createNamedParameter($share->getTarget()),
537+
'permissions' => $qb->createNamedParameter($share->getPermissions()),
538+
'stime' => $qb->createNamedParameter($share->getShareTime()->getTimestamp()),
539+
])->execute();
540+
541+
return $this->getShareById($share->getId(), $recipient);
542+
}
543+
469544
/**
470545
* @inheritdoc
471546
*

lib/private/Share20/Manager.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,26 @@ public function updateShare(\OCP\Share\IShare $share) {
927927
return $share;
928928
}
929929

930+
/**
931+
* Accept a share.
932+
*
933+
* @param IShare $share
934+
* @param string $recipientId
935+
* @return IShare The share object
936+
* @throws \InvalidArgumentException
937+
* @since 9.0.0
938+
*/
939+
public function acceptShare(IShare $share, string $recipientId): IShare {
940+
[$providerId, ] = $this->splitFullId($share->getFullId());
941+
$provider = $this->factory->getProvider($providerId);
942+
943+
$provider->acceptShare($share, $recipientId);
944+
$event = new GenericEvent($share);
945+
$this->eventDispatcher->dispatch('OCP\Share::postAcceptShare', $event);
946+
947+
return $share;
948+
}
949+
930950
/**
931951
* Updates the password of the given share if it is not the same as the
932952
* password of the original share.

lib/public/Share/IManager.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public function createShare(IShare $share);
5454
* Update a share.
5555
* The target of the share can't be changed this way: use moveShare
5656
* The share can't be removed this way (permission 0): use deleteShare
57+
* The state can't be changed this way: use acceptShare
5758
*
5859
* @param IShare $share
5960
* @return IShare The share object
@@ -62,6 +63,17 @@ public function createShare(IShare $share);
6263
*/
6364
public function updateShare(IShare $share);
6465

66+
/**
67+
* Accept a share.
68+
*
69+
* @param IShare $share
70+
* @param string $recipientId
71+
* @return IShare The share object
72+
* @throws \InvalidArgumentException
73+
* @since 9.0.0
74+
*/
75+
public function acceptShare(IShare $share, string $recipientId): IShare;
76+
6577
/**
6678
* Delete a share
6779
*

lib/public/Share/IShareProvider.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ public function create(\OCP\Share\IShare $share);
6363
*/
6464
public function update(\OCP\Share\IShare $share);
6565

66+
/**
67+
* Accept a share.
68+
*
69+
* @param IShare $share
70+
* @param string $recipient
71+
* @return IShare The share object
72+
* @since 9.0.0
73+
*/
74+
public function acceptShare(IShare $share, string $recipient): IShare;
75+
6676
/**
6777
* Delete a share
6878
*

0 commit comments

Comments
 (0)