diff --git a/application/modules/auth/models/UserModel.php b/application/modules/auth/models/UserModel.php index f6c6c0cb70ece48779bca6669b4093fe16c1e3f8..d3a25e6b604c29975c1836bb39ee1ebea683823d 100644 --- a/application/modules/auth/models/UserModel.php +++ b/application/modules/auth/models/UserModel.php @@ -96,7 +96,7 @@ class Auth_UserModel extends Unl_Model { { $username = Zend_Auth::getInstance()->getIdentity(); if (!$username) { - throw new Exception('You must be logged in to view this page.'); + self::authenticateUser(TRUE); } return self::findByUsername($username); @@ -405,6 +405,39 @@ class Auth_UserModel extends Unl_Model { $db->query($sql); } + /** + * If the user is not currently logged in, calling this will attempt to log them in + * and if $required is TRUE, additionally redirect them to the login page. + * @param bool $required + */ + static public function authenticateUser($required = FALSE) + { + $redirectUri = $_SERVER['REQUEST_URI']; + $baseUrl = Zend_Controller_Front::getInstance()->getBaseUrl(); + $redirectUri = substr($redirectUri, strlen($baseUrl)); + + $authSession = new Zend_Session_Namespace('Auth_IndexController'); + $authSession->referer = $redirectUri; + + + if ($_SERVER['HTTPS'] == 'on') { + $serviceUrl = 'https://'; + } else { + $serviceUrl = 'http://'; + } + $serviceUrl .= $_SERVER['SERVER_NAME'] . Zend_Controller_Front::getInstance()->getBaseUrl() . '/auth/index/validate'; + + $casAdapter = new Unl_Auth_Adapter_Cas($serviceUrl, 'https://login.unl.edu/cas'); + if (!$required) { + $casAdapter->setGateway(); + } + + if ($casAdapter->isTicketExpired() || !Zend_Auth::getInstance()->hasIdentity()) { + header('Location: ' . $casAdapter->getLoginUrl()); + exit; + } + } + public function __toString() { return $this->getFirstName() . ' ' . $this->getLastName(); diff --git a/application/modules/requests/controllers/IndexController.php b/application/modules/requests/controllers/IndexController.php index b67d5d06a11d45dd880900abdacfdc5ffcbc38ce..5a37969e0fbe8bb1f2ab023d15741bc5c3d0e453 100644 --- a/application/modules/requests/controllers/IndexController.php +++ b/application/modules/requests/controllers/IndexController.php @@ -16,7 +16,6 @@ class Requests_IndexController extends App_Controller_Action */ public function indexAction() { - $this->_authorize->requireLogin(); $in = $this->getRequest()->getParams(); $user = Auth_UserModel::findCurrentUser(); diff --git a/library/App/Controller/Action.php b/library/App/Controller/Action.php index e29d41841226aca77f801aa60dcb55a910e27974..0b162a25e950df90c5292cf7736fced4a7c60b59 100644 --- a/library/App/Controller/Action.php +++ b/library/App/Controller/Action.php @@ -35,29 +35,6 @@ class App_Controller_Action extends Unl_Controller_Action return; } - - - // Do a passive authentication check - $redirectUri = $_SERVER['REQUEST_URI']; - $baseUrl = Zend_Controller_Front::getInstance()->getBaseUrl(); - $redirectUri = substr($redirectUri, strlen($baseUrl)); - - $authSession = new Zend_Session_Namespace('Auth_IndexController'); - $authSession->referer = $redirectUri; - - - if ($_SERVER['HTTPS'] == 'on') { - $serviceUrl = 'https://'; - } else { - $serviceUrl = 'http://'; - } - $serviceUrl .= $_SERVER['SERVER_NAME'] . Zend_Controller_Front::getInstance()->getBaseUrl() . '/auth/index/validate'; - - $casAdapter = new Unl_Auth_Adapter_Cas($serviceUrl, 'https://login.unl.edu/cas', $this->_getParam('ticket')); - $casAdapter->setGateway(); - - if ($casAdapter->isTicketExpired()) { - $this->_redirect($casAdapter->getLoginUrl()); - } + Auth_UserModel::authenticateUser(); } }