From f52506793083d44674586257eadb230d02b15bba Mon Sep 17 00:00:00 2001 From: Louis Chemineau Date: Thu, 23 Jun 2022 16:32:52 +0200 Subject: [PATCH] Add argument to getSharesInFolder enable deep share lookup Signed-off-by: Louis Chemineau --- .../lib/FederatedShareProvider.php | 11 ++++++--- apps/files_sharing/lib/Updater.php | 2 +- apps/sharebymail/lib/ShareByMailProvider.php | 12 ++++++---- lib/private/Share20/DefaultShareProvider.php | 23 ++++++++++++------ lib/private/Share20/Manager.php | 24 +++---------------- lib/public/Share/IManager.php | 11 ++------- lib/public/Share/IShareProvider.php | 3 ++- 7 files changed, 40 insertions(+), 46 deletions(-) diff --git a/apps/federatedfilesharing/lib/FederatedShareProvider.php b/apps/federatedfilesharing/lib/FederatedShareProvider.php index 52b10cd9315..d504a3e2acc 100644 --- a/apps/federatedfilesharing/lib/FederatedShareProvider.php +++ b/apps/federatedfilesharing/lib/FederatedShareProvider.php @@ -638,7 +638,7 @@ class FederatedShareProvider implements IShareProvider { } - public function getSharesInFolder($userId, Folder $node, $reshares) { + public function getSharesInFolder($userId, Folder $node, $reshares, $shallow = true) { $qb = $this->dbConnection->getQueryBuilder(); $qb->select('*') ->from('share', 's') @@ -664,8 +664,13 @@ class FederatedShareProvider implements IShareProvider { ); } - $qb->innerJoin('s', 'filecache' ,'f', $qb->expr()->eq('s.file_source', 'f.fileid')); - $qb->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($node->getId()))); + $qb->innerJoin('s', 'filecache', 'f', $qb->expr()->eq('s.file_source', 'f.fileid')); + + if ($shallow) { + $qb->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($node->getId()))); + } else { + $qb->andWhere($qb->expr()->like('f.path', $qb->createNamedParameter($this->dbConnection->escapeLikeParameter($node->getInternalPath()) . '/%'))); + } $qb->orderBy('id'); diff --git a/apps/files_sharing/lib/Updater.php b/apps/files_sharing/lib/Updater.php index 071d4d3cca9..59e06e60299 100644 --- a/apps/files_sharing/lib/Updater.php +++ b/apps/files_sharing/lib/Updater.php @@ -69,7 +69,7 @@ class Updater { $shares = array_merge($shares, $shareManager->getSharesBy($userFolder->getOwner()->getUID(), IShare::TYPE_ROOM, $src, false, -1)); if ($src instanceof Folder) { - $subShares = $shareManager->getSharesInFolderRecursive($userFolder->getOwner()->getUID(), $src, false); + $subShares = $shareManager->getSharesInFolder($userFolder->getOwner()->getUID(), $src, false, false); foreach ($subShares as $subShare) { $shares = array_merge($shares, array_values($subShare)); } diff --git a/apps/sharebymail/lib/ShareByMailProvider.php b/apps/sharebymail/lib/ShareByMailProvider.php index ee14e354c52..01dd3b428d1 100644 --- a/apps/sharebymail/lib/ShareByMailProvider.php +++ b/apps/sharebymail/lib/ShareByMailProvider.php @@ -75,7 +75,6 @@ use OCP\Share\IShareProvider; * @package OCA\ShareByMail */ class ShareByMailProvider implements IShareProvider { - private IConfig $config; /** @var IDBConnection */ @@ -1159,7 +1158,7 @@ class ShareByMailProvider implements IShareProvider { return $data; } - public function getSharesInFolder($userId, Folder $node, $reshares) { + public function getSharesInFolder($userId, Folder $node, $reshares, $shallow = true) { $qb = $this->dbConnection->getQueryBuilder(); $qb->select('*') ->from('share', 's') @@ -1185,8 +1184,13 @@ class ShareByMailProvider implements IShareProvider { ); } - $qb->innerJoin('s', 'filecache' ,'f', $qb->expr()->eq('s.file_source', 'f.fileid')); - $qb->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($node->getId()))); + $qb->innerJoin('s', 'filecache', 'f', $qb->expr()->eq('s.file_source', 'f.fileid')); + + if ($shallow) { + $qb->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($node->getId()))); + } else { + $qb->andWhere($qb->expr()->like('f.path', $qb->createNamedParameter($this->dbConnection->escapeLikeParameter($node->getInternalPath()) . '/%'))); + } $qb->orderBy('id'); diff --git a/lib/private/Share20/DefaultShareProvider.php b/lib/private/Share20/DefaultShareProvider.php index 74f3e953a72..7719a1e6be3 100644 --- a/lib/private/Share20/DefaultShareProvider.php +++ b/lib/private/Share20/DefaultShareProvider.php @@ -641,7 +641,7 @@ class DefaultShareProvider implements IShareProvider { return $share; } - public function getSharesInFolder($userId, Folder $node, $reshares) { + public function getSharesInFolder($userId, Folder $node, $reshares, $shallow = true) { $qb = $this->dbConn->getQueryBuilder(); $qb->select('s.*', 'f.fileid', 'f.path', 'f.permissions AS f_permissions', 'f.storage', 'f.path_hash', @@ -682,12 +682,21 @@ class DefaultShareProvider implements IShareProvider { }, $childMountNodes); $qb->innerJoin('s', 'filecache', 'f', $qb->expr()->eq('s.file_source', 'f.fileid')); - $qb->andWhere( - $qb->expr()->orX( - $qb->expr()->eq('f.parent', $qb->createNamedParameter($node->getId())), - $qb->expr()->in('f.fileid', $qb->createParameter('chunk')) - ) - ); + if ($shallow) { + $qb->andWhere( + $qb->expr()->orX( + $qb->expr()->eq('f.parent', $qb->createNamedParameter($node->getId())), + $qb->expr()->in('f.fileid', $qb->createParameter('chunk')) + ) + ); + } else { + $qb->andWhere( + $qb->expr()->orX( + $qb->expr()->like('f.path', $qb->createNamedParameter($this->dbConn->escapeLikeParameter($node->getInternalPath()) . '/%')), + $qb->expr()->in('f.fileid', $qb->createParameter('chunk')) + ) + ); + } $qb->orderBy('id'); diff --git a/lib/private/Share20/Manager.php b/lib/private/Share20/Manager.php index fd1a27af8cf..46f256df54d 100644 --- a/lib/private/Share20/Manager.php +++ b/lib/private/Share20/Manager.php @@ -1303,11 +1303,11 @@ class Manager implements IManager { return $provider->move($share, $recipientId); } - public function getSharesInFolder($userId, Folder $node, $reshares = false) { + public function getSharesInFolder($userId, Folder $node, $reshares = false, $shallow = true) { $providers = $this->factory->getAllProviders(); - return array_reduce($providers, function ($shares, IShareProvider $provider) use ($userId, $node, $reshares) { - $newShares = $provider->getSharesInFolder($userId, $node, $reshares); + return array_reduce($providers, function ($shares, IShareProvider $provider) use ($userId, $node, $reshares, $shallow) { + $newShares = $provider->getSharesInFolder($userId, $node, $reshares, $shallow); foreach ($newShares as $fid => $data) { if (!isset($shares[$fid])) { $shares[$fid] = []; @@ -1319,24 +1319,6 @@ class Manager implements IManager { }, []); } - public function getSharesInFolderRecursive(string $userId, Folder $node, $reshares = false) { - $shares = $this->getSharesInFolder($userId, $node, $reshares); - - foreach ($node->getDirectoryListing() as $subnode) { - if (!$subnode instanceof Folder) { - continue; - } - - $subShares = $this->getSharesInFolderRecursive($userId, $subnode, $reshares); - - foreach ($subShares as $fileId => $subSharesForFile) { - $shares[$fileId] = array_merge($shares[$fileId] ?? [], $subSharesForFile); - } - } - - return $shares; - } - /** * @inheritdoc */ diff --git a/lib/public/Share/IManager.php b/lib/public/Share/IManager.php index b1c980733fb..0810acc673a 100644 --- a/lib/public/Share/IManager.php +++ b/lib/public/Share/IManager.php @@ -135,18 +135,11 @@ interface IManager { * @param string $userId * @param Folder $node * @param bool $reshares + * @param bool $shallow Whether the method should stop at the first level, or look into sub-folders. * @return IShare[][] [$fileId => IShare[], ...] * @since 11.0.0 */ - public function getSharesInFolder($userId, Folder $node, $reshares = false); - - /** - * Recursively get all shares shared by (initiated) by the provided user in a folder. - * - * @return IShare[][] [$fileId => IShare[], ...] - * @since 11.0.0 - */ - public function getSharesInFolderRecursive(string $userId, Folder $node, bool $reshares = false); + public function getSharesInFolder($userId, Folder $node, $reshares = false, $shallow = true); /** * Get shares shared by (initiated) by the provided user. diff --git a/lib/public/Share/IShareProvider.php b/lib/public/Share/IShareProvider.php index 6af513360fe..c549592d6f6 100644 --- a/lib/public/Share/IShareProvider.php +++ b/lib/public/Share/IShareProvider.php @@ -123,10 +123,11 @@ interface IShareProvider { * @param string $userId * @param Folder $node * @param bool $reshares Also get the shares where $user is the owner instead of just the shares where $user is the initiator + * @param bool $shallow Whether the method should stop at the first level, or look into sub-folders. * @return \OCP\Share\IShare[][] * @since 11.0.0 */ - public function getSharesInFolder($userId, Folder $node, $reshares); + public function getSharesInFolder($userId, Folder $node, $reshares, $shallow = true); /** * Get all shares by the given user -- 2.39.5