diff --git a/application/modules/opac/controllers/AuthController.php b/application/modules/opac/controllers/AuthController.php index a74d679a6c79158b70a3a9239d5e01cf536013ee..e00ef8760196c6554efb77814a51e631041dcae5 100644 --- a/application/modules/opac/controllers/AuthController.php +++ b/application/modules/opac/controllers/AuthController.php @@ -100,28 +100,33 @@ class AuthController extends ZendAfi_Controller_Action { $strategy = Auth_Strategy_Abstract::strategyForController($this); $strategy->setDefaultUrl($this->_getParam('redirect','/opac')); + $strategy->onLoginSuccess(function($user) { + $user->registerNotificationsOn($this->getHelper('notify')); + }); $strategy->processLogin(); $this->view->form_action = 'login'; } public function ajaxLoginAction(){ + $redirect = urldecode($this->_getParam('redirect')); + $location = urldecode($this->_getParam('location')); + $this->view->preferences = Class_Profil::getCurrentProfil()->getCfgModulesPreferences('auth','login'); + $strategy = Auth_Strategy_Abstract::strategyForController($this); $strategy->disableRedirect(); + $strategy->onLoginSuccess(function($user) use ($redirect, $location) { + $user->registerNotificationsOn($this->getHelper('notify')); + $this->renderPopup($redirect, $location); + }); $strategy->processLogin(); - $redirect = urldecode($this->_getParam('redirect')); - $location = urldecode($this->_getParam('location')); - - if (null !== Class_Users::getIdentity()) { - $this->renderPopup($redirect, $location); - return ; + if (!Class_Users::hasIdentity()) { + $this->renderPopup($this->view->url(['action' => 'popup-login']) + . '?redirect=' . urlencode($redirect) + . ($location ? '&location=' . urlencode($location) : '')); } - - $this->renderPopup($this->view->url(['action' => 'popup-login']) - . '?redirect=' . urlencode($redirect) - . ($location ? '&location=' . urlencode($location) : '')); } @@ -149,6 +154,10 @@ class AuthController extends ZendAfi_Controller_Action { $strategy = Auth_Strategy_Abstract::strategyForController($this); $strategy->setDefaultUrl($this->_request->getServer('HTTP_REFERER')); + $strategy->onLoginSuccess(function($user) { + $user->registerNotificationsOn($this->getHelper('notify')); + }); + $strategy->processLogin(); } @@ -434,6 +443,7 @@ abstract class Auth_Strategy_Abstract { $this->prepareLogin(); if ($this->getRequest()->isPost()) $this->handlePost(); + if ($this->shouldRedirect()) $this->controller->redirect($this->redirect_url); } @@ -461,6 +471,10 @@ abstract class Auth_Strategy_Abstract { return ''; return $this->redirect_url; } + + public function onLoginSuccess($do) { + $this->on_login_success_callback = $do; + } } @@ -473,6 +487,9 @@ class Auth_Strategy_NotLogged extends Auth_Strategy_Abstract{ $this->controller->notify($error); } + if (($user = Class_Users::getIdentity()) && isset($this->on_login_success_callback)) + call_user_func($this->on_login_success_callback, $user); + } } diff --git a/library/Class/Users.php b/library/Class/Users.php index 3e984898395158932e5f8939655bc1dea0fe36d3..ba836464ace3f84374d1f1c3de6b09dc56fb37f0 100644 --- a/library/Class/Users.php +++ b/library/Class/Users.php @@ -1507,5 +1507,11 @@ class Class_Users extends Storm_Model_Abstract { return true; return false; } + + + public function registerNotificationsOn($notifiable) { + $this->_notifications = []; + array_map([$notifiable, 'notify'], $this->_notifications); + } } ?> \ No newline at end of file diff --git a/tests/application/modules/AbstractControllerTestCase.php b/tests/application/modules/AbstractControllerTestCase.php index ef9ecc7b987bd12e5f6ddf980ef289b672baedd7..ec09002845775612e51f718f748540be42e81034 100644 --- a/tests/application/modules/AbstractControllerTestCase.php +++ b/tests/application/modules/AbstractControllerTestCase.php @@ -266,6 +266,7 @@ abstract class AbstractControllerTestCase extends Zend_Test_PHPUnit_ControllerTe public function assertFlashMessengerContentContains($value, $message = '') { $messages = $this->_getFlashMessengerMessages(); + $messages = array_filter($messages, 'is_string'); foreach($messages as $message_value){ if (false!==strpos($message_value, $value)) return; diff --git a/tests/application/modules/opac/controllers/AuthControllerTest.php b/tests/application/modules/opac/controllers/AuthControllerTest.php index 8c5a5095856e509fc345f90195973afbc4a98fd3..703dfb38a1e540c9920863d9c5c3b998d5d96618 100644 --- a/tests/application/modules/opac/controllers/AuthControllerTest.php +++ b/tests/application/modules/opac/controllers/AuthControllerTest.php @@ -735,15 +735,6 @@ class AuthControllerPostTest extends AuthControllerNobodyLoggedTestCase { ['username' => 'foo', 'password' => 'bar'],true); } - - public function loggingWithCorrectInformation() { - $user = Class_Users::getLoader()->findFirstBy(array()); - $this->postDispatch('/opac/auth/boite-login?id_module=4', - array('username' => $user->getLogin(), - 'password' => $user->getPassword())); - } - - public function ajaxLoginWithWrongInformation() { $this->postDispatch('/opac/auth/ajax-login?id_module=4', ['username' => 'foo', @@ -759,15 +750,6 @@ class AuthControllerPostTest extends AuthControllerNobodyLoggedTestCase { } - public function ajaxLoggingWithCorrectInformation() { - $user = Class_Users::getLoader()->findFirstBy(array()); - - $this->postDispatch('/opac/auth/ajax-login?id_module=4', - array('username' => $user->getLogin(), - 'password' => $user->getPassword())); - } - - /** @test */ public function emptyUsernameShouldRedirectToReferer() { $this->loggingWithOutFillingUsername(); @@ -793,19 +775,6 @@ class AuthControllerPostTest extends AuthControllerNobodyLoggedTestCase { $this->assertFlashMessengerContentContains('Identifiant ou mot de passe incorrect'); } - /** @test */ - public function validAuthenticationShouldRedirectToRefererrer() { - $this->loggingWithCorrectInformation(); - $this->assertRedirectTo($this->_referer); - } - - - /** @test */ - public function validAjaxAuthenticationShouldRedirectToReferrer() { - $this->ajaxLoggingWithCorrectInformation(); - $this->assertRedirectTo($this->_referer); - } - /** @test */ public function invalidAjaxAuthenticationShouldRedirectToActionReferrerWithPopupInFlash() { @@ -854,7 +823,9 @@ abstract class AuthControllerPostSimpleTestCase extends AuthControllerNobodyLogg abstract class AuthControllerPostSimpleSuccessfulTestCase extends AuthControllerPostSimpleTestCase { - protected $_web_analytics_client; + protected + $_web_analytics_client, + $_marcel; public function setUp() { parent::setUp(); @@ -863,16 +834,28 @@ abstract class AuthControllerPostSimpleSuccessfulTestCase extends AuthController 'libelle' => 'Multimedia']) ->addRight(Class_UserGroup::RIGHT_ACCES_MUSICME); - $marcel = Class_Users::newInstanceWithId(2, ['nom' => 'Marcel','login' =>'foo']) + $this->_marcel = Storm_Test_ObjectWrapper::on( + Class_Users::newInstanceWithId(2, ['nom' => 'Marcel','login' =>'foo']) ->beAbonneSIGB() - ->setUserGroups([$this->group_musicme]); + ->setUserGroups([$this->group_musicme])); + + Class_Users::cacheInstance($this->_marcel); + + $this->_marcel + ->whenCalled('registerNotificationsOn') + ->willDo(function($notifiable) { + $notifiable->notify('Message de notification'); + }); + $this->_auth ->whenCalled('authenticateLoginPassword') ->with('foo', 'bar') ->willDo( - function() use($marcel) { - $this->_auth->whenCalled('getIdentity')->answers($marcel); + function() { + $user = new stdClass(); + $user->ID_USER = $this->_marcel->getId(); + $this->_auth->whenCalled('getIdentity')->answers($user); return true; }); @@ -910,10 +893,59 @@ class AuthControllerPostSimpleSuccessfulTest extends AuthControllerPostSimpleSuc 'utilisateur', 2])); } + + + /** @test */ + public function flasMessengerShouldContainMessageDeNotification() { + $this->assertFlashMessengerContentContains('Message de notification'); + } +} + + + +class AuthControllerAjaxLoginPostTest extends AuthControllerPostSimpleSuccessfulTestCase { + public function setUp() { + parent::setUp(); + $_SERVER['HTTP_REFERER'] = '/recherche/viewnotice'; + $this->postDispatch('/opac/auth/ajax-login', + ['username' => 'foo', 'password' => 'bar'],true); + } + + + /** @test */ + public function flashMessengerShouldContainMessageDeNotification() { + $this->assertFlashMessengerContentContains('Message de notification'); + } + + /** @test */ + public function responseShouldRedirectToReferrer() { + $this->assertRedirectTo('/recherche/viewnotice'); + } } +class AuthControllerBoiteLoginPostTest extends AuthControllerPostSimpleSuccessfulTestCase { + public function setUp() { + parent::setUp(); + $_SERVER['HTTP_REFERER'] = '/recherche/viewnotice'; + $this->postDispatch('/opac/auth/boite-login', + ['username' => 'foo', 'password' => 'bar'],true); + } + + + /** @test */ + public function flashMessengerShouldContainMessageDeNotification() { + $this->assertFlashMessengerContentContains('Message de notification'); + } + + /** @test */ + public function responseShouldRedirectToReferrer() { + $this->assertRedirectTo('/recherche/viewnotice'); + } +} + + class AuthControllerPostSimpleSuccessfulWithRedirectTest extends AuthControllerPostSimpleSuccessfulTestCase { public function setUp() {