From b690d2f3753a53795566fae2a8c720170bbbb8cf Mon Sep 17 00:00:00 2001
From: gloas <gloas@afi-sa.fr>
Date: Fri, 22 Sep 2017 11:46:59 +0200
Subject: [PATCH] hotline #64837 add become action

---
 VERSIONS_HOTLINE/64837                        |  1 +
 .../admin/controllers/IndexController.php     | 18 +++++++++++
 .../admin/views/scripts/users/index.phtml     | 10 +++++-
 .../admin/controllers/IndexControllerTest.php | 31 +++++++++++++++++++
 .../admin/controllers/UsersControllerTest.php | 27 ++++++++++++++++
 5 files changed, 86 insertions(+), 1 deletion(-)
 create mode 100644 VERSIONS_HOTLINE/64837

diff --git a/VERSIONS_HOTLINE/64837 b/VERSIONS_HOTLINE/64837
new file mode 100644
index 00000000000..af6e0758d29
--- /dev/null
+++ b/VERSIONS_HOTLINE/64837
@@ -0,0 +1 @@
+ - ticket #64837 : Administration : correction des droits de suppresion des boites.
diff --git a/application/modules/admin/controllers/IndexController.php b/application/modules/admin/controllers/IndexController.php
index e1ff46101d8..ebbbe07871f 100644
--- a/application/modules/admin/controllers/IndexController.php
+++ b/application/modules/admin/controllers/IndexController.php
@@ -158,4 +158,22 @@ class Admin_IndexController extends ZendAfi_Controller_Action {
     $this->_helper->notify($message);
     $this->_redirect('admin/index/update-skin');
   }
+
+
+  public function becomeAction() {
+    if(!Class_Users::getIdentity()->isSuperAdmin()) {
+      $this->_helper->notify($this->_('Vous n\'avez pas les droits suffisants pour utiliser cette fonctionnalité.'));
+      return $this->_redirectToIndex();
+    }
+
+    if(!$user = Class_Users::find($this->_getParam('id', null))) {
+      $this->_helper->notify($this->_('Utilisateur invalide'));
+      return $this->_redirectToIndex();
+    }
+
+    ZendAfi_Auth::getInstance()->logUser($user);
+    $this->_helper->notify($this->_('Vous êtes maintenant connecté avec l\'utilisateur "%s"',
+                                    $user->getNomComplet()));
+    $this->_redirectToIndex();
+  }
 }
\ No newline at end of file
diff --git a/application/modules/admin/views/scripts/users/index.phtml b/application/modules/admin/views/scripts/users/index.phtml
index 22a26d3d9f8..eeb3a953340 100644
--- a/application/modules/admin/views/scripts/users/index.phtml
+++ b/application/modules/admin/views/scripts/users/index.phtml
@@ -33,7 +33,15 @@ $map =[['url' => ['module' => 'admin',
         'icon' => 'basket',
         'label' => $this->_('panier(s)'),
         'condition' => 'hasPaniers',
-        'anchorOptions' => ['target' => '_blank']]];
+        'anchorOptions' => ['target' => '_blank']],
+
+       ['url' => ['module' => 'admin',
+                  'controller' => 'index',
+                  'action' => 'become',
+                  'id' => '%s'],
+        'icon' => 'users',
+        'condition' => function() { return Class_Users::getIdentity()->isSuperAdmin();},
+        'label' => $this->_('Se connecter avec l\'identité')]];
 
 $actions = function($user) use($map) {
   return $this->renderModelActions($user,$map);
diff --git a/tests/application/modules/admin/controllers/IndexControllerTest.php b/tests/application/modules/admin/controllers/IndexControllerTest.php
index 8951deba852..a15a015ffcd 100644
--- a/tests/application/modules/admin/controllers/IndexControllerTest.php
+++ b/tests/application/modules/admin/controllers/IndexControllerTest.php
@@ -454,4 +454,35 @@ class Admin_IndexControllerAdminvarActionTest extends Admin_AbstractControllerTe
   public function varNewsletterUnsubscribeTextShouldBeLienPourSeDesinscrire() {
     $this->assertXPathContentContains('//tr[@data-adminvar="NEWSLETTER_UNSUBSCRIBE_TEXT"]//td', 'Lien pour se ');
   }
+}
+
+
+
+
+class IndexControllerBecomeActionTest extends Admin_AbstractControllerTestCase {
+  protected $_storm_default_to_volatile = true;
+
+
+  public function setUp() {
+    parent::setUp();
+
+    $this->fixture('Class_Users',
+                   ['id' => 78,
+                    'login' => 'test78',
+                    'password' => 'pwd']);
+
+    $this->dispatch('/admin/index/become/id/78', true);
+  }
+
+
+  /** @test */
+  public function shouldRedirect() {
+    $this->assertRedirect();
+  }
+
+
+  /** @test */
+  public function logedUserShouldBeTest78() {
+    $this->assertEquals(78, Class_Users::getIdentity()->getId());
+  }
 }
\ No newline at end of file
diff --git a/tests/application/modules/admin/controllers/UsersControllerTest.php b/tests/application/modules/admin/controllers/UsersControllerTest.php
index d65c549915a..474f9204ce0 100644
--- a/tests/application/modules/admin/controllers/UsersControllerTest.php
+++ b/tests/application/modules/admin/controllers/UsersControllerTest.php
@@ -764,6 +764,7 @@ class UsersControllerReferentIndexTest extends UsersControllerWithMarcusTestCase
 
 
 
+
 class UsersControllerAddActionPostTest extends UsersControllerWithMarcusTestCase {
   public function setUp() {
     parent::setUp();
@@ -1176,3 +1177,29 @@ class UsersControllerWithAdminPortalTest extends Admin_AbstractControllerTestCas
     $this->assertXPath('//th/a[contains(@href, "/search_order/nom/")]');
   }
 }
+
+
+
+
+class UsersControllerWithSuperAdminTest extends Admin_AbstractControllerTestCase {
+  protected $_storm_default_to_volatile = true;
+
+  public function setUp() {
+    parent::setUp();
+
+    ZendAfi_Auth::getInstance()
+      ->logUser($this->fixture('Class_Users',
+                               ['id' => 1,
+                                'login' => 'tom',
+                                'password' => 'rom',
+                                'role_level' => ZendAfi_Acl_AdminControllerRoles::SUPER_ADMIN]));
+
+    $this->dispatch('/admin/users/index', true);
+  }
+
+
+  /** @test */
+  public function linkToBecomeTimShouldBePresent() {
+    $this->assertXPath('//a[contains(@href, "/admin/index/become/")]', $this->_response->getBody());
+  }
+}
\ No newline at end of file
-- 
GitLab