Skip to content

Commit

Permalink
Move login redirect URL session stashing into SimpleCAS service class
Browse files Browse the repository at this point in the history
Having these methods in AuthController was a bit limiting, since there are various use cases where you might need to interact with the stashed attribute outside of that specific controller.
  • Loading branch information
jmikola committed Nov 11, 2010
1 parent 97a1dee commit e54596e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 20 deletions.
48 changes: 29 additions & 19 deletions Controller/AuthController.php
Expand Up @@ -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,
Expand All @@ -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()
Expand All @@ -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);
}

/**
Expand Down
41 changes: 40 additions & 1 deletion SimpleCAS.php
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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()
{
Expand All @@ -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.
*
Expand Down

0 comments on commit e54596e

Please sign in to comment.