Skip to content

Commit 5c0ee0f

Browse files
authored
Merge pull request #643 from magento-south/MAGETWO-60890
[South] Magetwo 60890: Fatal error logging in as admin user with restricted role
2 parents 78edba3 + 3faa421 commit 5c0ee0f

File tree

11 files changed

+173
-102
lines changed

11 files changed

+173
-102
lines changed

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

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77

88
use Magento\Framework\App\Config\ConfigSourceInterface;
99
use Magento\Framework\App\DeploymentConfig;
10+
use Magento\Store\Model\Group;
1011
use Magento\Store\Model\ResourceModel\Website\CollectionFactory as WebsiteCollectionFactory;
1112
use Magento\Store\Model\ResourceModel\Group\CollectionFactory as GroupCollectionFactory;
1213
use Magento\Store\Model\ResourceModel\Store\CollectionFactory as StoreCollectionFactory;
14+
use Magento\Store\Model\Store;
15+
use Magento\Store\Model\Website;
1316
use Magento\Store\Model\WebsiteFactory;
1417
use Magento\Store\Model\GroupFactory;
1518
use Magento\Store\Model\StoreFactory;
@@ -100,13 +103,13 @@ public function get($path = '')
100103
if ($this->canUseDatabase()) {
101104
switch ($scopePool) {
102105
case 'websites':
103-
$data = $this->getWebsitesData($scopeCode);
106+
$data['websites'] = $this->getWebsitesData($scopeCode);
104107
break;
105108
case 'groups':
106-
$data = $this->getGroupsData($scopeCode);
109+
$data['groups'] = $this->getGroupsData($scopeCode);
107110
break;
108111
case 'stores':
109-
$data = $this->getStoresData($scopeCode);
112+
$data['stores'] = $this->getStoresData($scopeCode);
110113
break;
111114
default:
112115
$data = [
@@ -127,14 +130,15 @@ public function get($path = '')
127130
*/
128131
private function getWebsitesData($code = null)
129132
{
130-
if ($code) {
133+
if ($code !== null) {
131134
$website = $this->websiteFactory->create();
132135
$website->load($code);
133-
$data = $website->getData();
136+
$data[$code] = $website->getData();
134137
} else {
135138
$collection = $this->websiteCollectionFactory->create();
136139
$collection->setLoadDefault(true);
137140
$data = [];
141+
/** @var Website $website */
138142
foreach ($collection as $website) {
139143
$data[$website->getCode()] = $website->getData();
140144
}
@@ -148,14 +152,15 @@ private function getWebsitesData($code = null)
148152
*/
149153
private function getGroupsData($id = null)
150154
{
151-
if ($id) {
155+
if ($id !== null) {
152156
$group = $this->groupFactory->create();
153157
$group->load($id);
154-
$data = $group->getData();
158+
$data[$id] = $group->getData();
155159
} else {
156160
$collection = $this->groupCollectionFactory->create();
157161
$collection->setLoadDefault(true);
158162
$data = [];
163+
/** @var Group $group */
159164
foreach ($collection as $group) {
160165
$data[$group->getId()] = $group->getData();
161166
}
@@ -169,14 +174,21 @@ private function getGroupsData($id = null)
169174
*/
170175
private function getStoresData($code = null)
171176
{
172-
if ($code) {
177+
if ($code !== null) {
173178
$store = $this->storeFactory->create();
174-
$store->load($code, 'code');
175-
$data = $store->getData();
179+
180+
if (is_numeric($code)) {
181+
$store->load($code);
182+
} else {
183+
$store->load($code, 'code');
184+
}
185+
186+
$data[$code] = $store->getData();
176187
} else {
177188
$collection = $this->storeCollectionFactory->create();
178189
$collection->setLoadDefault(true);
179190
$data = [];
191+
/** @var Store $store */
180192
foreach ($collection as $store) {
181193
$data[$store->getCode()] = $store->getData();
182194
}

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,18 @@ 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+
$patchChunks = explode("/", $path);
48+
49+
if (!$this->data->getData($path) || count($patchChunks) == 1) {
50+
$this->data->addData($this->source->get($path));
4851
}
4952

5053
return $this->data->getData($path);
@@ -57,6 +60,6 @@ public function get($path = '')
5760
*/
5861
public function clean()
5962
{
60-
$this->data = null;
63+
$this->data = new DataObject();
6164
}
6265
}

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

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

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

1120
/**
1221
* Fallback throguh different scopes and merge them
@@ -20,20 +29,67 @@ class Fallback implements PostProcessorInterface
2029
*/
2130
private $scopes;
2231

32+
/**
33+
* @var ResourceConnection
34+
*/
35+
private $resourceConnection;
36+
37+
/**
38+
* @var array
39+
*/
40+
private $storeData = [];
41+
42+
/**
43+
* @var array
44+
*/
45+
private $websiteData = [];
46+
47+
/**
48+
* @var Store
49+
*/
50+
private $storeResource;
51+
52+
/**
53+
* @var Website
54+
*/
55+
private $websiteResource;
56+
57+
/**
58+
* @var DeploymentConfig
59+
*/
60+
private $deploymentConfig;
61+
2362
/**
2463
* Fallback constructor.
2564
* @param Scopes $scopes
2665
*/
27-
public function __construct(Scopes $scopes)
28-
{
66+
public function __construct(
67+
Scopes $scopes,
68+
ResourceConnection $resourceConnection,
69+
Store $storeResource,
70+
Website $websiteResource,
71+
DeploymentConfig $deploymentConfig
72+
) {
2973
$this->scopes = $scopes;
74+
$this->resourceConnection = $resourceConnection;
75+
$this->storeResource = $storeResource;
76+
$this->websiteResource = $websiteResource;
77+
$this->deploymentConfig = $deploymentConfig;
3078
}
3179

3280
/**
3381
* @inheritdoc
3482
*/
3583
public function process(array $data)
3684
{
85+
if ($this->deploymentConfig->isDbAvailable()) {//read only from db
86+
$this->storeData = $this->storeResource->readAllStores();
87+
$this->websiteData = $this->websiteResource->readAllWebsites();
88+
} else {
89+
$this->storeData = $this->scopes->get('stores');
90+
$this->websiteData = $this->scopes->get('websites');
91+
}
92+
3793
$defaultConfig = isset($data['default']) ? $data['default'] : [];
3894
$result = [
3995
'default' => $defaultConfig,
@@ -57,12 +113,14 @@ public function process(array $data)
57113
* @param array $websitesConfig
58114
* @return array
59115
*/
60-
private function prepareWebsitesConfig(array $defaultConfig, array $websitesConfig)
61-
{
116+
private function prepareWebsitesConfig(
117+
array $defaultConfig,
118+
array $websitesConfig
119+
) {
62120
$result = [];
63-
foreach ($this->scopes->get('websites') as $websiteData) {
64-
$code = $websiteData['code'];
65-
$id = $websiteData['website_id'];
121+
foreach ($this->websiteData as $website) {
122+
$code = $website['code'];
123+
$id = $website['website_id'];
66124
$websiteConfig = isset($websitesConfig[$code]) ? $websitesConfig[$code] : [];
67125
$result[$code] = array_replace_recursive($defaultConfig, $websiteConfig);
68126
$result[$id] = $result[$code];
@@ -78,15 +136,19 @@ private function prepareWebsitesConfig(array $defaultConfig, array $websitesConf
78136
* @param array $storesConfig
79137
* @return array
80138
*/
81-
private function prepareStoresConfig(array $defaultConfig, array $websitesConfig, array $storesConfig)
82-
{
139+
private function prepareStoresConfig(
140+
array $defaultConfig,
141+
array $websitesConfig,
142+
array $storesConfig
143+
) {
83144
$result = [];
84-
foreach ($this->scopes->get('stores') as $storeData) {
85-
$code = $storeData['code'];
86-
$id = $storeData['store_id'];
145+
146+
foreach ($this->storeData as $store) {
147+
$code = $store['code'];
148+
$id = $store['store_id'];
87149
$websiteConfig = [];
88-
if (isset($storeData['website_id'])) {
89-
$websiteConfig = $this->getWebsiteConfig($websitesConfig, $storeData['website_id']);
150+
if (isset($store['website_id'])) {
151+
$websiteConfig = $this->getWebsiteConfig($websitesConfig, $store['website_id']);
90152
}
91153
$storeConfig = isset($storesConfig[$code]) ? $storesConfig[$code] : [];
92154
$result[$code] = array_replace_recursive($defaultConfig, $websiteConfig, $storeConfig);
@@ -104,9 +166,9 @@ private function prepareStoresConfig(array $defaultConfig, array $websitesConfig
104166
*/
105167
private function getWebsiteConfig(array $websites, $id)
106168
{
107-
foreach ($this->scopes->get('websites') as $websiteData) {
108-
if ($websiteData['website_id'] == $id) {
109-
$code = $websiteData['code'];
169+
foreach ($this->websiteData as $website) {
170+
if ($website['website_id'] == $id) {
171+
$code = $website['website_id'];
110172
return isset($websites[$code]) ? $websites[$code] : [];
111173
}
112174
}

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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
*/
1414
class Website extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
1515
{
16+
/**
17+
* @var array
18+
*/
19+
private $websitesCache;
20+
1621
/**
1722
* Define main table
1823
*
@@ -34,6 +39,20 @@ protected function _initUniqueFields()
3439
return $this;
3540
}
3641

42+
/**
43+
* Read information about all websites
44+
*
45+
* @return array
46+
*/
47+
public function readAllWebsites()
48+
{
49+
$select = $this->getConnection()
50+
->select()
51+
->from($this->getTable('store_website'));
52+
53+
return $this->getConnection()->fetchAll($select);
54+
}
55+
3756
/**
3857
* Validate website code before object save
3958
*

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)