Skip to content

Commit

Permalink
Merge pull request #5814 from magento-engcom/module-login-as-customer
Browse files Browse the repository at this point in the history
[Login As Customer] Bugfixing
  • Loading branch information
naydav authored Jun 17, 2020
2 parents 763e47b + 74a48fd commit 6fe5b63
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 39 deletions.
17 changes: 15 additions & 2 deletions app/code/Magento/LoginAsCustomer/Plugin/AdminLogoutPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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;
}
Expand All @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,14 @@
}
}
}

.page-actions {
.page-actions-buttons {
.login-button {
-ms-flex-order: -1;
-webkit-order: -1;
order: -1;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function __construct(
*/
public function getSectionData(): array
{
if (!$this->customerSession->getCustomerId()) {
if (!$this->customerSession->getCustomerId() || !$this->customerSession->getLoggedAsCustomerAdmindId()) {
return [];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</action>
</referenceBlock>
<referenceContainer name="content">
<block class="Magento\Framework\View\Element\Template" name="loginascustomer_login" template="Magento_LoginAsCustomerFrontendUi::login.phtml"/>
<block class="Magento\Framework\View\Element\Template" name="loginascustomer_login" template="Magento_LoginAsCustomerFrontendUi::login.phtml" cacheable="false"/>
</referenceContainer>
</body>
</page>
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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 <strong>%1</strong> 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 <strong>%1</strong> on %2')
.replace('%1', _.escape(this.fullname))
.replace('%2', _.escape(this.websiteName)));
}
}
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
</settings>
<bookmark name="bookmarks"/>
<columnsControls name="columns_controls"/>
<filterSearch name="name"/>
<filters name="listing_filters">
<settings>
<templates>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
*/
Expand Down Expand Up @@ -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
Expand All @@ -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);
}
}

0 comments on commit 6fe5b63

Please sign in to comment.