From b372288902b6e4989c8bf92fcf7ecd649d37e520 Mon Sep 17 00:00:00 2001
From: Laurent Laffont <llaffont@afi-sa.fr>
Date: Tue, 14 Nov 2017 11:30:20 +0100
Subject: [PATCH] dev #65756 when need authentication to access an admin URL,
 redirect to this URL after successful login

---
 application/modules/admin/controllers/AuthController.php   | 2 +-
 application/modules/admin/views/scripts/auth/login.phtml   | 5 ++++-
 library/ZendAfi/Controller/Plugin/AdminAuth.php            | 3 ++-
 .../modules/admin/controllers/AdminAuthControllerTest.php  | 7 +++++++
 4 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/application/modules/admin/controllers/AuthController.php b/application/modules/admin/controllers/AuthController.php
index fdb246217ab..67b695b2317 100644
--- a/application/modules/admin/controllers/AuthController.php
+++ b/application/modules/admin/controllers/AuthController.php
@@ -53,7 +53,7 @@ class Admin_AuthController extends Zend_Controller_Action {
     if (!$auth->authenticateLoginPassword($username, $password, [$auth->newAuthDb()]))
       return;
 
-    $this->_redirect($this->_getParam('redirect', 'admin/'));
+    $this->_redirect($this->_request->getPost('redirect', 'admin/'));
   }
 
 
diff --git a/application/modules/admin/views/scripts/auth/login.phtml b/application/modules/admin/views/scripts/auth/login.phtml
index 2afe6b402ac..f0ea7f961c7 100644
--- a/application/modules/admin/views/scripts/auth/login.phtml
+++ b/application/modules/admin/views/scripts/auth/login.phtml
@@ -19,7 +19,10 @@
           <td width="50%" height="55" align="left" valign="middle">&nbsp;&nbsp;<input type="password" name="password" onkeypress="if (event.keyCode == 13) {javascript:PicToolbarOver( getElementById('menu_item975'), 'menu_item975');this.form.submit();return false;}" value=""/></td>
         </tr>
         <tr class="dark">
-          <td colspan="2" width="50%" height="66" align="center"><?php echo $this->Button_Submit(); ?></td>
+          <td colspan="2" width="50%" height="66" align="center">
+            <input type="hidden" name="redirect" value="<?php echo $this->redirect ?>"/>
+            <?php echo $this->Button_Submit(); ?>
+          </td>
         </tr>
       </table>
     </div>
diff --git a/library/ZendAfi/Controller/Plugin/AdminAuth.php b/library/ZendAfi/Controller/Plugin/AdminAuth.php
index 4ffaea41cc9..221e20fad2e 100644
--- a/library/ZendAfi/Controller/Plugin/AdminAuth.php
+++ b/library/ZendAfi/Controller/Plugin/AdminAuth.php
@@ -72,7 +72,8 @@ class ZendAfi_Controller_Plugin_AdminAuth extends Zend_Controller_Plugin_Abstrac
 
     if (!$user = Class_Users::getIdentity()) {
       $request->setControllerName('auth')
-              ->setActionName('login');
+              ->setActionName('login')
+              ->setParam('redirect', $request->getRequestUri());
       return;
     }
 
diff --git a/tests/application/modules/admin/controllers/AdminAuthControllerTest.php b/tests/application/modules/admin/controllers/AdminAuthControllerTest.php
index f582cbfc609..07b0f13a869 100644
--- a/tests/application/modules/admin/controllers/AdminAuthControllerTest.php
+++ b/tests/application/modules/admin/controllers/AdminAuthControllerTest.php
@@ -58,6 +58,13 @@ class AdminAuthControllerNobodyLoggedTest extends Admin_AbstractControllerTestCa
   }
 
 
+  /** @test */
+  public function whileNotConnectedShouldIncludeHiddenInputWithRedirectToDesiredUrl() {
+    $this->dispatch('/admin/cms/edit/id/3', true);
+    $this->assertXPath('//input[@type="hidden"][@name="redirect"][@value="/admin/cms/edit/id/3"]');
+  }
+
+
   /** @test */
   public function withAuthenticationSuccessfullAndRedirectShouldRedirectToIt() {
     $this->_auth->whenCalled('authenticateLoginPassword')
-- 
GitLab