diff --git a/Controller/AuthController.php b/Controller/AuthController.php index da20e55..7fa770d 100644 --- a/Controller/AuthController.php +++ b/Controller/AuthController.php @@ -6,11 +6,6 @@ abstract class AuthController extends Controller { - /** - * Session attribute for stashing the login action's referrer - */ - const REFERER = '__SIMPLECAS_LOGIN_REFERER'; - /** * Returns the absolute service URL that CAS should redirect to after * logging out. This will also be used for redirection after logging in, @@ -36,19 +31,20 @@ public function loginAction() * assume they wish to reauthenticate as another user. Redirect the * user to the CAS logout URL, which should return to this login action. * - * The current referer will be saved if it is available. + * If a referer URL is available, it will be saved for post-login + * redirection. */ if ($simplecas->isAuthenticated()) { $simplecas->unauthenticate(); if ($referer = $this->getRefererUrl()) { - $this->getSession()->set(static::REFERER, $referer); + $simplecas->setLoginRedirectUrl($referer); } return $this->redirect($simplecas->getLogoutUrl()); } - return $this->redirect($simplecas->getLoginUrl($this->getRedirectUrlOnce())); + return $this->redirect($simplecas->getLoginUrl($this->getLoginRedirectUrlOnce())); } public function logoutAction() @@ -61,32 +57,46 @@ public function logoutAction() /** * Get the post-login redirect URL. * + * If no redirect URL is saved in the session, this will default to the + * referer. If either of those URL's is invalid (i.e. an internal CAS URL), + * the service URL will be returned. + * + * @see isValidRedirectUrl() * @return string */ - protected function getRedirectUrl() + protected function getLoginRedirectUrl() { - $redirectUrl = $this->getSession()->get(static::REFERER, $this->getRefererUrl()); + $loginRedirectUrl = $this->getSimpleCAS()->getLoginRedirectUrl($this->getRefererUrl()); // Default to service URL if the referrer is invalid - if (! $this->isValidRedirectUrl($redirectUrl)) { - $redirectUrl = $this->getServiceUrl(); + if (! $this->isValidRedirectUrl($loginRedirectUrl)) { + $loginRedirectUrl = $this->getServiceUrl(); } - return $redirectUrl; + return $loginRedirectUrl; } /** - * Get the post-login redirect URL and remove it from the session if it was - * stashed. + * Get the post-login redirect URL and ensure it's removed from the session. * * @return string */ - protected function getRedirectUrlOnce() + protected function getLoginRedirectUrlOnce() { - $redirectUrl = $this->getRedirectUrl(); - $this->getSession()->remove(static::REFERER); + $loginRedirectUrl = $this->getLoginRedirectUrl(); + $this->getSimpleCAS()->removeLoginRedirectUrl(); + + return $loginRedirectUrl; + } - return $redirectUrl; + /** + * Set the post-login redirect URL. + * + * @param string + */ + protected function setLoginRedirectUrl($loginRedirectUrl) + { + $this->getSimpleCAS()->setLoginRedirectUrl($loginRedirectUrl); } /** diff --git a/SimpleCAS.php b/SimpleCAS.php index 3acfcbf..740c6f9 100644 --- a/SimpleCAS.php +++ b/SimpleCAS.php @@ -23,6 +23,11 @@ class SimpleCAS */ const UID = '__SIMPLECAS_UID'; + /** + * Session attribute for login action redirection. + */ + const LOGIN_REDIRECT_URL = '__SIMPLECAS_LOGIN_REDIRECT_URL'; + /** * CAS service protocol. * @@ -269,7 +274,7 @@ public function getLogoutUrl($url = null) /** * Returns the current URL without CAS-affecting parameters. * - * @return string url + * @return string */ public function getCurrentUrl() { @@ -285,6 +290,40 @@ public function getCurrentUrl() return preg_replace(array_keys($replacements), array_values($replacements), $uri); } + /** + * Gets the login redirect URL from the session. + * + * @param mixed $default + * @return string + */ + public function getLoginRedirectUrl($default = null) + { + return $this->session->get(static::LOGIN_REDIRECT_URL, $default); + } + + /** + * Sets the login redirect URL in the session. + * + * @param string $loginRedirectUrl + * @return SimpleCAS + */ + public function setLoginRedirectUrl($loginRedirectUrl) + { + $this->session->set(static::LOGIN_REDIRECT_URL, $loginRedirectUrl); + return $this; + } + + /** + * Remove the login redirect URL from the session. + * + * @return SimpleCAS + */ + public function removeLoginRedirectUrl() + { + $this->session->remove(static::LOGIN_REDIRECT_URL); + return $this; + } + /** * Redirect the client to another URL. *