Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable26] fix(sharing): set name to target name in sharing cache #41071

Merged
merged 3 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions apps/files_sharing/lib/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
use OCP\Files\StorageNotAvailableException;
use OCP\ICacheFactory;
use OCP\IUserManager;
use OCP\Share\IShare;

/**
* Metadata cache for shared files
Expand All @@ -55,15 +56,22 @@ class Cache extends CacheJail {
private ?string $ownerDisplayName = null;
private $numericId;
private DisplayNameCache $displayNameCache;
private IShare $share;

/**
* @param SharedStorage $storage
*/
public function __construct($storage, ICacheEntry $sourceRootInfo, DisplayNameCache $displayNameCache) {
public function __construct(
$storage,
ICacheEntry $sourceRootInfo,
DisplayNameCache $displayNameCache,
IShare $share
) {
$this->storage = $storage;
$this->sourceRootInfo = $sourceRootInfo;
$this->numericId = $sourceRootInfo->getStorageId();
$this->displayNameCache = $displayNameCache;
$this->share = $share;

parent::__construct(
null,
Expand Down Expand Up @@ -150,16 +158,20 @@ protected function formatCacheEntry($entry, $path = null) {

try {
if (isset($entry['permissions'])) {
$entry['permissions'] &= $this->storage->getShare()->getPermissions();
$entry['permissions'] &= $this->share->getPermissions();
} else {
$entry['permissions'] = $this->storage->getPermissions($entry['path']);
}

if ($this->share->getNodeId() === $entry['fileid']) {
Fixed Show fixed Hide fixed
$entry['name'] = basename($this->share->getTarget());
}
} catch (StorageNotAvailableException $e) {
// thrown by FailedStorage e.g. when the sharer does not exist anymore
// (IDE may say the exception is never thrown – false negative)
$sharePermissions = 0;
}
$entry['uid_owner'] = $this->storage->getOwner('');
$entry['uid_owner'] = $this->share->getShareOwner();
$entry['displayname_owner'] = $this->getOwnerDisplayName();
if ($path === '') {
$entry['is_share_mount_point'] = true;
Expand All @@ -169,7 +181,7 @@ protected function formatCacheEntry($entry, $path = null) {

private function getOwnerDisplayName() {
if (!$this->ownerDisplayName) {
$uid = $this->storage->getOwner('');
$uid = $this->share->getShareOwner();
$this->ownerDisplayName = $this->displayNameCache->getDisplayName($uid) ?? $uid;
}
return $this->ownerDisplayName;
Expand Down
3 changes: 2 additions & 1 deletion apps/files_sharing/lib/SharedStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,8 @@ public function getCache($path = '', $storage = null) {
$this->cache = new \OCA\Files_Sharing\Cache(
$storage,
$sourceRoot,
\OC::$server->get(DisplayNameCache::class)
\OC::$server->get(DisplayNameCache::class),
$this->getShare()
);
return $this->cache;
}
Expand Down
37 changes: 37 additions & 0 deletions apps/files_sharing/tests/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ protected function setUp(): void {
$this->view->file_put_contents('container/shareddir/subdir/another.txt', $textData);
$this->view->file_put_contents('container/shareddir/subdir/another too.txt', $textData);
$this->view->file_put_contents('container/shareddir/subdir/not a text file.xml', '<xml></xml>');
$this->view->file_put_contents('simplefile.txt', $textData);

[$this->ownerStorage,] = $this->view->resolvePath('');
$this->ownerCache = $this->ownerStorage->getCache();
Expand Down Expand Up @@ -302,6 +303,42 @@ public function testGetFolderContentsInSubdir() {
);
}

/**
* This covers a bug where the share owners name was propagated
* to the recipient in the recent files API response where the
* share recipient has a different target set
*
* https://github.com/nextcloud/server/issues/39879
*/
public function testShareRenameOriginalFileInRecentResults() {
self::loginHelper(self::TEST_FILES_SHARING_API_USER1);

$rootFolder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER1);
$node = $rootFolder->get('simplefile.txt');
$share = $this->shareManager->newShare();
$share->setNode($node)
->setShareType(IShare::TYPE_USER)
->setSharedWith(self::TEST_FILES_SHARING_API_USER3)
->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
->setPermissions(\OCP\Constants::PERMISSION_READ);
$share = $this->shareManager->createShare($share);
$share->setStatus(IShare::STATUS_ACCEPTED);
$this->shareManager->updateShare($share);

self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
$node->move(self::TEST_FILES_SHARING_API_USER1 . '/files/simplefile2.txt');

self::loginHelper(self::TEST_FILES_SHARING_API_USER3);
$rootFolder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER3);
$recents = $rootFolder->getRecent(10);
self::assertEquals([
'welcome.txt',
'simplefile.txt'
], array_map(function($node) {
return $node->getFileInfo()['name'];
}, $recents));
}

public function testGetFolderContentsWhenSubSubdirShared() {
self::loginHelper(self::TEST_FILES_SHARING_API_USER1);

Expand Down
Loading