Skip to content

Commit 82dde33

Browse files
authored
Merge pull request #21 from ihorvansach/10-controllers-refactoring
Login As Customer Controllers refactoring
2 parents c47d091 + 6029cb7 commit 82dde33

File tree

6 files changed

+104
-56
lines changed

6 files changed

+104
-56
lines changed

app/code/Magento/LoginAsCustomer/Controller/Adminhtml/Login/Grid.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,30 @@
77

88
namespace Magento\LoginAsCustomer\Controller\Adminhtml\Login;
99

10+
use Magento\Framework\Controller\ResultFactory;
11+
use Magento\Framework\Controller\ResultInterface;
12+
use Magento\Framework\App\Action\HttpPostActionInterface;
13+
use Magento\Backend\App\Action;
14+
1015
/**
11-
* LoginAsCustomer log grid action
16+
* Login As Customer log grid ajax action
1217
*/
13-
class Grid extends \Magento\Backend\App\Action
18+
class Grid extends Action implements HttpPostActionInterface
1419
{
1520
/**
1621
* Authorization level of a basic admin session
22+
*
23+
* @see _isAllowed()
1724
*/
1825
const ADMIN_RESOURCE = 'Magento_LoginAsCustomer::login_log';
1926

2027
/**
21-
* Login as customer log grid
28+
* Login as customer log ajax grid
2229
*
23-
* @return \Magento\Framework\Controller\ResultInterface
30+
* @return ResultInterface
2431
*/
25-
public function execute()
32+
public function execute():ResultInterface
2633
{
27-
$this->_view->loadLayout(false);
28-
$this->_view->renderLayout();
34+
return $this->resultFactory->create(ResultFactory::TYPE_PAGE);
2935
}
3036
}

app/code/Magento/LoginAsCustomer/Controller/Adminhtml/Login/Index.php

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,22 @@
77

88
namespace Magento\LoginAsCustomer\Controller\Adminhtml\Login;
99

10+
use Magento\Framework\Controller\ResultFactory;
11+
use Magento\Framework\Controller\ResultInterface;
12+
use Magento\Framework\App\Action\HttpGetActionInterface;
13+
use Magento\Framework\App\Action\HttpPostActionInterface;
14+
use Magento\Backend\App\Action;
15+
1016
/**
11-
* LoginAsCustomer log action
17+
* Login As Customer log grid action
18+
* This action can be executed via GET when just visiting Grid page and POST ajax when filtering, sorting grid
1219
*/
13-
class Index extends \Magento\Backend\App\Action
20+
class Index extends Action implements HttpGetActionInterface, HttpPostActionInterface
1421
{
1522
/**
1623
* Authorization level of a basic admin session
24+
*
25+
* @see _isAllowed()
1726
*/
1827
const ADMIN_RESOURCE = 'Magento_LoginAsCustomer::login_log';
1928

@@ -34,25 +43,27 @@ public function __construct(
3443
parent::__construct($context);
3544
$this->loginModel = $loginModel;
3645
}
46+
3747
/**
38-
* Login as customer log
48+
* Login As Customer log grid action
3949
*
40-
* @return \Magento\Framework\Controller\ResultInterface
50+
* @return ResultInterface
4151
*/
42-
public function execute()
52+
public function execute():ResultInterface
4353
{
44-
if ($this->getRequest()->getParam('ajax')) {
45-
$this->_forward('grid');
46-
return;
54+
if ($this->getRequest()->isXmlHttpRequest()) {
55+
$resultForward = $this->resultFactory->create(ResultFactory::TYPE_FORWARD);
56+
$resultForward->forward('grid');
57+
return $resultForward;
4758
}
4859

4960
$this->loginModel->deleteNotUsed();
5061

51-
$this->_view->loadLayout();
52-
$this->_setActiveMenu('Magento_LoginAsCustomer::login_log');
53-
$title = __('Login As Customer Log ');
54-
$this->_view->getPage()->getConfig()->getTitle()->prepend($title);
55-
$this->_addBreadcrumb($title, $title);
56-
$this->_view->renderLayout();
62+
$resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
63+
$resultPage->setActiveMenu('Magento_LoginAsCustomer::login_log')
64+
->addBreadcrumb(__('Customer'), __('Login As Customer Log'));
65+
$resultPage->getConfig()->getTitle()->prepend(__('Login As Customer Log'));
66+
67+
return $resultPage;
5768
}
5869
}

app/code/Magento/LoginAsCustomer/Controller/Adminhtml/Login/Login.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,23 @@
77

88
namespace Magento\LoginAsCustomer\Controller\Adminhtml\Login;
99

10+
use Magento\Framework\Controller\ResultInterface;
11+
use Magento\Framework\App\Action\HttpGetActionInterface;
12+
use Magento\Framework\App\Action\HttpPostActionInterface;
13+
use Magento\Backend\App\Action;
14+
1015
/**
11-
* Class Login
12-
* @package Magento\LoginAsCustomer\Controller\Adminhtml\Login
16+
* Login as customer action
17+
* Generate secret key and forward to the storefront action
18+
*
19+
* This action can be executed via GET request when "Store View To Login In" is disabled, and POST when it is enabled
1320
*/
14-
class Login extends \Magento\Backend\App\Action
21+
class Login extends Action implements HttpGetActionInterface, HttpPostActionInterface
1522
{
1623
/**
1724
* Authorization level of a basic admin session
25+
*
26+
* @see _isAllowed()
1827
*/
1928
const ADMIN_RESOURCE = 'Magento_LoginAsCustomer::login_button';
2029

@@ -67,12 +76,13 @@ public function __construct(
6776
$this->url = $url;
6877
$this->config = $config;
6978
}
79+
7080
/**
71-
* Login as customer action
81+
* Login as customer
7282
*
73-
* @return \Magento\Framework\Controller\ResultInterface
83+
* @return ResultInterface
7484
*/
75-
public function execute()
85+
public function execute():ResultInterface
7686
{
7787
$request = $this->getRequest();
7888
$customerId = (int) $request->getParam('customer_id');
@@ -84,8 +94,7 @@ public function execute()
8494
$resultRedirect = $this->resultRedirectFactory->create();
8595

8696
if (!$this->config->isEnabled()) {
87-
$msg = strrev(__('.remotsuC sA nigoL > snoisnetxE nafegaM > noitarugifnoC > serotS ot etagivan esaelp noisnetxe eht elbane ot ,delbasid si remotsuC sA nigoL nafegaM'));
88-
$this->messageManager->addErrorMessage($msg);
97+
$this->messageManager->addErrorMessage(__('Login As Customer is disabled, to enable the extension please navigate to Stores > Configuration > Customers > Login As Customer.'));
8998
return $resultRedirect->setPath('customer/index/index');
9099
}
91100

@@ -118,6 +127,6 @@ public function execute()
118127
$redirectUrl = $this->url->setScope($store)
119128
->getUrl('loginascustomer/login/index', ['secret' => $login->getSecret(), '_nosid' => true]);
120129

121-
$this->getResponse()->setRedirect($redirectUrl);
130+
return $resultRedirect->setUrl($redirectUrl);
122131
}
123132
}

app/code/Magento/LoginAsCustomer/Controller/Adminhtml/Login/Manual.php

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,35 @@
77

88
namespace Magento\LoginAsCustomer\Controller\Adminhtml\Login;
99

10+
use Magento\Framework\Controller\ResultFactory;
11+
use Magento\Framework\Controller\ResultInterface;
12+
use Magento\Framework\App\Action\HttpGetActionInterface;
13+
use Magento\Backend\App\Action;
14+
1015
/**
11-
* Class Manual
12-
* @package Magento\LoginAsCustomer\Controller\Adminhtml\Login
16+
* Form to chose store view before login as customer
1317
*/
14-
class Manual extends \Magento\Backend\App\Action
18+
class Manual extends Action implements HttpGetActionInterface
1519
{
1620
/**
1721
* Authorization level of a basic admin session
22+
*
23+
* @see _isAllowed()
1824
*/
1925
const ADMIN_RESOURCE = 'Magento_LoginAsCustomer::login_button';
2026

2127
/**
22-
* @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface|void
28+
* Chose store view for Login as customer
29+
*
30+
* @return ResultInterface
2331
*/
24-
public function execute()
32+
public function execute():ResultInterface
2533
{
26-
$this->_view->loadLayout();
27-
$this->_setActiveMenu('Magento_LoginAsCustomer::login_button');
28-
$title = __('Store View To Login In ');
29-
$this->_view->getPage()->getConfig()->getTitle()->prepend($title);
30-
$this->_addBreadcrumb($title, $title);
31-
$this->_view->renderLayout();
34+
$resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
35+
$resultPage->setActiveMenu('Magento_LoginAsCustomer::login_button')
36+
->addBreadcrumb(__('Customer'), __('Login As Customer Log'), __('Store View To Login In'));
37+
$resultPage->getConfig()->getTitle()->prepend(__('Store View To Login In'));
38+
39+
return $resultPage;
3240
}
3341
}

app/code/Magento/LoginAsCustomer/Controller/Login/Index.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,23 @@
77

88
namespace Magento\LoginAsCustomer\Controller\Login;
99

10+
use Magento\Framework\Controller\ResultFactory;
11+
use Magento\Framework\Controller\ResultInterface;
1012
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\Framework\App\Action\HttpGetActionInterface;
14+
use Magento\Framework\App\Action\Action;
1115

1216
/**
13-
* LoginAsCustomer login action
17+
* Login As Customer storefront login action
1418
*/
15-
class Index extends \Magento\Framework\App\Action\Action
19+
class Index extends Action implements HttpGetActionInterface
1620
{
1721
/**
1822
* @var \Magento\LoginAsCustomer\Model\Login
1923
*/
2024
private $loginModel;
2125

26+
2227
/**
2328
* Index constructor.
2429
* @param \Magento\Framework\App\Action\Context $context
@@ -32,12 +37,13 @@ public function __construct(
3237
$this->loginModel = $loginModel;
3338
}
3439
/**
35-
* Login as customer action
40+
* Login As Customer storefront login
3641
*
37-
* @return \Magento\Framework\Controller\ResultInterface
42+
* @return ResultInterface
3843
*/
39-
public function execute()
44+
public function execute():ResultInterface
4045
{
46+
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
4147
try {
4248
$login = $this->_initLogin();
4349

@@ -48,13 +54,16 @@ public function execute()
4854
);
4955
} catch (LocalizedException $e) {
5056
$this->messageManager->addErrorMessage($e->getMessage());
51-
$this->_redirect('/');
52-
return;
57+
58+
$resultRedirect->setPath('/');
59+
return $resultRedirect;
60+
5361
} catch (\Exception $e) {
5462
$this->messageManager->addErrorMessage($e->getMessage());
5563
}
5664

57-
$this->_redirect('*/*/proceed');
65+
$resultRedirect->setPath('*/*/proceed');
66+
return $resultRedirect;
5867
}
5968

6069
/**

app/code/Magento/LoginAsCustomer/Controller/Login/Proceed.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,24 @@
66
declare(strict_types=1);
77
namespace Magento\LoginAsCustomer\Controller\Login;
88

9+
use Magento\Framework\Controller\ResultFactory;
10+
use Magento\Framework\Controller\ResultInterface;
11+
use Magento\Framework\App\Action\HttpGetActionInterface;
12+
use Magento\Framework\App\Action\Action;
13+
914
/**
10-
* LoginAsCustomer proceed action
15+
* Login as customer proxy page
16+
* Allows running JavaScript to load customer data to the browser local storage
1117
*/
12-
class Proceed extends \Magento\Framework\App\Action\Action
18+
class Proceed extends Action implements HttpGetActionInterface
1319
{
1420
/**
15-
* Login as customer action
21+
* Proxy page
1622
*
17-
* @return \Magento\Framework\Controller\ResultInterface
23+
* @return ResultInterface
1824
*/
19-
public function execute()
25+
public function execute():ResultInterface
2026
{
21-
$this->_view->loadLayout();
22-
$this->_view->renderLayout();
27+
return $this->resultFactory->create(ResultFactory::TYPE_PAGE);
2328
}
2429
}

0 commit comments

Comments
 (0)