diff options
author | Roeland Jago Douma <rullzer@owncloud.com> | 2015-12-03 10:51:41 +0100 |
---|---|---|
committer | Roeland Jago Douma <rullzer@owncloud.com> | 2016-01-21 15:56:44 +0100 |
commit | 3666c34a197adb75c6b488f877d5b3b2279d0dea (patch) | |
tree | b44409c1e1782167c923d8cf9459beb28752445c | |
parent | e2f231d05156dc382db32397e64f2df790a88a7b (diff) | |
download | nextcloud-server-3666c34a197adb75c6b488f877d5b3b2279d0dea.tar.gz nextcloud-server-3666c34a197adb75c6b488f877d5b3b2279d0dea.zip |
[Sharing 2.0] Start with getShares
-rw-r--r-- | apps/files_sharing/api/ocssharewrapper.php | 2 | ||||
-rw-r--r-- | apps/files_sharing/api/share20ocs.php | 56 | ||||
-rw-r--r-- | lib/private/share20/defaultshareprovider.php | 92 | ||||
-rw-r--r-- | lib/private/share20/ishareprovider.php | 10 | ||||
-rw-r--r-- | lib/private/share20/manager.php | 39 |
5 files changed, 185 insertions, 14 deletions
diff --git a/apps/files_sharing/api/ocssharewrapper.php b/apps/files_sharing/api/ocssharewrapper.php index a186a34cf6a..cc52d478615 100644 --- a/apps/files_sharing/api/ocssharewrapper.php +++ b/apps/files_sharing/api/ocssharewrapper.php @@ -37,7 +37,7 @@ class OCSShareWrapper { } public function getAllShares($params) { - return \OCA\Files_Sharing\API\Local::getAllShares($params); + return $this->getShare20OCS()->getShares(); } public function createShare() { diff --git a/apps/files_sharing/api/share20ocs.php b/apps/files_sharing/api/share20ocs.php index e4cc50d9c1a..56cc50d34fa 100644 --- a/apps/files_sharing/api/share20ocs.php +++ b/apps/files_sharing/api/share20ocs.php @@ -327,6 +327,62 @@ class Share20OCS { return new \OC_OCS_Result($share); } + private function getSharedWithMe() { + $userShares = $this->shareManager->getSharedWith($this->currentUser, \OCP\Share::SHARE_TYPE_USER, -1, 0); + $groupShares = $this->shareManager->getSharedWith($this->currentUser, \OCP\Share::SHARE_TYPE_GROUP, -1, 0); + //TODO add federated provider + + $shares = array_merge($userShares, $groupShares); + + $formatted = []; + foreach ($shares as $share) { + $formatted[] = $this->formatShare($share); + } + + return new \OC_OCS_Result($formatted); + } + + public function getShares() { + $sharedWithMe = $this->request->getParam('shared_with_me', null); + $reshares = $this->request->getParam('reshares', null); + $subfiles = $this->request->getParam('subfiles'); + $path = $this->request->getParam('path', null); + + if ($sharedWithMe === 'true') { + return $this->getSharedWithMe(); + } + + if ($path !== null) { + $userFolder = $this->rootFolder->getUserFolder($this->currentUser->getUID()); + try { + $path = $userFolder->get($path); + } catch (\OCP\Files\NotFoundException $e) { + return new \OC_OCS_Result(null, 404, 'wrong path, file/folder doesn\'t exist'); + } + } + + if ($reshares === 'true') { + $reshares = true; + } else { + $reshares = false; + } + + // Get all shares + $userShares = $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_USER, $path, $reshares, -1, 0); + $groupShares = $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_GROUP, $path, $reshares, -1, 0); + $linkShares = $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_LINK, $path, $reshares, -1, 0); + //TODO: Add federated shares + + $shares = array_merge($userShares, $groupShares, $linkShares); + + $formatted = []; + foreach ($shares as $share) { + $formatted[] = $this->formatShare($share); + } + + return new \OC_OCS_Result($formatted); + } + /** * @param IShare $share * @return bool diff --git a/lib/private/share20/defaultshareprovider.php b/lib/private/share20/defaultshareprovider.php index d47919d21a3..d296760412c 100644 --- a/lib/private/share20/defaultshareprovider.php +++ b/lib/private/share20/defaultshareprovider.php @@ -240,15 +240,56 @@ class DefaultShareProvider implements IShareProvider { } /** - * Get all shares by the given user + * Get all shares by the given user. Sharetype and path can be used to filter. * * @param IUser $user * @param int $shareType + * @param \OCP\Files\File|\OCP\Files\Folder $path + * @param bool $reshares + * @param int $limit The maximum number of shares to be returned, -1 for all shares * @param int $offset - * @param int $limit * @return Share[] */ - public function getShares(IUser $user, $shareType, $offset, $limit) { + public function getSharesBy(IUser $user, $shareType, $path, $reshares, $limit, $offset) { + $qb = $this->dbConn->getQueryBuilder(); + $qb->select('*') + ->from('share'); + + $qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter($shareType))); + + /** + * Reshares for this user are shares where they are the owner. + */ + if ($reshares === false) { + $qb->andWhere($qb->expr()->eq('uid_initiator', $qb->createNamedParameter($user->getUID()))); + } else { + $qb->andWhere( + $qb->expr()->orX( + $qb->expr()->eq('uid_owner', $qb->createNamedParameter($user->getUID())), + $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($user->getUID())) + ) + ); + } + + if ($path !== null) { + $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($path->getId()))); + } + + if ($limit !== -1) { + $qb->setMaxResults($limit); + $qb->setFirstResult($offset); + } + + $qb->orderBy('id'); + + $cursor = $qb->execute(); + $shares = []; + while($data = $cursor->fetch()) { + $shares[] = $this->createShare($data); + } + $cursor->closeCursor(); + + return $shares; } /** @@ -326,9 +367,50 @@ class DefaultShareProvider implements IShareProvider { * * @param IUser $user * @param int $shareType - * @param Share + * @param int $limit The maximum number of shares, -1 for all + * @param int $offset + * @return IShare[] + * @throws BackendError */ - public function getSharedWithMe(IUser $user, $shareType = null) { + public function getSharedWith(IUser $user, $shareType, $limit, $offset) { + $shares = []; + + if ($shareType === \OCP\Share::SHARE_TYPE_USER) { + //Get shares directly w ith me + $qb = $this->dbConn->getQueryBuilder(); + $qb->select('*') + ->from('share'); + + // Order by id + $qb->orderBy('id'); + + // Set limit and offset + if ($limit !== -1) { + $qb->setMaxResults($limit); + } + $qb->setFirstResult($offset); + + $qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER))); + $qb->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($user->getUID()))); + + $cursor = $qb->execute(); + + while($data = $cursor->fetch()) { + $shares[] = $this->createShare($data); + } + $cursor->closeCursor(); + + } else if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) { + //TODO This can certianly be optimized at some point. But for now keep it simple and working + + //TODO Get group shares for group of $user + //TODO Filter out user modified group shares and replace but keep original share stuff + } else { + throw new BackendError(); + } + + + return $shares; } /** diff --git a/lib/private/share20/ishareprovider.php b/lib/private/share20/ishareprovider.php index 81770a45874..a5a9d3b6708 100644 --- a/lib/private/share20/ishareprovider.php +++ b/lib/private/share20/ishareprovider.php @@ -62,11 +62,13 @@ interface IShareProvider { * * @param IUser $user * @param int $shareType + * @param \OCP\Files\File|\OCP\Files\Folder $path + * @param bool $reshares + * @param int $limit The maximum number of shares to be returned, -1 for all shares * @param int $offset - * @param int $limit * @return Share[] */ - public function getShares(IUser $user, $shareType, $offset, $limit); + public function getSharesBy(IUser $user, $shareType, $path, $reshares, $limit, $offset); /** * Get share by id @@ -98,9 +100,11 @@ interface IShareProvider { * * @param IUser $user * @param int $shareType + * @param int $limit The max number of entries returned, -1 for all + * @param int $offset * @param Share */ - public function getSharedWithMe(IUser $user, $shareType = null); + public function getSharedWith(IUser $user, $shareType, $limit, $offset); /** * Get a share by token diff --git a/lib/private/share20/manager.php b/lib/private/share20/manager.php index 2be8fb5174d..7d454b26f00 100644 --- a/lib/private/share20/manager.php +++ b/lib/private/share20/manager.php @@ -605,14 +605,43 @@ class Manager { \OC_Hook::emit('OCP\Share', 'post_unshare', $hookParams); } + /** - * Retrieve all shares by the current user + * Get shares shared by (initiated) by the provided user. * - * @param int $page - * @param int $perPage - * @return Share[] + * @param IUser $user + * @param int $shareType + * @param \OCP\Files\File|\OCP\Files\Folder $path + * @param bool $reshares + * @param int $limit The maximum number of returned results, -1 for all results + * @param int $offset + * @return IShare[] */ - public function getShares($page=0, $perPage=50) { + public function getSharesBy(IUser $user, $shareType, $path = null, $reshares = false, $limit = 50, $offset = 0) { + if ($path !== null && + !($path instanceof \OCP\Files\File) && + !($path instanceof \OCP\Files\Folder)) { + throw new \InvalidArgumentException('invalid path'); + } + + $provider = $this->factory->getProviderForType($shareType); + + return $provider->getSharesBy($user, $shareType, $path, $reshares, $limit, $offset); + } + + /** + * Get shares shared with $user. + * + * @param IUser $user + * @param int $shareType + * @param int $limit The maximum number of shares returned, -1 for all + * @param int $offset + * @return IShare[] + */ + public function getSharedWith(IUser $user, $shareType, $limit = 50, $offset = 0) { + $provider = $this->factory->getProviderForType($shareType); + + return $provider->getSharedWith($user, $shareType, $limit, $offset); } /** |