From e6763f83f27297e86d9c011f4780a27773912982 Mon Sep 17 00:00:00 2001
From: llaffont <llaffont@git-test.afi-sa.fr>
Date: Wed, 22 Aug 2012 13:15:12 +0000
Subject: [PATCH] Suppression duplication authentification admin / opac

---
 .../admin/controllers/AuthController.php      | 53 +++++--------------
 .../opac/controllers/AuthController.php       | 11 +---
 library/ZendAfi/Auth.php                      | 24 ++++++---
 .../ZendAfi/Controller/Plugin/AdminAuth.php   |  2 +-
 .../library/Class/Systeme/ModulesMenuTest.php |  4 +-
 .../View/Helper/ViewHelperTestCase.php        |  4 +-
 6 files changed, 38 insertions(+), 60 deletions(-)

diff --git a/application/modules/admin/controllers/AuthController.php b/application/modules/admin/controllers/AuthController.php
index 56b9ead0c13..e66cb4271f2 100644
--- a/application/modules/admin/controllers/AuthController.php
+++ b/application/modules/admin/controllers/AuthController.php
@@ -45,49 +45,24 @@ class Admin_AuthController extends Zend_Controller_Action
 	//----------------------------------------------------------------------------------
 	// Formulaire d'identification
 	//----------------------------------------------------------------------------------
-	function loginAction()
-	{
+	function loginAction() {
 		$this->view->message = '';
-		if ($this->_request->isPost())
-		{
-			// Champs de saisie
-			$f = new Zend_Filter_StripTags();
-			$username = $f->filter($this->_request->getPost('username'));
-			$password = $f->filter($this->_request->getPost('password'));
+		if (!$this->_request->isPost())
+			return;
 
-			if (empty($username)) $this->view->message = "Entrez votre nom d'utilisateur puis validez S.V.P.";
-			else
-			{
-				// setup Zend_/Auth adapter for a database table
-				$authAdapter = new Zend_Auth_Adapter_DbTable(Zend_Db_Table::getDefaultAdapter());
-				$authAdapter->setTableName('bib_admin_users');
-				$authAdapter->setIdentityColumn('LOGIN');
-				$authAdapter->setCredentialColumn('PASSWORD');
-				// Set the input credential values to authenticate against
-				$authAdapter->setIdentity($username);
-				$authAdapter->setCredential($password);
+		// Champs de saisie
+		$f = new Zend_Filter_StripTags();
+		$username = $f->filter($this->_request->getPost('username'));
+		$password = $f->filter($this->_request->getPost('password'));
 
-				// do the authentication
-				$auth = ZendAfi_Auth::getInstance();
-				try
-				{
-					$result = $auth->authenticate($authAdapter);
-					
-					// success: store database row to auth's storage (sauf le password)
-					if ($result->isValid())
-					{
-						$data = $authAdapter->getResultRowObject(null,'password');
-						$auth->getStorage()->write($data);
-						if($auth->hasIdentity()) $this->_redirect('admin/');
-					}
-				}
-				catch (Exception $e)
-				{
-					logErrorMessage('Class: Admin_AuthController; Function: loginAction' . NL . $e->getMessage());
-					$this->_redirect('admin/error/database');
-				}
-			}
+		if (empty($username))  {
+			$this->view->message = "Entrez votre nom d'utilisateur puis validez S.V.P.";
+			return;
 		}
+
+		$auth = ZendAfi_Auth::getInstance();
+		if ($auth->authenticateLoginPassword($username, $password, [$auth->newAuthDb()]))
+			$this->_redirect('admin/');
 	}
 
 	//----------------------------------------------------------------------------------
diff --git a/application/modules/opac/controllers/AuthController.php b/application/modules/opac/controllers/AuthController.php
index ab3ac517a81..8048cd4a144 100644
--- a/application/modules/opac/controllers/AuthController.php
+++ b/application/modules/opac/controllers/AuthController.php
@@ -51,15 +51,8 @@ class AuthController extends Zend_Controller_Action
 			return $this->view->_('Entrez votre mot de passe S.V.P.');
 
 		// do the authentication
-		$auth = ZendAfi_Auth::getInstance();
-
-		foreach ($auth->getOrderedAdaptersForLoginPassword($username, $password) as $authAdapter) {
-			if (!$auth->authenticate($authAdapter)->isValid()) continue;
-			$auth->getStorage()->write($authAdapter->getResultObject());
-			return null;
-		}
-
-		return $this->view->_('Identifiant ou mot de passe incorrect.');
+		if (!ZendAfi_Auth::getInstance()->authenticateLoginPassword($username, $password))
+			return $this->view->_('Identifiant ou mot de passe incorrect.');
 	}
 
 //------------------------------------------------------------------------------------------------------
diff --git a/library/ZendAfi/Auth.php b/library/ZendAfi/Auth.php
index 29c3ef1dbf0..f58a6383740 100644
--- a/library/ZendAfi/Auth.php
+++ b/library/ZendAfi/Auth.php
@@ -35,13 +35,7 @@ class ZendAfi_Auth extends Zend_Auth {
 
 
 	public function getOrderedAdaptersForLoginPassword($login, $password) {
-		$adapters = [ $this->newAuthDb(), 
-									$this->newAuthSIGB() ];
-		foreach ($adapters as $adapter) {
-			$adapter->setIdentity($login);
-			$adapter->setCredential($password);
-		}
-		return $adapters;
+		return  [ $this->newAuthDb(), $this->newAuthSIGB() ];
 	}
 
 	
@@ -57,6 +51,22 @@ class ZendAfi_Auth extends Zend_Auth {
 	public function newAuthSIGB() {
 		return new ZendAfi_Auth_Adapter_CommSigb();
 	}
+
+
+	public function authenticateLoginPassword($login, $password, $adapters = null) {
+		if (!$adapters)
+			$adapters = $this->getOrderedAdaptersForLoginPassword($login, $password);
+
+		foreach ($adapters as $authAdapter) {
+			$authAdapter->setIdentity($login);
+			$authAdapter->setCredential($password);
+	
+			if (!$this->authenticate($authAdapter)->isValid()) continue;
+			$this->getStorage()->write($authAdapter->getResultObject());
+			return true;
+		}
+		return false;
+	}
 }
 
 ?>
\ No newline at end of file
diff --git a/library/ZendAfi/Controller/Plugin/AdminAuth.php b/library/ZendAfi/Controller/Plugin/AdminAuth.php
index b5bdccd45fd..ffcfdd45103 100644
--- a/library/ZendAfi/Controller/Plugin/AdminAuth.php
+++ b/library/ZendAfi/Controller/Plugin/AdminAuth.php
@@ -33,7 +33,7 @@ class ZendAfi_Controller_Plugin_AdminAuth extends Zend_Controller_Plugin_Abstrac
 		$action = $this->_request->getActionName();
 		$session = Zend_Registry::get('session');
 
-		$auth = Zend_Auth::getInstance();
+		$auth = ZendAfi_Auth::getInstance();
 		
 		if (isset($session->baseUrl))
 		{
diff --git a/tests/library/Class/Systeme/ModulesMenuTest.php b/tests/library/Class/Systeme/ModulesMenuTest.php
index f33929b2e18..6f4b19c07d5 100644
--- a/tests/library/Class/Systeme/ModulesMenuTest.php
+++ b/tests/library/Class/Systeme/ModulesMenuTest.php
@@ -50,7 +50,7 @@ class ModulesMenuTest extends Storm_Test_ModelTestCase {
 
 	/** @test */
 	public function vodeclicUrlWithoutUserShouldBeLoginPage() {
-		Zend_Auth::getInstance()->clearIdentity();
+		ZendAfi_Auth::getInstance()->clearIdentity();
 		$this->assertEquals(array('url' => BASE_URL.'/auth/login', 'target' => '0'), 
 												$this->module_menu->getUrl('VODECLIC', array()));
 	}
@@ -62,7 +62,7 @@ class ModulesMenuTest extends Storm_Test_ModelTestCase {
 		$account->password     = 'password';
 		$account->ID_USER      = 34;
 		
-		Zend_Auth::getInstance()->getStorage()->write($account);
+		ZendAfi_Auth::getInstance()->getStorage()->write($account);
 
 		return Class_Users::getLoader()
 			->newInstanceWithId(34)
diff --git a/tests/library/ZendAfi/View/Helper/ViewHelperTestCase.php b/tests/library/ZendAfi/View/Helper/ViewHelperTestCase.php
index beb7d07ee67..c951925a326 100644
--- a/tests/library/ZendAfi/View/Helper/ViewHelperTestCase.php
+++ b/tests/library/ZendAfi/View/Helper/ViewHelperTestCase.php
@@ -123,7 +123,7 @@ abstract class ViewHelperTestCase extends PHPUnit_Framework_TestCase {
 
 
 	public function logout() {
-		Zend_Auth::getInstance()->clearIdentity();
+		ZendAfi_Auth::getInstance()->clearIdentity();
 	}
 
 	public function login($role) {
@@ -144,7 +144,7 @@ abstract class ViewHelperTestCase extends PHPUnit_Framework_TestCase {
 			->newInstanceWithId(1)
 			->setLibelle('Tombouctou');
 
-		Zend_Auth::getInstance()->getStorage()->write($account);
+		ZendAfi_Auth::getInstance()->getStorage()->write($account);
 	}
 }
 ?>
\ No newline at end of file
-- 
GitLab