summaryrefslogtreecommitdiffstats
path: root/apps
diff options
context:
space:
mode:
authorRoeland Jago Douma <rullzer@users.noreply.github.com>2018-03-05 11:11:43 +0100
committerGitHub <noreply@github.com>2018-03-05 11:11:43 +0100
commit05ef2d70e7258ff8ba25eac32526a9e982980e3d (patch)
treef809bbc5d4fe504bc1d1d46b9ceed7bf67877788 /apps
parent36d89a922386adbf6c80e3bb6581776512956f0f (diff)
parent3824e6f631e3345401ac5720d35ec53e8983dcaa (diff)
downloadnextcloud-server-05ef2d70e7258ff8ba25eac32526a9e982980e3d.tar.gz
nextcloud-server-05ef2d70e7258ff8ba25eac32526a9e982980e3d.zip
Merge pull request #8590 from nextcloud/redirect-to-download-after-share
Sharing: redirect to download after authentification if requested
Diffstat (limited to 'apps')
-rw-r--r--apps/files_sharing/lib/Controller/ShareController.php20
-rw-r--r--apps/files_sharing/tests/Controller/ShareControllerTest.php42
2 files changed, 51 insertions, 11 deletions
diff --git a/apps/files_sharing/lib/Controller/ShareController.php b/apps/files_sharing/lib/Controller/ShareController.php
index 1f8864fc5f3..f793d35e3ae 100644
--- a/apps/files_sharing/lib/Controller/ShareController.php
+++ b/apps/files_sharing/lib/Controller/ShareController.php
@@ -170,10 +170,11 @@ class ShareController extends Controller {
*
* Authenticates against password-protected shares
* @param string $token
+ * @param string $redirect
* @param string $password
* @return RedirectResponse|TemplateResponse|NotFoundResponse
*/
- public function authenticate($token, $password = '') {
+ public function authenticate($token, $redirect, $password = '') {
// Check whether share exists
try {
@@ -184,8 +185,17 @@ class ShareController extends Controller {
$authenticate = $this->linkShareAuth($share, $password);
- if($authenticate === true) {
- return new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.showShare', array('token' => $token)));
+ // if download was requested before auth, redirect to download
+ if ($authenticate === true && $redirect === 'download') {
+ return new RedirectResponse($this->urlGenerator->linkToRoute(
+ 'files_sharing.sharecontroller.downloadShare',
+ array('token' => $token))
+ );
+ } else if ($authenticate === true) {
+ return new RedirectResponse($this->urlGenerator->linkToRoute(
+ 'files_sharing.sharecontroller.showShare',
+ array('token' => $token))
+ );
}
$response = new TemplateResponse($this->appName, 'authenticate', array('wrongpw' => true), 'guest');
@@ -294,7 +304,7 @@ class ShareController extends Controller {
// Share is password protected - check whether the user is permitted to access the share
if ($share->getPassword() !== null && !$this->linkShareAuth($share)) {
return new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.authenticate',
- array('token' => $token)));
+ array('token' => $token, 'redirect' => 'preview')));
}
if (!$this->validateShare($share)) {
@@ -480,7 +490,7 @@ class ShareController extends Controller {
// Share is password protected - check whether the user is permitted to access the share
if ($share->getPassword() !== null && !$this->linkShareAuth($share)) {
return new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.authenticate',
- ['token' => $token]));
+ ['token' => $token, 'redirect' => 'download']));
}
$files_list = null;
diff --git a/apps/files_sharing/tests/Controller/ShareControllerTest.php b/apps/files_sharing/tests/Controller/ShareControllerTest.php
index 6dc577a354c..a977a422e7d 100644
--- a/apps/files_sharing/tests/Controller/ShareControllerTest.php
+++ b/apps/files_sharing/tests/Controller/ShareControllerTest.php
@@ -218,7 +218,7 @@ class ShareControllerTest extends \Test\TestCase {
->with('token')
->will($this->throwException(new \OCP\Share\Exceptions\ShareNotFound()));
- $response = $this->shareController->authenticate('token');
+ $response = $this->shareController->authenticate('token', 'preview');
$expectedResponse = new NotFoundResponse();
$this->assertEquals($expectedResponse, $response);
}
@@ -249,7 +249,38 @@ class ShareControllerTest extends \Test\TestCase {
->with('files_sharing.sharecontroller.showShare', ['token'=>'token'])
->willReturn('redirect');
- $response = $this->shareController->authenticate('token', 'validpassword');
+ $response = $this->shareController->authenticate('token', 'preview', 'validpassword');
+ $expectedResponse = new RedirectResponse('redirect');
+ $this->assertEquals($expectedResponse, $response);
+ }
+
+ public function testAuthenticateValidPasswordAndDownload() {
+ $share = \OC::$server->getShareManager()->newShare();
+ $share->setId(42);
+
+ $this->shareManager
+ ->expects($this->once())
+ ->method('getShareByToken')
+ ->with('token')
+ ->willReturn($share);
+
+ $this->shareManager
+ ->expects($this->once())
+ ->method('checkPassword')
+ ->with($share, 'validpassword')
+ ->willReturn(true);
+
+ $this->session
+ ->expects($this->once())
+ ->method('set')
+ ->with('public_link_authenticated', '42');
+
+ $this->urlGenerator->expects($this->once())
+ ->method('linkToRoute')
+ ->with('files_sharing.sharecontroller.downloadShare', ['token'=>'token'])
+ ->willReturn('redirect');
+
+ $response = $this->shareController->authenticate('token', 'download', 'validpassword');
$expectedResponse = new RedirectResponse('redirect');
$this->assertEquals($expectedResponse, $response);
}
@@ -292,7 +323,7 @@ class ShareControllerTest extends \Test\TestCase {
$data['errorMessage'] === 'Wrong password';
}));
- $response = $this->shareController->authenticate('token', 'invalidpassword');
+ $response = $this->shareController->authenticate('token', 'preview', 'invalidpassword');
$expectedResponse = new TemplateResponse($this->appName, 'authenticate', array('wrongpw' => true), 'guest');
$expectedResponse->throttle();
$this->assertEquals($expectedResponse, $response);
@@ -323,7 +354,7 @@ class ShareControllerTest extends \Test\TestCase {
$this->urlGenerator->expects($this->once())
->method('linkToRoute')
- ->with('files_sharing.sharecontroller.authenticate', ['token' => 'validtoken'])
+ ->with('files_sharing.sharecontroller.authenticate', ['token' => 'validtoken', 'redirect' => 'preview'])
->willReturn('redirect');
// Test without a not existing token
@@ -505,7 +536,7 @@ class ShareControllerTest extends \Test\TestCase {
$this->urlGenerator->expects($this->once())
->method('linkToRoute')
- ->with('files_sharing.sharecontroller.authenticate', ['token' => 'validtoken'])
+ ->with('files_sharing.sharecontroller.authenticate', ['token' => 'validtoken', 'redirect' => 'download'])
->willReturn('redirect');
// Test with a password protected share and no authentication
@@ -533,5 +564,4 @@ class ShareControllerTest extends \Test\TestCase {
$expectedResponse = new DataResponse('Share is read-only');
$this->assertEquals($expectedResponse, $response);
}
-
}