Skip to content

Commit 6fe5b63

Browse files
authored
Merge pull request #5814 from magento-engcom/module-login-as-customer
[Login As Customer] Bugfixing
2 parents 763e47b + 74a48fd commit 6fe5b63

File tree

9 files changed

+71
-39
lines changed

9 files changed

+71
-39
lines changed

app/code/Magento/LoginAsCustomer/Plugin/AdminLogoutPlugin.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,22 @@
88
namespace Magento\LoginAsCustomer\Plugin;
99

1010
use Magento\Backend\Model\Auth;
11+
use Magento\Backend\Model\Auth\Session as AuthSession;
1112
use Magento\LoginAsCustomerApi\Api\ConfigInterface;
1213
use Magento\LoginAsCustomerApi\Api\DeleteAuthenticationDataForUserInterface;
1314

1415
/**
1516
* Delete all Login as Customer sessions for logging out admin.
17+
*
18+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
1619
*/
1720
class AdminLogoutPlugin
1821
{
22+
/**
23+
* @var AuthSession
24+
*/
25+
private $authSession;
26+
1927
/**
2028
* @var ConfigInterface
2129
*/
@@ -27,13 +35,16 @@ class AdminLogoutPlugin
2735
private $deleteAuthenticationDataForUser;
2836

2937
/**
38+
* @param AuthSession $authSession
3039
* @param ConfigInterface $config
3140
* @param DeleteAuthenticationDataForUserInterface $deleteAuthenticationDataForUser
3241
*/
3342
public function __construct(
43+
AuthSession $authSession,
3444
ConfigInterface $config,
3545
DeleteAuthenticationDataForUserInterface $deleteAuthenticationDataForUser
3646
) {
47+
$this->authSession = $authSession;
3748
$this->config = $config;
3849
$this->deleteAuthenticationDataForUser = $deleteAuthenticationDataForUser;
3950
}
@@ -45,8 +56,10 @@ public function __construct(
4556
*/
4657
public function beforeLogout(Auth $subject): void
4758
{
48-
if ($this->config->isEnabled()) {
49-
$userId = (int)$subject->getUser()->getId();
59+
$user = $subject->getUser();
60+
$isLoggedAsCustomer = $this->authSession->getIsLoggedAsCustomer();
61+
if ($this->config->isEnabled() && $user && $isLoggedAsCustomer) {
62+
$userId = (int)$user->getId();
5063
$this->deleteAuthenticationDataForUser->execute($userId);
5164
}
5265
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ public function execute(): ResultInterface
167167

168168
$this->deleteAuthenticationDataForUser->execute($userId);
169169
$secret = $this->saveAuthenticationData->execute($authenticationData);
170+
$this->authSession->setIsLoggedAsCustomer(true);
170171

171172
$redirectUrl = $this->getLoginProceedRedirectUrl($secret, $storeId);
172173
$resultRedirect->setUrl($redirectUrl);

app/code/Magento/LoginAsCustomerAdminUi/Ui/Customer/Component/Control/LoginAsCustomerButton.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function getButtonData(): array
6969
'on_click' => 'window.lacConfirmationPopup("'
7070
. $this->escaper->escapeHtml($this->escaper->escapeJs($this->getLoginUrl()))
7171
. '")',
72-
'sort_order' => 70,
72+
'sort_order' => 15,
7373
];
7474
}
7575

app/code/Magento/LoginAsCustomerAdminUi/view/adminhtml/web/css/source/_module.less

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,14 @@
3939
}
4040
}
4141
}
42+
43+
.page-actions {
44+
.page-actions-buttons {
45+
.login-button {
46+
-ms-flex-order: -1;
47+
-webkit-order: -1;
48+
order: -1;
49+
}
50+
}
51+
}
4252
}

app/code/Magento/LoginAsCustomerFrontendUi/CustomerData/LoginAsCustomerUi.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function __construct(
4949
*/
5050
public function getSectionData(): array
5151
{
52-
if (!$this->customerSession->getCustomerId()) {
52+
if (!$this->customerSession->getCustomerId() || !$this->customerSession->getLoggedAsCustomerAdmindId()) {
5353
return [];
5454
}
5555

app/code/Magento/LoginAsCustomerFrontendUi/view/frontend/layout/loginascustomer_login_index.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
</action>
1414
</referenceBlock>
1515
<referenceContainer name="content">
16-
<block class="Magento\Framework\View\Element\Template" name="loginascustomer_login" template="Magento_LoginAsCustomerFrontendUi::login.phtml"/>
16+
<block class="Magento\Framework\View\Element\Template" name="loginascustomer_login" template="Magento_LoginAsCustomerFrontendUi::login.phtml" cacheable="false"/>
1717
</referenceContainer>
1818
</body>
1919
</page>

app/code/Magento/LoginAsCustomerFrontendUi/view/frontend/web/js/view/loginAsCustomer.js

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55

66
define([
77
'jquery',
8+
'underscore',
89
'uiComponent',
910
'Magento_Customer/js/customer-data',
1011
'mage/translate'
11-
], function ($, Component, customerData) {
12+
], function ($, _, Component, customer) {
1213
'use strict';
1314

1415
return Component.extend({
@@ -19,23 +20,53 @@ define([
1920

2021
/** @inheritdoc */
2122
initialize: function () {
23+
var customerData, loggedAsCustomerData;
24+
2225
this._super();
2326

24-
this.customer = customerData.get('customer');
25-
this.loginAsCustomer = customerData.get('loggedAsCustomer');
26-
this.isVisible(this.loginAsCustomer().adminUserId);
27+
customerData = customer.get('customer');
28+
loggedAsCustomerData = customer.get('loggedAsCustomer');
29+
30+
customerData.subscribe(function (data) {
31+
this.fullname = data.fullname;
32+
this.updateBanner();
33+
}.bind(this));
34+
loggedAsCustomerData.subscribe(function (data) {
35+
this.adminUserId = data.adminUserId;
36+
this.websiteName = data.websiteName;
37+
this.updateBanner();
38+
}.bind(this));
39+
40+
this.fullname = customerData().fullname;
41+
this.adminUserId = loggedAsCustomerData().adminUserId;
42+
this.websiteName = loggedAsCustomerData().websiteName;
2743

28-
this.notificationText = $.mage.__('You are connected as <strong>%1</strong> on %2')
29-
.replace('%1', this.customer().fullname)
30-
.replace('%2', this.loginAsCustomer().websiteName);
44+
this.updateBanner();
3145
},
3246

3347
/** @inheritdoc */
3448
initObservable: function () {
3549
this._super()
36-
.observe('isVisible');
50+
.observe(['isVisible', 'notificationText']);
3751

3852
return this;
53+
},
54+
55+
/**
56+
* Update banner area
57+
*
58+
* @returns void
59+
*/
60+
updateBanner: function () {
61+
if (this.adminUserId !== undefined) {
62+
this.isVisible(this.adminUserId);
63+
}
64+
65+
if (this.fullname !== undefined && this.websiteName !== undefined) {
66+
this.notificationText($.mage.__('You are connected as <strong>%1</strong> on %2')
67+
.replace('%1', _.escape(this.fullname))
68+
.replace('%2', _.escape(this.websiteName)));
69+
}
3970
}
4071
});
4172
});

app/code/Magento/LoginAsCustomerLog/view/adminhtml/ui_component/login_as_customer_log_listing.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
</settings>
3939
<bookmark name="bookmarks"/>
4040
<columnsControls name="columns_controls"/>
41-
<filterSearch name="name"/>
4241
<filters name="listing_filters">
4342
<settings>
4443
<templates>

app/code/Magento/LoginAsCustomerQuote/Plugin/LoginAsCustomerApi/ProcessShoppingCartPlugin.php

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Magento\LoginAsCustomerApi\Api\GetAuthenticationDataBySecretInterface;
1616

1717
/**
18-
* Remove all items from guest shopping cart before execute. Mark customer cart as not-guest after execute
18+
* Remove all items from guest shopping cart and mark cart as not-guest
1919
*
2020
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
2121
*/
@@ -60,7 +60,7 @@ public function __construct(
6060
}
6161

6262
/**
63-
* Remove all items from guest shopping cart
63+
* Remove all items from guest shopping cart and mark cart as not-guest
6464
*
6565
* @param AuthenticateCustomerBySecretInterface $subject
6666
* @param string $secret
@@ -77,31 +77,9 @@ public function beforeExecute(
7777
$quote = $this->checkoutSession->getQuote();
7878
/* Remove items from guest cart */
7979
$quote->removeAllItems();
80+
$quote->setCustomerIsGuest(0);
8081
$this->quoteRepository->save($quote);
8182
}
8283
return null;
8384
}
84-
85-
/**
86-
* Mark customer cart as not-guest
87-
*
88-
* @param AuthenticateCustomerBySecretInterface $subject
89-
* @param void $result
90-
* @param string $secret
91-
* @return void
92-
* @throws LocalizedException
93-
*
94-
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
95-
*/
96-
public function afterExecute(
97-
AuthenticateCustomerBySecretInterface $subject,
98-
$result,
99-
string $secret
100-
) {
101-
$this->checkoutSession->loadCustomerQuote();
102-
$quote = $this->checkoutSession->getQuote();
103-
104-
$quote->setCustomerIsGuest(0);
105-
$this->quoteRepository->save($quote);
106-
}
10785
}

0 commit comments

Comments
 (0)