Skip to content

Commit f9d722d

Browse files
committed
MAGETWO-43691: Custom admin path does not work correctly
- refactor to do check for HTTP_HOST in one place - fix unit test
1 parent 809dfa9 commit f9d722d

File tree

6 files changed

+38
-34
lines changed

6 files changed

+38
-34
lines changed

app/code/Magento/Backend/App/Area/FrontNameResolver.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ class FrontNameResolver implements \Magento\Framework\App\Area\FrontNameResolver
3838
*/
3939
protected $deploymentConfig;
4040

41+
/**
42+
* @var \Magento\Backend\Model\UrlInterface
43+
*/
44+
protected $backendUrl;
45+
4146
/**
4247
* @param \Magento\Backend\App\Config $config
4348
* @param DeploymentConfig $deploymentConfig
@@ -56,21 +61,29 @@ public function __construct(
5661
/**
5762
* Retrieve area front name
5863
*
59-
* @param string $host If set, verify front name is valid for this url (hostname is correct)
60-
* @return string
64+
* @param bool $checkHost If true, verify front name is valid for this url (hostname is correct)
65+
* @return string|bool
6166
*/
62-
public function getFrontName($host = null)
67+
public function getFrontName($checkHost = false)
6368
{
64-
if ($host) {
65-
$bu = $this->backendUrl->getBaseUrl();
66-
if (stripos($bu, $host) === false) {
67-
return false;
68-
}
69+
if ($checkHost && !$this->isHostBackend()) {
70+
return false;
6971
}
7072
$isCustomPathUsed = (bool)(string)$this->config->getValue(self::XML_PATH_USE_CUSTOM_ADMIN_PATH);
7173
if ($isCustomPathUsed) {
7274
return (string)$this->config->getValue(self::XML_PATH_CUSTOM_ADMIN_PATH);
7375
}
7476
return $this->defaultFrontName;
7577
}
78+
79+
/**
80+
* Return whether the host from request is the backend host
81+
* @return bool
82+
*/
83+
public function isHostBackend()
84+
{
85+
$backendHost = parse_url(trim($this->backendUrl->getBaseUrl()), PHP_URL_HOST);
86+
$host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '';
87+
return (strcasecmp($backendHost, $host) === 0);
88+
}
7689
}

app/code/Magento/Backend/App/Router/NoRouteHandler.php

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,16 @@ class NoRouteHandler implements \Magento\Framework\App\Router\NoRouteHandlerInte
1919
*/
2020
protected $routeConfig;
2121

22-
/**
23-
* @var \Magento\Backend\Model\UrlInterface
24-
*/
25-
protected $backendUrl;
26-
2722
/**
2823
* @param \Magento\Backend\Helper\Data $helper
2924
* @param \Magento\Framework\App\Route\ConfigInterface $routeConfig
30-
* @param \Magento\Backend\Model\UrlInterface $backendUrl
3125
*/
3226
public function __construct(
3327
\Magento\Backend\Helper\Data $helper,
34-
\Magento\Framework\App\Route\ConfigInterface $routeConfig,
35-
\Magento\Backend\Model\UrlInterface $backendUrl
28+
\Magento\Framework\App\Route\ConfigInterface $routeConfig
3629
) {
3730
$this->helper = $helper;
3831
$this->routeConfig = $routeConfig;
39-
$this->backendUrl = $backendUrl;
4032
}
4133

4234
/**
@@ -50,15 +42,12 @@ public function process(\Magento\Framework\App\RequestInterface $request)
5042
$requestPathParams = explode('/', trim($request->getPathInfo(), '/'));
5143
$areaFrontName = array_shift($requestPathParams);
5244

53-
if ($areaFrontName == $this->helper->getAreaFrontName()) {
54-
$baseUrl = $this->backendUrl->getBaseUrl();
55-
if (!stripos($baseUrl, $_SERVER['HTTP_HOST']) === false) {
56-
$moduleName = $this->routeConfig->getRouteFrontName('adminhtml');
57-
$actionNamespace = 'noroute';
58-
$actionName = 'index';
59-
$request->setModuleName($moduleName)->setControllerName($actionNamespace)->setActionName($actionName);
60-
return true;
61-
}
45+
if ($areaFrontName === $this->helper->getAreaFrontName(true)) {
46+
$moduleName = $this->routeConfig->getRouteFrontName('adminhtml');
47+
$actionNamespace = 'noroute';
48+
$actionName = 'index';
49+
$request->setModuleName($moduleName)->setControllerName($actionNamespace)->setActionName($actionName);
50+
return true;
6251
}
6352
return false;
6453
}

app/code/Magento/Backend/Helper/Data.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,11 @@ public function getHomePageUrl()
198198
/**
199199
* Return Backend area front name
200200
*
201-
* @return string
201+
* bool $checkHost
202+
* @return bool|string
202203
*/
203-
public function getAreaFrontName()
204+
public function getAreaFrontName($checkHost = false)
204205
{
205-
return $this->_frontNameResolver->getFrontName();
206+
return $this->_frontNameResolver->getFrontName($checkHost);
206207
}
207208
}

app/code/Magento/Backend/Test/Unit/App/Area/FrontNameResolverTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ protected function setUp()
3333
->with(ConfigOptionsList::CONFIG_PATH_BACKEND_FRONTNAME)
3434
->will($this->returnValue($this->_defaultFrontName));
3535
$this->_configMock = $this->getMock('\Magento\Backend\App\Config', [], [], '', false);
36-
$this->_model = new FrontNameResolver($this->_configMock, $deploymentConfigMock);
36+
$backendUrlMock = $this->getMock('\Magento\Backend\Model\UrlInterface', [], [], '', false);
37+
$this->_model = new FrontNameResolver($this->_configMock, $deploymentConfigMock, $backendUrlMock);
3738
}
3839

3940
public function testIfCustomPathUsed()

lib/internal/Magento/Framework/App/Area/FrontNameResolverInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ interface FrontNameResolverInterface
1616
/**
1717
* Retrieve front name
1818
*
19-
* @param null|string $host If set, only return frontname if it is valid for the host
20-
* @return null|string
19+
* @param bool if true, only return frontname if it is valid for the host
20+
* @return string|bool
2121
*/
22-
public function getFrontName($host = null);
22+
public function getFrontName($checkHost = false);
2323
}

lib/internal/Magento/Framework/App/AreaList.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function getCodeByFrontName($frontName)
7070
foreach ($this->_areas as $areaCode => &$areaInfo) {
7171
if (!isset($areaInfo['frontName']) && isset($areaInfo['frontNameResolver'])) {
7272
$resolver = $this->_resolverFactory->create($areaInfo['frontNameResolver']);
73-
$areaInfo['frontName'] = $resolver->getFrontName($_SERVER['HTTP_HOST']);
73+
$areaInfo['frontName'] = $resolver->getFrontName(true);
7474
}
7575
if ($areaInfo['frontName'] == $frontName) {
7676
return $areaCode;

0 commit comments

Comments
 (0)