Skip to content
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
19 changes: 16 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,13 +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 @@ -101,6 +101,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 @@ -113,9 +119,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 @@ -129,9 +135,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 @@ -145,9 +151,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 @@ -320,7 +326,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 @@ -331,11 +337,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 @@ -344,9 +350,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 @@ -359,7 +366,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 @@ -377,23 +384,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 @@ -427,6 +440,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 @@ -484,9 +521,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