Skip to content

Commit

Permalink
Merge pull request #30383 from owncloud/private-link-dav-location
Browse files Browse the repository at this point in the history
Add Webdav-Location header in private link redirect
  • Loading branch information
DeepDiver1975 authored Feb 6, 2018
2 parents 9630ac5 + c67d7a4 commit ad6bae5
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 23 deletions.
18 changes: 15 additions & 3 deletions apps/files/lib/Controller/ViewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
use OCP\IURLGenerator;
use OCP\IUserSession;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use OCP\AppFramework\Http;

/**
* Class ViewController
Expand Down Expand Up @@ -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;
}
}

Expand All @@ -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();
}
}
77 changes: 57 additions & 20 deletions apps/files/tests/Controller/ViewControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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'));
}

Expand All @@ -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'));
}

Expand All @@ -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'));
}

Expand Down Expand Up @@ -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'));

Expand All @@ -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'));
Expand All @@ -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 {
Expand All @@ -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'));

Expand All @@ -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 {
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit ad6bae5

Please sign in to comment.