Skip to content

Commit d9a9eb4

Browse files
author
Alex Paliarush
committed
MAGETWO-69015: Cannot login as admin user with limited user role
1 parent 86794cb commit d9a9eb4

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
@@ -34,15 +34,18 @@ public function __construct(
3434
ConfigSourceInterface $source
3535
) {
3636
$this->source = $source;
37+
$this->data = new DataObject();
3738
}
3839

3940
/**
4041
* @inheritdoc
4142
*/
4243
public function get($path = '')
4344
{
44-
if (!$this->data) {
45-
$this->data = new DataObject($this->source->get());
45+
$patchChunks = explode("/", $path);
46+
47+
if (!$this->data->getData($path) || count($patchChunks) == 1) {
48+
$this->data->addData($this->source->get($path));
4649
}
4750

4851
return $this->data->getData($path);
@@ -55,6 +58,6 @@ public function get($path = '')
5558
*/
5659
public function clean()
5760
{
58-
$this->data = null;
61+
$this->data = new DataObject();
5962
}
6063
}

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 through different scopes and merge them
@@ -18,20 +27,67 @@ class Fallback implements PostProcessorInterface
1827
*/
1928
private $scopes;
2029

30+
/**
31+
* @var ResourceConnection
32+
*/
33+
private $resourceConnection;
34+
35+
/**
36+
* @var array
37+
*/
38+
private $storeData = [];
39+
40+
/**
41+
* @var array
42+
*/
43+
private $websiteData = [];
44+
45+
/**
46+
* @var Store
47+
*/
48+
private $storeResource;
49+
50+
/**
51+
* @var Website
52+
*/
53+
private $websiteResource;
54+
55+
/**
56+
* @var DeploymentConfig
57+
*/
58+
private $deploymentConfig;
59+
2160
/**
2261
* Fallback constructor.
2362
* @param Scopes $scopes
2463
*/
25-
public function __construct(Scopes $scopes)
26-
{
64+
public function __construct(
65+
Scopes $scopes,
66+
ResourceConnection $resourceConnection,
67+
Store $storeResource,
68+
Website $websiteResource,
69+
DeploymentConfig $deploymentConfig
70+
) {
2771
$this->scopes = $scopes;
72+
$this->resourceConnection = $resourceConnection;
73+
$this->storeResource = $storeResource;
74+
$this->websiteResource = $websiteResource;
75+
$this->deploymentConfig = $deploymentConfig;
2876
}
2977

3078
/**
3179
* @inheritdoc
3280
*/
3381
public function process(array $data)
3482
{
83+
if ($this->deploymentConfig->isDbAvailable()) {//read only from db
84+
$this->storeData = $this->storeResource->readAllStores();
85+
$this->websiteData = $this->websiteResource->readAllWebsites();
86+
} else {
87+
$this->storeData = $this->scopes->get('stores');
88+
$this->websiteData = $this->scopes->get('websites');
89+
}
90+
3591
$defaultConfig = isset($data['default']) ? $data['default'] : [];
3692
$result = [
3793
'default' => $defaultConfig,
@@ -55,12 +111,14 @@ public function process(array $data)
55111
* @param array $websitesConfig
56112
* @return array
57113
*/
58-
private function prepareWebsitesConfig(array $defaultConfig, array $websitesConfig)
59-
{
114+
private function prepareWebsitesConfig(
115+
array $defaultConfig,
116+
array $websitesConfig
117+
) {
60118
$result = [];
61-
foreach ((array)$this->scopes->get('websites') as $websiteData) {
62-
$code = $websiteData['code'];
63-
$id = $websiteData['website_id'];
119+
foreach ((array)$this->websiteData as $website) {
120+
$code = $website['code'];
121+
$id = $website['website_id'];
64122
$websiteConfig = isset($websitesConfig[$code]) ? $websitesConfig[$code] : [];
65123
$result[$code] = array_replace_recursive($defaultConfig, $websiteConfig);
66124
$result[$id] = $result[$code];
@@ -76,15 +134,19 @@ private function prepareWebsitesConfig(array $defaultConfig, array $websitesConf
76134
* @param array $storesConfig
77135
* @return array
78136
*/
79-
private function prepareStoresConfig(array $defaultConfig, array $websitesConfig, array $storesConfig)
80-
{
137+
private function prepareStoresConfig(
138+
array $defaultConfig,
139+
array $websitesConfig,
140+
array $storesConfig
141+
) {
81142
$result = [];
82-
foreach ((array)$this->scopes->get('stores') as $storeData) {
83-
$code = $storeData['code'];
84-
$id = $storeData['store_id'];
143+
144+
foreach ((array)$this->storeData as $store) {
145+
$code = $store['code'];
146+
$id = $store['store_id'];
85147
$websiteConfig = [];
86-
if (isset($storeData['website_id'])) {
87-
$websiteConfig = $this->getWebsiteConfig($websitesConfig, $storeData['website_id']);
148+
if (isset($store['website_id'])) {
149+
$websiteConfig = $this->getWebsiteConfig($websitesConfig, $store['website_id']);
88150
}
89151
$storeConfig = isset($storesConfig[$code]) ? $storesConfig[$code] : [];
90152
$result[$code] = array_replace_recursive($defaultConfig, $websiteConfig, $storeConfig);
@@ -102,9 +164,9 @@ private function prepareStoresConfig(array $defaultConfig, array $websitesConfig
102164
*/
103165
private function getWebsiteConfig(array $websites, $id)
104166
{
105-
foreach ($this->scopes->get('websites') as $websiteData) {
106-
if ($websiteData['website_id'] == $id) {
107-
$code = $websiteData['code'];
167+
foreach ((array)$this->websiteData as $website) {
168+
if ($website['website_id'] == $id) {
169+
$code = $website['website_id'];
108170
return isset($websites[$code]) ? $websites[$code] : [];
109171
}
110172
}

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
@@ -157,6 +157,20 @@ protected function _changeGroup(\Magento\Framework\Model\AbstractModel $model)
157157
return $this;
158158
}
159159

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

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
*/
1616
class Website extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
1717
{
18+
/**
19+
* @var array
20+
*/
21+
private $websitesCache;
22+
1823
/**
1924
* Define main table
2025
*
@@ -36,6 +41,20 @@ protected function _initUniqueFields()
3641
return $this;
3742
}
3843

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

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

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

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

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,8 @@ public function getById($id)
9292
if (isset($this->entitiesById[$id])) {
9393
return $this->entitiesById[$id];
9494
}
95-
$websiteData = [];
96-
$websites = $this->getAppConfig()->get('scopes', 'websites', []);
97-
foreach ($websites as $data) {
98-
if (isset($data['website_id']) && $data['website_id'] == $id) {
99-
$websiteData = $data;
100-
break;
101-
}
102-
}
95+
96+
$websiteData = $this->getAppConfig()->get('scopes', "websites/$id", []);
10397
$website = $this->factory->create([
10498
'data' => $websiteData
10599
]);
@@ -185,7 +179,7 @@ private function getAppConfig()
185179
*/
186180
private function initDefaultWebsite()
187181
{
188-
$websites = (array)$this->getAppConfig()->get('scopes', 'websites', []);
182+
$websites = (array) $this->getAppConfig()->get('scopes', 'websites', []);
189183
foreach ($websites as $data) {
190184
if (isset($data['is_default']) && $data['is_default'] == 1) {
191185
if ($this->default) {

0 commit comments

Comments
 (0)