Skip to content

Commit

Permalink
Merge pull request #123 from ihorvansach/issue-110
Browse files Browse the repository at this point in the history
Added spinner/loader while Admin is logging in as Customer
  • Loading branch information
naydav authored Apr 30, 2020
2 parents dc7ba27 + 6746254 commit 2c8b742
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<field id="disable_page_cache" translate="label comment" type="select" sortOrder="20" showInDefault="1" canRestore="1">
<label>Disable Page Cache For Admin User</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment><![CDATA[If set to "Yes", when login as customer Page Cache will be disabled.]]></comment>
<comment><![CDATA[If set to "Yes", page caching is disabled when the Admin user is logged in as a customer.]]></comment>
</field>
</group>
</section>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,17 @@ public function getJsLayout()

return $this->json->serialize($layout);
}

/**
* Render block HTML
*
* @return string
*/
protected function _toHtml()
{
if (!$this->config->isEnabled()) {
return '';
}
return parent::_toHtml();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ public function execute(): ResultInterface
$this->messageManager->addSuccessMessage(
__('You are logged in as customer: %1', $customer->getFirstname() . ' ' . $customer->getLastname())
);
$resultRedirect->setPath('*/*/proceed');
$resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
$resultPage->getConfig()->getTitle()->set(__('You are logged in'));
return $this->resultFactory->create(ResultFactory::TYPE_PAGE);

} catch (LocalizedException $e) {
$this->messageManager->addErrorMessage($e->getMessage());
Expand Down
46 changes: 0 additions & 46 deletions app/code/Magento/LoginAsCustomerUi/Controller/Login/Proceed.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function toOptionArray(): array
{
return [
['value' => self::AUTODETECT, 'label' => __('Auto-Detection (default)')],
['value' => self::MANUAL, 'label' => __('Manual Choose')],
['value' => self::MANUAL, 'label' => __('Manual Selection')],
];
}
}
24 changes: 13 additions & 11 deletions app/code/Magento/LoginAsCustomerUi/Plugin/Button/ToolbarPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Magento\Framework\View\Element\AbstractBlock;
use Magento\Framework\Escaper;
use Magento\Framework\AuthorizationInterface;
use Magento\LoginAsCustomerApi\Api\ConfigInterface;

/**
* Plugin for \Magento\Backend\Block\Widget\Button\Toolbar.
Expand All @@ -28,16 +29,24 @@ class ToolbarPlugin
*/
private $escaper;

/**
* @var ConfigInterface
*/
private $config;

/**
* ToolbarPlugin constructor.
* @param AuthorizationInterface $authorization
* @param ConfigInterface $config
* @param Escaper $escaper
*/
public function __construct(
AuthorizationInterface $authorization,
ConfigInterface $config,
Escaper $escaper
) {
$this->authorization = $authorization;
$this->config = $config;
$this->escaper = $escaper;
}

Expand Down Expand Up @@ -67,7 +76,10 @@ public function beforePushButtons(
$order = $context->getCreditmemo()->getOrder();
}
if ($order) {
if ($this->isAllowed()) {

$isAllowed = $this->authorization->isAllowed('Magento_LoginAsCustomer::login_button');
$isEnabled = $this->config->isEnabled();
if ($isAllowed && $isEnabled) {
if (!empty($order['customer_id'])) {
$buttonUrl = $context->getUrl('loginascustomer/login/login', [
'customer_id' => $order['customer_id']
Expand All @@ -87,14 +99,4 @@ public function beforePushButtons(
}
}
}

/**
* Check is allowed access
*
* @return bool
*/
private function isAllowed(): bool
{
return (bool)$this->authorization->isAllowed('Magento_LoginAsCustomer::login_button');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,28 @@
namespace Magento\LoginAsCustomerUi\Ui\Customer\Component\Control;

use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;
use Magento\Framework\AuthorizationInterface;
use Magento\Framework\Escaper;
use Magento\Framework\Registry;
use Magento\Backend\Block\Widget\Context;
use Magento\Customer\Block\Adminhtml\Edit\GenericButton;
use Magento\LoginAsCustomerApi\Api\ConfigInterface;

/**
* Login As Customer button UI component.
*/
class LoginAsCustomerButton extends GenericButton implements ButtonProviderInterface
{
/**
* @var \Magento\Framework\AuthorizationInterface
* @var AuthorizationInterface
*/
private $authorization;

/**
* @var ConfigInterface
*/
private $config;

/**
* Escaper
*
Expand All @@ -33,13 +40,16 @@ class LoginAsCustomerButton extends GenericButton implements ButtonProviderInter
/**
* @param Context $context
* @param Registry $registry
* @param ConfigInterface $config
*/
public function __construct(
Context $context,
Registry $registry
Registry $registry,
ConfigInterface $config
) {
parent::__construct($context, $registry);
$this->authorization = $context->getAuthorization();
$this->config = $config;
$this->escaper = $context->getEscaper();
}

Expand All @@ -50,8 +60,9 @@ public function getButtonData(): array
{
$customerId = $this->getCustomerId();
$data = [];
$canModify = $customerId && $this->authorization->isAllowed('Magento_LoginAsCustomer::login_button');
if ($canModify) {
$isAllowed = $customerId && $this->authorization->isAllowed('Magento_LoginAsCustomer::login_button');
$isEnabled = $this->config->isEnabled();
if ($isAllowed && $isEnabled) {
$data = [
'label' => __('Login As Customer'),
'class' => 'login login-button',
Expand Down
12 changes: 6 additions & 6 deletions app/code/Magento/LoginAsCustomerUi/etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<section id="login_as_customer" translate="label" type="text" sortOrder="1" showInDefault="1" >
<section id="login_as_customer" translate="label" type="text" sortOrder="1" showInDefault="1">
<class>separator-top</class>
<label>Login As Customer</label>
<label>Login as Customer</label>
<tab>customer</tab>
<resource>Magento_LoginAsCustomer::config_section</resource>
<group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" canRestore="1">
<label>Login As Customer Information</label>
<label>Login as Customer Settings</label>
<field id="enabled" translate="label" type="select" sortOrder="10" showInDefault="1" canRestore="1">
<label>Enable Extension</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="store_view_manual_choice_enabled" translate="label comment" type="select" sortOrder="40" showInDefault="1" canRestore="1">
<label>Store View To Login In</label>
<label>Store View To Login To</label>
<source_model>Magento\LoginAsCustomerUi\Model\Config\Source\StoreViewLogin</source_model>
<comment><![CDATA[
Use "Manual Choose" option on multi-website Magento setup with enabled global Share Customer Accounts.
If set to "Manual Choose" then after click on "Login As Customer" admin will need to choose from a select box a store view to login in.
Use the "Manual Selection" option on a multi-website setup that has "Share Customer Accounts" enabled globally.
If set to "Manual Selection", the "Login as Customer" admin can select a store view after logging in.
]]></comment>
</field>
</group>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
*/

define([
'jquery',
'Magento_Customer/js/customer-data',
'Magento_Customer/js/section-config'
], function (customerData, sectionConfig) {
], function ($, customerData, sectionConfig) {

'use strict';

return function (config) {
$('body').trigger('processStart');
customerData.reload(sectionConfig.getSectionNames()).done(function () {
window.location.href = config.redirectUrl;
});
Expand Down

0 comments on commit 2c8b742

Please sign in to comment.