diff options
author | Björn Schießle <bjoern@schiessle.org> | 2013-10-17 11:07:47 -0700 |
---|---|---|
committer | Björn Schießle <bjoern@schiessle.org> | 2013-10-17 11:07:47 -0700 |
commit | 76be7cd1ac54a8f4a5707c7a50eee106383aa51d (patch) | |
tree | 3a8bfdb78405026f42ba351813f8d43d4f537592 /apps/files_sharing/lib/api.php | |
parent | 2845747528e1e79fa8a4d44768e83568aa61c4b7 (diff) | |
parent | e7dc6b21c8994a7ade7d88ab4e27957e8a4ec9c9 (diff) | |
download | nextcloud-server-76be7cd1ac54a8f4a5707c7a50eee106383aa51d.tar.gz nextcloud-server-76be7cd1ac54a8f4a5707c7a50eee106383aa51d.zip |
Merge pull request #5382 from owncloud/sharing_api_include_reshares
add reshare option for the OCS Share API
Diffstat (limited to 'apps/files_sharing/lib/api.php')
-rw-r--r-- | apps/files_sharing/lib/api.php | 110 |
1 files changed, 86 insertions, 24 deletions
diff --git a/apps/files_sharing/lib/api.php b/apps/files_sharing/lib/api.php index bd9beddf063..d92d30156cb 100644 --- a/apps/files_sharing/lib/api.php +++ b/apps/files_sharing/lib/api.php @@ -36,10 +36,17 @@ class Api { $params['itemSource'] = self::getFileId($_GET['path']); $params['path'] = $_GET['path']; $params['itemType'] = self::getItemType($_GET['path']); - if (isset($_GET['subfiles']) && $_GET['subfiles'] === 'true') { + + if ( isset($_GET['reshares']) && $_GET['reshares'] !== 'false' ) { + $params['reshares'] = true; + } else { + $params['reshares'] = false; + } + + if (isset($_GET['subfiles']) && $_GET['subfiles'] !== 'false') { return self::getSharesFromFolder($params); } - return self::getShare($params); + return self::collectShares($params); } $share = \OCP\Share::getItemShared('file', null); @@ -59,34 +66,49 @@ class Api { * @return \OC_OCS_Result share information */ public static function getShare($params) { - // either the $params already contains a itemSource if we come from - // getAllShare() or we need to translate the shareID to a itemSource - if(isset($params['itemSource'])) { - $itemSource = $params['itemSource']; - $itemType = $params['itemType']; - $getSpecificShare = true; - } else { - $s = self::getShareFromId($params['id']); - $itemSource = $s['item_source']; - $itemType = $s['item_type']; - $getSpecificShare = false; - } + + $s = self::getShareFromId($params['id']); + $params['itemSource'] = $s['item_source']; + $params['itemType'] = $s['item_type']; + $params['specificShare'] = true; + + return self::collectShares($params); + } + + /** + * @brief collect all share information, either of a specific share or all + * shares for a given path + * @param array $params + * @return \OC_OCS_Result + */ + private static function collectShares($params) { + + $itemSource = $params['itemSource']; + $itemType = $params['itemType']; + $getSpecificShare = isset($params['specificShare']) ? $params['specificShare'] : false; if ($itemSource !== null) { $shares = \OCP\Share::getItemShared($itemType, $itemSource); - $reshare = \OCP\Share::getItemSharedWithBySource($itemType, $itemSource); + $receivedFrom = \OCP\Share::getItemSharedWithBySource($itemType, $itemSource); // if a specific share was specified only return this one - if ($getSpecificShare === false) { + if ($getSpecificShare === true) { foreach ($shares as $share) { - if ($share['id'] === (int)$params['id']) { + if ($share['id'] === (int) $params['id']) { $shares = array('element' => $share); break; } } } - if ($reshare) { - $shares['received_from'] = $reshare['uid_owner']; - $shares['received_from_displayname'] = \OCP\User::getDisplayName($reshare['uid_owner']); + + // include also reshares in the lists. This means that the result + // will contain every user with access to the file. + if (isset($params['reshares']) && $params['reshares'] === true) { + $shares = self::addReshares($shares, $itemSource); + } + + if ($receivedFrom) { + $shares['received_from'] = $receivedFrom['uid_owner']; + $shares['received_from_displayname'] = \OCP\User::getDisplayName($receivedFrom['uid_owner']); } } else { $shares = null; @@ -100,6 +122,46 @@ class Api { } /** + * @brief add reshares to a array of shares + * @param array $shares array of shares + * @param int $itemSource item source ID + * @return array new shares array which includes reshares + */ + private static function addReshares($shares, $itemSource) { + + // if there are no shares than there are also no reshares + if (count($shares) > 0) { + $ids = array(); + $firstShare = reset($shares); + $path = $firstShare['path']; + foreach ($shares as $share) { + $ids[] = $share['id']; + } + } else { + return $shares; + } + + $select = '`*PREFIX*share`.`id`, `item_type`, `*PREFIX*share`.`parent`, `share_type`, `share_with`, `file_source`, `path` , `permissions`, `stime`, `expiration`, `token`, `storage`, `mail_send`, `mail_send`'; + $getReshares = \OC_DB::prepare('SELECT ' . $select . ' FROM `*PREFIX*share` INNER JOIN `*PREFIX*filecache` ON `file_source` = `*PREFIX*filecache`.`fileid` WHERE `*PREFIX*share`.`file_source` = ? AND `*PREFIX*share`.`item_type` IN (\'file\', \'folder\')'); + $reshares = $getReshares->execute(array($itemSource))->fetchAll(); + + foreach ($reshares as $key => $reshare) { + // remove reshares we already have in the shares array. + if (in_array($reshare['id'], $ids)) { + unset($reshares[$key]); + continue; + } + if (isset($reshare['share_with']) && $reshare['share_with'] !== '') { + $reshares[$key]['share_with_displayname'] = \OCP\User::getDisplayName($reshare['share_with']); + } + // add correct path to the result + $reshares[$key]['path'] = $path; + } + + return array_merge($shares, $reshares); + } + + /** * @brief get share from all files in a given folder (non-recursive) * @param array $params contains 'path' to the folder * @return \OC_OCS_Result @@ -119,10 +181,10 @@ class Api { // workaround because folders are named 'dir' in this context $itemType = $file['type'] === 'file' ? 'file' : 'folder'; $share = \OCP\Share::getItemShared($itemType, $file['fileid']); - $reshare = \OCP\Share::getItemSharedWithBySource($itemType, $file['fileid']); - if ($reshare) { - $share['received_from'] = $reshare['uid_owner']; - $share['received_from_displayname'] = \OCP\User::getDisplayName($reshare['uid_owner']); + $receivedFrom = \OCP\Share::getItemSharedWithBySource($itemType, $file['fileid']); + if ($receivedFrom) { + $share['received_from'] = $receivedFrom['uid_owner']; + $share['received_from_displayname'] = \OCP\User::getDisplayName($receivedFrom['uid_owner']); } if ($share) { $share['filename'] = $file['name']; |