From cf935e33ae08f49fec42ec5bc9ceca2d8830ef4b Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Tue, 9 Jul 2024 18:33:45 +0200 Subject: [PATCH] fix: `OCP\Files\Node\Folder::search` was not setting the owner The owner was not set on the file info causing e.g. webdav searches to never return the known owner. Signed-off-by: Ferdinand Thiessen --- lib/private/Files/Node/Folder.php | 25 ++++++++++++++++++++++++- tests/lib/Files/Node/FolderTest.php | 14 +++++++++++--- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/lib/private/Files/Node/Folder.php b/lib/private/Files/Node/Folder.php index 7ae55d046b9ea..79daa63d7b3c7 100644 --- a/lib/private/Files/Node/Folder.php +++ b/lib/private/Files/Node/Folder.php @@ -12,6 +12,7 @@ use OC\Files\Search\SearchOrder; use OC\Files\Search\SearchQuery; use OC\Files\Utils\PathHelper; +use OC\User\LazyUser; use OCP\Files\Cache\ICacheEntry; use OCP\Files\FileInfo; use OCP\Files\Mount\IMountPoint; @@ -26,6 +27,9 @@ use OCP\IUserManager; class Folder extends Node implements \OCP\Files\Folder { + + private ?IUserManager $userManager = null; + /** * Creates a Folder that represents a non-existing path * @@ -245,7 +249,26 @@ private function cacheEntryToFileInfo(IMountPoint $mount, string $appendRoot, IC $cacheEntry['internalPath'] = $cacheEntry['path']; $cacheEntry['path'] = rtrim($appendRoot . $cacheEntry->getPath(), '/'); $subPath = $cacheEntry['path'] !== '' ? '/' . $cacheEntry['path'] : ''; - return new \OC\Files\FileInfo($this->path . $subPath, $mount->getStorage(), $cacheEntry['internalPath'], $cacheEntry, $mount); + $storage = $mount->getStorage(); + + $owner = null; + $ownerId = $storage->getOwner($cacheEntry['internalPath']); + if (!empty($ownerId)) { + // Cache the user manager (for performance) + if ($this->userManager === null) { + $this->userManager = \OCP\Server::get(IUserManager::class); + } + $owner = new LazyUser($ownerId, $this->userManager); + } + + return new \OC\Files\FileInfo( + $this->path . $subPath, + $storage, + $cacheEntry['internalPath'], + $cacheEntry, + $mount, + $owner, + ); } /** diff --git a/tests/lib/Files/Node/FolderTest.php b/tests/lib/Files/Node/FolderTest.php index 546c9f84a1a30..46fda6c3a7665 100644 --- a/tests/lib/Files/Node/FolderTest.php +++ b/tests/lib/Files/Node/FolderTest.php @@ -29,6 +29,7 @@ use OCP\Files\Search\ISearchComparison; use OCP\Files\Search\ISearchOrder; use OCP\Files\Storage; +use PHPUnit\Framework\MockObject\MockObject; /** * Class FolderTest @@ -290,7 +291,7 @@ public function testSearch() { ->getMock(); $root->method('getUser') ->willReturn($this->user); - /** @var Storage\IStorage $storage */ + /** @var Storage\IStorage&MockObject $storage */ $storage = $this->createMock(Storage\IStorage::class); $storage->method('getId')->willReturn('test::1'); $cache = new Cache($storage); @@ -298,10 +299,17 @@ public function testSearch() { $storage->method('getCache') ->willReturn($cache); + $storage->expects($this->atLeastOnce()) + ->method('getOwner') + ->with('qwerty') + ->willReturn(false); + $mount = $this->createMock(IMountPoint::class); - $mount->method('getStorage') + $mount->expects($this->atLeastOnce()) + ->method('getStorage') ->willReturn($storage); - $mount->method('getInternalPath') + $mount->expects($this->atLeastOnce()) + ->method('getInternalPath') ->willReturn('foo'); $cache->insert('foo', ['size' => 200, 'mtime' => 55, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]);