Skip to content

Commit

Permalink
Proper message shown when private links accessed
Browse files Browse the repository at this point in the history
When user tries to access private links which are
not accessible, then proper message is delivered
instead of Internal server error message. So is the
case when user is logged in and tries to access
private links not accessible.

Signed-off-by: Sujith H <[email protected]>
  • Loading branch information
sharidas committed Jun 29, 2017
1 parent 1a2a671 commit d77aee2
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
16 changes: 13 additions & 3 deletions apps/files/lib/Controller/ViewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,12 @@ public function showFile($fileId) {
$params = [];

if (empty($files) && $this->appManager->isEnabledForUser('files_trashbin')) {
$baseFolder = $this->rootFolder->get($uid . '/files_trashbin/files/');
$files = $baseFolder->getById($fileId);
$params['view'] = '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';
}
}

if (!empty($files)) {
Expand All @@ -299,6 +302,13 @@ public function showFile($fileId) {
}
return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index', $params));
}

if ( $this->userSession->isLoggedIn() and empty($files)) {
$l = \OC::$server->getL10N("core");
$param["error"] = $l->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');
}

throw new \OCP\Files\NotFoundException();
}
}
7 changes: 6 additions & 1 deletion apps/files/tests/Controller/ViewControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,10 @@ public function testShowFileRouteWithTrashedFile($useShowFile) {
->with('files_trashbin')
->will($this->returnValue(true));

$this->rootFolder->expects($this->once())
->method('nodeExists')
->will($this->returnValue(true));

$parentNode = $this->createMock('\OCP\Files\Folder');
$parentNode->expects($this->once())
->method('getPath')
Expand All @@ -440,7 +444,8 @@ public function testShowFileRouteWithTrashedFile($useShowFile) {
->method('get')
->with('testuser1/files/')
->will($this->returnValue($baseFolderFiles));
$this->rootFolder->expects($this->at(1))
//The index is pointing to 2, because nodeExists internally calls get method.
$this->rootFolder->expects($this->at(2))
->method('get')
->with('testuser1/files_trashbin/files/')
->will($this->returnValue($baseFolderTrash));
Expand Down
15 changes: 15 additions & 0 deletions core/Controller/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,21 @@ public function showLoginForm($user, $redirect_url, $remember_login) {
$parameters['user_autofocus'] = true;
}

/**
* If redirect_url is not null and remember_login is null and
* user not logged in and no login name and check if the string
* webroot+"/index.php/f/" is in redirect_url then
* user is trying to access files for which he needs to login.
*/
$fpath = \OC::$WEBROOT. "/index.php/f/";
if (( $redirect_url !== null) and ($remember_login === null) and
($this->userSession->isLoggedIn() === false) and
($this->userSession->getLoginName() === null) and
strpos(urldecode($redirect_url), $fpath) !== false) {

$parameters['accessLink'] = true;
}

return new TemplateResponse(
$this->appName, 'login', $parameters, 'guest'
);
Expand Down
5 changes: 5 additions & 0 deletions core/templates/login.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@
<?php p($l->t('Wrong password.')); ?>
</p>
<?php } ?>
<?php if (!empty($_['accessLink'])) { ?>
<p class="warning">
<?php p($l->t("You are trying to access a private link. Please log in first.")) ?>
</p>
<?php } ?>
<?php if ($_['rememberLoginAllowed'] === true) : ?>
<div class="remember-login-container">
<?php if ($_['rememberLoginState'] === 0) { ?>
Expand Down

0 comments on commit d77aee2

Please sign in to comment.