]> source.dussan.org Git - nextcloud-server.git/commitdiff
Extend ViewOnly DAV plugin to versions endpoint 36299/head
authorVincent Petry <vincent@nextcloud.com>
Thu, 12 Jan 2023 10:28:03 +0000 (11:28 +0100)
committerbackportbot-nextcloud[bot] <backportbot-nextcloud[bot]@users.noreply.github.com>
Mon, 23 Jan 2023 13:46:41 +0000 (13:46 +0000)
Signed-off-by: Vincent Petry <vincent@nextcloud.com>
apps/dav/lib/DAV/ViewOnlyPlugin.php
apps/dav/tests/unit/DAV/ViewOnlyPluginTest.php

index e0c4e26f598a29923af11e4cc1c27990b7fa3c3b..51e3622142db3195cf671b222e677aad061438ab 100644 (file)
@@ -23,6 +23,7 @@ namespace OCA\DAV\DAV;
 
 use OCA\DAV\Connector\Sabre\Exception\Forbidden;
 use OCA\DAV\Connector\Sabre\File as DavFile;
+use OCA\Files_Versions\Sabre\VersionFile;
 use OCP\Files\NotFoundException;
 use Psr\Log\LoggerInterface;
 use Sabre\DAV\Server;
@@ -70,11 +71,14 @@ class ViewOnlyPlugin extends ServerPlugin {
                try {
                        assert($this->server !== null);
                        $davNode = $this->server->tree->getNodeForPath($path);
-                       if (!($davNode instanceof DavFile)) {
+                       if ($davNode instanceof DavFile) {
+                               // Restrict view-only to nodes which are shared
+                               $node = $davNode->getNode();
+                       } else if ($davNode instanceof VersionFile) {
+                               $node = $davNode->getVersion()->getSourceFile();
+                       } else {
                                return true;
                        }
-                       // Restrict view-only to nodes which are shared
-                       $node = $davNode->getNode();
 
                        $storage = $node->getStorage();
 
index f86a60fb4bfd52c049a585a9127d611a8ffac367..e0f6f1302a5ab566746bb22f05e207b39b6e7c01 100644 (file)
@@ -23,6 +23,8 @@ namespace OCA\DAV\Tests\unit\DAV;
 use OCA\DAV\DAV\ViewOnlyPlugin;
 use OCA\Files_Sharing\SharedStorage;
 use OCA\DAV\Connector\Sabre\File as DavFile;
+use OCA\Files_Versions\Versions\IVersion;
+use OCA\Files_Versions\Sabre\VersionFile;
 use OCP\Files\File;
 use OCP\Files\Storage\IStorage;
 use OCP\Share\IAttributes;
@@ -80,34 +82,62 @@ class ViewOnlyPluginTest extends TestCase {
        public function providesDataForCanGet(): array {
                return [
                        // has attribute permissions-download enabled - can get file
-                       [ $this->createMock(File::class), true, true],
+                       [false, true, true],
                        // has no attribute permissions-download - can get file
-                       [ $this->createMock(File::class), null, true],
+                       [false, null, true],
                        // has attribute permissions-download disabled- cannot get the file
-                       [ $this->createMock(File::class), false, false],
+                       [false, false, false],
+                       // has attribute permissions-download enabled - can get file version
+                       [true, true, true],
+                       // has no attribute permissions-download - can get file version
+                       [true, null, true],
+                       // has attribute permissions-download disabled- cannot get the file version
+                       [true, false, false],
                ];
        }
 
        /**
         * @dataProvider providesDataForCanGet
         */
-       public function testCanGet(File $nodeInfo, ?bool $attrEnabled, bool $expectCanDownloadFile): void {
-               $this->request->expects($this->once())->method('getPath')->willReturn('files/test/target');
+       public function testCanGet(bool $isVersion, ?bool $attrEnabled, bool $expectCanDownloadFile): void {
+               $nodeInfo = $this->createMock(File::class);
+               if ($isVersion) {
+                       $davPath = 'versions/alice/versions/117/123456';
+                       $version = $this->createMock(IVersion::class);
+                       $version->expects($this->once())
+                               ->method('getSourceFile')
+                               ->willReturn($nodeInfo);
+                       $davNode = $this->createMock(VersionFile::class);
+                       $davNode->expects($this->once())
+                               ->method('getVersion')
+                               ->willReturn($version);
+               } else {
+                       $davPath = 'files/path/to/file.odt';
+                       $davNode = $this->createMock(DavFile::class);
+                       $davNode->method('getNode')->willReturn($nodeInfo);
+               }
 
-               $davNode = $this->createMock(DavFile::class);
-               $this->tree->method('getNodeForPath')->willReturn($davNode);
+               $this->request->expects($this->once())->method('getPath')->willReturn($davPath);
 
-               $davNode->method('getNode')->willReturn($nodeInfo);
+               $this->tree->expects($this->once())
+                        ->method('getNodeForPath')
+                        ->with($davPath)
+                        ->willReturn($davNode);
 
                $storage = $this->createMock(SharedStorage::class);
                $share = $this->createMock(IShare::class);
-               $nodeInfo->method('getStorage')->willReturn($storage);
+               $nodeInfo->expects($this->once())
+                       ->method('getStorage')
+                       ->willReturn($storage);
                $storage->method('instanceOfStorage')->with(SharedStorage::class)->willReturn(true);
                $storage->method('getShare')->willReturn($share);
 
                $extAttr = $this->createMock(IAttributes::class);
                $share->method('getAttributes')->willReturn($extAttr);
-               $extAttr->method('getAttribute')->with('permissions', 'download')->willReturn($attrEnabled);
+               $extAttr->expects($this->once())
+                 ->method('getAttribute')
+                 ->with('permissions', 'download')
+                 ->willReturn($attrEnabled);
 
                if (!$expectCanDownloadFile) {
                        $this->expectException(Forbidden::class);