diff --git a/apps/files/lib/Controller/ViewController.php b/apps/files/lib/Controller/ViewController.php index ccff2d7e113f..6f690fea07a9 100644 --- a/apps/files/lib/Controller/ViewController.php +++ b/apps/files/lib/Controller/ViewController.php @@ -41,6 +41,7 @@ use OCP\IURLGenerator; use OCP\IUserSession; use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use OCP\AppFramework\Http; /** * Class ViewController @@ -282,12 +283,14 @@ public function showFile($fileId) { $files = $baseFolder->getById($fileId); $params = []; + $isFilesView = true; if (empty($files) && $this->appManager->isEnabledForUser('files_trashbin')) { // Access files_trashbin if it exists if ( $this->rootFolder->nodeExists($uid . '/files_trashbin/files/')) { $baseFolder = $this->rootFolder->get($uid . '/files_trashbin/files/'); $files = $baseFolder->getById($fileId); $params['view'] = 'trashbin'; + $isFilesView = false; } } @@ -302,14 +305,23 @@ public function showFile($fileId) { // and scroll to the entry $params['scrollto'] = $file->getName(); } - return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index', $params)); + $response = new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index', $params)); + if ($isFilesView) { + $webdavUrl = $this->urlGenerator->linkTo('', 'remote.php') . '/dav/files/' . $uid . '/'; + $webdavUrl .= ltrim($baseFolder->getRelativePath($file->getPath()), '/'); + $response->addHeader('Webdav-Location', $webdavUrl); + } + return $response; } - if ( $this->userSession->isLoggedIn() and empty($files)) { + if ($this->userSession->isLoggedIn() and empty($files)) { $param["error"] = $this->l10n->t("You don't have permissions to access this file/folder - Please contact the owner to share it with you."); - return new TemplateResponse("core", 'error', ["errors" => [$param]], 'guest'); + $response = new TemplateResponse("core", 'error', ["errors" => [$param]], 'guest'); + $response->setStatus(Http::STATUS_NOT_FOUND); + return $response; } + // FIXME: potentially dead code as the user is normally always logged in non-public routes throw new \OCP\Files\NotFoundException(); } } diff --git a/apps/files/tests/Controller/ViewControllerTest.php b/apps/files/tests/Controller/ViewControllerTest.php index 4be6adc27173..457e211e76bc 100644 --- a/apps/files/tests/Controller/ViewControllerTest.php +++ b/apps/files/tests/Controller/ViewControllerTest.php @@ -102,6 +102,12 @@ public function setUp() { 'renderScript' ]) ->getMock(); + + $this->urlGenerator + ->expects($this->any()) + ->method('linkTo') + ->with('', 'remote.php') + ->will($this->returnValue('/owncloud/remote.php')); } public function testIndexWithIE8RedirectAndDirDefined() { @@ -114,9 +120,9 @@ public function testIndexWithIE8RedirectAndDirDefined() { ->expects($this->once()) ->method('linkToRoute') ->with('files.view.index') - ->will($this->returnValue('/apps/files/')); + ->will($this->returnValue('/owncloud/index.php/apps/files/')); - $expected = new Http\RedirectResponse('/apps/files/#?dir=MyDir'); + $expected = new Http\RedirectResponse('/owncloud/index.php/apps/files/#?dir=MyDir'); $this->assertEquals($expected, $this->viewController->index('MyDir')); } @@ -130,9 +136,9 @@ public function testIndexWithIE8RedirectAndViewDefined() { ->expects($this->once()) ->method('linkToRoute') ->with('files.view.index') - ->will($this->returnValue('/apps/files/')); + ->will($this->returnValue('/owncloud/index.php/apps/files/')); - $expected = new Http\RedirectResponse('/apps/files/#?dir=/&view=MyView'); + $expected = new Http\RedirectResponse('/owncloud/index.php/apps/files/#?dir=/&view=MyView'); $this->assertEquals($expected, $this->viewController->index('', 'MyView')); } @@ -146,9 +152,9 @@ public function testIndexWithIE8RedirectAndViewAndDirDefined() { ->expects($this->once()) ->method('linkToRoute') ->with('files.view.index') - ->will($this->returnValue('/apps/files/')); + ->will($this->returnValue('/owncloud/index.php/apps/files/')); - $expected = new RedirectResponse('/apps/files/#?dir=MyDir&view=MyView'); + $expected = new RedirectResponse('/owncloud/index.php/apps/files/#?dir=MyDir&view=MyView'); $this->assertEquals($expected, $this->viewController->index('MyDir', 'MyView')); } @@ -321,7 +327,7 @@ public function showFileMethodProvider() { */ public function testShowFileRouteWithFolder($useShowFile) { $node = $this->createMock('\OCP\Files\Folder'); - $node->expects($this->once()) + $node->expects($this->any()) ->method('getPath') ->will($this->returnValue('/testuser1/files/test/sub')); @@ -332,11 +338,11 @@ public function testShowFileRouteWithFolder($useShowFile) { ->with('testuser1/files/') ->will($this->returnValue($baseFolder)); - $baseFolder->expects($this->at(0)) + $baseFolder->expects($this->any()) ->method('getById') ->with(123) ->will($this->returnValue([$node])); - $baseFolder->expects($this->at(1)) + $baseFolder->expects($this->any()) ->method('getRelativePath') ->with('/testuser1/files/test/sub') ->will($this->returnValue('/test/sub')); @@ -345,9 +351,10 @@ public function testShowFileRouteWithFolder($useShowFile) { ->expects($this->once()) ->method('linkToRoute') ->with('files.view.index', ['dir' => '/test/sub']) - ->will($this->returnValue('/apps/files/?dir=/test/sub')); + ->will($this->returnValue('/owncloud/index.php/apps/files/?dir=/test/sub')); - $expected = new Http\RedirectResponse('/apps/files/?dir=/test/sub'); + $expected = new Http\RedirectResponse('/owncloud/index.php/apps/files/?dir=/test/sub'); + $expected->addHeader('Webdav-Location', '/owncloud/remote.php/dav/files/testuser1/test/sub'); if ($useShowFile) { $this->assertEquals($expected, $this->viewController->showFile(123)); } else { @@ -360,7 +367,7 @@ public function testShowFileRouteWithFolder($useShowFile) { */ public function testShowFileRouteWithFile($useShowFile) { $parentNode = $this->createMock('\OCP\Files\Folder'); - $parentNode->expects($this->once()) + $parentNode->expects($this->any()) ->method('getPath') ->will($this->returnValue('testuser1/files/test')); @@ -378,23 +385,29 @@ public function testShowFileRouteWithFile($useShowFile) { $node->expects($this->once()) ->method('getName') ->will($this->returnValue('somefile.txt')); + $node->expects($this->any()) + ->method('getPath') + ->will($this->returnValue('testuser1/files/test/somefile.txt')); - $baseFolder->expects($this->at(0)) + $baseFolder->expects($this->any()) ->method('getById') ->with(123) ->will($this->returnValue([$node])); - $baseFolder->expects($this->at(1)) + $baseFolder->expects($this->any()) ->method('getRelativePath') - ->with('testuser1/files/test') - ->will($this->returnValue('/test')); + ->will($this->returnValueMap([ + ['testuser1/files/test', '/test'], + ['testuser1/files/test/somefile.txt', '/test/somefile.txt'], + ])); $this->urlGenerator ->expects($this->once()) ->method('linkToRoute') ->with('files.view.index', ['dir' => '/test', 'scrollto' => 'somefile.txt']) - ->will($this->returnValue('/apps/files/?dir=/test/sub&scrollto=somefile.txt')); + ->will($this->returnValue('/owncloud/index.php/apps/files/?dir=/test&scrollto=somefile.txt')); - $expected = new Http\RedirectResponse('/apps/files/?dir=/test/sub&scrollto=somefile.txt'); + $expected = new Http\RedirectResponse('/owncloud/index.php/apps/files/?dir=/test&scrollto=somefile.txt'); + $expected->addHeader('Webdav-Location', '/owncloud/remote.php/dav/files/testuser1/test/somefile.txt'); if ($useShowFile) { $this->assertEquals($expected, $this->viewController->showFile(123)); } else { @@ -428,6 +441,30 @@ public function testShowFileRouteWithInvalidFileId($useShowFile) { } } + /** + */ + public function testShowFileRouteWithInvalidFileIdLoggedIn() { + $baseFolder = $this->createMock('\OCP\Files\Folder'); + $this->rootFolder->expects($this->once()) + ->method('get') + ->with('testuser1/files/') + ->will($this->returnValue($baseFolder)); + + $baseFolder->expects($this->at(0)) + ->method('getById') + ->with(123) + ->will($this->returnValue([])); + + $this->userSession->expects($this->any()) + ->method('isLoggedIn') + ->will($this->returnValue(true)); + + $response = $this->viewController->index('MyDir', 'MyView', '123'); + $this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $response); + $params = $response->getParams(); + $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus()); + } + /** * @dataProvider showFileMethodProvider */ @@ -485,9 +522,9 @@ public function testShowFileRouteWithTrashedFile($useShowFile) { ->expects($this->once()) ->method('linkToRoute') ->with('files.view.index', ['view' => 'trashbin', 'dir' => '/test.d1462861890/sub', 'scrollto' => 'somefile.txt']) - ->will($this->returnValue('/apps/files/?view=trashbin&dir=/test.d1462861890/sub&scrollto=somefile.txt')); + ->will($this->returnValue('/owncloud/index.php/apps/files/?view=trashbin&dir=/test.d1462861890/sub&scrollto=somefile.txt')); - $expected = new Http\RedirectResponse('/apps/files/?view=trashbin&dir=/test.d1462861890/sub&scrollto=somefile.txt'); + $expected = new Http\RedirectResponse('/owncloud/index.php/apps/files/?view=trashbin&dir=/test.d1462861890/sub&scrollto=somefile.txt'); if ($useShowFile) { $this->assertEquals($expected, $this->viewController->showFile(123)); } else {