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

[stable25] fix: always use proper path on node api when calling the view #38129

Merged
merged 2 commits into from
May 10, 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
11 changes: 7 additions & 4 deletions apps/dav/lib/Connector/Sabre/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,22 +100,25 @@ public function __construct(View $view, FileInfo $info, IManager $shareManager =
if ($info instanceof Folder || $info instanceof File) {
$this->node = $info;
} else {
// The Node API assumes that the view passed doesn't have a fake root
$rootView = \OC::$server->get(View::class);
$root = \OC::$server->get(IRootFolder::class);
if ($info->getType() === FileInfo::TYPE_FOLDER) {
$this->node = new Folder($root, $view, $this->path, $info);
$this->node = new Folder($root, $rootView, $this->fileView->getAbsolutePath($this->path), $info);
} else {
$this->node = new File($root, $view, $this->path, $info);
$this->node = new File($root, $rootView, $this->fileView->getAbsolutePath($this->path), $info);
}
}
}

protected function refreshInfo() {
$this->info = $this->fileView->getFileInfo($this->path);
$root = \OC::$server->get(IRootFolder::class);
$rootView = \OC::$server->get(View::class);
if ($this->info->getType() === FileInfo::TYPE_FOLDER) {
$this->node = new Folder($root, $this->fileView, $this->path, $this->info);
$this->node = new Folder($root, $rootView, $this->path, $this->info);
} else {
$this->node = new File($root, $this->fileView, $this->path, $this->info);
$this->node = new File($root, $rootView, $this->path, $this->info);
}
}

Expand Down
20 changes: 18 additions & 2 deletions apps/dav/tests/unit/Connector/Sabre/DirectoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@
namespace OCA\DAV\Tests\Unit\Connector\Sabre;

use OC\Files\FileInfo;
use OC\Files\Filesystem;
use OC\Files\Node\Node;
use OC\Files\Storage\Wrapper\Quota;
use OC\Files\View;
use OCA\DAV\Connector\Sabre\Directory;
use OCP\Constants;
use OCP\Files\ForbiddenException;
use OCP\Files\Mount\IMountPoint;
use Test\Traits\UserTrait;
Expand Down Expand Up @@ -92,6 +95,10 @@ protected function setUp(): void {
->willReturn(Node::TYPE_FOLDER);
$this->info->method('getName')
->willReturn("folder");
$this->info->method('getPath')
->willReturn("/admin/files/folder");
$this->info->method('getPermissions')
->willReturn(Constants::PERMISSION_READ);
}

private function getDir($path = '/') {
Expand Down Expand Up @@ -208,12 +215,21 @@ public function testGetChildren() {

$this->view->expects($this->once())
->method('getDirectoryContent')
->with('')
->willReturn([$info1, $info2]);

$this->view->expects($this->any())
->method('getRelativePath')
->willReturn('');
->willReturnCallback(function ($path) {
return str_replace('/admin/files/', '', $path);
});

$this->view->expects($this->any())
->method('getAbsolutePath')
->willReturnCallback(function ($path) {
return Filesystem::normalizePath('/admin/files' . $path);
});

$this->overwriteService(View::class, $this->view);

$dir = new Directory($this->view, $this->info);
$nodes = $dir->getChildren();
Expand Down
8 changes: 5 additions & 3 deletions apps/dav/tests/unit/Files/FileSearchBackendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,11 @@ protected function setUp(): void {
->disableOriginalConstructor()
->getMock();

$this->view = $this->getMockBuilder(View::class)
->disableOriginalConstructor()
->getMock();
$this->view = $this->createMock(View::class);

$this->view->expects($this->any())
->method('getRoot')
->willReturn('');

$this->view->expects($this->any())
->method('getRelativePath')
Expand Down
6 changes: 5 additions & 1 deletion lib/private/Files/Node/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\Lock\LockedException;
use OCP\PreConditionNotMetException;
use Symfony\Component\EventDispatcher\GenericEvent;

// FIXME: this class really should be abstract
Expand All @@ -52,7 +53,7 @@ class Node implements \OCP\Files\Node {
protected $root;

/**
* @var string $path
* @var string $path Absolute path to the node (e.g. /admin/files/folder/file)
*/
protected $path;

Expand All @@ -73,6 +74,9 @@ class Node implements \OCP\Files\Node {
* @param FileInfo $fileInfo
*/
public function __construct($root, $view, $path, $fileInfo = null, ?Node $parent = null) {
if (Filesystem::normalizePath($view->getRoot()) !== '/') {
throw new PreConditionNotMetException('The view passed to the node should not have any fake root set');
}
$this->view = $view;
$this->root = $root;
$this->path = $path;
Expand Down
4 changes: 2 additions & 2 deletions tests/lib/Files/Node/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public function testFOpenWrite() {

$root = new \OC\Files\Node\Root(
$this->manager,
new $this->view,
$this->view,
$this->user,
$this->userMountCache,
$this->logger,
Expand Down Expand Up @@ -277,7 +277,7 @@ public function testFOpenReadWriteNoWritePermissions() {

$root = new \OC\Files\Node\Root(
$this->manager,
new $this->view,
$this->view,
$this->user,
$this->userMountCache,
$this->logger,
Expand Down
Loading