From 292ca4c989c25389cb5437cc4abcd5275570a825 Mon Sep 17 00:00:00 2001
From: pbarroca <pbarroca@afi-sa.fr>
Date: Fri, 2 Jun 2017 11:41:37 +0200
Subject: [PATCH] dev #40971 fix RT

---
 VERSIONS_WIP/40971                            |  2 +-
 .../opac/controllers/AuthController.php       | 83 ++++++++++++-------
 .../PatronPasswordNotSecureException.php      | 36 --------
 .../Class/WebService/SIGB/Nanook/Service.php  |  7 +-
 4 files changed, 61 insertions(+), 67 deletions(-)
 delete mode 100644 library/Class/WebService/SIGB/Nanook/PatronPasswordNotSecureException.php

diff --git a/VERSIONS_WIP/40971 b/VERSIONS_WIP/40971
index f399597798d..6bac9e99215 100644
--- a/VERSIONS_WIP/40971
+++ b/VERSIONS_WIP/40971
@@ -1 +1 @@
- - ticket #40971 : Pouvoir prendre l'email comme identifiant alternatif [CCTP Agglo2b]
\ No newline at end of file
+ - ticket #40971 : Nanook : Pouvoir prendre l'email comme identifiant alternatif de l'abonné
\ No newline at end of file
diff --git a/application/modules/opac/controllers/AuthController.php b/application/modules/opac/controllers/AuthController.php
index 5a1d080547e..9e8f537d0a6 100644
--- a/application/modules/opac/controllers/AuthController.php
+++ b/application/modules/opac/controllers/AuthController.php
@@ -269,7 +269,7 @@ class AuthController extends ZendAfi_Controller_Action {
                                                  $user->registerNotificationsOn($this->getHelper('notify')->bePopup());
                                                });
 
-      $strategy->processLogin();
+    $strategy->processLogin();
   }
 
 
@@ -591,7 +591,6 @@ abstract class Auth_Strategy_Abstract {
     if ($controller->isLectura())
       return new Auth_Strategy_Lectura($controller);
 
-
     if (static::isLogged())
       return new Auth_Strategy_Logged($controller);
 
@@ -603,55 +602,71 @@ abstract class Auth_Strategy_Abstract {
     return Class_Users::getIdentity();
   }
 
+
   public function disableRedirect() {
     $this->disable_redirect = true;
     return $this;
   }
 
+
   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()) {
-      try {
-        $this->handlePost();
-      } catch (Class_WebService_SIGB_Nanook_PatronPasswordNotSecureException $e) {
-        return $this->controller->onPasswordNotSecure($e->getMessage(),
-                                                      $e->getPattern());
-      }
-    }
 
+    if (!$this->getRequest()->isPost())
+      return $this->_handleRedirect();
+
+    $callback = $this->_handlePost();
 
+    $password_not_secure = Class_WebService_SIGB_Nanook_PatronPasswordNotSecure::getInstance();
+
+    if ($message = $password_not_secure->getMessage())
+      return $this->controller->onPasswordNotSecure($message,
+                                                    $password_not_secure->getPattern());
+
+    $callback();
+    return $this->_handleRedirect();
+  }
+
+
+  protected function _handleRedirect() {
     if ($this->shouldRedirect())
       $this->controller->redirect($this->redirect_url);
+
+    return $this;
   }
 
 
   public function setDefaultUrl($url) {
-    $this->default_url=$url;
+    $this->default_url = $url;
   }
 
 
   public function prepareLogin() {
-
   }
 
-  /** @codeCoverageIgnore */
-  public function handlePost() {
 
+  /** @codeCoverageIgnore */
+  protected function _handlePost() {
+    return function() {};
   }
 
+
   public function shouldRedirect() {
     return ($this->getRedirectUrl()!='');
   }
 
+
   public function getRedirectUrl() {
     if ($this->disable_redirect)
       return '';
@@ -689,15 +704,16 @@ abstract class Auth_Strategy_Abstract {
 
 
 class Auth_Strategy_NotLogged extends Auth_Strategy_Abstract{
-  public function handlePost() {
-    $this->redirect_url=$this->default_url;
+  protected function _handlePost() {
+    $this->redirect_url = $this->default_url;
+
+    if (!$error = $this->controller->_authenticate())
+      return function() { return $this->_doOnLoginSuccess(); };
 
-    if ($error = $this->controller->_authenticate()) {
+    return function() use($error) {
       $this->controller->notify($error);
       return $this->_doOnLoginFail();
-    }
-
-    $this->_doOnLoginSuccess();
+    };
   }
 }
 
@@ -759,10 +775,12 @@ class Auth_Strategy_Cas_Logged extends Auth_Strategy_Cas_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();
+  protected function _handlePost() {
+    if ($error = $this->controller->_authenticate())
+      return function() use($error) { return $this->controller->notify($error); };
+
+    $this->redirect_url = $this->urlServiceCas();
+    return function() {};
   }
 }
 
@@ -770,16 +788,20 @@ class Auth_Strategy_Cas_NotLogged extends Auth_Strategy_Cas_Abstract{
 
 
 class Auth_Strategy_Lectura extends Auth_Strategy_Abstract {
-  public function handlePost() {
+  protected function _handlePost() {
     $this->controller->getHelper('ViewRenderer')->setNoRender();
-    $response= $this->controller->getResponse();
+
+    $response = $this->controller->getResponse();
     $view = $this->controller->view;
     $request = $this->controller->getRequest();
+
     $response->setHeader('Content-Type', 'application/xml;charset=utf-8');
     $login = $request->getPost('CAB');
     $password = $request->getPost('PWD');
     $response->setBody($this->getXmlResponse($view,
                                              ZendAfi_Auth::getInstance()->authenticateLoginPassword($login, $password)));
+
+    return function() {};
   }
 
 
@@ -795,10 +817,11 @@ class Auth_Strategy_Lectura extends Auth_Strategy_Abstract {
 
 
 class Auth_Strategy_OAuth extends Auth_Strategy_NotLogged {
-  public function handlePost() {
-    parent::handlePost();
+  protected function _handlePost() {
+    parent::_handlePost();
+
     if (!$user = Class_Users::getIdentity())
-      return $this;
+      return function() {};
 
     $request = $this->controller->getRequest();
 
@@ -807,5 +830,7 @@ class Auth_Strategy_OAuth extends Auth_Strategy_NotLogged {
     $this->redirect_url = sprintf('%s#token=%s',
                                   $request->getParam('redirect_uri'),
                                   $token->getToken());
+
+    return function() {};
   }
 }
\ No newline at end of file
diff --git a/library/Class/WebService/SIGB/Nanook/PatronPasswordNotSecureException.php b/library/Class/WebService/SIGB/Nanook/PatronPasswordNotSecureException.php
deleted file mode 100644
index cbce66340ce..00000000000
--- a/library/Class/WebService/SIGB/Nanook/PatronPasswordNotSecureException.php
+++ /dev/null
@@ -1,36 +0,0 @@
-<?php
-/**
- * Copyright (c) 2012-2017, Agence Française Informatique (AFI). All rights reserved.
- *
- * BOKEH is free software; you can redistribute it and/or modify
- * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
- * the Free Software Foundation.
- *
- * There are special exceptions to the terms and conditions of the AGPL as it
- * is applied to this software (see README file).
- *
- * BOKEH is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
- *
- * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
- * along with BOKEH; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
- */
-
-
-class Class_WebService_SIGB_Nanook_PatronPasswordNotSecureException extends Exception {
-  protected $_pattern;
-
-  public function __construct($message, $pattern) {
-    parent::__construct($message);
-    $this->_pattern = $pattern;
-  }
-
-
-  public function getPattern() {
-    return $this->_pattern;
-  }
-}
-?>
\ No newline at end of file
diff --git a/library/Class/WebService/SIGB/Nanook/Service.php b/library/Class/WebService/SIGB/Nanook/Service.php
index a0cd71f0549..7c38e2365de 100644
--- a/library/Class/WebService/SIGB/Nanook/Service.php
+++ b/library/Class/WebService/SIGB/Nanook/Service.php
@@ -75,7 +75,12 @@ class Class_Webservice_SIGB_Nanook_Service extends Class_WebService_SIGB_Abstrac
     if ('PatronPasswordNotSecure' == $this->_getTagData($xml, 'error')) {
       $message = $this->_getTagData($xml, 'securePasswordLabel');
       $pattern = '/' . $this->_getTagData($xml, 'securePasswordPattern') . '/';
-      throw new Class_WebService_SIGB_Nanook_PatronPasswordNotSecureException($message, $pattern);
+
+      Class_WebService_SIGB_Nanook_PatronPasswordNotSecure::getInstance()
+        ->setMessage($message)
+        ->setPattern($pattern);
+
+      return false;
     }
 
     if ('' != $patronId = $this->_getTagData($xml, 'patronId')) {
-- 
GitLab