Skip to content

Commit c1e806f

Browse files
author
Sergii Kovalenko
committed
MAGETWO-60890: Fatal error logging in as admin user with restricted role
1 parent ec5a7f5 commit c1e806f

File tree

8 files changed

+120
-58
lines changed

8 files changed

+120
-58
lines changed

app/code/Magento/Store/App/Config/Source/RuntimeConfigSource.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,13 @@ public function get($path = '')
100100
if ($this->canUseDatabase()) {
101101
switch ($scopePool) {
102102
case 'websites':
103-
$data = $this->getWebsitesData($scopeCode);
103+
$data['websites'] = $this->getWebsitesData($scopeCode);
104104
break;
105105
case 'groups':
106-
$data = $this->getGroupsData($scopeCode);
106+
$data['groups'] = $this->getGroupsData($scopeCode);
107107
break;
108108
case 'stores':
109-
$data = $this->getStoresData($scopeCode);
109+
$data['stores'] = $this->getStoresData($scopeCode);
110110
break;
111111
default:
112112
$data = [
@@ -127,10 +127,10 @@ public function get($path = '')
127127
*/
128128
private function getWebsitesData($code = null)
129129
{
130-
if ($code) {
130+
if ($code !== null) {
131131
$website = $this->websiteFactory->create();
132132
$website->load($code);
133-
$data = $website->getData();
133+
$data[$code] = $website->getData();
134134
} else {
135135
$collection = $this->websiteCollectionFactory->create();
136136
$collection->setLoadDefault(true);
@@ -148,10 +148,10 @@ private function getWebsitesData($code = null)
148148
*/
149149
private function getGroupsData($id = null)
150150
{
151-
if ($id) {
151+
if ($id !== null) {
152152
$group = $this->groupFactory->create();
153153
$group->load($id);
154-
$data = $group->getData();
154+
$data[$id] = $group->getData();
155155
} else {
156156
$collection = $this->groupCollectionFactory->create();
157157
$collection->setLoadDefault(true);
@@ -169,10 +169,16 @@ private function getGroupsData($id = null)
169169
*/
170170
private function getStoresData($code = null)
171171
{
172-
if ($code) {
172+
if ($code !== null) {
173173
$store = $this->storeFactory->create();
174-
$store->load($code, 'code');
175-
$data = $store->getData();
174+
175+
if (is_numeric($code)) {
176+
$store->load($code);
177+
} else {
178+
$store->load($code, 'code');
179+
}
180+
181+
$data[$code] = $store->getData();
176182
} else {
177183
$collection = $this->storeCollectionFactory->create();
178184
$collection->setLoadDefault(true);

app/code/Magento/Store/App/Config/Type/Scopes.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,16 @@ public function __construct(
3636
ConfigSourceInterface $source
3737
) {
3838
$this->source = $source;
39+
$this->data = new DataObject();
3940
}
4041

4142
/**
4243
* @inheritdoc
4344
*/
4445
public function get($path = '')
4546
{
46-
if (!$this->data) {
47-
$this->data = new DataObject($this->source->get());
47+
if (!$this->data->getData($path)) {
48+
$this->data->addData($this->source->get($path));
4849
}
4950

5051
return $this->data->getData($path);
@@ -57,6 +58,6 @@ public function get($path = '')
5758
*/
5859
public function clean()
5960
{
60-
$this->data = null;
61+
$this->data = new DataObject();
6162
}
6263
}

app/code/Magento/Store/Model/Config/Processor/Fallback.php

Lines changed: 67 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,15 @@
66
namespace Magento\Store\Model\Config\Processor;
77

88
use Magento\Framework\App\Config\Spi\PostProcessorInterface;
9+
use Magento\Framework\App\ResourceConnection;
10+
use Magento\Store\Api\Data\StoreInterface;
11+
use Magento\Store\Api\Data\WebsiteInterface;
912
use Magento\Store\App\Config\Type\Scopes;
13+
use Magento\Store\Model\ResourceModel\Store;
14+
use Magento\Store\Model\ResourceModel\Store\AllStoresCollectionFactory;
15+
use Magento\Store\Model\ResourceModel\Website;
16+
use Magento\Store\Model\ResourceModel\Website\AllWebsitesCollection;
17+
use Magento\Store\Model\ResourceModel\Website\AllWebsitesCollectionFactory;
1018

1119
/**
1220
* Fallback throguh different scopes and merge them
@@ -20,20 +28,53 @@ class Fallback implements PostProcessorInterface
2028
*/
2129
private $scopes;
2230

31+
/**
32+
* @var ResourceConnection
33+
*/
34+
private $resourceConnection;
35+
36+
/**
37+
* @var array
38+
*/
39+
private $storeData = [];
40+
41+
/**
42+
* @var array
43+
*/
44+
private $websiteData = [];
45+
/**
46+
* @var Store
47+
*/
48+
private $storeResource;
49+
/**
50+
* @var Website
51+
*/
52+
private $websiteResource;
53+
2354
/**
2455
* Fallback constructor.
2556
* @param Scopes $scopes
2657
*/
27-
public function __construct(Scopes $scopes)
28-
{
58+
public function __construct(
59+
Scopes $scopes,
60+
ResourceConnection $resourceConnection,
61+
Store $storeResource,
62+
Website $websiteResource
63+
) {
2964
$this->scopes = $scopes;
65+
$this->resourceConnection = $resourceConnection;
66+
$this->storeResource = $storeResource;
67+
$this->websiteResource = $websiteResource;
3068
}
3169

3270
/**
3371
* @inheritdoc
3472
*/
3573
public function process(array $data)
3674
{
75+
$this->storeData = $this->storeResource->readAllStores();
76+
$this->websiteData = $this->websiteResource->readAlllWebsites();
77+
3778
$defaultConfig = isset($data['default']) ? $data['default'] : [];
3879
$result = [
3980
'default' => $defaultConfig,
@@ -57,12 +98,15 @@ public function process(array $data)
5798
* @param array $websitesConfig
5899
* @return array
59100
*/
60-
private function prepareWebsitesConfig(array $defaultConfig, array $websitesConfig)
61-
{
101+
private function prepareWebsitesConfig(
102+
array $defaultConfig,
103+
array $websitesConfig
104+
) {
62105
$result = [];
63-
foreach ($this->scopes->get('websites') as $websiteData) {
64-
$code = $websiteData['code'];
65-
$id = $websiteData['website_id'];
106+
/** @var WebsiteInterface $website */
107+
foreach ($this->websiteData as $website) {
108+
$code = $website['code'];
109+
$id = $website['website_id'];
66110
$websiteConfig = isset($websitesConfig[$code]) ? $websitesConfig[$code] : [];
67111
$result[$code] = array_replace_recursive($defaultConfig, $websiteConfig);
68112
$result[$id] = $result[$code];
@@ -78,15 +122,20 @@ private function prepareWebsitesConfig(array $defaultConfig, array $websitesConf
78122
* @param array $storesConfig
79123
* @return array
80124
*/
81-
private function prepareStoresConfig(array $defaultConfig, array $websitesConfig, array $storesConfig)
82-
{
125+
private function prepareStoresConfig(
126+
array $defaultConfig,
127+
array $websitesConfig,
128+
array $storesConfig
129+
) {
83130
$result = [];
84-
foreach ($this->scopes->get('stores') as $storeData) {
85-
$code = $storeData['code'];
86-
$id = $storeData['store_id'];
131+
132+
/** @var StoreInterface $store */
133+
foreach ($this->storeData as $store) {
134+
$code = $store['code'];
135+
$id = $store['store_id'];
87136
$websiteConfig = [];
88-
if (isset($storeData['website_id'])) {
89-
$websiteConfig = $this->getWebsiteConfig($websitesConfig, $storeData['website_id']);
137+
if (isset($store['website_id'])) {
138+
$websiteConfig = $this->getWebsiteConfig($websitesConfig, $store['website_id']);
90139
}
91140
$storeConfig = isset($storesConfig[$code]) ? $storesConfig[$code] : [];
92141
$result[$code] = array_replace_recursive($defaultConfig, $websiteConfig, $storeConfig);
@@ -104,9 +153,10 @@ private function prepareStoresConfig(array $defaultConfig, array $websitesConfig
104153
*/
105154
private function getWebsiteConfig(array $websites, $id)
106155
{
107-
foreach ($this->scopes->get('websites') as $websiteData) {
108-
if ($websiteData['website_id'] == $id) {
109-
$code = $websiteData['code'];
156+
/** @var WebsiteInterface $website */
157+
foreach ($this->websiteData as $website) {
158+
if ($website['website_id'] == $id) {
159+
$code = $website['website_id'];
110160
return isset($websites[$code]) ? $websites[$code] : [];
111161
}
112162
}

app/code/Magento/Store/Model/GroupRepository.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,8 @@ public function get($id)
6262
return $this->entities[$id];
6363
}
6464

65-
$groupData = [];
66-
$groups = $this->getAppConfig()->get('scopes', 'groups', []);
67-
if ($groups) {
68-
foreach ($groups as $data) {
69-
if (isset($data['group_id']) && $data['group_id'] == $id) {
70-
$groupData = $data;
71-
break;
72-
}
73-
}
74-
}
7565
$group = $this->groupFactory->create([
76-
'data' => $groupData
66+
'data' => $this->getAppConfig()->get('scopes', "groups/$id", [])
7767
]);
7868

7969
if (null === $group->getId()) {

app/code/Magento/Store/Model/ResourceModel/Store.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,20 @@ protected function _changeGroup(\Magento\Framework\Model\AbstractModel $model)
155155
return $this;
156156
}
157157

158+
/**
159+
* Read information about all stores
160+
*
161+
* @return array
162+
*/
163+
public function readAllStores()
164+
{
165+
$select = $this->getConnection()
166+
->select()
167+
->from($this->getTable('store'));
168+
169+
return $this->getConnection()->fetchAll($select);
170+
}
171+
158172
/**
159173
* Retrieve select object for load object data
160174
*

app/code/Magento/Store/Model/ResourceModel/Website.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,20 @@ protected function _initUniqueFields()
3434
return $this;
3535
}
3636

37+
/**
38+
* Read information about all websites
39+
*
40+
* @return array
41+
*/
42+
public function readAlllWebsites()
43+
{
44+
$select = $this->getConnection()
45+
->select()
46+
->from($this->getTable('store_website'));
47+
48+
return $this->getConnection()->fetchAll($select);
49+
}
50+
3751
/**
3852
* Validate website code before object save
3953
*

app/code/Magento/Store/Model/StoreRepository.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,7 @@ public function getById($id)
102102
return $this->entitiesById[$id];
103103
}
104104

105-
$storeData = [];
106-
$stores = $this->getAppConfig()->get('scopes', "stores", []);
107-
foreach ($stores as $data) {
108-
if (isset($data['store_id']) && $data['store_id'] == $id) {
109-
$storeData = $data;
110-
break;
111-
}
112-
}
105+
$storeData = $this->getAppConfig()->get('scopes', "stores/$id", []);
113106
$store = $this->storeFactory->create([
114107
'data' => $storeData
115108
]);

app/code/Magento/Store/Model/WebsiteRepository.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,8 @@ public function getById($id)
9494
if (isset($this->entitiesById[$id])) {
9595
return $this->entitiesById[$id];
9696
}
97-
$websiteData = [];
98-
$websites = $this->getAppConfig()->get('scopes', 'websites', []);
99-
foreach ($websites as $data) {
100-
if (isset($data['website_id']) && $data['website_id'] == $id) {
101-
$websiteData = $data;
102-
break;
103-
}
104-
}
97+
98+
$websiteData = $this->getAppConfig()->get('scopes', "websites/$id", []);
10599
$website = $this->factory->create([
106100
'data' => $websiteData
107101
]);
@@ -187,7 +181,7 @@ private function getAppConfig()
187181
*/
188182
private function initDefaultWebsite()
189183
{
190-
$websites = (array)$this->getAppConfig()->get('scopes', 'websites', []);
184+
$websites = (array) $this->getAppConfig()->get('scopes', 'websites', []);
191185
foreach ($websites as $data) {
192186
if (isset($data['is_default']) && $data['is_default'] == 1) {
193187
if ($this->default) {

0 commit comments

Comments
 (0)