Skip to content

Commit

Permalink
Controllers refactoring #10 [Login As Customer]
Browse files Browse the repository at this point in the history
  • Loading branch information
ihorvansach authored and naydav committed Apr 17, 2020
1 parent c47d091 commit 6029cb7
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,30 @@

namespace Magento\LoginAsCustomer\Controller\Adminhtml\Login;

use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Backend\App\Action;

/**
* LoginAsCustomer log grid action
* Login As Customer log grid ajax action
*/
class Grid extends \Magento\Backend\App\Action
class Grid extends Action implements HttpPostActionInterface
{
/**
* Authorization level of a basic admin session
*
* @see _isAllowed()
*/
const ADMIN_RESOURCE = 'Magento_LoginAsCustomer::login_log';

/**
* Login as customer log grid
* Login as customer log ajax grid
*
* @return \Magento\Framework\Controller\ResultInterface
* @return ResultInterface
*/
public function execute()
public function execute():ResultInterface
{
$this->_view->loadLayout(false);
$this->_view->renderLayout();
return $this->resultFactory->create(ResultFactory::TYPE_PAGE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,22 @@

namespace Magento\LoginAsCustomer\Controller\Adminhtml\Login;

use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Backend\App\Action;

/**
* LoginAsCustomer log action
* Login As Customer log grid action
* This action can be executed via GET when just visiting Grid page and POST ajax when filtering, sorting grid
*/
class Index extends \Magento\Backend\App\Action
class Index extends Action implements HttpGetActionInterface, HttpPostActionInterface
{
/**
* Authorization level of a basic admin session
*
* @see _isAllowed()
*/
const ADMIN_RESOURCE = 'Magento_LoginAsCustomer::login_log';

Expand All @@ -34,25 +43,27 @@ public function __construct(
parent::__construct($context);
$this->loginModel = $loginModel;
}

/**
* Login as customer log
* Login As Customer log grid action
*
* @return \Magento\Framework\Controller\ResultInterface
* @return ResultInterface
*/
public function execute()
public function execute():ResultInterface
{
if ($this->getRequest()->getParam('ajax')) {
$this->_forward('grid');
return;
if ($this->getRequest()->isXmlHttpRequest()) {
$resultForward = $this->resultFactory->create(ResultFactory::TYPE_FORWARD);
$resultForward->forward('grid');
return $resultForward;
}

$this->loginModel->deleteNotUsed();

$this->_view->loadLayout();
$this->_setActiveMenu('Magento_LoginAsCustomer::login_log');
$title = __('Login As Customer Log ');
$this->_view->getPage()->getConfig()->getTitle()->prepend($title);
$this->_addBreadcrumb($title, $title);
$this->_view->renderLayout();
$resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
$resultPage->setActiveMenu('Magento_LoginAsCustomer::login_log')
->addBreadcrumb(__('Customer'), __('Login As Customer Log'));
$resultPage->getConfig()->getTitle()->prepend(__('Login As Customer Log'));

return $resultPage;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,23 @@

namespace Magento\LoginAsCustomer\Controller\Adminhtml\Login;

use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Backend\App\Action;

/**
* Class Login
* @package Magento\LoginAsCustomer\Controller\Adminhtml\Login
* Login as customer action
* Generate secret key and forward to the storefront action
*
* This action can be executed via GET request when "Store View To Login In" is disabled, and POST when it is enabled
*/
class Login extends \Magento\Backend\App\Action
class Login extends Action implements HttpGetActionInterface, HttpPostActionInterface
{
/**
* Authorization level of a basic admin session
*
* @see _isAllowed()
*/
const ADMIN_RESOURCE = 'Magento_LoginAsCustomer::login_button';

Expand Down Expand Up @@ -67,12 +76,13 @@ public function __construct(
$this->url = $url;
$this->config = $config;
}

/**
* Login as customer action
* Login as customer
*
* @return \Magento\Framework\Controller\ResultInterface
* @return ResultInterface
*/
public function execute()
public function execute():ResultInterface
{
$request = $this->getRequest();
$customerId = (int) $request->getParam('customer_id');
Expand All @@ -84,8 +94,7 @@ public function execute()
$resultRedirect = $this->resultRedirectFactory->create();

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

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

$this->getResponse()->setRedirect($redirectUrl);
return $resultRedirect->setUrl($redirectUrl);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,35 @@

namespace Magento\LoginAsCustomer\Controller\Adminhtml\Login;

use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Backend\App\Action;

/**
* Class Manual
* @package Magento\LoginAsCustomer\Controller\Adminhtml\Login
* Form to chose store view before login as customer
*/
class Manual extends \Magento\Backend\App\Action
class Manual extends Action implements HttpGetActionInterface
{
/**
* Authorization level of a basic admin session
*
* @see _isAllowed()
*/
const ADMIN_RESOURCE = 'Magento_LoginAsCustomer::login_button';

/**
* @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface|void
* Chose store view for Login as customer
*
* @return ResultInterface
*/
public function execute()
public function execute():ResultInterface
{
$this->_view->loadLayout();
$this->_setActiveMenu('Magento_LoginAsCustomer::login_button');
$title = __('Store View To Login In ');
$this->_view->getPage()->getConfig()->getTitle()->prepend($title);
$this->_addBreadcrumb($title, $title);
$this->_view->renderLayout();
$resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
$resultPage->setActiveMenu('Magento_LoginAsCustomer::login_button')
->addBreadcrumb(__('Customer'), __('Login As Customer Log'), __('Store View To Login In'));
$resultPage->getConfig()->getTitle()->prepend(__('Store View To Login In'));

return $resultPage;
}
}
25 changes: 17 additions & 8 deletions app/code/Magento/LoginAsCustomer/Controller/Login/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,23 @@

namespace Magento\LoginAsCustomer\Controller\Login;

use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\Action\Action;

/**
* LoginAsCustomer login action
* Login As Customer storefront login action
*/
class Index extends \Magento\Framework\App\Action\Action
class Index extends Action implements HttpGetActionInterface
{
/**
* @var \Magento\LoginAsCustomer\Model\Login
*/
private $loginModel;


/**
* Index constructor.
* @param \Magento\Framework\App\Action\Context $context
Expand All @@ -32,12 +37,13 @@ public function __construct(
$this->loginModel = $loginModel;
}
/**
* Login as customer action
* Login As Customer storefront login
*
* @return \Magento\Framework\Controller\ResultInterface
* @return ResultInterface
*/
public function execute()
public function execute():ResultInterface
{
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
try {
$login = $this->_initLogin();

Expand All @@ -48,13 +54,16 @@ public function execute()
);
} catch (LocalizedException $e) {
$this->messageManager->addErrorMessage($e->getMessage());
$this->_redirect('/');
return;

$resultRedirect->setPath('/');
return $resultRedirect;

} catch (\Exception $e) {
$this->messageManager->addErrorMessage($e->getMessage());
}

$this->_redirect('*/*/proceed');
$resultRedirect->setPath('*/*/proceed');
return $resultRedirect;
}

/**
Expand Down
19 changes: 12 additions & 7 deletions app/code/Magento/LoginAsCustomer/Controller/Login/Proceed.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,24 @@
declare(strict_types=1);
namespace Magento\LoginAsCustomer\Controller\Login;

use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\Action\Action;

/**
* LoginAsCustomer proceed action
* Login as customer proxy page
* Allows running JavaScript to load customer data to the browser local storage
*/
class Proceed extends \Magento\Framework\App\Action\Action
class Proceed extends Action implements HttpGetActionInterface
{
/**
* Login as customer action
* Proxy page
*
* @return \Magento\Framework\Controller\ResultInterface
* @return ResultInterface
*/
public function execute()
public function execute():ResultInterface
{
$this->_view->loadLayout();
$this->_view->renderLayout();
return $this->resultFactory->create(ResultFactory::TYPE_PAGE);
}
}

0 comments on commit 6029cb7

Please sign in to comment.