aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/Share20/DefaultShareProvider.php
diff options
context:
space:
mode:
authorJoas Schilling <coding@schilljs.com>2019-09-04 16:50:52 +0200
committerJoas Schilling <coding@schilljs.com>2019-11-12 17:36:58 +0100
commit520042bbd0512e19717d18705c3b045b2d8400a7 (patch)
tree286e21a2dd06feca025a7dabe2dcbb63d65c5498 /lib/private/Share20/DefaultShareProvider.php
parentc79a56481bc4bd9fb94b0dfbf483537400c76569 (diff)
downloadnextcloud-server-520042bbd0512e19717d18705c3b045b2d8400a7.tar.gz
nextcloud-server-520042bbd0512e19717d18705c3b045b2d8400a7.zip
Allow to accept group shares
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'lib/private/Share20/DefaultShareProvider.php')
-rw-r--r--lib/private/Share20/DefaultShareProvider.php125
1 files changed, 99 insertions, 26 deletions
diff --git a/lib/private/Share20/DefaultShareProvider.php b/lib/private/Share20/DefaultShareProvider.php
index 66d28869dbd..ffcaf4f1065 100644
--- a/lib/private/Share20/DefaultShareProvider.php
+++ b/lib/private/Share20/DefaultShareProvider.php
@@ -321,6 +321,71 @@ class DefaultShareProvider implements IShareProvider {
}
/**
+ * Accept a share.
+ *
+ * @param IShare $share
+ * @param string $recipient
+ * @return IShare The share object
+ * @since 9.0.0
+ */
+ public function acceptShare(IShare $share, string $recipient): IShare {
+ if ($share->getShareType() === IShare::TYPE_GROUP) {
+ $group = $this->groupManager->get($share->getSharedWith());
+ $user = $this->userManager->get($recipient);
+
+ if (is_null($group)) {
+ throw new ProviderException('Group "' . $share->getSharedWith() . '" does not exist');
+ }
+
+ if (!$group->inGroup($user)) {
+ throw new ProviderException('Recipient not in receiving group');
+ }
+
+ // Try to fetch user specific share
+ $qb = $this->dbConn->getQueryBuilder();
+ $stmt = $qb->select('*')
+ ->from('share')
+ ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)))
+ ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($recipient)))
+ ->andWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))
+ ->andWhere($qb->expr()->orX(
+ $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
+ $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
+ ))
+ ->execute();
+
+ $data = $stmt->fetch();
+
+ /*
+ * Check if there already is a user specific group share.
+ * If there is update it (if required).
+ */
+ if ($data === false) {
+ $id = $this->createUserSpecificGroupShare($share, $recipient);
+ } else {
+ $id = $data['id'];
+ }
+
+ } else if ($share->getShareType() === IShare::TYPE_USER) {
+ if ($share->getSharedWith() !== $recipient) {
+ throw new ProviderException('Recipient does not match');
+ }
+
+ $id = $share->getId();
+ } else {
+ throw new ProviderException('Invalid shareType');
+ }
+
+ $qb = $this->dbConn->getQueryBuilder();
+ $qb->update('share')
+ ->set('accepted', $qb->createNamedParameter(IShare::STATUS_ACCEPTED))
+ ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
+ ->execute();
+
+ return $share;
+ }
+
+ /**
* Get all children of this share
* FIXME: remove once https://github.com/owncloud/core/pull/21660 is in
*
@@ -384,13 +449,13 @@ class DefaultShareProvider implements IShareProvider {
* Unshare a share from the recipient. If this is a group share
* this means we need a special entry in the share db.
*
- * @param \OCP\Share\IShare $share
+ * @param IShare $share
* @param string $recipient UserId of recipient
* @throws BackendError
* @throws ProviderException
*/
- public function deleteFromSelf(\OCP\Share\IShare $share, $recipient) {
- if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
+ public function deleteFromSelf(IShare $share, $recipient) {
+ if ($share->getShareType() === IShare::TYPE_GROUP) {
$group = $this->groupManager->get($share->getSharedWith());
$user = $this->userManager->get($recipient);
@@ -423,37 +488,23 @@ class DefaultShareProvider implements IShareProvider {
* If there is update it (if required).
*/
if ($data === false) {
- $qb = $this->dbConn->getQueryBuilder();
-
- $type = $share->getNodeType();
-
- //Insert new share
- $qb->insert('share')
- ->values([
- 'share_type' => $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP),
- 'share_with' => $qb->createNamedParameter($recipient),
- 'uid_owner' => $qb->createNamedParameter($share->getShareOwner()),
- 'uid_initiator' => $qb->createNamedParameter($share->getSharedBy()),
- 'parent' => $qb->createNamedParameter($share->getId()),
- 'item_type' => $qb->createNamedParameter($type),
- 'item_source' => $qb->createNamedParameter($share->getNodeId()),
- 'file_source' => $qb->createNamedParameter($share->getNodeId()),
- 'file_target' => $qb->createNamedParameter($share->getTarget()),
- 'permissions' => $qb->createNamedParameter(0),
- 'stime' => $qb->createNamedParameter($share->getShareTime()->getTimestamp()),
- ])->execute();
-
- } else if ($data['permissions'] !== 0) {
+ $id = $this->createUserSpecificGroupShare($share, $recipient);
+ $permissions = $share->getPermissions();
+ } else {
+ $permissions = $data['permissions'];
+ $id = $data['id'];
+ }
+ if ($permissions !== 0) {
// Update existing usergroup share
$qb = $this->dbConn->getQueryBuilder();
$qb->update('share')
->set('permissions', $qb->createNamedParameter(0))
- ->where($qb->expr()->eq('id', $qb->createNamedParameter($data['id'])))
+ ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
->execute();
}
- } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
+ } else if ($share->getShareType() === IShare::TYPE_USER) {
if ($share->getSharedWith() !== $recipient) {
throw new ProviderException('Recipient does not match');
@@ -466,6 +517,28 @@ class DefaultShareProvider implements IShareProvider {
}
}
+ protected function createUserSpecificGroupShare(IShare $share, string $recipient): int {
+ $type = $share->getNodeType();
+
+ $qb = $this->dbConn->getQueryBuilder();
+ $qb->insert('share')
+ ->values([
+ 'share_type' => $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP),
+ 'share_with' => $qb->createNamedParameter($recipient),
+ 'uid_owner' => $qb->createNamedParameter($share->getShareOwner()),
+ 'uid_initiator' => $qb->createNamedParameter($share->getSharedBy()),
+ 'parent' => $qb->createNamedParameter($share->getId()),
+ 'item_type' => $qb->createNamedParameter($type),
+ 'item_source' => $qb->createNamedParameter($share->getNodeId()),
+ 'file_source' => $qb->createNamedParameter($share->getNodeId()),
+ 'file_target' => $qb->createNamedParameter($share->getTarget()),
+ 'permissions' => $qb->createNamedParameter($share->getPermissions()),
+ 'stime' => $qb->createNamedParameter($share->getShareTime()->getTimestamp()),
+ ])->execute();
+
+ return $qb->getLastInsertId();
+ }
+
/**
* @inheritdoc
*