diff --git a/app/code/Magento/LoginAsCustomer/Plugin/AdminLogoutPlugin.php b/app/code/Magento/LoginAsCustomer/Plugin/AdminLogoutPlugin.php index 3b8d26129a91e..244fed20c21e8 100644 --- a/app/code/Magento/LoginAsCustomer/Plugin/AdminLogoutPlugin.php +++ b/app/code/Magento/LoginAsCustomer/Plugin/AdminLogoutPlugin.php @@ -8,14 +8,22 @@ namespace Magento\LoginAsCustomer\Plugin; use Magento\Backend\Model\Auth; +use Magento\Backend\Model\Auth\Session as AuthSession; use Magento\LoginAsCustomerApi\Api\ConfigInterface; use Magento\LoginAsCustomerApi\Api\DeleteAuthenticationDataForUserInterface; /** * Delete all Login as Customer sessions for logging out admin. + * + * @SuppressWarnings(PHPMD.CookieAndSessionMisuse) */ class AdminLogoutPlugin { + /** + * @var AuthSession + */ + private $authSession; + /** * @var ConfigInterface */ @@ -27,13 +35,16 @@ class AdminLogoutPlugin private $deleteAuthenticationDataForUser; /** + * @param AuthSession $authSession * @param ConfigInterface $config * @param DeleteAuthenticationDataForUserInterface $deleteAuthenticationDataForUser */ public function __construct( + AuthSession $authSession, ConfigInterface $config, DeleteAuthenticationDataForUserInterface $deleteAuthenticationDataForUser ) { + $this->authSession = $authSession; $this->config = $config; $this->deleteAuthenticationDataForUser = $deleteAuthenticationDataForUser; } @@ -45,8 +56,10 @@ public function __construct( */ public function beforeLogout(Auth $subject): void { - if ($this->config->isEnabled()) { - $userId = (int)$subject->getUser()->getId(); + $user = $subject->getUser(); + $isLoggedAsCustomer = $this->authSession->getIsLoggedAsCustomer(); + if ($this->config->isEnabled() && $user && $isLoggedAsCustomer) { + $userId = (int)$user->getId(); $this->deleteAuthenticationDataForUser->execute($userId); } } diff --git a/app/code/Magento/LoginAsCustomerAdminUi/Controller/Adminhtml/Login/Login.php b/app/code/Magento/LoginAsCustomerAdminUi/Controller/Adminhtml/Login/Login.php index 7ccdcfe45e482..70eef5347f8e3 100644 --- a/app/code/Magento/LoginAsCustomerAdminUi/Controller/Adminhtml/Login/Login.php +++ b/app/code/Magento/LoginAsCustomerAdminUi/Controller/Adminhtml/Login/Login.php @@ -167,6 +167,7 @@ public function execute(): ResultInterface $this->deleteAuthenticationDataForUser->execute($userId); $secret = $this->saveAuthenticationData->execute($authenticationData); + $this->authSession->setIsLoggedAsCustomer(true); $redirectUrl = $this->getLoginProceedRedirectUrl($secret, $storeId); $resultRedirect->setUrl($redirectUrl); diff --git a/app/code/Magento/LoginAsCustomerAdminUi/Ui/Customer/Component/Control/LoginAsCustomerButton.php b/app/code/Magento/LoginAsCustomerAdminUi/Ui/Customer/Component/Control/LoginAsCustomerButton.php index 0f8f7750262f2..d900641c131a3 100644 --- a/app/code/Magento/LoginAsCustomerAdminUi/Ui/Customer/Component/Control/LoginAsCustomerButton.php +++ b/app/code/Magento/LoginAsCustomerAdminUi/Ui/Customer/Component/Control/LoginAsCustomerButton.php @@ -69,7 +69,7 @@ public function getButtonData(): array 'on_click' => 'window.lacConfirmationPopup("' . $this->escaper->escapeHtml($this->escaper->escapeJs($this->getLoginUrl())) . '")', - 'sort_order' => 70, + 'sort_order' => 15, ]; } diff --git a/app/code/Magento/LoginAsCustomerAdminUi/view/adminhtml/web/css/source/_module.less b/app/code/Magento/LoginAsCustomerAdminUi/view/adminhtml/web/css/source/_module.less index 2901f95f0e279..d702bc49f23ed 100644 --- a/app/code/Magento/LoginAsCustomerAdminUi/view/adminhtml/web/css/source/_module.less +++ b/app/code/Magento/LoginAsCustomerAdminUi/view/adminhtml/web/css/source/_module.less @@ -39,4 +39,14 @@ } } } + + .page-actions { + .page-actions-buttons { + .login-button { + -ms-flex-order: -1; + -webkit-order: -1; + order: -1; + } + } + } } diff --git a/app/code/Magento/LoginAsCustomerFrontendUi/CustomerData/LoginAsCustomerUi.php b/app/code/Magento/LoginAsCustomerFrontendUi/CustomerData/LoginAsCustomerUi.php index 6303989c0c667..985561df0bbc4 100644 --- a/app/code/Magento/LoginAsCustomerFrontendUi/CustomerData/LoginAsCustomerUi.php +++ b/app/code/Magento/LoginAsCustomerFrontendUi/CustomerData/LoginAsCustomerUi.php @@ -49,7 +49,7 @@ public function __construct( */ public function getSectionData(): array { - if (!$this->customerSession->getCustomerId()) { + if (!$this->customerSession->getCustomerId() || !$this->customerSession->getLoggedAsCustomerAdmindId()) { return []; } diff --git a/app/code/Magento/LoginAsCustomerFrontendUi/view/frontend/layout/loginascustomer_login_index.xml b/app/code/Magento/LoginAsCustomerFrontendUi/view/frontend/layout/loginascustomer_login_index.xml index efb866690c401..768b63cbbecea 100644 --- a/app/code/Magento/LoginAsCustomerFrontendUi/view/frontend/layout/loginascustomer_login_index.xml +++ b/app/code/Magento/LoginAsCustomerFrontendUi/view/frontend/layout/loginascustomer_login_index.xml @@ -13,7 +13,7 @@ - + diff --git a/app/code/Magento/LoginAsCustomerFrontendUi/view/frontend/web/js/view/loginAsCustomer.js b/app/code/Magento/LoginAsCustomerFrontendUi/view/frontend/web/js/view/loginAsCustomer.js index 7f6cad6ce3f2d..c19adbf0dfb4f 100644 --- a/app/code/Magento/LoginAsCustomerFrontendUi/view/frontend/web/js/view/loginAsCustomer.js +++ b/app/code/Magento/LoginAsCustomerFrontendUi/view/frontend/web/js/view/loginAsCustomer.js @@ -5,10 +5,11 @@ define([ 'jquery', + 'underscore', 'uiComponent', 'Magento_Customer/js/customer-data', 'mage/translate' -], function ($, Component, customerData) { +], function ($, _, Component, customer) { 'use strict'; return Component.extend({ @@ -19,23 +20,53 @@ define([ /** @inheritdoc */ initialize: function () { + var customerData, loggedAsCustomerData; + this._super(); - this.customer = customerData.get('customer'); - this.loginAsCustomer = customerData.get('loggedAsCustomer'); - this.isVisible(this.loginAsCustomer().adminUserId); + customerData = customer.get('customer'); + loggedAsCustomerData = customer.get('loggedAsCustomer'); + + customerData.subscribe(function (data) { + this.fullname = data.fullname; + this.updateBanner(); + }.bind(this)); + loggedAsCustomerData.subscribe(function (data) { + this.adminUserId = data.adminUserId; + this.websiteName = data.websiteName; + this.updateBanner(); + }.bind(this)); + + this.fullname = customerData().fullname; + this.adminUserId = loggedAsCustomerData().adminUserId; + this.websiteName = loggedAsCustomerData().websiteName; - this.notificationText = $.mage.__('You are connected as %1 on %2') - .replace('%1', this.customer().fullname) - .replace('%2', this.loginAsCustomer().websiteName); + this.updateBanner(); }, /** @inheritdoc */ initObservable: function () { this._super() - .observe('isVisible'); + .observe(['isVisible', 'notificationText']); return this; + }, + + /** + * Update banner area + * + * @returns void + */ + updateBanner: function () { + if (this.adminUserId !== undefined) { + this.isVisible(this.adminUserId); + } + + if (this.fullname !== undefined && this.websiteName !== undefined) { + this.notificationText($.mage.__('You are connected as %1 on %2') + .replace('%1', _.escape(this.fullname)) + .replace('%2', _.escape(this.websiteName))); + } } }); }); diff --git a/app/code/Magento/LoginAsCustomerLog/view/adminhtml/ui_component/login_as_customer_log_listing.xml b/app/code/Magento/LoginAsCustomerLog/view/adminhtml/ui_component/login_as_customer_log_listing.xml index 077fd6e18db7c..fdd1bf55c91b9 100644 --- a/app/code/Magento/LoginAsCustomerLog/view/adminhtml/ui_component/login_as_customer_log_listing.xml +++ b/app/code/Magento/LoginAsCustomerLog/view/adminhtml/ui_component/login_as_customer_log_listing.xml @@ -38,7 +38,6 @@ - diff --git a/app/code/Magento/LoginAsCustomerQuote/Plugin/LoginAsCustomerApi/ProcessShoppingCartPlugin.php b/app/code/Magento/LoginAsCustomerQuote/Plugin/LoginAsCustomerApi/ProcessShoppingCartPlugin.php index cf25962a104b2..4aa068a0ccc61 100644 --- a/app/code/Magento/LoginAsCustomerQuote/Plugin/LoginAsCustomerApi/ProcessShoppingCartPlugin.php +++ b/app/code/Magento/LoginAsCustomerQuote/Plugin/LoginAsCustomerApi/ProcessShoppingCartPlugin.php @@ -15,7 +15,7 @@ use Magento\LoginAsCustomerApi\Api\GetAuthenticationDataBySecretInterface; /** - * Remove all items from guest shopping cart before execute. Mark customer cart as not-guest after execute + * Remove all items from guest shopping cart and mark cart as not-guest * * @SuppressWarnings(PHPMD.CookieAndSessionMisuse) */ @@ -60,7 +60,7 @@ public function __construct( } /** - * Remove all items from guest shopping cart + * Remove all items from guest shopping cart and mark cart as not-guest * * @param AuthenticateCustomerBySecretInterface $subject * @param string $secret @@ -77,31 +77,9 @@ public function beforeExecute( $quote = $this->checkoutSession->getQuote(); /* Remove items from guest cart */ $quote->removeAllItems(); + $quote->setCustomerIsGuest(0); $this->quoteRepository->save($quote); } return null; } - - /** - * Mark customer cart as not-guest - * - * @param AuthenticateCustomerBySecretInterface $subject - * @param void $result - * @param string $secret - * @return void - * @throws LocalizedException - * - * @SuppressWarnings(PHPMD.UnusedFormalParameter) - */ - public function afterExecute( - AuthenticateCustomerBySecretInterface $subject, - $result, - string $secret - ) { - $this->checkoutSession->loadCustomerQuote(); - $quote = $this->checkoutSession->getQuote(); - - $quote->setCustomerIsGuest(0); - $this->quoteRepository->save($quote); - } }