diff options
author | skjnldsv <skjnldsv@protonmail.com> | 2024-06-06 18:55:33 +0200 |
---|---|---|
committer | backportbot[bot] <backportbot[bot]@users.noreply.github.com> | 2024-06-12 11:06:10 +0000 |
commit | 697f9032cf510bb70885ded449f64f85a232ae51 (patch) | |
tree | 28f5dd1e2ebf56c89e28109a3da7776e7b30ae96 /apps/dav | |
parent | 4ad83e9fa327ed969138a94d2cfcfba98ff9f836 (diff) | |
download | nextcloud-server-697f9032cf510bb70885ded449f64f85a232ae51.tar.gz nextcloud-server-697f9032cf510bb70885ded449f64f85a232ae51.zip |
fix(dav): also return shared-with-me shares data
Signed-off-by: skjnldsv <skjnldsv@protonmail.com>
Diffstat (limited to 'apps/dav')
-rw-r--r-- | apps/dav/lib/Connector/Sabre/SharesPlugin.php | 75 | ||||
-rw-r--r-- | apps/dav/lib/Server.php | 6 | ||||
-rw-r--r-- | apps/dav/tests/unit/Connector/Sabre/SharesPluginTest.php | 22 |
3 files changed, 68 insertions, 35 deletions
diff --git a/apps/dav/lib/Connector/Sabre/SharesPlugin.php b/apps/dav/lib/Connector/Sabre/SharesPlugin.php index 4cda346af01..d1cadee5f8b 100644 --- a/apps/dav/lib/Connector/Sabre/SharesPlugin.php +++ b/apps/dav/lib/Connector/Sabre/SharesPlugin.php @@ -28,6 +28,7 @@ */ namespace OCA\DAV\Connector\Sabre; +use OC\Share20\Exception\BackendError; use OCA\DAV\Connector\Sabre\Node as DavNode; use OCP\Files\Folder; use OCP\Files\Node; @@ -54,24 +55,19 @@ class SharesPlugin extends \Sabre\DAV\ServerPlugin { * @var \Sabre\DAV\Server */ private $server; - private IManager $shareManager; - private Tree $tree; private string $userId; - private Folder $userFolder; + /** @var IShare[][] */ private array $cachedShares = []; /** @var string[] */ private array $cachedFolders = []; public function __construct( - Tree $tree, - IUserSession $userSession, - Folder $userFolder, - IManager $shareManager + private Tree $tree, + private IUserSession $userSession, + private Folder $userFolder, + private IManager $shareManager, ) { - $this->tree = $tree; - $this->shareManager = $shareManager; - $this->userFolder = $userFolder; $this->userId = $userSession->getUser()->getUID(); } @@ -112,18 +108,29 @@ class SharesPlugin extends \Sabre\DAV\ServerPlugin { IShare::TYPE_DECK, IShare::TYPE_SCIENCEMESH, ]; + foreach ($requestedShareTypes as $requestedShareType) { - $shares = $this->shareManager->getSharesBy( + $result = array_merge($result, $this->shareManager->getSharesBy( $this->userId, $requestedShareType, $node, false, -1 - ); - foreach ($shares as $share) { - $result[] = $share; + )); + + // Also check for shares where the user is the recipient + try { + $result = array_merge($result, $this->shareManager->getSharedWith( + $this->userId, + $requestedShareType, + $node, + -1 + )); + } catch (BackendError $e) { + // ignore } } + return $result; } @@ -145,27 +152,29 @@ class SharesPlugin extends \Sabre\DAV\ServerPlugin { */ private function getShares(DavNode $sabreNode): array { if (isset($this->cachedShares[$sabreNode->getId()])) { - $shares = $this->cachedShares[$sabreNode->getId()]; - } else { - [$parentPath,] = \Sabre\Uri\split($sabreNode->getPath()); - if ($parentPath === '') { - $parentPath = '/'; - } - // if we already cached the folder this file is in we know there are no shares for this file - if (array_search($parentPath, $this->cachedFolders) === false) { - try { - $node = $sabreNode->getNode(); - } catch (NotFoundException $e) { - return []; - } - $shares = $this->getShare($node); - $this->cachedShares[$sabreNode->getId()] = $shares; - } else { + return $this->cachedShares[$sabreNode->getId()]; + } + + [$parentPath,] = \Sabre\Uri\split($sabreNode->getPath()); + if ($parentPath === '') { + $parentPath = '/'; + } + + // if we already cached the folder containing this file + // then we already know there are no shares here. + if (array_search($parentPath, $this->cachedFolders) === false) { + try { + $node = $sabreNode->getNode(); + } catch (NotFoundException $e) { return []; } + + $shares = $this->getShare($node); + $this->cachedShares[$sabreNode->getId()] = $shares; + return $shares; } - return $shares; + return []; } /** @@ -182,7 +191,9 @@ class SharesPlugin extends \Sabre\DAV\ServerPlugin { return; } - // need prefetch ? + // If the node is a directory and we are requesting share types or sharees + // then we get all the shares in the folder and cache them. + // This is more performant than iterating each files afterwards. if ($sabreNode instanceof Directory && $propFind->getDepth() !== 0 && ( diff --git a/apps/dav/lib/Server.php b/apps/dav/lib/Server.php index 6e388c4af25..9c9ec628956 100644 --- a/apps/dav/lib/Server.php +++ b/apps/dav/lib/Server.php @@ -295,13 +295,15 @@ class Server { $this->server->tree, \OC::$server->getTagManager() ) ); + // TODO: switch to LazyUserFolder $userFolder = \OC::$server->getUserFolder(); + $shareManager = \OCP\Server::get(\OCP\Share\IManager::class); $this->server->addPlugin(new SharesPlugin( $this->server->tree, $userSession, $userFolder, - \OC::$server->getShareManager() + $shareManager, )); $this->server->addPlugin(new CommentPropertiesPlugin( \OC::$server->getCommentsManager(), @@ -324,7 +326,7 @@ class Server { $this->server->tree, $user, \OC::$server->getRootFolder(), - \OC::$server->getShareManager(), + $shareManager, $view, \OCP\Server::get(IFilesMetadataManager::class) )); diff --git a/apps/dav/tests/unit/Connector/Sabre/SharesPluginTest.php b/apps/dav/tests/unit/Connector/Sabre/SharesPluginTest.php index e6e90838966..8949493b20b 100644 --- a/apps/dav/tests/unit/Connector/Sabre/SharesPluginTest.php +++ b/apps/dav/tests/unit/Connector/Sabre/SharesPluginTest.php @@ -119,7 +119,7 @@ class SharesPluginTest extends \Test\TestCase { ->with( $this->equalTo('user1'), $this->anything(), - $this->anything(), + $this->equalTo($node), $this->equalTo(false), $this->equalTo(-1) ) @@ -133,6 +133,16 @@ class SharesPluginTest extends \Test\TestCase { return []; }); + $this->shareManager->expects($this->any()) + ->method('getSharedWith') + ->with( + $this->equalTo('user1'), + $this->anything(), + $this->equalTo($node), + $this->equalTo(-1) + ) + ->willReturn([]); + $propFind = new \Sabre\DAV\PropFind( '/dummyPath', [self::SHARETYPES_PROPERTYNAME], @@ -222,6 +232,16 @@ class SharesPluginTest extends \Test\TestCase { }); $this->shareManager->expects($this->any()) + ->method('getSharedWith') + ->with( + $this->equalTo('user1'), + $this->anything(), + $this->equalTo($node), + $this->equalTo(-1) + ) + ->willReturn([]); + + $this->shareManager->expects($this->any()) ->method('getSharesInFolder') ->with( $this->equalTo('user1'), |