diff --git a/app/code/Magento/LoginAsCustomer/Model/AuthenticateCustomerBySecret.php b/app/code/Magento/LoginAsCustomer/Model/AuthenticateCustomerBySecret.php index a728f4c3a4393..808b01bac58aa 100644 --- a/app/code/Magento/LoginAsCustomer/Model/AuthenticateCustomerBySecret.php +++ b/app/code/Magento/LoginAsCustomer/Model/AuthenticateCustomerBySecret.php @@ -8,9 +8,11 @@ namespace Magento\LoginAsCustomer\Model; use Magento\Customer\Model\Session; +use Magento\Framework\App\ObjectManager; use Magento\Framework\Exception\LocalizedException; use Magento\LoginAsCustomerApi\Api\AuthenticateCustomerBySecretInterface; use Magento\LoginAsCustomerApi\Api\GetAuthenticationDataBySecretInterface; +use Magento\LoginAsCustomerApi\Api\SetLoggedAsCustomerAdminIdInterface; /** * @inheritdoc @@ -29,16 +31,25 @@ class AuthenticateCustomerBySecret implements AuthenticateCustomerBySecretInterf */ private $customerSession; + /** + * @var SetLoggedAsCustomerAdminIdInterface + */ + private $setLoggedAsCustomerAdminId; + /** * @param GetAuthenticationDataBySecretInterface $getAuthenticationDataBySecret * @param Session $customerSession + * @param SetLoggedAsCustomerAdminIdInterface $setLoggedAsCustomerAdminId */ public function __construct( GetAuthenticationDataBySecretInterface $getAuthenticationDataBySecret, - Session $customerSession + Session $customerSession, + ?SetLoggedAsCustomerAdminIdInterface $setLoggedAsCustomerAdminId = null ) { $this->getAuthenticationDataBySecret = $getAuthenticationDataBySecret; $this->customerSession = $customerSession; + $this->setLoggedAsCustomerAdminId = $setLoggedAsCustomerAdminId + ?? ObjectManager::getInstance()->get(SetLoggedAsCustomerAdminIdInterface::class); } /** @@ -58,6 +69,6 @@ public function execute(string $secret): void } $this->customerSession->regenerateId(); - $this->customerSession->setLoggedAsCustomerAdmindId($authenticationData->getAdminId()); + $this->setLoggedAsCustomerAdminId->execute($authenticationData->getAdminId()); } } diff --git a/app/code/Magento/LoginAsCustomer/Model/GetLoggedAsCustomerAdminId.php b/app/code/Magento/LoginAsCustomer/Model/GetLoggedAsCustomerAdminId.php new file mode 100644 index 0000000000000..17af8a3b5c11f --- /dev/null +++ b/app/code/Magento/LoginAsCustomer/Model/GetLoggedAsCustomerAdminId.php @@ -0,0 +1,40 @@ +session = $session; + } + + /** + * @inheritdoc + */ + public function execute(): int + { + return (int)$this->session->getLoggedAsCustomerAdmindId(); + } +} diff --git a/app/code/Magento/LoginAsCustomer/Model/GetLoggedAsCustomerCustomerId.php b/app/code/Magento/LoginAsCustomer/Model/GetLoggedAsCustomerCustomerId.php new file mode 100644 index 0000000000000..9783b04a5a03f --- /dev/null +++ b/app/code/Magento/LoginAsCustomer/Model/GetLoggedAsCustomerCustomerId.php @@ -0,0 +1,40 @@ +session = $session; + } + + /** + * @inheritdoc + */ + public function execute(): int + { + return (int)$this->session->getLoggedAsCustomerCustomerId(); + } +} diff --git a/app/code/Magento/LoginAsCustomer/Model/IsLoginAsCustomerEnabledForCustomerChain.php b/app/code/Magento/LoginAsCustomer/Model/IsLoginAsCustomerEnabledForCustomerChain.php new file mode 100644 index 0000000000000..6937a11eb3b58 --- /dev/null +++ b/app/code/Magento/LoginAsCustomer/Model/IsLoginAsCustomerEnabledForCustomerChain.php @@ -0,0 +1,66 @@ +config = $config; + $this->resultFactory = $resultFactory; + $this->resolvers = $resolvers; + } + + /** + * @inheritdoc + */ + public function execute(int $customerId): IsLoginAsCustomerEnabledForCustomerResultInterface + { + $messages = [[]]; + /** @var IsLoginAsCustomerEnabledForCustomerResultInterface $resolver */ + foreach ($this->resolvers as $resolver) { + $resolverResult = $resolver->execute($customerId); + if (!$resolverResult->isEnabled()) { + $messages[] = $resolverResult->getMessages(); + } + } + + return $this->resultFactory->create(['messages' => array_merge(...$messages)]); + } +} diff --git a/app/code/Magento/LoginAsCustomer/Model/IsLoginAsCustomerEnabledForCustomerResult.php b/app/code/Magento/LoginAsCustomer/Model/IsLoginAsCustomerEnabledForCustomerResult.php new file mode 100644 index 0000000000000..0d2af8669777c --- /dev/null +++ b/app/code/Magento/LoginAsCustomer/Model/IsLoginAsCustomerEnabledForCustomerResult.php @@ -0,0 +1,53 @@ +messages = $messages; + } + + /** + * @inheritdoc + */ + public function isEnabled(): bool + { + return empty($this->messages); + } + + /** + * @inheritdoc + */ + public function getMessages(): array + { + return $this->messages; + } + + /** + * @inheritdoc + */ + public function setMessages(array $messages): void + { + $this->messages = $messages; + } +} diff --git a/app/code/Magento/LoginAsCustomer/Model/Resolver/IsLoginAsCustomerEnabledResolver.php b/app/code/Magento/LoginAsCustomer/Model/Resolver/IsLoginAsCustomerEnabledResolver.php new file mode 100644 index 0000000000000..de16a798983c0 --- /dev/null +++ b/app/code/Magento/LoginAsCustomer/Model/Resolver/IsLoginAsCustomerEnabledResolver.php @@ -0,0 +1,54 @@ +config = $config; + $this->resultFactory = $resultFactory; + } + + /** + * @inheritdoc + */ + public function execute(int $customerId): IsLoginAsCustomerEnabledForCustomerResultInterface + { + $messages = []; + if (!$this->config->isEnabled()) { + $messages[] = __('Login as Customer is disabled.'); + } + + return $this->resultFactory->create(['messages' => $messages]); + } +} diff --git a/app/code/Magento/LoginAsCustomer/Model/SetLoggedAsCustomerAdminId.php b/app/code/Magento/LoginAsCustomer/Model/SetLoggedAsCustomerAdminId.php new file mode 100644 index 0000000000000..aa16dbcd4f808 --- /dev/null +++ b/app/code/Magento/LoginAsCustomer/Model/SetLoggedAsCustomerAdminId.php @@ -0,0 +1,40 @@ +session = $session; + } + + /** + * @inheritdoc + */ + public function execute(int $adminId): void + { + $this->session->setLoggedAsCustomerAdmindId($adminId); + } +} diff --git a/app/code/Magento/LoginAsCustomer/Model/SetLoggedAsCustomerCustomerId.php b/app/code/Magento/LoginAsCustomer/Model/SetLoggedAsCustomerCustomerId.php new file mode 100644 index 0000000000000..95e159bdeded3 --- /dev/null +++ b/app/code/Magento/LoginAsCustomer/Model/SetLoggedAsCustomerCustomerId.php @@ -0,0 +1,40 @@ +session = $session; + } + + /** + * @inheritdoc + */ + public function execute(int $customerId): void + { + $this->session->setLoggedAsCustomerCustomerId($customerId); + } +} diff --git a/app/code/Magento/LoginAsCustomer/Plugin/AdminLogoutPlugin.php b/app/code/Magento/LoginAsCustomer/Plugin/AdminLogoutPlugin.php index 244fed20c21e8..9b8567663578d 100644 --- a/app/code/Magento/LoginAsCustomer/Plugin/AdminLogoutPlugin.php +++ b/app/code/Magento/LoginAsCustomer/Plugin/AdminLogoutPlugin.php @@ -8,9 +8,9 @@ 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; +use Magento\LoginAsCustomerApi\Api\GetLoggedAsCustomerCustomerIdInterface; /** * Delete all Login as Customer sessions for logging out admin. @@ -19,11 +19,6 @@ */ class AdminLogoutPlugin { - /** - * @var AuthSession - */ - private $authSession; - /** * @var ConfigInterface */ @@ -35,18 +30,23 @@ class AdminLogoutPlugin private $deleteAuthenticationDataForUser; /** - * @param AuthSession $authSession + * @var GetLoggedAsCustomerCustomerIdInterface + */ + private $getLoggedAsCustomerCustomerId; + + /** * @param ConfigInterface $config * @param DeleteAuthenticationDataForUserInterface $deleteAuthenticationDataForUser + * @param GetLoggedAsCustomerCustomerIdInterface $getLoggedAsCustomerCustomerId */ public function __construct( - AuthSession $authSession, ConfigInterface $config, - DeleteAuthenticationDataForUserInterface $deleteAuthenticationDataForUser + DeleteAuthenticationDataForUserInterface $deleteAuthenticationDataForUser, + GetLoggedAsCustomerCustomerIdInterface $getLoggedAsCustomerCustomerId ) { - $this->authSession = $authSession; $this->config = $config; $this->deleteAuthenticationDataForUser = $deleteAuthenticationDataForUser; + $this->getLoggedAsCustomerCustomerId = $getLoggedAsCustomerCustomerId; } /** @@ -57,7 +57,7 @@ public function __construct( public function beforeLogout(Auth $subject): void { $user = $subject->getUser(); - $isLoggedAsCustomer = $this->authSession->getIsLoggedAsCustomer(); + $isLoggedAsCustomer = (bool)$this->getLoggedAsCustomerCustomerId->execute(); if ($this->config->isEnabled() && $user && $isLoggedAsCustomer) { $userId = (int)$user->getId(); $this->deleteAuthenticationDataForUser->execute($userId); diff --git a/app/code/Magento/LoginAsCustomer/composer.json b/app/code/Magento/LoginAsCustomer/composer.json index ec81374528e7b..e58ec90e8f8bb 100755 --- a/app/code/Magento/LoginAsCustomer/composer.json +++ b/app/code/Magento/LoginAsCustomer/composer.json @@ -4,6 +4,7 @@ "require": { "php": "~7.3.0||~7.4.0", "magento/framework": "*", + "magento/module-backend": "*", "magento/module-customer": "*", "magento/module-login-as-customer-api": "*" }, diff --git a/app/code/Magento/LoginAsCustomer/etc/config.xml b/app/code/Magento/LoginAsCustomer/etc/config.xml index 936ae1ff2f05d..7e39cc39145eb 100644 --- a/app/code/Magento/LoginAsCustomer/etc/config.xml +++ b/app/code/Magento/LoginAsCustomer/etc/config.xml @@ -10,7 +10,7 @@ - 0 + 1 0 60 diff --git a/app/code/Magento/LoginAsCustomer/etc/di.xml b/app/code/Magento/LoginAsCustomer/etc/di.xml index c0ba4901ba7b8..e9e4983957d31 100755 --- a/app/code/Magento/LoginAsCustomer/etc/di.xml +++ b/app/code/Magento/LoginAsCustomer/etc/di.xml @@ -5,14 +5,35 @@ * See COPYING.txt for license details. */ --> - - - - - - + + + + + + + + + + + + + + + + Magento\LoginAsCustomer\Model\Resolver\IsLoginAsCustomerEnabledResolver + + + + diff --git a/app/code/Magento/LoginAsCustomerAdminUi/Controller/Adminhtml/Login/Login.php b/app/code/Magento/LoginAsCustomerAdminUi/Controller/Adminhtml/Login/Login.php index 70eef5347f8e3..39a7055ed65bb 100644 --- a/app/code/Magento/LoginAsCustomerAdminUi/Controller/Adminhtml/Login/Login.php +++ b/app/code/Magento/LoginAsCustomerAdminUi/Controller/Adminhtml/Login/Login.php @@ -12,6 +12,7 @@ use Magento\Backend\Model\Auth\Session; use Magento\Customer\Api\CustomerRepositoryInterface; use Magento\Framework\App\Action\HttpGetActionInterface; +use Magento\Framework\App\ObjectManager; use Magento\Framework\Controller\Result\Redirect; use Magento\Framework\Controller\ResultFactory; use Magento\Framework\Controller\ResultInterface; @@ -22,7 +23,9 @@ use Magento\LoginAsCustomerApi\Api\Data\AuthenticationDataInterface; use Magento\LoginAsCustomerApi\Api\Data\AuthenticationDataInterfaceFactory; use Magento\LoginAsCustomerApi\Api\DeleteAuthenticationDataForUserInterface; +use Magento\LoginAsCustomerApi\Api\IsLoginAsCustomerEnabledForCustomerInterface; use Magento\LoginAsCustomerApi\Api\SaveAuthenticationDataInterface; +use Magento\LoginAsCustomerApi\Api\SetLoggedAsCustomerCustomerIdInterface; use Magento\Store\Model\StoreManagerInterface; /** @@ -80,6 +83,16 @@ class Login extends Action implements HttpGetActionInterface */ private $url; + /** + * @var SetLoggedAsCustomerCustomerIdInterface + */ + private $setLoggedAsCustomerCustomerId; + + /** + * @var IsLoginAsCustomerEnabledForCustomerInterface + */ + private $isLoginAsCustomerEnabled; + /** * @param Context $context * @param Session $authSession @@ -87,9 +100,13 @@ class Login extends Action implements HttpGetActionInterface * @param CustomerRepositoryInterface $customerRepository * @param ConfigInterface $config * @param AuthenticationDataInterfaceFactory $authenticationDataFactory - * @param SaveAuthenticationDataInterface $saveAuthenticationData , + * @param SaveAuthenticationDataInterface $saveAuthenticationData * @param DeleteAuthenticationDataForUserInterface $deleteAuthenticationDataForUser * @param Url $url + * @param SetLoggedAsCustomerCustomerIdInterface $setLoggedAsCustomerCustomerId + * @param IsLoginAsCustomerEnabledForCustomerInterface $isLoginAsCustomerEnabled + * + * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function __construct( Context $context, @@ -100,7 +117,9 @@ public function __construct( AuthenticationDataInterfaceFactory $authenticationDataFactory, SaveAuthenticationDataInterface $saveAuthenticationData, DeleteAuthenticationDataForUserInterface $deleteAuthenticationDataForUser, - Url $url + Url $url, + ?SetLoggedAsCustomerCustomerIdInterface $setLoggedAsCustomerCustomerId = null, + ?IsLoginAsCustomerEnabledForCustomerInterface $isLoginAsCustomerEnabled = null ) { parent::__construct($context); @@ -112,6 +131,10 @@ public function __construct( $this->saveAuthenticationData = $saveAuthenticationData; $this->deleteAuthenticationDataForUser = $deleteAuthenticationDataForUser; $this->url = $url; + $this->setLoggedAsCustomerCustomerId = $setLoggedAsCustomerCustomerId + ?? ObjectManager::getInstance()->get(SetLoggedAsCustomerCustomerIdInterface::class); + $this->isLoginAsCustomerEnabled = $isLoginAsCustomerEnabled + ?? ObjectManager::getInstance()->get(IsLoginAsCustomerEnabledForCustomerInterface::class); } /** @@ -126,20 +149,24 @@ public function execute(): ResultInterface /** @var Redirect $resultRedirect */ $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT); - if (!$this->config->isEnabled()) { - $this->messageManager->addErrorMessage(__('Login as Customer is disabled.')); - return $resultRedirect->setPath('customer/index/index'); - } - $customerId = (int)$this->_request->getParam('customer_id'); if (!$customerId) { $customerId = (int)$this->_request->getParam('entity_id'); } + $isLoginAsCustomerEnabled = $this->isLoginAsCustomerEnabled->execute($customerId); + if (!$isLoginAsCustomerEnabled->isEnabled()) { + foreach ($isLoginAsCustomerEnabled->getMessages() as $message) { + $this->messageManager->addErrorMessage(__($message)); + } + + return $resultRedirect->setPath('customer/index/index'); + } + try { $customer = $this->customerRepository->getById($customerId); } catch (NoSuchEntityException $e) { - $this->messageManager->addErrorMessage(__('Customer with this ID are no longer exist.')); + $this->messageManager->addErrorMessage('Customer with this ID are no longer exist.'); return $resultRedirect->setPath('customer/index/index'); } @@ -167,7 +194,7 @@ public function execute(): ResultInterface $this->deleteAuthenticationDataForUser->execute($userId); $secret = $this->saveAuthenticationData->execute($authenticationData); - $this->authSession->setIsLoggedAsCustomer(true); + $this->setLoggedAsCustomerCustomerId->execute($customerId); $redirectUrl = $this->getLoginProceedRedirectUrl($secret, $storeId); $resultRedirect->setUrl($redirectUrl); diff --git a/app/code/Magento/LoginAsCustomerAdminUi/Plugin/Button/ToolbarPlugin.php b/app/code/Magento/LoginAsCustomerAdminUi/Plugin/Button/ToolbarPlugin.php index 89ee2791e38af..c67b0d9dd5273 100644 --- a/app/code/Magento/LoginAsCustomerAdminUi/Plugin/Button/ToolbarPlugin.php +++ b/app/code/Magento/LoginAsCustomerAdminUi/Plugin/Button/ToolbarPlugin.php @@ -8,10 +8,10 @@ namespace Magento\LoginAsCustomerAdminUi\Plugin\Button; use Magento\Backend\Block\Widget\Button\ButtonList; -use Magento\Backend\Block\Widget\Button\Toolbar; -use Magento\Framework\View\Element\AbstractBlock; -use Magento\Framework\Escaper; use Magento\Framework\AuthorizationInterface; +use Magento\Framework\Escaper; +use Magento\Framework\View\Element\AbstractBlock; +use Magento\LoginAsCustomerAdminUi\Ui\Customer\Component\Button\DataProvider; use Magento\LoginAsCustomerApi\Api\ConfigInterface; /** @@ -34,38 +34,71 @@ class ToolbarPlugin */ private $config; + /** + * @var DataProvider + */ + private $dataProvider; + /** * ToolbarPlugin constructor. * @param AuthorizationInterface $authorization * @param ConfigInterface $config * @param Escaper $escaper + * @param DataProvider $dataProvider */ public function __construct( AuthorizationInterface $authorization, ConfigInterface $config, - Escaper $escaper + Escaper $escaper, + DataProvider $dataProvider ) { $this->authorization = $authorization; $this->config = $config; $this->escaper = $escaper; + $this->dataProvider = $dataProvider; } /** * Add Login as Customer button. * - * @param \Magento\Backend\Block\Widget\Button\Toolbar $subject + * @param \Magento\Backend\Block\Widget\Button\ToolbarInterface $subject * @param \Magento\Framework\View\Element\AbstractBlock $context * @param \Magento\Backend\Block\Widget\Button\ButtonList $buttonList * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function beforePushButtons( - Toolbar $subject, + \Magento\Backend\Block\Widget\Button\ToolbarInterface $subject, AbstractBlock $context, ButtonList $buttonList - ):void { - $order = false; + ): void { $nameInLayout = $context->getNameInLayout(); + $order = $this->getOrder($nameInLayout, $context); + if ($order + && !empty($order['customer_id']) + && $this->config->isEnabled() + && $this->authorization->isAllowed('Magento_LoginAsCustomer::login_button') + ) { + $customerId = (int)$order['customer_id']; + $buttonList->add( + 'guest_to_customer', + $this->dataProvider->getData($customerId), + -1 + ); + } + } + + /** + * Extract order data from context. + * + * @param string $nameInLayout + * @param AbstractBlock $context + * @return array|null + */ + private function getOrder(string $nameInLayout, AbstractBlock $context) + { + $order = null; + if ('sales_order_edit' == $nameInLayout) { $order = $context->getOrder(); } elseif ('sales_invoice_view' == $nameInLayout) { @@ -75,28 +108,7 @@ public function beforePushButtons( } elseif ('sales_creditmemo_view' == $nameInLayout) { $order = $context->getCreditmemo()->getOrder(); } - if ($order) { - $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'] - ]); - $buttonList->add( - 'guest_to_customer', - [ - 'label' => __('Login as Customer'), - 'onclick' => 'window.lacConfirmationPopup("' - . $this->escaper->escapeHtml($this->escaper->escapeJs($buttonUrl)) - . '")', - 'class' => 'reset' - ], - -1 - ); - } - } - } + return $order; } } diff --git a/app/code/Magento/LoginAsCustomerAdminUi/Ui/Customer/Component/Button/DataProvider.php b/app/code/Magento/LoginAsCustomerAdminUi/Ui/Customer/Component/Button/DataProvider.php new file mode 100644 index 0000000000000..24a70fc429467 --- /dev/null +++ b/app/code/Magento/LoginAsCustomerAdminUi/Ui/Customer/Component/Button/DataProvider.php @@ -0,0 +1,77 @@ +escaper = $escaper; + $this->urlBuilder = $urlBuilder; + $this->data = $data; + } + + /** + * Get data for Login as Customer button. + * + * @param int $customerId + * @return array + */ + public function getData(int $customerId): array + { + $buttonData = [ + 'on_click' => 'window.lacConfirmationPopup("' + . $this->escaper->escapeHtml($this->escaper->escapeJs($this->getLoginUrl($customerId))) + . '")', + ]; + + return array_merge_recursive($buttonData, $this->data); + } + + /** + * Get Login as Customer login url. + * + * @param int $customerId + * @return string + */ + private function getLoginUrl(int $customerId): string + { + return $this->urlBuilder->getUrl('loginascustomer/login/login', ['customer_id' => $customerId]); + } +} 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 d900641c131a3..ab43fca3d447e 100644 --- a/app/code/Magento/LoginAsCustomerAdminUi/Ui/Customer/Component/Control/LoginAsCustomerButton.php +++ b/app/code/Magento/LoginAsCustomerAdminUi/Ui/Customer/Component/Control/LoginAsCustomerButton.php @@ -7,12 +7,13 @@ namespace Magento\LoginAsCustomerAdminUi\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\Framework\App\ObjectManager; +use Magento\Framework\AuthorizationInterface; +use Magento\Framework\Registry; +use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface; +use Magento\LoginAsCustomerAdminUi\Ui\Customer\Component\Button\DataProvider; use Magento\LoginAsCustomerApi\Api\ConfigInterface; /** @@ -31,26 +32,26 @@ class LoginAsCustomerButton extends GenericButton implements ButtonProviderInter private $config; /** - * Escaper - * - * @var Escaper + * @var DataProvider */ - private $escaper; + private $dataProvider; /** * @param Context $context * @param Registry $registry * @param ConfigInterface $config + * @param DataProvider $dataProvider */ public function __construct( Context $context, Registry $registry, - ConfigInterface $config + ConfigInterface $config, + ?DataProvider $dataProvider = null ) { parent::__construct($context, $registry); $this->authorization = $context->getAuthorization(); $this->config = $config; - $this->escaper = $context->getEscaper(); + $this->dataProvider = $dataProvider ?? ObjectManager::getInstance()->get(DataProvider::class); } /** @@ -58,31 +59,14 @@ public function __construct( */ public function getButtonData(): array { - $customerId = $this->getCustomerId(); + $customerId = (int)$this->getCustomerId(); $data = []; $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', - 'on_click' => 'window.lacConfirmationPopup("' - . $this->escaper->escapeHtml($this->escaper->escapeJs($this->getLoginUrl())) - . '")', - 'sort_order' => 15, - ]; + $data = $this->dataProvider->getData($customerId); } return $data; } - - /** - * Get Login as Customer login url. - * - * @return string - */ - public function getLoginUrl(): string - { - return $this->getUrl('loginascustomer/login/login', ['customer_id' => $this->getCustomerId()]); - } } diff --git a/app/code/Magento/LoginAsCustomerAdminUi/etc/adminhtml/di.xml b/app/code/Magento/LoginAsCustomerAdminUi/etc/adminhtml/di.xml index dabab45205527..b73a1d856c888 100644 --- a/app/code/Magento/LoginAsCustomerAdminUi/etc/adminhtml/di.xml +++ b/app/code/Magento/LoginAsCustomerAdminUi/etc/adminhtml/di.xml @@ -6,7 +6,17 @@ */ --> - + + + + Magento\LoginAsCustomerAdminUi\Ui\Customer\Component\Control\LoginAsCustomerButton\DataProvider + + + + + Magento\LoginAsCustomerAdminUi\Plugin\Button\ToolbarPlugin\DataProvider + + diff --git a/app/code/Magento/LoginAsCustomerAdminUi/etc/di.xml b/app/code/Magento/LoginAsCustomerAdminUi/etc/di.xml new file mode 100644 index 0000000000000..8ba8c5c6ead43 --- /dev/null +++ b/app/code/Magento/LoginAsCustomerAdminUi/etc/di.xml @@ -0,0 +1,31 @@ + + + + + + + + Login as Customer + login login-button + 15 + + + + + + + + Login as Customer + reset + + + + diff --git a/app/code/Magento/LoginAsCustomerApi/Api/Data/IsLoginAsCustomerEnabledForCustomerResultInterface.php b/app/code/Magento/LoginAsCustomerApi/Api/Data/IsLoginAsCustomerEnabledForCustomerResultInterface.php new file mode 100644 index 0000000000000..b7d3a616176ef --- /dev/null +++ b/app/code/Magento/LoginAsCustomerApi/Api/Data/IsLoginAsCustomerEnabledForCustomerResultInterface.php @@ -0,0 +1,35 @@ +customerSession = $customerSession; $this->storeManager = $storeManager; + $this->getLoggedAsCustomerAdminId = $getLoggedAsCustomerAdminId + ?? ObjectManager::getInstance()->get(GetLoggedAsCustomerAdminIdInterface::class); } /** @@ -49,12 +60,14 @@ public function __construct( */ public function getSectionData(): array { - if (!$this->customerSession->getCustomerId() || !$this->customerSession->getLoggedAsCustomerAdmindId()) { + $adminId = $this->getLoggedAsCustomerAdminId->execute(); + + if (!$adminId || !$this->customerSession->getCustomerId()) { return []; } return [ - 'adminUserId' => $this->customerSession->getLoggedAsCustomerAdmindId(), + 'adminUserId' => $adminId, 'websiteName' => $this->storeManager->getWebsite()->getName() ]; } diff --git a/app/code/Magento/LoginAsCustomerFrontendUi/Plugin/InvalidateExpiredSessionPlugin.php b/app/code/Magento/LoginAsCustomerFrontendUi/Plugin/InvalidateExpiredSessionPlugin.php index b68e871c5f955..c1e035ac9637c 100644 --- a/app/code/Magento/LoginAsCustomerFrontendUi/Plugin/InvalidateExpiredSessionPlugin.php +++ b/app/code/Magento/LoginAsCustomerFrontendUi/Plugin/InvalidateExpiredSessionPlugin.php @@ -9,6 +9,7 @@ use Magento\Customer\Model\Session; use Magento\Framework\App\ActionInterface; use Magento\LoginAsCustomerApi\Api\ConfigInterface; +use Magento\LoginAsCustomerApi\Api\GetLoggedAsCustomerAdminIdInterface; use Magento\LoginAsCustomerApi\Api\IsLoginAsCustomerSessionActiveInterface; /** @@ -31,19 +32,27 @@ class InvalidateExpiredSessionPlugin */ private $isLoginAsCustomerSessionActive; + /** + * @var GetLoggedAsCustomerAdminIdInterface + */ + private $getLoggedAsCustomerAdminId; + /** * @param ConfigInterface $config * @param Session $session * @param IsLoginAsCustomerSessionActiveInterface $isLoginAsCustomerSessionActive + * @param GetLoggedAsCustomerAdminIdInterface $getLoggedAsCustomerAdminId */ public function __construct( ConfigInterface $config, Session $session, - IsLoginAsCustomerSessionActiveInterface $isLoginAsCustomerSessionActive + IsLoginAsCustomerSessionActiveInterface $isLoginAsCustomerSessionActive, + GetLoggedAsCustomerAdminIdInterface $getLoggedAsCustomerAdminId ) { $this->session = $session; $this->isLoginAsCustomerSessionActive = $isLoginAsCustomerSessionActive; $this->config = $config; + $this->getLoggedAsCustomerAdminId = $getLoggedAsCustomerAdminId; } /** @@ -57,11 +66,13 @@ public function __construct( public function beforeExecute(ActionInterface $subject) { if ($this->config->isEnabled()) { - $adminId = (int)$this->session->getLoggedAsCustomerAdmindId(); + $adminId = $this->getLoggedAsCustomerAdminId->execute(); $customerId = (int)$this->session->getCustomerId(); if ($adminId && $customerId) { if (!$this->isLoginAsCustomerSessionActive->execute($customerId, $adminId)) { - $this->session->destroy(); + $this->session->clearStorage(); + $this->session->expireSessionCookie(); + $this->session->regenerateId(); } } } diff --git a/app/code/Magento/LoginAsCustomerFrontendUi/Plugin/KeepLoginAsCustomerSessionDataPlugin.php b/app/code/Magento/LoginAsCustomerFrontendUi/Plugin/KeepLoginAsCustomerSessionDataPlugin.php new file mode 100644 index 0000000000000..9519f3a54077b --- /dev/null +++ b/app/code/Magento/LoginAsCustomerFrontendUi/Plugin/KeepLoginAsCustomerSessionDataPlugin.php @@ -0,0 +1,73 @@ +config = $config; + $this->getLoggedAsCustomerAdminId = $getLoggedAsCustomerAdminId; + $this->setLoggedAsCustomerAdminId = $setLoggedAsCustomerAdminId; + } + + /** + * Keep adminId in customer session if session data is cleared. + * + * @param SessionManagerInterface $subject + * @param \Closure $proceed + * @return SessionManagerInterface + * + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + public function aroundClearStorage( + SessionManagerInterface $subject, + \Closure $proceed + ): SessionManagerInterface { + $enabled = $this->config->isEnabled(); + $adminId = $enabled ? $this->getLoggedAsCustomerAdminId->execute() : null; + $result = $proceed(); + if ($enabled && $adminId) { + $this->setLoggedAsCustomerAdminId->execute($adminId); + } + + return $result; + } +} diff --git a/app/code/Magento/LoginAsCustomerFrontendUi/ViewModel/Configuration.php b/app/code/Magento/LoginAsCustomerFrontendUi/ViewModel/Configuration.php index 7d8738d06f54f..357ede238585b 100644 --- a/app/code/Magento/LoginAsCustomerFrontendUi/ViewModel/Configuration.php +++ b/app/code/Magento/LoginAsCustomerFrontendUi/ViewModel/Configuration.php @@ -8,7 +8,10 @@ namespace Magento\LoginAsCustomerFrontendUi\ViewModel; use Magento\Customer\Model\Context; +use Magento\Framework\App\Http\Context as HttpContext; +use Magento\Framework\App\ObjectManager; use Magento\LoginAsCustomerApi\Api\ConfigInterface; +use Magento\LoginAsCustomerApi\Api\GetLoggedAsCustomerAdminIdInterface; /** * View model to get extension configuration in the template @@ -21,20 +24,29 @@ class Configuration implements \Magento\Framework\View\Element\Block\ArgumentInt private $config; /** - * @var \Magento\Framework\App\Http\Context + * @var HttpContext */ private $httpContext; + /** + * @var GetLoggedAsCustomerAdminIdInterface + */ + private $getLoggedAsCustomerAdminId; + /** * @param ConfigInterface $config - * @param \Magento\Framework\App\Http\Context $httpContext + * @param HttpContext $httpContext + * @param GetLoggedAsCustomerAdminIdInterface $getLoggedAsCustomerAdminId */ public function __construct( ConfigInterface $config, - \Magento\Framework\App\Http\Context $httpContext + HttpContext $httpContext, + ?GetLoggedAsCustomerAdminIdInterface $getLoggedAsCustomerAdminId = null ) { $this->config = $config; $this->httpContext = $httpContext; + $this->getLoggedAsCustomerAdminId = $getLoggedAsCustomerAdminId + ?? ObjectManager::getInstance()->get(GetLoggedAsCustomerAdminIdInterface::class); } /** @@ -44,7 +56,7 @@ public function __construct( */ public function isEnabled(): bool { - return $this->config->isEnabled() && $this->isLoggedIn(); + return $this->config->isEnabled() && $this->isLoggedIn() && $this->getLoggedAsCustomerAdminId->execute(); } /** diff --git a/app/code/Magento/LoginAsCustomerFrontendUi/etc/frontend/di.xml b/app/code/Magento/LoginAsCustomerFrontendUi/etc/frontend/di.xml index 2204402b7dd30..bff511b6bb6e6 100644 --- a/app/code/Magento/LoginAsCustomerFrontendUi/etc/frontend/di.xml +++ b/app/code/Magento/LoginAsCustomerFrontendUi/etc/frontend/di.xml @@ -17,4 +17,8 @@ + + + diff --git a/app/code/Magento/LoginAsCustomerPageCache/Plugin/PageCache/Model/Config/DisablePageCacheIfNeededPlugin.php b/app/code/Magento/LoginAsCustomerPageCache/Plugin/PageCache/Model/Config/DisablePageCacheIfNeededPlugin.php index 6b36a0720ecb3..dabf8c62e1dee 100644 --- a/app/code/Magento/LoginAsCustomerPageCache/Plugin/PageCache/Model/Config/DisablePageCacheIfNeededPlugin.php +++ b/app/code/Magento/LoginAsCustomerPageCache/Plugin/PageCache/Model/Config/DisablePageCacheIfNeededPlugin.php @@ -7,8 +7,8 @@ namespace Magento\LoginAsCustomerPageCache\Plugin\PageCache\Model\Config; -use Magento\Customer\Model\Session; use Magento\Framework\App\Config\ScopeConfigInterface; +use Magento\LoginAsCustomerApi\Api\GetLoggedAsCustomerAdminIdInterface; use Magento\PageCache\Model\Config; use Magento\Store\Model\ScopeInterface; @@ -27,20 +27,20 @@ class DisablePageCacheIfNeededPlugin private $scopeConfig; /** - * @var Session + * @var GetLoggedAsCustomerAdminIdInterface */ - private $customerSession; + private $getLoggedAsCustomerAdminId; /** * @param ScopeConfigInterface $scopeConfig - * @param Session $customerSession + * @param GetLoggedAsCustomerAdminIdInterface $getLoggedAsCustomerAdminId */ public function __construct( ScopeConfigInterface $scopeConfig, - Session $customerSession + GetLoggedAsCustomerAdminIdInterface $getLoggedAsCustomerAdminId ) { $this->scopeConfig = $scopeConfig; - $this->customerSession = $customerSession; + $this->getLoggedAsCustomerAdminId = $getLoggedAsCustomerAdminId; } /** @@ -58,7 +58,7 @@ public function afterIsEnabled(Config $subject, $isEnabled): bool 'login_as_customer/general/disable_page_cache', ScopeInterface::SCOPE_STORE ); - $adminId = $this->customerSession->getLoggedAsCustomerAdmindId(); + $adminId = $this->getLoggedAsCustomerAdminId->execute(); if ($disable && $adminId) { $isEnabled = false; } diff --git a/app/code/Magento/LoginAsCustomerPageCache/composer.json b/app/code/Magento/LoginAsCustomerPageCache/composer.json index 195a08fc19d83..84d7f2e2a6730 100644 --- a/app/code/Magento/LoginAsCustomerPageCache/composer.json +++ b/app/code/Magento/LoginAsCustomerPageCache/composer.json @@ -4,8 +4,8 @@ "require": { "php": "~7.3.0||~7.4.0", "magento/framework": "*", - "magento/module-customer": "*", - "magento/module-store": "*" + "magento/module-store": "*", + "magento/module-login-as-customer-api": "*" }, "suggest": { "magento/module-page-cache": "*" diff --git a/app/code/Magento/LoginAsCustomerSales/Plugin/FrontAddCommentOnOrderPlacementPlugin.php b/app/code/Magento/LoginAsCustomerSales/Plugin/FrontAddCommentOnOrderPlacementPlugin.php index dc7b295f61c4d..87ffe81998d58 100644 --- a/app/code/Magento/LoginAsCustomerSales/Plugin/FrontAddCommentOnOrderPlacementPlugin.php +++ b/app/code/Magento/LoginAsCustomerSales/Plugin/FrontAddCommentOnOrderPlacementPlugin.php @@ -7,7 +7,7 @@ namespace Magento\LoginAsCustomerSales\Plugin; -use Magento\Customer\Model\Session; +use Magento\LoginAsCustomerApi\Api\GetLoggedAsCustomerAdminIdInterface; use Magento\Sales\Model\Order; use Magento\User\Model\UserFactory; @@ -19,25 +19,25 @@ class FrontAddCommentOnOrderPlacementPlugin { /** - * @var Session + * @var UserFactory */ - private $customerSession; + private $userFactory; /** - * @var UserFactory + * @var GetLoggedAsCustomerAdminIdInterface */ - private $userFactory; + private $getLoggedAsCustomerAdminId; /** - * @param Session $session * @param UserFactory $userFactory + * @param GetLoggedAsCustomerAdminIdInterface $getLoggedAsCustomerAdminId */ public function __construct( - Session $session, - UserFactory $userFactory + UserFactory $userFactory, + GetLoggedAsCustomerAdminIdInterface $getLoggedAsCustomerAdminId ) { - $this->customerSession = $session; $this->userFactory = $userFactory; + $this->getLoggedAsCustomerAdminId = $getLoggedAsCustomerAdminId; } /** @@ -49,7 +49,7 @@ public function __construct( */ public function afterPlace(Order $subject, Order $result): Order { - $adminId = $this->customerSession->getLoggedAsCustomerAdmindId(); + $adminId = $this->getLoggedAsCustomerAdminId->execute(); if ($adminId) { $adminUser = $this->userFactory->create()->load($adminId); $subject->addCommentToStatusHistory( diff --git a/app/code/Magento/LoginAsCustomerSales/composer.json b/app/code/Magento/LoginAsCustomerSales/composer.json index 3965e8acf87d8..3891504e54092 100644 --- a/app/code/Magento/LoginAsCustomerSales/composer.json +++ b/app/code/Magento/LoginAsCustomerSales/composer.json @@ -5,8 +5,8 @@ "php": "~7.3.0||~7.4.0", "magento/framework": "*", "magento/module-backend": "*", - "magento/module-customer": "*", - "magento/module-user": "*" + "magento/module-user": "*", + "magento/module-login-as-customer-api": "*" }, "suggest": { "magento/module-sales": "*" diff --git a/app/code/Magento/LoginAsCustomerSales/etc/frontend/di.xml b/app/code/Magento/LoginAsCustomerSales/etc/frontend/di.xml new file mode 100644 index 0000000000000..1a010fcdead85 --- /dev/null +++ b/app/code/Magento/LoginAsCustomerSales/etc/frontend/di.xml @@ -0,0 +1,12 @@ + + + + + + + diff --git a/app/code/Magento/LoginAsCustomerSales/etc/webapi_rest/di.xml b/app/code/Magento/LoginAsCustomerSales/etc/webapi_rest/di.xml index 1a010fcdead85..6dda349f1e60d 100644 --- a/app/code/Magento/LoginAsCustomerSales/etc/webapi_rest/di.xml +++ b/app/code/Magento/LoginAsCustomerSales/etc/webapi_rest/di.xml @@ -7,6 +7,6 @@ --> - +