Skip to content

Commit 5fec821

Browse files
tjwiebelldevpatil7
authored andcommitted
Frontname Whitelist (#19)
* [TASK] Added PWA area, Added setting to disable or enable storeview per store, Added setting to skip certain routers and load from default magento frontend, Dynamically set _SERVER variables for pwa application * [BUGFIX] Explode delimiter fix for excluded routes * [BUGFIX] https fix * - Cherrypick frontname whitelist code from Experius branch * - Handle empty config value - Skip evaluation of empty front name - Remove unneeded adminhtml controller preferece * Add missing copyright * - Use linebreak constant - Write unit test for new functionality * Roll back usage of constant since Magento saves Textarea values this way * Missing linebreak and EOF * Fix quote usage and run phpcs * Address PR feedback
1 parent a24336c commit 5fec821

File tree

7 files changed

+162
-6
lines changed

7 files changed

+162
-6
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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\UpwardConnector\Plugin\Magento\Framework\App;
9+
10+
use Magento\Framework\App\Config\ScopeConfigInterface;
11+
use Magento\Store\Model\ScopeInterface;
12+
13+
class AreaList
14+
{
15+
/**
16+
* @var ScopeConfigInterface
17+
*/
18+
private $scopeConfig;
19+
20+
/**
21+
* Controller or frontname to load from default magento frontend
22+
*/
23+
const UPWARD_CONFIG_PATH_FRONT_NAMES_TO_SKIP = 'web/upward/front_names_to_skip';
24+
25+
/**
26+
* @param ScopeConfigInterface $scopeConfig
27+
*/
28+
public function __construct(ScopeConfigInterface $scopeConfig)
29+
{
30+
$this->scopeConfig = $scopeConfig;
31+
}
32+
33+
/**
34+
* @param \Magento\Framework\App\AreaList $subject
35+
* @param string|null $result
36+
* @param string $frontName
37+
*
38+
* @return string|null
39+
*/
40+
public function afterGetCodeByFrontName(
41+
\Magento\Framework\App\AreaList $subject,
42+
$result,
43+
$frontName
44+
) {
45+
46+
if ($result != 'frontend') {
47+
return $result;
48+
}
49+
50+
$frontNamesToSkip = explode(
51+
"\r\n",
52+
$this->scopeConfig->getValue(
53+
self::UPWARD_CONFIG_PATH_FRONT_NAMES_TO_SKIP,
54+
ScopeInterface::SCOPE_STORE
55+
) ?? ''
56+
);
57+
58+
if ($frontName && in_array($frontName, $frontNamesToSkip)) {
59+
return $result;
60+
}
61+
62+
return 'pwa';
63+
}
64+
}

Test/Unit/Controller/UpwardControllerFactoryTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,5 @@ public function testCreateWillThrow()
7070
$this->expectException(\RuntimeException::class);
7171
$this->expectExceptionMessage('Path to UPWARD configuration file not set.');
7272
$this->upwardControllerFactory->create($requestMock);
73-
7473
}
7574
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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\UpwardConnector\Test\Unit\Magento\Framework\App;
9+
10+
use Magento\Framework\App\Config\ScopeConfigInterface;
11+
use Magento\UpwardConnector\Plugin\Magento\Framework\App\AreaList as AreaListPlugin;
12+
13+
class AreaListTest extends \PHPUnit\Framework\TestCase
14+
{
15+
/**
16+
* @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
17+
*/
18+
private $scopeConfig;
19+
20+
/**
21+
* \Magento\Framework\App\AreaList|\PHPUnit_Framework_MockObject_MockObject
22+
*/
23+
private $areaListMock;
24+
25+
/**
26+
* @var AreaListPlugin
27+
*/
28+
private $areaListPlugin;
29+
30+
protected function setUp()
31+
{
32+
$this->scopeConfig = $this->createMock(ScopeConfigInterface::class);
33+
$this->areaListMock = $this->createMock(\Magento\Framework\App\AreaList::class);
34+
35+
$this->scopeConfig->expects($this->any())->method('getValue')->willReturn(
36+
"foo\r\nbar"
37+
);
38+
39+
$objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
40+
$this->areaListPlugin = $objectManagerHelper->getObject(
41+
AreaListPlugin::class,
42+
['scopeConfig' => $this->scopeConfig]
43+
);
44+
}
45+
46+
/**
47+
* @param string $resultParam
48+
* @param string $frontNameParam
49+
* @param string $expected
50+
*
51+
* @dataProvider afterGetCodeByFrontNameDataProvider
52+
*/
53+
public function testAfterGetCodeByFrontName(string $resultParam, string $frontNameParam, string $expected)
54+
{
55+
$result = $this->areaListPlugin->afterGetCodeByFrontName(
56+
$this->areaListMock,
57+
$resultParam,
58+
$frontNameParam
59+
);
60+
61+
$this->assertEquals($expected, $result);
62+
}
63+
64+
/**
65+
* @return []
66+
*/
67+
public function afterGetCodeByFrontNameDataProvider()
68+
{
69+
return [
70+
'Adminhtml area passes through' => ['adminhtml', '', 'adminhtml'],
71+
'Frontend area w/o frontname goes to UPWARD' => ['frontend', '', 'pwa'],
72+
'Frontend area w/o frontname in whitelist goes to UPWARD' => ['frontend', 'baz', 'pwa'],
73+
'Frontend area with frontname in whitelist passes through' => ['frontend', 'foo', 'frontend']
74+
];
75+
}
76+
}

etc/adminhtml/system.xml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@
1111
<section id="web">
1212
<group id="upward" translate="label" type="text" sortOrder="300"
1313
showInDefault="1" showInWebsite="0" showInStore="0">
14-
<label>UPWARD Configuration</label>
14+
<label>UPWARD PWA Configuration</label>
1515
<field id="config_path" type="text" translate="label,comment"
16-
sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0">
16+
sortOrder="20" showInDefault="1" showInWebsite="0" showInStore="0">
1717
<label>UPWARD Config File</label>
1818
<config_path>web/upward/path</config_path>
1919
<comment>Server path to YAML configuration file for UPWARD</comment>
2020
</field>
21+
<field id="front_names_to_skip" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="30" translate="label,comment" type="textarea">
22+
<label>Front Name Whitelist</label>
23+
<comment>Line break separated list of URL's that will load the default Magento frontend.</comment>
24+
</field>
2125
</group>
2226
</section>
2327
</system>

etc/di.xml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,14 @@
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
99
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
10-
<preference for="Magento\Framework\App\FrontControllerInterface" type="Magento\UpwardConnector\Controller\Upward" />
10+
<type name="Magento\Framework\App\AreaList">
11+
<arguments>
12+
<argument name="areas" xsi:type="array">
13+
<item name="pwa" xsi:type="array">
14+
<item name="frontName" xsi:type="string">pwa</item>
15+
</item>
16+
</argument>
17+
</arguments>
18+
<plugin name="upward_connector_app_areaList" sortOrder="10" type="Magento\UpwardConnector\Plugin\Magento\Framework\App\AreaList" />
19+
</type>
1120
</config>

etc/module.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,9 @@
66
*/
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
9-
<module name="Magento_UpwardConnector" />
9+
<module name="Magento_UpwardConnector">
10+
<sequence>
11+
<module name="Magento_Framework"/>
12+
</sequence>
13+
</module>
1014
</config>

etc/adminhtml/di.xml renamed to etc/pwa/di.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
99
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
10-
<preference for="Magento\Framework\App\FrontControllerInterface" type="Magento\Framework\App\FrontController" />
10+
<preference for="Magento\Framework\App\FrontControllerInterface" type="Magento\UpwardConnector\Controller\Upward" />
1111
</config>

0 commit comments

Comments
 (0)