Skip to content

Commit 6a6bf28

Browse files
author
OlgaVasyltsun
committed
MAGETWO-99796: [Backport for 2.1.x]User roles not correctly enforced for Stores->Settings->Configuration. Can change any configuration option with config_path
1 parent 82d9c19 commit 6a6bf28

File tree

3 files changed

+143
-1
lines changed

3 files changed

+143
-1
lines changed

app/code/Magento/Config/Controller/Adminhtml/System/AbstractConfig.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ protected function _isAllowed()
6868
{
6969
$sectionId = $this->_request->getParam('section');
7070
return parent::_isAllowed()
71-
&& $this->_configStructure->getElement($sectionId)->isAllowed();
71+
|| $this->_configStructure->getElement($sectionId)->isAllowed();
7272
}
7373

7474
/**

app/code/Magento/Config/Controller/Adminhtml/System/Config/Save.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,68 @@ public function __construct(
5555
$this->string = $string;
5656
}
5757

58+
/**
59+
* @inheritdoc
60+
*/
61+
protected function _isAllowed()
62+
{
63+
return parent::_isAllowed() && $this->isSectionAllowed();
64+
}
65+
66+
/**
67+
* Checks if user has access to section.
68+
*
69+
* @return bool
70+
*/
71+
private function isSectionAllowed()
72+
{
73+
$sectionId = $this->_request->getParam('section');
74+
$isAllowed = $this->_configStructure->getElement($sectionId)->isAllowed();
75+
if (!$isAllowed) {
76+
$groups = $this->getRequest()->getPost('groups');
77+
$fieldPath = $this->getFirstFieldPath($groups, $sectionId);
78+
79+
$fieldPaths = $this->_configStructure->getFieldPaths();
80+
$fieldPath = !empty($fieldPaths[$fieldPath][0]) ? $fieldPaths[$fieldPath][0] : $sectionId;
81+
$explodedConfigPath = explode('/', $fieldPath);
82+
$configSectionId = !empty($explodedConfigPath[0]) ? $explodedConfigPath[0] : $sectionId;
83+
84+
$isAllowed = $this->_configStructure->getElement($configSectionId)->isAllowed();
85+
}
86+
87+
return $isAllowed;
88+
}
89+
90+
/**
91+
* Return field path as string.
92+
*
93+
* @param array $elements
94+
* @param string $fieldPath
95+
* @return string
96+
*/
97+
private function getFirstFieldPath($elements, $fieldPath)
98+
{
99+
$groupData = [];
100+
foreach ($elements as $elementName => $element) {
101+
if (!empty($element)) {
102+
$fieldPath .= '/' . $elementName;
103+
104+
if (!empty($element['fields'])) {
105+
$groupData = $element['fields'];
106+
} elseif (!empty($element['groups'])) {
107+
$groupData = $element['groups'];
108+
}
109+
110+
if (!empty($groupData)) {
111+
$fieldPath = $this->getFirstFieldPath($groupData, $fieldPath);
112+
}
113+
break;
114+
}
115+
}
116+
117+
return $fieldPath;
118+
}
119+
58120
/**
59121
* Get groups for save
60122
*
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Paypal\Controller\Adminhtml\System;
8+
9+
use Magento\Framework\App\Config\ScopeConfigInterface;
10+
use Magento\Framework\App\Request\Http as HttpRequest;
11+
use Magento\TestFramework\Helper\Bootstrap;
12+
13+
/**
14+
* @magentoAppArea adminhtml
15+
*/
16+
class ConfigTest extends \Magento\TestFramework\TestCase\AbstractBackendController
17+
{
18+
/**
19+
* @magentoAppIsolation enabled
20+
* @magentoDbIsolation enabled
21+
*
22+
* @dataProvider saveMerchantCountryDataProvider
23+
*
24+
* @param string $section
25+
* @param array $groups
26+
* @return void
27+
*/
28+
public function testSaveMerchantCountry($section, $groups)
29+
{
30+
/** @var ScopeConfigInterface $scopeConfig */
31+
$scopeConfig = Bootstrap::getObjectManager()->get(ScopeConfigInterface::class);
32+
33+
$request = $this->getRequest();
34+
$request->setPostValue($groups)
35+
->setParam('section', $section)
36+
->setMethod(HttpRequest::METHOD_POST);
37+
38+
$this->dispatch('backend/admin/system_config/save');
39+
40+
$this->assertSessionMessages($this->equalTo(['You saved the configuration.']));
41+
42+
$this->assertEquals(
43+
'GB',
44+
$scopeConfig->getValue('paypal/general/merchant_country')
45+
);
46+
}
47+
48+
/**
49+
* @return array
50+
*/
51+
public function saveMerchantCountryDataProvider()
52+
{
53+
return [
54+
[
55+
'section' => 'paypal',
56+
'groups' => [
57+
'groups' => [
58+
'general' => [
59+
'fields' => [
60+
'merchant_country' => ['value' => 'GB'],
61+
],
62+
],
63+
],
64+
],
65+
],
66+
[
67+
'section' => 'payment',
68+
'groups' => [
69+
'groups' => [
70+
'account' => [
71+
'fields' => [
72+
'merchant_country' => ['value' => 'GB'],
73+
],
74+
],
75+
],
76+
],
77+
],
78+
];
79+
}
80+
}

0 commit comments

Comments
 (0)