From c93a94143dfdc4d1c88af53e1b65989d1cd5fd62 Mon Sep 17 00:00:00 2001
From: pbarroca <pbarroca@git-test.afi-sa.fr>
Date: Wed, 4 Jul 2012 09:51:37 +0000
Subject: [PATCH] =?UTF-8?q?Multim=C3=A9dia:=20l'authentification=20renvoit?=
 =?UTF-8?q?=20les=20nouveaux=20groupes=20d'utilisateurs=20+=20n=C3=A9cessi?=
 =?UTF-8?q?te=20de=20sp=C3=A9cifier=20un=20poste?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../opac/controllers/AbonneController.php     | 119 ++++++++----------
 library/Class/Users.php                       |  11 ++
 .../AbonneControllerMultimediaTest.php        |  69 +++++++---
 3 files changed, 114 insertions(+), 85 deletions(-)

diff --git a/application/modules/opac/controllers/AbonneController.php b/application/modules/opac/controllers/AbonneController.php
index 6a423cc2bea..b6f83840635 100644
--- a/application/modules/opac/controllers/AbonneController.php
+++ b/application/modules/opac/controllers/AbonneController.php
@@ -18,18 +18,11 @@
  * along with AFI-OPAC 2.0; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA 
  */
-///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-//  OPAC3: ABONNE
-///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-
 class AbonneController extends Zend_Controller_Action
 {
 	protected $_user = null;								// Le user connecté
 
-//------------------------------------------------------------------------------------------------------
-// Initialisation controller
-//------------------------------------------------------------------------------------------------------
-	function init()	{
+	public function init()	{
 		if ("authenticate" == $this->getRequest()->getActionName())
 				return;
 		
@@ -104,26 +97,19 @@ class AbonneController extends Zend_Controller_Action
 		$this->view->session = $session;
 	}
 
-//------------------------------------------------------------------------------------------------------
-// Voir ses avis
-//------------------------------------------------------------------------------------------------------
+
 	public function viewavisAction(){
 		$this->_redirect('blog/viewauteur/id/'.$this->_user->getId());
 	}
 
-//------------------------------------------------------------------------------------------------------
-// Donner son avis
-//------------------------------------------------------------------------------------------------------
 
-	private function handleAvis($readSourceMethod, $writeAvisMethod)
-	{
+	private function handleAvis($readSourceMethod, $writeAvisMethod) {
 		$cls_user= new Class_Users();
 
 		$avis = new Class_Avis();
 
 		// Validation du formulaire
-		if ($this->_request->isPost())
-		{
+		if ($this->_request->isPost()) {
 			// Bornage du texte
 			$longueur_min = Class_AdminVar::get("AVIS_MIN_SAISIE");
 			$longueur_max = Class_AdminVar::get("AVIS_MAX_SAISIE");
@@ -185,16 +171,16 @@ class AbonneController extends Zend_Controller_Action
 
 	protected function _renderRefreshOnglet() {
 		$this->getResponse()->setHeader('Content-Type', 'text/html;charset=utf-8');
+		$js = 'location.reload()';
 		if (array_key_exists('onglets', $_SESSION))
-			$this->getResponse()->setBody("<script>window.top.hidePopWin(false);window.top.refreshOnglet('".$_SESSION["onglets"]["avis"]."');</script>");
-		else
-			$this->getResponse()->setBody("<script>window.top.hidePopWin(false); window.top.location.reload();</script>");
+			$js = "refreshOnglet('" . $_SESSION["onglets"]["avis"] . "')";
+		$this->getResponse()->setBody("<script>window.top.hidePopWin(false);window.top." . $js. ";</script>");
 		$viewRenderer = $this->getHelper('ViewRenderer');
 		$viewRenderer->setNoRender();
 	}
 
 
-	function avisAction()	{
+	public function avisAction()	{
 		$id_notice = $this->_request->getParam('id_notice', 0);
 		$this
 			->getHelper('ViewRenderer')
@@ -238,24 +224,13 @@ class AbonneController extends Zend_Controller_Action
 	}
 
 
-
-//------------------------------------------------------------------------------------------------------
-// AVIS CMS
-//------------------------------------------------------------------------------------------------------
-	function cmsavisAction()	{
+	public function cmsavisAction()	{
 		$this->handleAvis('getCmsAvisById', 'ecrireCmsAvis');
 	}
 
 
-
-//------------------------------------------------------------------------------------------------------
-// Proposer des tags
-//------------------------------------------------------------------------------------------------------
-	function tagnoticeAction()
-	{
-
-		if ($this->_request->isPost())
-		{
+	public function tagnoticeAction() {
+		if ($this->_request->isPost()) {
 			$filter = new Zend_Filter_StripTags();
 			$abonneTag1 = trim($filter->filter($this->_request->getPost('abonneTag1')));
 			$abonneTag2 = trim($filter->filter($this->_request->getPost('abonneTag2')));
@@ -292,10 +267,8 @@ class AbonneController extends Zend_Controller_Action
 		}
 	}
 
-//------------------------------------------------------------------------------------------------------
-// Fiche abonné
-//------------------------------------------------------------------------------------------------------
-	function ficheAction() {
+
+	public function ficheAction() {
 		$abonnement = '';
 		$nb_prets = '';
 		$nb_resas = '';
@@ -366,16 +339,14 @@ class AbonneController extends Zend_Controller_Action
 		$this->view->error = $error;
 	}
 
-//------------------------------------------------------------------------------------------------------
-// Liste des prets en cours
-//------------------------------------------------------------------------------------------------------
-	function pretsAction()	{
+
+	public function pretsAction()	{
 		$this->view->fiche = $this->_user->getFicheSigb();
 		$this->view->user = $this->_user;
 	}
 
 
-	function prolongerpretAction() {
+	public function prolongerpretAction() {
 		$id_pret = $this->_request->getParam('id_pret');
 		$cls_comm = new Class_CommSigb();
 
@@ -392,10 +363,8 @@ class AbonneController extends Zend_Controller_Action
 		$this->renderScript('abonne/prets.phtml');
 	}
 
-//------------------------------------------------------------------------------------------------------
-// Liste des reservations en cours
-//------------------------------------------------------------------------------------------------------
-	function reservationsAction()	{
+
+	public function reservationsAction()	{
 		// Mode Suppression
 		if (null !== ($delete = $this->_getParam('id_delete'))) {
 			$cls_comm = new Class_CommSigb();
@@ -486,7 +455,7 @@ class AbonneController extends Zend_Controller_Action
 	}
 
 
-	function editAction() {
+	public function editAction() {
 		$form = $this->_userForm($this->_user);
 
 		if ($this->getRequest()->isPost() && $form->isValid($_POST)) {
@@ -522,29 +491,47 @@ class AbonneController extends Zend_Controller_Action
 		$this->view->help = nl2br(Class_AdminVar::get('AIDE_FICHE_ABONNE'));
 	}
 	
-	public function authenticateAction(){
+
+	public function authenticateAction() {
 		$this->getHelper('ViewRenderer')->setNoRender();
-		$response = new StdClass();
 		
-		$login = $this->_getParam('login');
-		$password = $this->_getParam('password');
-		
-		$user = Class_Users::getLoader()->findFirstBy(array('login' => $login));
+		$response = new StdClass();
+		$response->auth = 0;
+		$response->until = '';
+
+		if (!($login = $this->_getParam('login'))
+				|| !($password = $this->_getParam('password'))
+				|| !($poste = $this->_getParam('poste'))) {
+			$response->error = 'MissingParameter';
+			$this->_response->setBody(json_encode($response));
+			return;
+		}
 		
-		if(!$user )
+		if (!$user = Class_Users::getLoader()->findFirstBy(array('login' => $login))) {
 			$response->error = 'UserNotFound';
-		else if (($user->getPassword() !== $password)) 
+			$this->_response->setBody(json_encode($response));
+			return;
+		}
+
+		if (($user->getPassword() !== $password)) {
 			$response->error = 'PasswordIsWrong';
-		else if (!$user->isAbonnementValid()) 
+			$this->_response->setBody(json_encode($response));
+			return;
+	  }
+
+		if (!$user->isAbonnementValid()) {
 			$response->error='SubscriptionExpired';
-		else {
-			foreach(array('id', 'login', 'password', 'nom', 'prenom') as $attribute) {
-				$response->$attribute = $user->$attribute;
-			}
-			$response->groupes=$user->getGroupes();
-			$response->date_naissance=$user->getDateNaissanceIso8601();
+			$this->_response->setBody(json_encode($response));
+			return;
+    }
+		
+		foreach(array('id', 'login', 'password', 'nom', 'prenom') as $attribute) {
+			$response->$attribute = $user->$attribute;
 		}
-				
+
+		$response->groupes = $user->getUserGroupsLabels();
+		$response->date_naissance = $user->getDateNaissanceIso8601();
+
 		$this->_response->setBody(json_encode($response));
 	}
 }
\ No newline at end of file
diff --git a/library/Class/Users.php b/library/Class/Users.php
index b2d4ffd6e73..b8eb836bba7 100644
--- a/library/Class/Users.php
+++ b/library/Class/Users.php
@@ -330,6 +330,17 @@ class Class_Users extends Storm_Model_Abstract {
 		return $groupes;
 	}
 
+
+	/**
+	 * @return array
+	 */
+	public function getUserGroupsLabels() {
+		$labels = array();
+		foreach ($this->getUserGroups() as $group)
+			$labels[] = $group->getLibelle();
+		return $labels;
+	}
+
 	
 	/**
 	 * @return string
diff --git a/tests/application/modules/opac/controllers/AbonneControllerMultimediaTest.php b/tests/application/modules/opac/controllers/AbonneControllerMultimediaTest.php
index b58c32f8698..fa43d9569f8 100644
--- a/tests/application/modules/opac/controllers/AbonneControllerMultimediaTest.php
+++ b/tests/application/modules/opac/controllers/AbonneControllerMultimediaTest.php
@@ -21,9 +21,7 @@
 
 require_once 'AbstractControllerTestCase.php';
 
-
 class AbonneControllerMultimediaTest extends AbstractControllerTestCase{
-	
 	public function setUp() {
 		parent::setUp();
 		Zend_Auth::getInstance()->clearIdentity();
@@ -71,23 +69,51 @@ class AbonneControllerMultimediaTest extends AbstractControllerTestCase{
 						
 						->whenCalled('findFirstBy')
 						->answers(null);
-	}
-	
-	
-	protected function getJson($url) {
-		$this->dispatch($url);
-		return json_decode($this->_response->getBody());
+
+		Storm_Test_ObjectWrapper::onLoaderOfModel('Class_UserGroupMembership')
+				->whenCalled('findAllBy')
+				->with(array('role' => 'user', 'model' => $laurent))
+				->answers(array(Class_UserGroupMembership::getLoader()
+						->newInstance()
+						->setUserGroup(Class_UserGroup::getLoader()
+							->newInstanceWithId(1)
+							->setLibelle('Devs agiles'))))
+
+				->whenCalled('findAllBy')
+				->with(array('role' => 'user', 'model' => $baptiste))
+				->answers(array(Class_UserGroupMembership::getLoader()
+						->newInstance()
+						->setUserGroup(Class_UserGroup::getLoader()
+							->newInstanceWithId(2)
+							->setLibelle('Devs Oldschool'))))
+
+				->whenCalled('findAllBy')
+				->with(array('role' => 'user', 'model' => $arnaud))
+				->answers(array(Class_UserGroupMembership::getLoader()
+						->newInstance()
+						->setUserGroup(Class_UserGroup::getLoader()
+							->newInstanceWithId(3)
+							->setLibelle('Invité'))));
 	}
 
+
 	/** @test */
 	public function responseShouldNotBeARedirect() {
 		$json = $this->getJson('/abonne/authenticate/login/laurent/password/afi');
 		$this->assertNotRedirect();
 	}
-	
+
+
+	/** @test */
+	public function withoutPosteShouldReturnErrorMissingParameter() {
+		$json = $this->getJson('/abonne/authenticate/login/laurent/password');
+		$this->assertEquals('MissingParameter', $json->error);
+	}
+
+
 	/** @test */
 	public function getAbonneZorkShouldReturnErrorUserNotFound() {
-		$json= $this->getJson('/abonne/authenticate/login/zork/password/toto');
+		$json= $this->getJson('/abonne/authenticate/login/zork/password/toto/poste/1');
 		$this->assertEquals("UserNotFound", $json->error);
 		
 	}
@@ -95,14 +121,14 @@ class AbonneControllerMultimediaTest extends AbstractControllerTestCase{
 
 	/** @test */
 	public function authenticateAbonneLaurentPasswordXXXShouldReturnWrongPassword() {
-		$json=$this->getJson('/abonne/authenticate/login/laurent/password/xxx');
+		$json=$this->getJson('/abonne/authenticate/login/laurent/password/xxx/poste/1');
 		$this->assertEquals("PasswordIsWrong",$json->error);	
 	}
 
 	
 	/** @test */
 	public function rightAuthenticationShouldNotReturnError() {
-		$json = $this->getJson('/abonne/authenticate/login/laurent/password/afi');
+		$json = $this->getJson('/abonne/authenticate/login/laurent/password/afi/poste/1');
 		$this->assertFalse(property_exists($json,'error'));
 		return $json;
 	}
@@ -166,7 +192,7 @@ class AbonneControllerMultimediaTest extends AbstractControllerTestCase{
 	 * @depends rightAuthenticationShouldNotReturnError
 	 */
 	public function laurentGroupeShoudBeAdulteAndAbonne($json) {
-		$this->assertEquals(array('adulte','abonne','admin_bib'),$json->groupes);
+		$this->assertEquals(array('Devs agiles'), $json->groupes);
 	}
 	
 	
@@ -174,8 +200,8 @@ class AbonneControllerMultimediaTest extends AbstractControllerTestCase{
 	 * @test 
 	 */
 	public function baptisteGroupesShouldBeMineur(){
-		$json=$this->getJson('/abonne/authenticate/login/baptiste/password/afi');
-		$this->assertEquals(array('mineur','abonne_sigb'),$json->groupes);	
+		$json = $this->getJson('/abonne/authenticate/login/baptiste/password/afi/poste/1');
+		$this->assertEquals(array('Devs Oldschool'), $json->groupes);	
 	}
 	
 	
@@ -183,7 +209,7 @@ class AbonneControllerMultimediaTest extends AbstractControllerTestCase{
 	 * @test 
 	 */
 		public function mireilleAuthenticateShouldReturnSubscriptionExpired(){
-		$json=$this->getJson('/abonne/authenticate/login/mireille/password/afi');
+		$json=$this->getJson('/abonne/authenticate/login/mireille/password/afi/poste/1');
 		$this->assertEquals('SubscriptionExpired',$json->error);	
 	}
 	
@@ -192,10 +218,15 @@ class AbonneControllerMultimediaTest extends AbstractControllerTestCase{
 	 * @test 
 	 */
 	public function arnaudGroupesShouldBeInvite(){
-		$json=$this->getJson('/abonne/authenticate/login/arnaud/password/lelache');
-		$this->assertEquals(array('invite'),$json->groupes);	
+		$json=$this->getJson('/abonne/authenticate/login/arnaud/password/lelache/poste/1');
+		$this->assertEquals(array('Invité'), $json->groupes);	
+	}
+
+
+	protected function getJson($url) {
+		$this->dispatch($url);
+		return json_decode($this->_response->getBody());
 	}
-	
 }
 
 ?>
-- 
GitLab