From 015b9b1dac1e7f90e4e28d2b19697bedf83e56fe Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 2 Apr 2014 12:54:41 +0200 Subject: [PATCH 1/2] Add option to getUsersSharingFile() to get the paths for the shared users --- lib/private/share/share.php | 61 ++++++++++++++++++++++++++++++------- lib/public/share.php | 7 +++-- 2 files changed, 54 insertions(+), 14 deletions(-) diff --git a/lib/private/share/share.php b/lib/private/share/share.php index 7bab98b00b..3d22f6fd45 100644 --- a/lib/private/share/share.php +++ b/lib/private/share/share.php @@ -91,23 +91,25 @@ class Share extends \OC\Share\Constants { /** * Find which users can access a shared item - * @param $path to the file - * @param $user owner of the file - * @param include owner to the list of users with access to the file + * @param string $path to the file + * @param string $ownerUser owner of the file + * @param bool $includeOwner include owner to the list of users with access to the file + * @param bool $returnUserPaths Return an array with the user => path map * @return array * @note $path needs to be relative to user data dir, e.g. 'file.txt' * not '/admin/data/file.txt' */ - public static function getUsersSharingFile($path, $user, $includeOwner = false) { + public static function getUsersSharingFile($path, $ownerUser, $includeOwner = false, $returnUserPaths = false) { - $shares = array(); + $shares = $sharePaths = $fileTargets = array(); $publicShare = false; $source = -1; $cache = false; - $view = new \OC\Files\View('/' . $user . '/files'); + $view = new \OC\Files\View('/' . $ownerUser . '/files'); if ($view->file_exists($path)) { $meta = $view->getFileInfo($path); + $path = substr($meta->getPath(), strlen('/' . $ownerUser . '/files')); } else { // if the file doesn't exists yet we start with the parent folder $meta = $view->getFileInfo(dirname($path)); @@ -119,10 +121,9 @@ class Share extends \OC\Share\Constants { } while ($source !== -1) { - // Fetch all shares with another user $query = \OC_DB::prepare( - 'SELECT `share_with` + 'SELECT `share_with`, `file_source`, `file_target` FROM `*PREFIX*share` WHERE @@ -136,12 +137,15 @@ class Share extends \OC\Share\Constants { } else { while ($row = $result->fetchRow()) { $shares[] = $row['share_with']; + if ($returnUserPaths) { + $fileTargets[(int) $row['file_source']][$row['share_with']] = $row; + } } } - // We also need to take group shares into account + // We also need to take group shares into account $query = \OC_DB::prepare( - 'SELECT `share_with` + 'SELECT `share_with`, `file_source`, `file_target` FROM `*PREFIX*share` WHERE @@ -156,6 +160,11 @@ class Share extends \OC\Share\Constants { while ($row = $result->fetchRow()) { $usersInGroup = \OC_Group::usersInGroup($row['share_with']); $shares = array_merge($shares, $usersInGroup); + if ($returnUserPaths) { + foreach ($usersInGroup as $user) { + $fileTargets[(int) $row['file_source']][$user] = $row; + } + } } } @@ -188,9 +197,39 @@ class Share extends \OC\Share\Constants { $source = -1; } } + // Include owner in list of users, if requested if ($includeOwner) { - $shares[] = $user; + $shares[] = $ownerUser; + if ($returnUserPaths) { + $sharePaths[$ownerUser] = $path; + } + } + + if ($returnUserPaths) { + $fileTargetIDs = array_keys($fileTargets); + $fileTargetIDs = array_unique($fileTargetIDs); + + $query = \OC_DB::prepare( + 'SELECT `fileid`, `path` + FROM `*PREFIX*filecache` + WHERE `fileid` IN (' . implode(',', $fileTargetIDs) . ')' + ); + $result = $query->execute(); + + if (\OCP\DB::isError($result)) { + \OCP\Util::writeLog('OCP\Share', \OC_DB::getErrorMessage($result), \OC_Log::ERROR); + } else { + while ($row = $result->fetchRow()) { + foreach ($fileTargets[$row['fileid']] as $uid => $shareData) { + $sharedPath = '/Shared' . $shareData['file_target']; + $sharedPath .= substr($path, strlen($row['path']) -5); + $sharePaths[$uid] = $sharedPath; + } + } + } + + return $sharePaths; } return array("users" => array_unique($shares), "public" => $publicShare); diff --git a/lib/public/share.php b/lib/public/share.php index 564839e86b..c694314ad0 100644 --- a/lib/public/share.php +++ b/lib/public/share.php @@ -64,14 +64,15 @@ class Share extends \OC\Share\Constants { /** * Find which users can access a shared item * @param string $path to the file - * @param string $user owner of the file + * @param string $ownerUser owner of the file * @param bool $includeOwner include owner to the list of users with access to the file + * @param bool $returnUserPaths Return an array with the user => path map * @return array * @note $path needs to be relative to user data dir, e.g. 'file.txt' * not '/admin/data/file.txt' */ - public static function getUsersSharingFile($path, $user, $includeOwner = false) { - return \OC\Share\Share::getUsersSharingFile($path, $user, $includeOwner); + public static function getUsersSharingFile($path, $ownerUser, $includeOwner = false, $returnUserPaths = false) { + return \OC\Share\Share::getUsersSharingFile($path, $ownerUser, $includeOwner, $returnUserPaths); } /** From d418e176ce4ce3ee07a5b9e632150470c0387f87 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 9 Apr 2014 15:01:39 +0200 Subject: [PATCH 2/2] Do not query when the list is empty --- lib/private/share/share.php | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/lib/private/share/share.php b/lib/private/share/share.php index 3d22f6fd45..59826d0303 100644 --- a/lib/private/share/share.php +++ b/lib/private/share/share.php @@ -210,21 +210,23 @@ class Share extends \OC\Share\Constants { $fileTargetIDs = array_keys($fileTargets); $fileTargetIDs = array_unique($fileTargetIDs); - $query = \OC_DB::prepare( - 'SELECT `fileid`, `path` - FROM `*PREFIX*filecache` - WHERE `fileid` IN (' . implode(',', $fileTargetIDs) . ')' - ); - $result = $query->execute(); + if (!empty($fileTargetIDs)) { + $query = \OC_DB::prepare( + 'SELECT `fileid`, `path` + FROM `*PREFIX*filecache` + WHERE `fileid` IN (' . implode(',', $fileTargetIDs) . ')' + ); + $result = $query->execute(); - if (\OCP\DB::isError($result)) { - \OCP\Util::writeLog('OCP\Share', \OC_DB::getErrorMessage($result), \OC_Log::ERROR); - } else { - while ($row = $result->fetchRow()) { - foreach ($fileTargets[$row['fileid']] as $uid => $shareData) { - $sharedPath = '/Shared' . $shareData['file_target']; - $sharedPath .= substr($path, strlen($row['path']) -5); - $sharePaths[$uid] = $sharedPath; + if (\OCP\DB::isError($result)) { + \OCP\Util::writeLog('OCP\Share', \OC_DB::getErrorMessage($result), \OC_Log::ERROR); + } else { + while ($row = $result->fetchRow()) { + foreach ($fileTargets[$row['fileid']] as $uid => $shareData) { + $sharedPath = '/Shared' . $shareData['file_target']; + $sharedPath .= substr($path, strlen($row['path']) -5); + $sharePaths[$uid] = $sharedPath; + } } } }