Skip to content

Commit 97abce2

Browse files
committed
magento2-login-as-customer/issues/104: Global refactoring
- Extract LoginAsCustomerPageCache
1 parent 0153d82 commit 97abce2

File tree

14 files changed

+176
-78
lines changed

14 files changed

+176
-78
lines changed

app/code/Magento/LoginAsCustomerLog/registration.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
67

78
use Magento\Framework\Component\ComponentRegistrar;
89

9-
declare(strict_types=1);
10-
1110
ComponentRegistrar::register(
1211
ComponentRegistrar::MODULE,
1312
'Magento_LoginAsCustomerLog',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\LoginAsCustomerPageCache\Plugin\PageCache\Model\Config;
9+
10+
use Magento\Customer\Model\Session;
11+
use Magento\Framework\App\Config\ScopeConfigInterface;
12+
use Magento\PageCache\Model\Config;
13+
use Magento\Store\Model\ScopeInterface;
14+
15+
/**
16+
* Disable PageCache if enabled corresponding option in configuration
17+
*/
18+
class DisablePageCacheIfNeededPlugin
19+
{
20+
/**
21+
* Core store config
22+
*
23+
* @var ScopeConfigInterface
24+
*/
25+
private $scopeConfig;
26+
27+
/**
28+
* @var Session
29+
*/
30+
private $customerSession;
31+
32+
/**
33+
* @param ScopeConfigInterface $scopeConfig
34+
* @param Session $customerSession
35+
*/
36+
public function __construct(
37+
ScopeConfigInterface $scopeConfig,
38+
Session $customerSession
39+
) {
40+
$this->scopeConfig = $scopeConfig;
41+
$this->customerSession = $customerSession;
42+
}
43+
44+
/**
45+
* Disable page cache if needed when admin is logged as customer
46+
*
47+
* @param Config $subject
48+
* @param bool $isEnabled
49+
* @return bool
50+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
51+
*/
52+
public function afterIsEnabled(Config $subject, $isEnabled): bool
53+
{
54+
if ($isEnabled) {
55+
$disable = $this->scopeConfig->getValue(
56+
'login_as_customer/general/disable_page_cache',
57+
ScopeInterface::SCOPE_STORE
58+
);
59+
$adminId = $this->customerSession->getLoggedAsCustomerAdmindId();
60+
if ($disable && $adminId) {
61+
$isEnabled = false;
62+
}
63+
}
64+
return $isEnabled;
65+
}
66+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# LoginAsCustomerPageCache module
2+
3+
The Magento_LoginAsCustomerPageCache module provides adaptation to PageCache functionality
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "magento/module-login-as-customer-page-cache",
3+
"description": "",
4+
"require": {
5+
"php": "~7.1.3||~7.2.0||~7.3.0",
6+
"magento/framework": "*",
7+
"magento/module-customer": "*",
8+
"magento/module-store": "*"
9+
},
10+
"suggest": {
11+
"magento/module-page-cache": "*"
12+
},
13+
"type": "magento2-module",
14+
"license": [
15+
"OSL-3.0",
16+
"AFL-3.0"
17+
],
18+
"autoload": {
19+
"files": [ "registration.php" ],
20+
"psr-4": {
21+
"Magento\\LoginAsCustomerPageCache\\": ""
22+
}
23+
}
24+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
9+
<system>
10+
<section id="login_as_customer">
11+
<group id="general">
12+
<field id="disable_page_cache" translate="label comment" type="select" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
13+
<label>Disable Page Cache For Admin User</label>
14+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
15+
<comment><![CDATA[If set to "Yes", when login as customer Page Cache will be disabled.]]></comment>
16+
</field>
17+
</group>
18+
</section>
19+
</system>
20+
</config>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
10+
<default>
11+
<login_as_customer>
12+
<general>
13+
<disable_page_cache>1</disable_page_cache>
14+
</general>
15+
</login_as_customer>
16+
</default>
17+
</config>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
10+
<type name="Magento\PageCache\Model\Config">
11+
<plugin name="login-as-customer-disable-page-cache"
12+
type="Magento\LoginAsCustomerPageCache\Plugin\PageCache\Model\Config\DisablePageCacheIfNeededPlugin"/>
13+
</type>
14+
</config>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" ?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
10+
<module name="Magento_LoginAsCustomerPageCache"/>
11+
</config>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento\Framework\Component\ComponentRegistrar;
9+
10+
ComponentRegistrar::register(
11+
ComponentRegistrar::MODULE,
12+
'Magento_LoginAsCustomerPageCache',
13+
__DIR__
14+
);

app/code/Magento/LoginAsCustomerUi/Model/PageCache/ConfigPlugin.php

Lines changed: 0 additions & 67 deletions
This file was deleted.

0 commit comments

Comments
 (0)