diff options
author | Roeland Jago Douma <rullzer@owncloud.com> | 2015-12-15 09:53:16 +0100 |
---|---|---|
committer | Roeland Jago Douma <rullzer@owncloud.com> | 2016-01-06 14:53:43 +0100 |
commit | a08c497808e6dde60508c1eaaaaad819e9210814 (patch) | |
tree | e1a63d331e66d1bd9454be050c2afa8278520777 /lib/private | |
parent | 4f2e84a0eccfb5e85102a30a93799522e510a375 (diff) | |
download | nextcloud-server-a08c497808e6dde60508c1eaaaaad819e9210814.tar.gz nextcloud-server-a08c497808e6dde60508c1eaaaaad819e9210814.zip |
[Share 2.0] Make share provider ready for create shares
Diffstat (limited to 'lib/private')
-rw-r--r-- | lib/private/share20/defaultshareprovider.php | 124 | ||||
-rw-r--r-- | lib/private/share20/ishareprovider.php | 3 |
2 files changed, 114 insertions, 13 deletions
diff --git a/lib/private/share20/defaultshareprovider.php b/lib/private/share20/defaultshareprovider.php index bc3bc0ce9ed..a7155644920 100644 --- a/lib/private/share20/defaultshareprovider.php +++ b/lib/private/share20/defaultshareprovider.php @@ -64,11 +64,90 @@ class DefaultShareProvider implements IShareProvider { /** * Share a path - * + * * @param IShare $share * @return IShare The share object + * @throws ShareNotFound + * @throws \Exception */ public function create(IShare $share) { + $qb = $this->dbConn->getQueryBuilder(); + + $qb->insert('share'); + $qb->setValue('share_type', $qb->createNamedParameter($share->getShareType())); + + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { + //Set the UID of the user we share with + $qb->setValue('share_with', $qb->createNamedParameter($share->getSharedWith()->getUID())); + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { + //Set the GID of the group we share with + $qb->setValue('share_with', $qb->createNamedParameter($share->getSharedWith()->getGID())); + } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { + //Set the token of the share + $qb->setValue('token', $qb->createNamedParameter($share->getToken())); + + //If a password is set store it + if ($share->getPassword() !== null) { + $qb->setValue('share_with', $qb->createNamedParameter($share->getPassword())); + } + + //If an expiration date is set store it + if ($share->getExpirationDate() !== null) { + $qb->setValue('expiration', $qb->createNamedParameter($share->getExpirationDate(), 'datetime')); + } + } else { + throw new \Exception('invalid share type!'); + } + + // Set what is shares + $qb->setValue('item_type', $qb->createParameter('itemType')); + if ($share->getPath() instanceof \OCP\Files\File) { + $qb->setParameter('itemType', 'file'); + } else { + $qb->setParameter('itemType', 'folder'); + } + + // Set the file id + $qb->setValue('item_source', $qb->createNamedParameter($share->getPath()->getId())); + $qb->setValue('file_source', $qb->createNamedParameter($share->getPath()->getId())); + + // set the permissions + $qb->setValue('permissions', $qb->createNamedParameter($share->getPermissions())); + + // Set who created this share + $qb->setValue('uid_initiator', $qb->createNamedParameter($share->getSharedBy()->getUID())); + + // Set who is the owner of this file/folder (and this the owner of the share) + $qb->setValue('uid_owner', $qb->createNamedParameter($share->getShareOwner()->getUID())); + + // Set the file target + $qb->setValue('file_target', $qb->createNamedParameter($share->getTarget())); + + // Set the time this share was created + $qb->setValue('stime', $qb->createNamedParameter(time())); + + // insert the data and fetch the id of the share + $this->dbConn->beginTransaction(); + $qb->execute(); + $id = $this->dbConn->lastInsertId('*PREFIX*share'); + $this->dbConn->commit(); + + // Now fetch the inserted share and create a complete share object + $qb = $this->dbConn->getQueryBuilder(); + $qb->select('*') + ->from('*PREFIX*share') + ->where($qb->expr()->eq('id', $qb->createNamedParameter($id))); + + $cursor = $qb->execute(); + $data = $cursor->fetch(); + $cursor->closeCursor(); + + if ($data === false) { + throw new ShareNotFound(); + } + + $share = $this->createShare($data); + return $share; } /** @@ -170,11 +249,29 @@ class DefaultShareProvider implements IShareProvider { /** * Get shares for a given path * - * @param \OCP\IUser $user * @param \OCP\Files\Node $path * @return IShare[] */ - public function getSharesByPath(IUser $user, Node $path) { + public function getSharesByPath(Node $path) { + $qb = $this->dbConn->getQueryBuilder(); + + $cursor = $qb->select('*') + ->from('share') + ->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($path->getId()))) + ->andWhere( + $qb->expr()->orX( + $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)), + $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)) + ) + )->execute(); + + $shares = []; + while($data = $cursor->fetch()) { + $shares[] = $this->createShare($data); + } + $cursor->closeCursor(); + + return $shares; } /** @@ -223,16 +320,21 @@ class DefaultShareProvider implements IShareProvider { $share->setSharedWith($data['share_with']); } - $share->setSharedBy($this->userManager->get($data['uid_owner'])); - - // TODO: getById can return an array. How to handle this properly?? - $folder = $this->rootFolder->getUserFolder($share->getSharedBy()->getUID()); - $path = $folder->getById((int)$data['file_source'])[0]; + if ($data['uid_initiator'] === null) { + //OLD SHARE + $share->setSharedBy($this->userManager->get($data['uid_owner'])); + $folder = $this->rootFolder->getUserFolder($share->getSharedBy()->getUID()); + $path = $folder->getById((int)$data['file_source'])[0]; - $owner = $path->getOwner(); - $share->setShareOwner($owner); + $owner = $path->getOwner(); + $share->setShareOwner($owner); + } else { + //New share! + $share->setSharedBy($this->userManager->get($data['uid_initiator'])); + $share->setShareOwner($this->userManager->get($data['uid_owner'])); + } - $path = $this->rootFolder->getUserFolder($owner->getUID())->getById((int)$data['file_source'])[0]; + $path = $this->rootFolder->getUserFolder($share->getShareOwner()->getUID())->getById((int)$data['file_source'])[0]; $share->setPath($path); if ($data['expiration'] !== null) { diff --git a/lib/private/share20/ishareprovider.php b/lib/private/share20/ishareprovider.php index 56a550acf71..97a2b728d5f 100644 --- a/lib/private/share20/ishareprovider.php +++ b/lib/private/share20/ishareprovider.php @@ -81,11 +81,10 @@ interface IShareProvider { /** * Get shares for a given path * - * @param \OCP\IUser $user * @param \OCP\Files\Node $path * @return IShare[] */ - public function getSharesByPath(\OCP\IUser $user, \OCP\Files\Node $path); + public function getSharesByPath(\OCP\Files\Node $path); /** * Get shared with the given user |