Skip to content

Commit 4f61556

Browse files
committed
Merge pull request #183 from magento-ogre/MAGETWO-45420-custom-Admin-URL-with-https-is-enabled-with-errors
[Ogres] Bugfixes
2 parents 5923c2b + 05993db commit 4f61556

File tree

17 files changed

+198
-51
lines changed

17 files changed

+198
-51
lines changed

app/code/Magento/Backend/App/Action/Plugin/Authentication.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ class Authentication
6363
*/
6464
protected $resultRedirectFactory;
6565

66+
/**
67+
* @var \Magento\Framework\Data\Form\FormKey\Validator
68+
*/
69+
protected $formKeyValidator;
70+
6671
/**
6772
* @param \Magento\Backend\Model\Auth $auth
6873
* @param \Magento\Backend\Model\UrlInterface $url
@@ -72,6 +77,7 @@ class Authentication
7277
* @param \Magento\Backend\Model\UrlInterface $backendUrl
7378
* @param \Magento\Framework\Controller\Result\RedirectFactory $resultRedirectFactory
7479
* @param \Magento\Backend\App\BackendAppList $backendAppList
80+
* @param \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator
7581
*/
7682
public function __construct(
7783
\Magento\Backend\Model\Auth $auth,
@@ -81,7 +87,8 @@ public function __construct(
8187
\Magento\Framework\Message\ManagerInterface $messageManager,
8288
\Magento\Backend\Model\UrlInterface $backendUrl,
8389
\Magento\Framework\Controller\Result\RedirectFactory $resultRedirectFactory,
84-
\Magento\Backend\App\BackendAppList $backendAppList
90+
\Magento\Backend\App\BackendAppList $backendAppList,
91+
\Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator
8592
) {
8693
$this->_auth = $auth;
8794
$this->_url = $url;
@@ -91,11 +98,12 @@ public function __construct(
9198
$this->backendUrl = $backendUrl;
9299
$this->resultRedirectFactory = $resultRedirectFactory;
93100
$this->backendAppList = $backendAppList;
101+
$this->formKeyValidator = $formKeyValidator;
94102
}
95103

96104
/**
97105
* @param \Magento\Backend\App\AbstractAction $subject
98-
* @param callable $proceed
106+
* @param \Closure $proceed
99107
* @param \Magento\Framework\App\RequestInterface $request
100108
*
101109
* @return mixed
@@ -144,8 +152,17 @@ public function aroundDispatch(
144152
protected function _processNotLoggedInUser(\Magento\Framework\App\RequestInterface $request)
145153
{
146154
$isRedirectNeeded = false;
147-
if ($request->getPost('login') && $this->_performLogin($request)) {
148-
$isRedirectNeeded = $this->_redirectIfNeededAfterLogin($request);
155+
if ($request->getPost('login')) {
156+
if ($this->formKeyValidator->validate($request)) {
157+
if ($this->_performLogin($request)) {
158+
$isRedirectNeeded = $this->_redirectIfNeededAfterLogin($request);
159+
}
160+
} else {
161+
$this->_actionFlag->set('', \Magento\Framework\App\ActionInterface::FLAG_NO_DISPATCH, true);
162+
$this->_response->setRedirect($this->_url->getCurrentUrl());
163+
$this->messageManager->addError(__('Invalid Form Key. Please refresh the page.'));
164+
$isRedirectNeeded = true;
165+
}
149166
}
150167
if (!$isRedirectNeeded && !$request->isForwarded()) {
151168
if ($request->getParam('isIframe')) {

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ class FrontNameResolver implements \Magento\Framework\App\Area\FrontNameResolver
1919

2020
const XML_PATH_CUSTOM_ADMIN_PATH = 'admin/url/custom_path';
2121

22+
const XML_PATH_USE_CUSTOM_ADMIN_URL = 'admin/url/use_custom';
23+
24+
const XML_PATH_CUSTOM_ADMIN_URL = 'admin/url/custom';
25+
2226
/**
2327
* Backend area code
2428
*/
@@ -89,7 +93,11 @@ public function getFrontName($checkHost = false)
8993
*/
9094
public function isHostBackend()
9195
{
92-
$backendUrl = $this->scopeConfig->getValue(Store::XML_PATH_UNSECURE_BASE_URL, ScopeInterface::SCOPE_STORE);
96+
if ($this->scopeConfig->getValue(self::XML_PATH_USE_CUSTOM_ADMIN_URL, ScopeInterface::SCOPE_STORE)) {
97+
$backendUrl = $this->scopeConfig->getValue(self::XML_PATH_CUSTOM_ADMIN_URL, ScopeInterface::SCOPE_STORE);
98+
} else {
99+
$backendUrl = $this->scopeConfig->getValue(Store::XML_PATH_UNSECURE_BASE_URL, ScopeInterface::SCOPE_STORE);
100+
}
93101
$host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '';
94102
return stripos($this->getHostWithPort($backendUrl), $host) !== false;
95103
}

app/code/Magento/Backend/Block/Dashboard/Graph.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,16 @@ public function getChartUrl($directUrl = true)
214214
true
215215
);
216216

217-
$dateStart->setTimezone(new \DateTimeZone($timezoneLocal));
218-
$dateEnd->setTimezone(new \DateTimeZone($timezoneLocal));
217+
if ($this->getDataHelper()->getParam('period') == '24h') {
218+
$dateStart->setTimezone(new \DateTimeZone($timezoneLocal));
219+
$dateEnd->setTimezone(new \DateTimeZone($timezoneLocal));
220+
$dateEnd->modify('-1 hour');
221+
}
219222

220223
$dates = [];
221224
$datas = [];
222225

223-
while ($dateStart < $dateEnd) {
226+
while ($dateStart <= $dateEnd) {
224227
switch ($this->getDataHelper()->getParam('period')) {
225228
case '7d':
226229
case '1m':
@@ -398,7 +401,11 @@ public function getChartUrl($directUrl = true)
398401
break;
399402
case '7d':
400403
case '1m':
401-
$this->_axisLabels[$idx][$_index] = $this->_localeDate->formatDateTime($period);
404+
$this->_axisLabels[$idx][$_index] = $this->_localeDate->formatDateTime(
405+
$period,
406+
\IntlDateFormatter::SHORT,
407+
\IntlDateFormatter::NONE
408+
);
402409
break;
403410
case '1y':
404411
case '2y':

app/code/Magento/Backend/Controller/Adminhtml/Auth/Login.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,30 @@ public function execute()
3838
if ($this->_auth->getAuthStorage()->isFirstPageAfterLogin()) {
3939
$this->_auth->getAuthStorage()->setIsFirstPageAfterLogin(true);
4040
}
41-
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
42-
$resultRedirect = $this->resultRedirectFactory->create();
43-
$resultRedirect->setPath($this->_backendUrl->getStartupPageUrl());
44-
return $resultRedirect;
41+
return $this->getRedirect($this->_backendUrl->getStartupPageUrl());
4542
}
46-
return $this->resultPageFactory->create();
43+
44+
$requestUrl = $this->getRequest()->getUri();
45+
$backendUrl = $this->getUrl('*');
46+
// redirect according to rewrite rule
47+
if ($requestUrl != $backendUrl) {
48+
return $this->getRedirect($backendUrl);
49+
} else {
50+
return $this->resultPageFactory->create();
51+
}
52+
}
53+
54+
/**
55+
* Get redirect response
56+
*
57+
* @param string $path
58+
* @return \Magento\Backend\Model\View\Result\Redirect
59+
*/
60+
private function getRedirect($path)
61+
{
62+
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
63+
$resultRedirect = $this->resultRedirectFactory->create();
64+
$resultRedirect->setPath($path);
65+
return $resultRedirect;
4766
}
4867
}

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

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,37 @@ public function testIfCustomPathNotUsed()
8484
}
8585

8686
/**
87-
* @param $url
88-
* @param $host
87+
* @param string $url
88+
* @param string $host
89+
* @param string $useCustomAdminUrl
90+
* @param string $customAdminUrl
91+
* @param string $expectedValue
8992
* @dataProvider hostsDataProvider
9093
*/
91-
public function testIsHostBackend($url, $host, $expectedValue)
94+
public function testIsHostBackend($url, $host, $useCustomAdminUrl, $customAdminUrl, $expectedValue)
9295
{
93-
$backendUrl = $url;
9496
$_SERVER['HTTP_HOST'] = $host;
95-
$this->scopeConfigMock->expects($this->once())
97+
$this->scopeConfigMock->expects($this->exactly(2))
9698
->method('getValue')
97-
->with(Store::XML_PATH_UNSECURE_BASE_URL, ScopeInterface::SCOPE_STORE)
98-
->willReturn($backendUrl);
99+
->will(
100+
$this->returnValueMap(
101+
[
102+
[Store::XML_PATH_UNSECURE_BASE_URL, ScopeInterface::SCOPE_STORE, null, $url],
103+
[
104+
FrontNameResolver::XML_PATH_USE_CUSTOM_ADMIN_URL,
105+
ScopeInterface::SCOPE_STORE,
106+
null,
107+
$useCustomAdminUrl
108+
],
109+
[
110+
FrontNameResolver::XML_PATH_CUSTOM_ADMIN_URL,
111+
ScopeInterface::SCOPE_STORE,
112+
null,
113+
$customAdminUrl
114+
],
115+
]
116+
)
117+
);
99118
$this->assertEquals($this->model->isHostBackend(), $expectedValue);
100119
}
101120

@@ -105,31 +124,57 @@ public function hostsDataProvider()
105124
'withoutPort' => [
106125
'url' => 'http://magento2.loc/',
107126
'host' => 'magento2.loc',
127+
'useCustomAdminUrl' => '0',
128+
'customAdminUrl' => '',
108129
'expectedValue' => true
109130
],
110131
'withPort' => [
111132
'url' => 'http://magento2.loc:8080/',
112133
'host' => 'magento2.loc:8080',
134+
'useCustomAdminUrl' => '0',
135+
'customAdminUrl' => '',
113136
'expectedValue' => true
114137
],
115138
'withStandartPortInUrlWithoutPortInHost' => [
116139
'url' => 'http://magento2.loc:80/',
117140
'host' => 'magento2.loc',
141+
'useCustomAdminUrl' => '0',
142+
'customAdminUrl' => '',
118143
'expectedValue' => true
119144
],
120145
'withoutStandartPortInUrlWithPortInHost' => [
121146
'url' => 'https://magento2.loc/',
122147
'host' => 'magento2.loc:443',
148+
'useCustomAdminUrl' => '0',
149+
'customAdminUrl' => '',
123150
'expectedValue' => true
124151
],
125152
'differentHosts' => [
126153
'url' => 'http://m2.loc/',
127154
'host' => 'magento2.loc',
155+
'useCustomAdminUrl' => '0',
156+
'customAdminUrl' => '',
128157
'expectedValue' => false
129158
],
130159
'differentPortsOnOneHost' => [
131160
'url' => 'http://magento2.loc/',
132161
'host' => 'magento2.loc:8080',
162+
'useCustomAdminUrl' => '0',
163+
'customAdminUrl' => '',
164+
'expectedValue' => false
165+
],
166+
'withCustomAdminUrl' => [
167+
'url' => 'http://magento2.loc/',
168+
'host' => 'myhost.loc',
169+
'useCustomAdminUrl' => '1',
170+
'customAdminUrl' => 'https://myhost.loc/',
171+
'expectedValue' => true
172+
],
173+
'withCustomAdminUrlWrongHost' => [
174+
'url' => 'http://magento2.loc/',
175+
'host' => 'SomeOtherHost.loc',
176+
'useCustomAdminUrl' => '1',
177+
'customAdminUrl' => 'https://myhost.loc/',
133178
'expectedValue' => false
134179
]
135180
];

app/code/Magento/CatalogUrlRewrite/Model/CategoryUrlPathGenerator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function __construct(
6161
*/
6262
public function getUrlPath($category)
6363
{
64-
if ($category->getParentId() == Category::TREE_ROOT_ID) {
64+
if (in_array($category->getParentId(), [Category::ROOT_CATEGORY_ID, Category::TREE_ROOT_ID])) {
6565
return '';
6666
}
6767
$path = $category->getUrlPath();
@@ -87,7 +87,7 @@ public function getUrlPath($category)
8787
*/
8888
protected function isNeedToGenerateUrlPathForParent($category)
8989
{
90-
return $category->getLevel() >= self::MINIMAL_CATEGORY_LEVEL_FOR_PROCESSING;
90+
return $category->isObjectNew() || $category->getLevel() >= self::MINIMAL_CATEGORY_LEVEL_FOR_PROCESSING;
9191
}
9292

9393
/**

app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/CategoryUrlPathGeneratorTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ public function getUrlPathDataProvider()
9696
$noGenerationLevel = CategoryUrlPathGenerator::MINIMAL_CATEGORY_LEVEL_FOR_PROCESSING - 1;
9797
return [
9898
[Category::TREE_ROOT_ID, 'url-path', $noGenerationLevel, '', false, false, ''],
99-
['parent_id', 'url-path', $noGenerationLevel, '', false, false, 'url-path'],
100-
['parent_id', 'url-path', $noGenerationLevel, 'url-key', true, false, 'url-key'],
101-
['parent_id', 'url-path', $noGenerationLevel, 'url-key', false, true, 'url-key'],
99+
[13, 'url-path', $noGenerationLevel, '', false, false, 'url-path'],
100+
[13, 'url-path', $noGenerationLevel, 'url-key', true, false, 'url-key'],
101+
[13, 'url-path', $noGenerationLevel, 'url-key', false, true, 'url-key'],
102102
];
103103
}
104104

@@ -110,7 +110,7 @@ public function getUrlPathWithParentDataProvider()
110110
$requireGenerationLevel = CategoryUrlPathGenerator::MINIMAL_CATEGORY_LEVEL_FOR_PROCESSING;
111111
$noGenerationLevel = CategoryUrlPathGenerator::MINIMAL_CATEGORY_LEVEL_FOR_PROCESSING - 1;
112112
return [
113-
['url-key', false, $requireGenerationLevel, 'parent_id', 'parent-path', 'parent-path/url-key'],
113+
['url-key', false, $requireGenerationLevel, 13, 'parent-path', 'parent-path/url-key'],
114114
['url-key', false, $requireGenerationLevel, Category::TREE_ROOT_ID, null, 'url-key'],
115115
['url-key', true, $noGenerationLevel, Category::TREE_ROOT_ID, null, 'url-key'],
116116
];
@@ -136,7 +136,7 @@ public function testGetUrlPathWithParent(
136136
$urlPath = null;
137137
$parentLevel = CategoryUrlPathGenerator::MINIMAL_CATEGORY_LEVEL_FOR_PROCESSING - 1;
138138
$this->category->expects($this->any())->method('getParentId')
139-
->will($this->returnValue('parent_id'));
139+
->will($this->returnValue(13));
140140
$this->category->expects($this->any())->method('getLevel')
141141
->will($this->returnValue($level));
142142
$this->category->expects($this->any())->method('getUrlPath')->will($this->returnValue($urlPath));
@@ -152,7 +152,7 @@ public function testGetUrlPathWithParent(
152152
$parentCategory->expects($this->any())->method('dataHasChangedFor')
153153
->will($this->returnValueMap([['url_key', false], ['path_ids', false]]));
154154

155-
$this->categoryRepository->expects($this->any())->method('get')->with('parent_id')
155+
$this->categoryRepository->expects($this->any())->method('get')->with(13)
156156
->will($this->returnValue($parentCategory));
157157

158158
$this->assertEquals($result, $this->categoryUrlPathGenerator->getUrlPath($this->category));
@@ -205,7 +205,7 @@ public function testGetUrlPathWithSuffixWithoutStore()
205205
$result = 'url-path.html';
206206

207207
$this->category->expects($this->any())->method('getStoreId')->will($this->returnValue($storeId));
208-
$this->category->expects($this->once())->method('getParentId')->will($this->returnValue('parent_id'));
208+
$this->category->expects($this->once())->method('getParentId')->will($this->returnValue(2));
209209
$this->category->expects($this->once())->method('getUrlPath')->will($this->returnValue($urlPath));
210210
$this->category->expects($this->exactly(2))->method('dataHasChangedFor')
211211
->will($this->returnValueMap([['url_key', false], ['path_ids', false]]));

app/code/Magento/Rss/App/Action/Plugin/BackendAuthentication.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class BackendAuthentication extends \Magento\Backend\App\Action\Plugin\Authentic
4848
* @param \Magento\Backend\Model\UrlInterface $backendUrl
4949
* @param \Magento\Framework\Controller\Result\RedirectFactory $resultRedirectFactory
5050
* @param \Magento\Backend\App\BackendAppList $backendAppList
51+
* @param \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator
5152
* @param \Magento\Framework\HTTP\Authentication $httpAuthentication
5253
* @param \Psr\Log\LoggerInterface $logger
5354
* @param \Magento\Framework\AuthorizationInterface $authorization
@@ -63,6 +64,7 @@ public function __construct(
6364
\Magento\Backend\Model\UrlInterface $backendUrl,
6465
\Magento\Framework\Controller\Result\RedirectFactory $resultRedirectFactory,
6566
\Magento\Backend\App\BackendAppList $backendAppList,
67+
\Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator,
6668
\Magento\Framework\HTTP\Authentication $httpAuthentication,
6769
\Psr\Log\LoggerInterface $logger,
6870
\Magento\Framework\AuthorizationInterface $authorization,
@@ -80,7 +82,8 @@ public function __construct(
8082
$messageManager,
8183
$backendUrl,
8284
$resultRedirectFactory,
83-
$backendAppList
85+
$backendAppList,
86+
$formKeyValidator
8487
);
8588
}
8689

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@
223223
"Magento\\": "app/code/Magento/"
224224
},
225225
"psr-0": {
226-
"_empty_": "app/code/"
226+
"": "app/code/"
227227
},
228228
"files": [
229229
"app/etc/NonComposerComponentRegistration.php"

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dev/tests/functional/lib/Magento/Mtf/Util/Protocol/CurlTransport/BackendDecorator.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,16 @@ public function __construct(CurlTransport $transport, DataInterface $configurati
6363
*/
6464
protected function authorize()
6565
{
66+
// Perform GET to backend url so form_key is set
67+
$url = $_ENV['app_backend_url'];
68+
$this->transport->write($url, [], CurlInterface::GET);
69+
$this->read();
70+
6671
$url = $_ENV['app_backend_url'] . $this->configuration->get('application/0/backendLoginUrl/0/value');
6772
$data = [
6873
'login[username]' => $this->configuration->get('application/0/backendLogin/0/value'),
6974
'login[password]' => $this->configuration->get('application/0/backendPassword/0/value'),
75+
'form_key' => $this->formKey,
7076
];
7177
$this->transport->write($url, $data, CurlInterface::POST);
7278
$response = $this->read();

0 commit comments

Comments
 (0)