From 2316cb1f8b9704621b47e6eb1c2cbf30c06c552d Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Fri, 29 Jan 2016 10:27:39 +0100 Subject: [Share 2.0] Allow moving of shares * Only recipient can move a share * Unit tests --- lib/private/share20/defaultshareprovider.php | 58 ++++++++++++++++++++++++++++ lib/private/share20/manager.php | 19 +++++++++ 2 files changed, 77 insertions(+) (limited to 'lib/private/share20') diff --git a/lib/private/share20/defaultshareprovider.php b/lib/private/share20/defaultshareprovider.php index ed9c03020b6..224dddf4019 100644 --- a/lib/private/share20/defaultshareprovider.php +++ b/lib/private/share20/defaultshareprovider.php @@ -20,6 +20,7 @@ */ namespace OC\Share20; +use OCP\Files\File; use OCP\Share\IShareProvider; use OC\Share20\Exception\InvalidShare; use OC\Share20\Exception\ProviderException; @@ -383,6 +384,63 @@ class DefaultShareProvider implements IShareProvider { } } + /** + * @inheritdoc + */ + public function move(\OCP\Share\IShare $share, IUser $recipient) { + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { + // Just update the target + $qb = $this->dbConn->getQueryBuilder(); + $qb->update('share') + ->set('file_target', $qb->createNamedParameter($share->getTarget())) + ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId()))) + ->execute(); + + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { + + // Check if there is a usergroup share + $qb = $this->dbConn->getQueryBuilder(); + $stmt = $qb->select('id') + ->from('share') + ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP))) + ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($recipient->getUID()))) + ->andWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId()))) + ->setMaxResults(1) + ->execute(); + + $data = $stmt->fetch(); + $stmt->closeCursor(); + + if ($data === false) { + // No usergroup share yet. Create one. + $qb = $this->dbConn->getQueryBuilder(); + $qb->insert('share') + ->values([ + 'share_type' => $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP), + 'share_with' => $qb->createNamedParameter($recipient->getUID()), + 'uid_owner' => $qb->createNamedParameter($share->getShareOwner()->getUID()), + 'uid_initiator' => $qb->createNamedParameter($share->getSharedBy()->getUID()), + 'parent' => $qb->createNamedParameter($share->getId()), + 'item_type' => $qb->createNamedParameter($share->getNode() instanceof File ? 'file' : 'folder'), + 'item_source' => $qb->createNamedParameter($share->getNode()->getId()), + 'file_source' => $qb->createNamedParameter($share->getNode()->getId()), + 'file_target' => $qb->createNamedParameter($share->getTarget()), + 'permissions' => $qb->createNamedParameter($share->getPermissions()), + 'stime' => $qb->createNamedParameter($share->getShareTime()->getTimestamp()), + ])->execute(); + } else { + // Already a usergroup share. Update it. + $qb = $this->dbConn->getQueryBuilder(); + $qb->update('share') + ->set('file_target', $qb->createNamedParameter($share->getTarget())) + ->where($qb->expr()->eq('id', $qb->createNamedParameter($data['id']))) + ->execute(); + } + } + + return $share; + } + /** * Get all shares by the given user. Sharetype and path can be used to filter. * diff --git a/lib/private/share20/manager.php b/lib/private/share20/manager.php index 3c5bd197ae8..438396501e3 100644 --- a/lib/private/share20/manager.php +++ b/lib/private/share20/manager.php @@ -714,6 +714,25 @@ class Manager implements IManager { $provider->deleteFromSelf($share, $recipient); } + /** + * @inheritdoc + */ + public function moveShare(\OCP\Share\IShare $share, IUser $recipient) { + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { + throw new \InvalidArgumentException('Can\'t change target of link share'); + } + + if (($share->getShareType() === \OCP\Share::SHARE_TYPE_USER && $share->getSharedWith() !== $recipient) || + ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP && !$share->getSharedWith()->inGroup($recipient))) { + throw new \InvalidArgumentException('Invalid recipient'); + } + + list($providerId, ) = $this->splitFullId($share->getId()); + $provider = $this->factory->getProvider($providerId); + + $provider->move($share, $recipient); + } + /** * Get shares shared by (initiated) by the provided user. * -- cgit v1.2.3