Skip to content

Commit 342bf65

Browse files
committed
Ensure referrer is saved in session even when AuthComponent::$loginRedirect is set.
Clarified redirectUrl() docblock.
1 parent 9d367e1 commit 342bf65

File tree

2 files changed

+16
-30
lines changed

2 files changed

+16
-30
lines changed

lib/Cake/Controller/Component/AuthComponent.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ public function startup(Controller $controller) {
310310

311311
if ($loginAction == $url) {
312312
if (empty($request->data)) {
313-
if (!$this->Session->check('Auth.redirect') && !$this->loginRedirect && env('HTTP_REFERER')) {
313+
if (!$this->Session->check('Auth.redirect') && env('HTTP_REFERER')) {
314314
$this->Session->write('Auth.redirect', $controller->referer(null, true));
315315
}
316316
}
@@ -554,7 +554,7 @@ public function login($user = null) {
554554
}
555555

556556
/**
557-
* Log a user out.
557+
* Log a user out.
558558
*
559559
* Returns the login action to redirect to. Triggers the logout() method of
560560
* all the authenticate objects, so they can perform custom logout logic.
@@ -645,9 +645,17 @@ public function redirect($url = null) {
645645
/**
646646
* Get the URL a use should be redirected to upon login.
647647
*
648-
* If no parameter is passed, gets the authentication redirect URL. Pass a url in to
649-
* set the destination a user should be redirected to upon logging in. Will fallback to
650-
* AuthComponent::$loginRedirect if there is no stored redirect value.
648+
* Pass a url in to set the destination a user should be redirected to upon
649+
* logging in.
650+
*
651+
* If no parameter is passed, gets the authentication redirect URL. The url
652+
* returned is as per following rules:
653+
*
654+
* - Returns the session Auth.redirect value if it is present and for the same
655+
* domain the current app is running on.
656+
* - If there is no session value and there is a $loginRedirect, the $loginRedirect
657+
* value is returned.
658+
* - If there is no session and no $loginRedirect, / is returned.
651659
*
652660
* @param string|array $url Optional URL to write as the login redirect URL.
653661
* @return string Redirect URL
@@ -663,8 +671,10 @@ public function redirectUrl($url = null) {
663671
if (Router::normalize($redir) == Router::normalize($this->loginAction)) {
664672
$redir = $this->loginRedirect;
665673
}
666-
} else {
674+
} elseif ($this->loginRedirect) {
667675
$redir = $this->loginRedirect;
676+
} else {
677+
$redir = '/';
668678
}
669679
return Router::normalize($redir);
670680
}

lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -417,30 +417,6 @@ public function testLogin() {
417417
$this->assertEquals($user, $this->Auth->user());
418418
}
419419

420-
/**
421-
* test that being redirected to the login page, with no post data does
422-
* not set the session value. Saving the session value in this circumstance
423-
* can cause the user to be redirected to an already public page.
424-
*
425-
* @return void
426-
*/
427-
public function testLoginActionNotSettingAuthRedirect() {
428-
$_SERVER['HTTP_REFERER'] = '/pages/display/about';
429-
430-
$this->Controller->data = array();
431-
$this->Controller->request->addParams(Router::parse('auth_test/login'));
432-
$this->Controller->request->url = 'auth_test/login';
433-
$this->Auth->Session->delete('Auth');
434-
435-
$this->Auth->loginRedirect = '/users/dashboard';
436-
$this->Auth->loginAction = 'auth_test/login';
437-
$this->Auth->userModel = 'AuthUser';
438-
439-
$this->Auth->startup($this->Controller);
440-
$redirect = $this->Auth->Session->read('Auth.redirect');
441-
$this->assertNull($redirect);
442-
}
443-
444420
/**
445421
* testAuthorizeFalse method
446422
*

0 commit comments

Comments
 (0)