Skip to content

Commit 5d1054a

Browse files
committed
Proper message shown when private links accessed
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]>
1 parent 3aefba6 commit 5d1054a

File tree

5 files changed

+56
-4
lines changed

5 files changed

+56
-4
lines changed

apps/files/lib/Controller/ViewController.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,12 @@ public function showFile($fileId) {
281281
$params = [];
282282

283283
if (empty($files) && $this->appManager->isEnabledForUser('files_trashbin')) {
284-
$baseFolder = $this->rootFolder->get($uid . '/files_trashbin/files/');
285-
$files = $baseFolder->getById($fileId);
286-
$params['view'] = 'trashbin';
284+
// Access files_trashbin if it exists
285+
if ( $this->rootFolder->nodeExists($uid . '/files_trashbin/files/')) {
286+
$baseFolder = $this->rootFolder->get($uid . '/files_trashbin/files/');
287+
$files = $baseFolder->getById($fileId);
288+
$params['view'] = 'trashbin';
289+
}
287290
}
288291

289292
if (!empty($files)) {
@@ -299,6 +302,13 @@ public function showFile($fileId) {
299302
}
300303
return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index', $params));
301304
}
305+
306+
if ( $this->userSession->isLoggedIn() and empty($files)) {
307+
$l = \OC::$server->getL10N("core");
308+
$param["error"] = $l->t("You don't have permissions to access this file/folder - Please contact the owner to share it with you.");
309+
return new TemplateResponse("core", 'error', ["errors" => [$param]], 'guest');
310+
}
311+
302312
throw new \OCP\Files\NotFoundException();
303313
}
304314
}

apps/files/tests/Controller/ViewControllerTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,10 @@ public function testShowFileRouteWithTrashedFile($useShowFile) {
428428
->with('files_trashbin')
429429
->will($this->returnValue(true));
430430

431+
$this->rootFolder->expects($this->once())
432+
->method('nodeExists')
433+
->will($this->returnValue(true));
434+
431435
$parentNode = $this->createMock('\OCP\Files\Folder');
432436
$parentNode->expects($this->once())
433437
->method('getPath')
@@ -440,7 +444,8 @@ public function testShowFileRouteWithTrashedFile($useShowFile) {
440444
->method('get')
441445
->with('testuser1/files/')
442446
->will($this->returnValue($baseFolderFiles));
443-
$this->rootFolder->expects($this->at(1))
447+
//The index is pointing to 2, because nodeExists internally calls get method.
448+
$this->rootFolder->expects($this->at(2))
444449
->method('get')
445450
->with('testuser1/files_trashbin/files/')
446451
->will($this->returnValue($baseFolderTrash));

core/Controller/LoginController.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,21 @@ public function showLoginForm($user, $redirect_url, $remember_login) {
166166
$parameters['user_autofocus'] = true;
167167
}
168168

169+
/**
170+
* If redirect_url is not empty and remember_login is null and
171+
* user not logged in and check if the string
172+
* webroot+"/index.php/f/" is in redirect_url then
173+
* user is trying to access files for which he needs to login.
174+
*/
175+
176+
if ((!empty($redirect_url)) and ($remember_login === null) and
177+
($this->userSession->isLoggedIn() === false) and
178+
(strpos(urldecode($this->urlGenerator->getAbsoluteURL(urldecode($redirect_url))),
179+
urldecode($this->urlGenerator->getAbsoluteURL('/index.php/f/'))) !== false)) {
180+
181+
$parameters['accessLink'] = true;
182+
}
183+
169184
return new TemplateResponse(
170185
$this->appName, 'login', $parameters, 'guest'
171186
);

core/templates/login.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@
6666
<?php p($l->t('Wrong password.')); ?>
6767
</p>
6868
<?php } ?>
69+
<?php if (!empty($_['accessLink'])) { ?>
70+
<p class="warning">
71+
<?php p($l->t("You are trying to access a private link. Please log in first.")) ?>
72+
</p>
73+
<?php } ?>
6974
<?php if ($_['rememberLoginAllowed'] === true) : ?>
7075
<div class="remember-login-container">
7176
<?php if ($_['rememberLoginState'] === 0) { ?>

tests/Core/Controller/LoginControllerTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,23 @@ public function testShowLoginFormForLoggedInUsers() {
137137
$this->assertEquals($expectedResponse, $this->loginController->showLoginForm('', '', ''));
138138
}
139139

140+
public function testResponseForNotLoggedinUser() {
141+
$params = [
142+
'messages' => Array (),
143+
'loginName' => '',
144+
'user_autofocus' => true,
145+
'redirect_url' => '%2Findex.php%2Ff%2F17',
146+
'canResetPassword' => true,
147+
'resetPasswordLink' => null,
148+
'alt_login' => Array (),
149+
'rememberLoginAllowed' => false,
150+
'rememberLoginState' => 0
151+
];
152+
153+
$expectedResponse = new TemplateResponse('core', 'login', $params, 'guest');
154+
$this->assertEquals($expectedResponse, $this->loginController->showLoginForm('', '%2Findex.php%2Ff%2F17', ''));
155+
}
156+
140157
public function testShowLoginFormWithErrorsInSession() {
141158
$this->userSession
142159
->expects($this->once())

0 commit comments

Comments
 (0)