Skip to content

Commit 855e7a2

Browse files
authored
Merge pull request #36774 from nextcloud/bugfix/noid/sabre-nodes
fix: always use proper path on node api when calling the view
2 parents e10e509 + d9c81f5 commit 855e7a2

File tree

8 files changed

+102
-135
lines changed

8 files changed

+102
-135
lines changed

apps/dav/lib/Connector/Sabre/Node.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,13 @@ public function __construct(View $view, FileInfo $info, IManager $shareManager =
9191
if ($info instanceof Folder || $info instanceof File) {
9292
$this->node = $info;
9393
} else {
94+
// The Node API assumes that the view passed doesn't have a fake root
95+
$rootView = \OC::$server->get(View::class);
9496
$root = \OC::$server->get(IRootFolder::class);
9597
if ($info->getType() === FileInfo::TYPE_FOLDER) {
96-
$this->node = new Folder($root, $view, $this->path, $info);
98+
$this->node = new Folder($root, $rootView, $this->fileView->getAbsolutePath($this->path), $info);
9799
} else {
98-
$this->node = new File($root, $view, $this->path, $info);
100+
$this->node = new File($root, $rootView, $this->fileView->getAbsolutePath($this->path), $info);
99101
}
100102
}
101103
}
@@ -107,10 +109,11 @@ protected function refreshInfo(): void {
107109
}
108110
$this->info = $info;
109111
$root = \OC::$server->get(IRootFolder::class);
112+
$rootView = \OC::$server->get(View::class);
110113
if ($this->info->getType() === FileInfo::TYPE_FOLDER) {
111-
$this->node = new Folder($root, $this->fileView, $this->path, $this->info);
114+
$this->node = new Folder($root, $rootView, $this->path, $this->info);
112115
} else {
113-
$this->node = new File($root, $this->fileView, $this->path, $this->info);
116+
$this->node = new File($root, $rootView, $this->path, $this->info);
114117
}
115118
}
116119

apps/dav/tests/unit/Connector/Sabre/DirectoryTest.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,12 @@
2929
namespace OCA\DAV\Tests\Unit\Connector\Sabre;
3030

3131
use OC\Files\FileInfo;
32+
use OC\Files\Filesystem;
3233
use OC\Files\Node\Node;
3334
use OC\Files\Storage\Wrapper\Quota;
35+
use OC\Files\View;
3436
use OCA\DAV\Connector\Sabre\Directory;
37+
use OCP\Constants;
3538
use OCP\Files\ForbiddenException;
3639
use OCP\Files\Mount\IMountPoint;
3740
use Test\Traits\UserTrait;
@@ -91,6 +94,10 @@ protected function setUp(): void {
9194
->willReturn(Node::TYPE_FOLDER);
9295
$this->info->method('getName')
9396
->willReturn("folder");
97+
$this->info->method('getPath')
98+
->willReturn("/admin/files/folder");
99+
$this->info->method('getPermissions')
100+
->willReturn(Constants::PERMISSION_READ);
94101
}
95102

96103
private function getDir($path = '/') {
@@ -207,12 +214,21 @@ public function testGetChildren(): void {
207214

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

213219
$this->view->expects($this->any())
214220
->method('getRelativePath')
215-
->willReturn('');
221+
->willReturnCallback(function ($path) {
222+
return str_replace('/admin/files/', '', $path);
223+
});
224+
225+
$this->view->expects($this->any())
226+
->method('getAbsolutePath')
227+
->willReturnCallback(function ($path) {
228+
return Filesystem::normalizePath('/admin/files' . $path);
229+
});
230+
231+
$this->overwriteService(View::class, $this->view);
216232

217233
$dir = new Directory($this->view, $this->info);
218234
$nodes = $dir->getChildren();

apps/dav/tests/unit/Files/FileSearchBackendTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,11 @@ protected function setUp(): void {
8686
->disableOriginalConstructor()
8787
->getMock();
8888

89-
$this->view = $this->getMockBuilder(View::class)
90-
->disableOriginalConstructor()
91-
->getMock();
89+
$this->view = $this->createMock(View::class);
90+
91+
$this->view->expects($this->any())
92+
->method('getRoot')
93+
->willReturn('');
9294

9395
$this->view->expects($this->any())
9496
->method('getRelativePath')

lib/private/Files/Node/Node.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
use OCP\Files\NotFoundException;
3838
use OCP\Files\NotPermittedException;
3939
use OCP\Lock\LockedException;
40+
use OCP\PreConditionNotMetException;
4041
use Symfony\Component\EventDispatcher\GenericEvent;
4142

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

5455
/**
55-
* @var string $path
56+
* @var string $path Absolute path to the node (e.g. /admin/files/folder/file)
5657
*/
5758
protected $path;
5859

@@ -72,6 +73,9 @@ class Node implements \OCP\Files\Node {
7273
* @param FileInfo $fileInfo
7374
*/
7475
public function __construct($root, $view, $path, $fileInfo = null, ?Node $parent = null, bool $infoHasSubMountsIncluded = true) {
76+
if (Filesystem::normalizePath($view->getRoot()) !== '/') {
77+
throw new PreConditionNotMetException('The view passed to the node should not have any fake root set');
78+
}
7579
$this->view = $view;
7680
$this->root = $root;
7781
$this->path = $path;

tests/lib/Files/Node/FileTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ public function testFOpenWrite() {
185185

186186
$root = new \OC\Files\Node\Root(
187187
$this->manager,
188-
new $this->view,
188+
$this->view,
189189
$this->user,
190190
$this->userMountCache,
191191
$this->logger,
@@ -277,7 +277,7 @@ public function testFOpenReadWriteNoWritePermissions() {
277277

278278
$root = new \OC\Files\Node\Root(
279279
$this->manager,
280-
new $this->view,
280+
$this->view,
281281
$this->user,
282282
$this->userMountCache,
283283
$this->logger,

0 commit comments

Comments
 (0)