From fb32f198387bdb6ac2d293db4a1be0d66bb8ca02 Mon Sep 17 00:00:00 2001
From: llaffont <llaffont@afi-sa.fr>
Date: Sat, 13 Dec 2014 16:11:31 +0100
Subject: [PATCH] hotline #18457 probleme redirection

when accessing abonne/fiche and not connected, be sure that you are
redirected to this url after successfull connection
---
 library/Class/Url.php                         | 69 +++++++++++++++----
 .../ZendAfi/Controller/Plugin/AdminAuth.php   |  1 +
 library/ZendAfi/View/Helper/AbsoluteUrl.php   | 30 +-------
 library/startup.php                           |  2 +-
 .../controllers/AbonneControllerFicheTest.php | 33 ++++++++-
 5 files changed, 87 insertions(+), 48 deletions(-)

diff --git a/library/Class/Url.php b/library/Class/Url.php
index 0773bc14175..28c91469336 100644
--- a/library/Class/Url.php
+++ b/library/Class/Url.php
@@ -16,24 +16,63 @@
  *
  * 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 
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
  */
 
 class Class_Url {
-    /**
-     * Generates an url given the name of a route.
-     *
-     * @access public
-     *
-     * @param  array $urlOptions Options passed to the assemble method of the Route object.
-     * @param  mixed $name The name of a Route to use. If null it will use the current Route
-     * @param  bool $reset Whether or not to reset the route defaults with those provided
-     * @return string Url for the link href attribute.
-     */
-    public static function assemble(array $urlOptions = array(), $name = null, $reset = true, $encode = true) {
-        $router = Zend_Controller_Front::getInstance()->getRouter();
-        return $router->assemble($urlOptions, $name, $reset, $encode);
-    }
+	protected static $_do_not_add_base_url;
+
+	public static function doNotAddBaseUrl() {
+		static::$_do_not_add_base_url = true;
+	}
+
+
+
+	/**
+	 * Generates an url given the name of a route.
+	 *
+	 * @access public
+	 *
+	 * @param  array $urlOptions Options passed to the assemble method of the Route object.
+	 * @param  mixed $name The name of a Route to use. If null it will use the current Route
+	 * @param  bool $reset Whether or not to reset the route defaults with those provided
+	 * @return string Url for the link href attribute.
+	 */
+	public static function assemble(array $urlOptions = array(), $name = null, $reset = true, $encode = true) {
+		$router = Zend_Controller_Front::getInstance()->getRouter();
+		return $router->assemble($urlOptions, $name, $reset, $encode);
+	}
+
+
+	public static function absolute($url_array_or_string = [], $name = null, $reset = false, $encode = true) {
+		return (new static())->absoluteUrl($url_array_or_string, $name, $reset, $encode);
+	}
+
+
+	public function absoluteUrl($url_array_or_string = [], $name = null, $reset = false, $encode = true) {
+		$url = $this->prepare($url_array_or_string, $name, $reset, $encode);
+		if (preg_match('/http[s]?:\/\//', $url))
+			return $url;
+
+		if ((!static::$_do_not_add_base_url) &&  (0 !== strpos($url, BASE_URL)))
+			$url = BASE_URL . ($url[0] == '/' ? $url : '/'.$url);
+
+		return 'http://' . $_SERVER['SERVER_NAME'] . $url;
+	}
+
+
+	protected function prepare($url_array_or_string, $name, $reset, $encode) {
+		if (is_string($url_array_or_string))
+			return $url_array_or_string;
+
+		$inspector_gadget = Zend_Controller_Front::getInstance()
+			->getPlugin('ZendAfi_Controller_Plugin_InspectorGadget');
+
+		if ($inspector_gadget)
+			$url_array_or_string = $inspector_gadget->addToParams($url_array_or_string);
+
+		return static::assemble($url_array_or_string, $name, $reset, $encode);
+	}
 }
 
 
diff --git a/library/ZendAfi/Controller/Plugin/AdminAuth.php b/library/ZendAfi/Controller/Plugin/AdminAuth.php
index 6acc4ecd10b..19753e8d23c 100644
--- a/library/ZendAfi/Controller/Plugin/AdminAuth.php
+++ b/library/ZendAfi/Controller/Plugin/AdminAuth.php
@@ -64,6 +64,7 @@ class ZendAfi_Controller_Plugin_AdminAuth extends Zend_Controller_Plugin_Abstrac
 			}
 
 			if ((!$user = Class_Users::getIdentity()) && ($controller == "abonne" && $action !== "authenticate")) {
+				$request->setParam('redirect', Class_Url::absolute());
 				$controller = 'auth';
 				$action = 'login';
 			}
diff --git a/library/ZendAfi/View/Helper/AbsoluteUrl.php b/library/ZendAfi/View/Helper/AbsoluteUrl.php
index 25aff3a25bd..c8bbfd302a9 100644
--- a/library/ZendAfi/View/Helper/AbsoluteUrl.php
+++ b/library/ZendAfi/View/Helper/AbsoluteUrl.php
@@ -19,35 +19,7 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
  */
 class ZendAfi_View_Helper_AbsoluteUrl extends Zend_View_Helper_HtmlElement {
-	protected static $_do_not_add_base_url;
-
-	public static function doNotAddBaseUrl() {
-		static::$_do_not_add_base_url = true;
-	}
-
-
 	public function absoluteUrl($url_array_or_string = [], $name = null, $reset = false, $encode = true) {
-		$url = $this->prepare($url_array_or_string, $name, $reset, $encode);
-		if (preg_match('/http[s]?:\/\//', $url))
-			return $url;
-
-		if ((!static::$_do_not_add_base_url) &&  (0 !== strpos($url, BASE_URL)))
-			$url = BASE_URL . ($url[0] == '/' ? $url : '/'.$url);
-
-		return 'http://' . $_SERVER['SERVER_NAME'] . $url;
-	}
-
-
-	protected function prepare($url_array_or_string, $name, $reset, $encode) {
-		if (is_string($url_array_or_string))
-			return $url_array_or_string;
-
-		$inspector_gadget = Zend_Controller_Front::getInstance()
-			->getPlugin('ZendAfi_Controller_Plugin_InspectorGadget');
-
-		if ($inspector_gadget)
-			$url_array_or_string = $inspector_gadget->addToParams($url_array_or_string);
-
-		return $this->view->url($url_array_or_string, $name, $reset, $encode);
+		return Class_Url::absolute($url_array_or_string, $name, $reset, $encode);
 	}
 }
\ No newline at end of file
diff --git a/library/startup.php b/library/startup.php
index d3a736af625..5f51f13eba8 100644
--- a/library/startup.php
+++ b/library/startup.php
@@ -311,7 +311,7 @@ function setupRoutes($front_controller, $cfg) {
 			->setBaseUrl('')
 			->setRouter(new ZendAfi_Controller_Router_RewriteWithoutBaseUrl());
 
-		ZendAfi_View_Helper_AbsoluteUrl::doNotAddBaseUrl();
+		Class_AbsoluteUrl::doNotAddBaseUrl();
 	}
 
 	$front_controller
diff --git a/tests/application/modules/opac/controllers/AbonneControllerFicheTest.php b/tests/application/modules/opac/controllers/AbonneControllerFicheTest.php
index 07f5b370dc2..66c67758236 100644
--- a/tests/application/modules/opac/controllers/AbonneControllerFicheTest.php
+++ b/tests/application/modules/opac/controllers/AbonneControllerFicheTest.php
@@ -16,7 +16,7 @@
  *
  * 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 
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
  */
 
 abstract class AbstractAbonneControllerFicheTest extends AbstractControllerTestCase {
@@ -42,7 +42,7 @@ class AbonneControllerFicheAsAdminTest extends AbstractAbonneControllerFicheTest
 
 	/** @test */
 	public function linkShouldToSuggestionAchatShouldBePresent() {
-		$this->assertXPathContentContains('//a[contains(@href, "/abonne/suggestion-achat")]', 
+		$this->assertXPathContentContains('//a[contains(@href, "/abonne/suggestion-achat")]',
 																			'Suggérer un achat');
 	}
 
@@ -97,7 +97,7 @@ class AbonneControllerFicheAsAbonneTest extends AbstractAbonneControllerFicheTes
 		$this->assertXPathContentContains('//div[@class="abonneTitre"]', 'Marcus');
 	}
 
-	
+
 		/** @test */
 	public function marcusLastNameShouldBePresent() {
 		$this->assertXPathContentContains('//div[@class="abonneTitre"]//span[@data-name="last-name"]', 'Miller');
@@ -105,4 +105,31 @@ class AbonneControllerFicheAsAbonneTest extends AbstractAbonneControllerFicheTes
 }
 
 
+
+class AbonneControllerFicheNobodyLoggedTest extends AbstractAbonneControllerFicheTest {
+	public function setUp() {
+		parent::setUp();
+		ZendAfi_Auth::getInstance()->clearIdentity();
+		$this->dispatch('/abonne/fiche', true);
+	}
+
+
+	/** @test */
+	public function controllerShouldBeAuth() {
+		$this->assertController('auth');
+	}
+
+	/** @test */
+	public function formLoginShouldBePresent() {
+		$this->assertXPath('//form[@action="/auth/login"]');
+	}
+
+
+	/** @test */
+	public function hiddenInputRedirectShouldContainsAbonneFiche() {
+		$this->assertXPath('//input[@name="redirect"][@value="http://localhost' . BASE_URL . '/abonne/fiche"]');
+	}
+}
+
+
 ?>
\ No newline at end of file
-- 
GitLab