diff --git a/application/modules/opac/controllers/AuthController.php b/application/modules/opac/controllers/AuthController.php index a4e14f9bd3234ceb4ec658c1dcdd36c93da34b80..b173290bbadceed2df3ea4c71d8f2cb7da8e770b 100644 --- a/application/modules/opac/controllers/AuthController.php +++ b/application/modules/opac/controllers/AuthController.php @@ -35,12 +35,23 @@ class AuthController extends ZendAfi_Controller_Action { public function isCasRequest() { - $service_cas_server=$this->_request->getParam('service'); - return (strlen($service_cas_server)>0); + return strlen($this->getCasServerUrl())>0; + } + + + public function getCasServerUrl() { + return $this->_request->getParam('service'); + } + + public function notify($message) { + $this->_helper->notify($message); + } + + public function getRedirectDefaultUrl() { + return $this->_request->getParam('redirect','/opac'); } protected function processCas() { - xdebug_break(); $service_cas_server=$this->_request->getParam('service'); if (strlen($service_cas_server)<1) @@ -68,6 +79,10 @@ class AuthController extends ZendAfi_Controller_Action { } + public function redirect($redirect_url) { + $this->_redirect($redirect_url); + } + protected function _loginResult($redirect = '', $no_redirection = false){ $redirect = ($redirect == '') ? $this->_request->getServer('HTTP_REFERER') : $redirect; @@ -110,29 +125,6 @@ class AuthController extends ZendAfi_Controller_Action { } - public function isCasDeconnected($service_cas_server) { - - if (strlen($service_cas_server)>0) { - if (stristr($service_cas_server,'deconnexion=ok') != FALSE) - return true; - } - return false; - } - - - public function urlServiceCas($service_cas_server){ - - $ticket=md5(Zend_Session::getId()); - $queries=[]; - $url_cas=array_merge(['query'=> '', - 'path' => ''],parse_url($service_cas_server)); - - parse_str($url_cas['query'],$queries); - $queries['ticket']=$ticket; - $path=$url_cas['path']?$url_cas['path']:''; - return 'http://'.$url_cas['host'].$path.'?'.http_build_query($queries); - } - function loginAction() { $this->view->preferences = Class_Profil::getCurrentProfil()->getCfgModulesPreferences('auth','login'); @@ -141,17 +133,21 @@ class AuthController extends ZendAfi_Controller_Action { $service = $this->_getParam('service',''); $this->view->service = $service; - $this->_loginResult($redirect); + $strategy = Auth_Strategy_Abstract::strategyForController($this); + $strategy->setDefaultUrl($this->_getParam('redirect','/opac')); + $strategy->processLogin(); + } function ajaxLoginAction(){ $this->view->preferences = Class_Profil::getCurrentProfil()->getCfgModulesPreferences('auth','login'); + $strategy = Auth_Strategy_Abstract::strategyForController($this); + $strategy->disableRedirect(); + $strategy->processLogin(); $redirect = urldecode($this->_getParam('redirect')); - - $this->_loginResult('',true); - + if (!Class_Users::getLoader()->getIdentity()==null) { $this->renderPopup($redirect); return ; @@ -172,7 +168,11 @@ class AuthController extends ZendAfi_Controller_Action { function boiteLoginAction() { $this->view->preferences = Class_Profil::getCurrentProfil()->getModuleAccueilPreferencesByType('auth'); - $this->_loginResult(); + + $strategy = Auth_Strategy_Abstract::strategyForController($this); + $strategy->setDefaultUrl($this->_request->getServer('HTTP_REFERER')); + $strategy->processLogin(); + } @@ -302,14 +302,16 @@ class AuthController extends ZendAfi_Controller_Action { abstract class Auth_Strategy_Abstract { protected $redirect_url=''; + protected $disable_redirect=false; + static public function strategyForController($controller) { - if ($controller->isCasRequest() && $this->isLogged()) + if ($controller->isCasRequest() && static::isLogged()) return new Auth_Strategy_Cas_Logged($controller); - if ($controller->isCasRequest() && !$this->isLogged()) + if ($controller->isCasRequest() && !static::isLogged()) return new Auth_Strategy_Cas_NotLogged($controller); - if ($this->isLogged()) + if (static::isLogged()) return new Auth_Strategy_Logged($controller); return new Auth_Strategy_NotLogged($controller); @@ -319,20 +321,35 @@ abstract class Auth_Strategy_Abstract { return Class_Users::getIdentity(); } + public function disableRedirect() { + $this->disable_redirect = true; + } + public function __construct($controller) { $this->controller=$controller; + $this->default_url=$this->controller->getRedirectDefaultUrl(); } + public function getRequest(){ + return $this->controller->getRequest(); + } + + public function processLogin() { $this->prepareLogin(); if ($this->getRequest()->isPost()) $this->handlePost(); if ($this->shouldRedirect()) - $controller->redirect($this->getRedirectUrl()); + $this->controller->redirect($this->redirect_url); } + public function setDefaultUrl($url) { + $this->default_url=$url; + } + + public function prepareLogin() { } @@ -346,6 +363,8 @@ abstract class Auth_Strategy_Abstract { } public function getRedirectUrl() { + if ($this->disable_redirect) + return ''; return $this->redirect_url; } } @@ -353,40 +372,65 @@ abstract class Auth_Strategy_Abstract { class Auth_Strategy_NotLogged extends Auth_Strategy_Abstract{ - - public function handlePost() { - if (!$controller->_authenticate()) - $controller->getHelper()->notify($error); + public function prepareLogin() { } + + public function handlePost() { + $this->redirect_url=$this->default_url; + if ($error=$this->controller->_authenticate()) { + $this->controller->notify($error); + } + } - } class Auth_Strategy_Logged extends Auth_Strategy_Abstract{ +} - public function prepareLogin() { +class Auth_Strategy_Cas_Abstract extends Auth_Strategy_Abstract{ + + public function urlServiceCas(){ + $ticket=md5(Zend_Session::getId()); + $queries=[]; + $url_cas=array_merge(['query'=> '', + 'path' => ''],parse_url($this->controller->getCasServerUrl())); + parse_str($url_cas['query'],$queries); + $queries['ticket']=$ticket; + $path=$url_cas['path']?$url_cas['path']:''; + return 'http://'.$url_cas['host'].$path.'?'.http_build_query($queries); } +} - public function shouldRedirect() { - return false; +class Auth_Strategy_Cas_Logged extends Auth_Strategy_Cas_Abstract{ + + public function prepareLogin() { + if ($this->isCasDeconnected()) + return $this->redirect_url='/opac'; + $this->redirect_url=$this->urlServiceCas(); } - public function getRedirectUrl() { - return ''; + + protected function isCasDeconnected() { + return stristr($this->controller->getCasServerUrl(),'deconnexion=ok') != FALSE; + } } -class Auth_Strategy_Cas_Logged extends Auth_Strategy_Abstract{ +class Auth_Strategy_Cas_NotLogged extends Auth_Strategy_Cas_Abstract{ + + public function handlePost() { + if ($error=$this->controller->_authenticate()) + return $this->controller->notify($error); + $this->redirect_url=$this->urlServiceCas(); + } -} -class Auth_Strategy_Cas_NotLogged extends Auth_Strategy_Abstract{ } \ No newline at end of file diff --git a/tests/application/modules/opac/controllers/AuthControllerTest.php b/tests/application/modules/opac/controllers/AuthControllerTest.php index 2c8deb673948d3b27c2180fd67d8317b7084cdb6..235f86cbd0bb1893e39a05ecaff9dccfe5e1ba8c 100644 --- a/tests/application/modules/opac/controllers/AuthControllerTest.php +++ b/tests/application/modules/opac/controllers/AuthControllerTest.php @@ -283,6 +283,7 @@ class AuthControllerNobodyLoggedAndRegistrationAllowedAjaxLoginTest extends Auth class AuthControllerNobodyLoggedAndNoRegistrationTest extends AuthControllerNobodyLoggedTestCase { + public function setUp() { $interdire_enregistrement = new Class_AdminVar(); $interdire_enregistrement @@ -743,10 +744,10 @@ class AuthControllerPostSimpleFailureTest extends AuthControllerPostSimpleTestCa } /** @test */ - public function withAuthenticationFailureResponseShouldNotBeARedirect() { + public function withAuthenticationFailureResponseShouldBeRedirect() { $this->postDispatch('/opac/auth/login', ['username' => 'foo', 'password' => 'bar']); - $this->assertRedirect(); + $this->assertRedirectTo('/opac'); }