|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\LoginAsCustomerFrontendUi\Plugin; |
| 8 | + |
| 9 | +use Magento\Framework\Session\SessionManagerInterface; |
| 10 | +use Magento\LoginAsCustomerApi\Api\ConfigInterface; |
| 11 | +use Magento\LoginAsCustomerApi\Api\GetLoggedAsCustomerAdminIdInterface; |
| 12 | +use Magento\LoginAsCustomerApi\Api\SetLoggedAsCustomerAdminIdInterface; |
| 13 | + |
| 14 | +/** |
| 15 | + * Keep adminId in customer session if session data is cleared. |
| 16 | + */ |
| 17 | +class KeepLoginAsCustomerSessionDataPlugin |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @var ConfigInterface |
| 21 | + */ |
| 22 | + private $config; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var GetLoggedAsCustomerAdminIdInterface |
| 26 | + */ |
| 27 | + private $getLoggedAsCustomerAdminId; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var SetLoggedAsCustomerAdminIdInterface |
| 31 | + */ |
| 32 | + private $setLoggedAsCustomerAdminId; |
| 33 | + |
| 34 | + /** |
| 35 | + * @param ConfigInterface $config |
| 36 | + * @param GetLoggedAsCustomerAdminIdInterface $getLoggedAsCustomerAdminId |
| 37 | + * @param SetLoggedAsCustomerAdminIdInterface $setLoggedAsCustomerAdminId |
| 38 | + */ |
| 39 | + public function __construct( |
| 40 | + ConfigInterface $config, |
| 41 | + GetLoggedAsCustomerAdminIdInterface $getLoggedAsCustomerAdminId, |
| 42 | + SetLoggedAsCustomerAdminIdInterface $setLoggedAsCustomerAdminId |
| 43 | + ) { |
| 44 | + $this->config = $config; |
| 45 | + $this->getLoggedAsCustomerAdminId = $getLoggedAsCustomerAdminId; |
| 46 | + $this->setLoggedAsCustomerAdminId = $setLoggedAsCustomerAdminId; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Keep adminId in customer session if session data is cleared. |
| 51 | + * |
| 52 | + * @param SessionManagerInterface $subject |
| 53 | + * @param \Closure $proceed |
| 54 | + * @return SessionManagerInterface |
| 55 | + * |
| 56 | + * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
| 57 | + */ |
| 58 | + public function aroundClearStorage( |
| 59 | + SessionManagerInterface $subject, |
| 60 | + \Closure $proceed |
| 61 | + ): SessionManagerInterface { |
| 62 | + $enabled = $this->config->isEnabled(); |
| 63 | + $adminId = $enabled ? $this->getLoggedAsCustomerAdminId->execute() : null; |
| 64 | + $result = $proceed(); |
| 65 | + if ($enabled && $adminId) { |
| 66 | + $this->setLoggedAsCustomerAdminId->execute($adminId); |
| 67 | + } |
| 68 | + |
| 69 | + return $result; |
| 70 | + } |
| 71 | +} |
0 commit comments