From c1e806fa653714870fb0bd44a30e84e64bc75602 Mon Sep 17 00:00:00 2001 From: Sergii Kovalenko Date: Wed, 30 Nov 2016 17:47:26 +0200 Subject: [PATCH 1/8] MAGETWO-60890: Fatal error logging in as admin user with restricted role --- .../App/Config/Source/RuntimeConfigSource.php | 26 +++--- .../Magento/Store/App/Config/Type/Scopes.php | 7 +- .../Store/Model/Config/Processor/Fallback.php | 84 +++++++++++++++---- .../Magento/Store/Model/GroupRepository.php | 12 +-- .../Store/Model/ResourceModel/Store.php | 14 ++++ .../Store/Model/ResourceModel/Website.php | 14 ++++ .../Magento/Store/Model/StoreRepository.php | 9 +- .../Magento/Store/Model/WebsiteRepository.php | 12 +-- 8 files changed, 120 insertions(+), 58 deletions(-) diff --git a/app/code/Magento/Store/App/Config/Source/RuntimeConfigSource.php b/app/code/Magento/Store/App/Config/Source/RuntimeConfigSource.php index 015ba1d6ef633..e3672410e9610 100644 --- a/app/code/Magento/Store/App/Config/Source/RuntimeConfigSource.php +++ b/app/code/Magento/Store/App/Config/Source/RuntimeConfigSource.php @@ -100,13 +100,13 @@ public function get($path = '') if ($this->canUseDatabase()) { switch ($scopePool) { case 'websites': - $data = $this->getWebsitesData($scopeCode); + $data['websites'] = $this->getWebsitesData($scopeCode); break; case 'groups': - $data = $this->getGroupsData($scopeCode); + $data['groups'] = $this->getGroupsData($scopeCode); break; case 'stores': - $data = $this->getStoresData($scopeCode); + $data['stores'] = $this->getStoresData($scopeCode); break; default: $data = [ @@ -127,10 +127,10 @@ public function get($path = '') */ private function getWebsitesData($code = null) { - if ($code) { + if ($code !== null) { $website = $this->websiteFactory->create(); $website->load($code); - $data = $website->getData(); + $data[$code] = $website->getData(); } else { $collection = $this->websiteCollectionFactory->create(); $collection->setLoadDefault(true); @@ -148,10 +148,10 @@ private function getWebsitesData($code = null) */ private function getGroupsData($id = null) { - if ($id) { + if ($id !== null) { $group = $this->groupFactory->create(); $group->load($id); - $data = $group->getData(); + $data[$id] = $group->getData(); } else { $collection = $this->groupCollectionFactory->create(); $collection->setLoadDefault(true); @@ -169,10 +169,16 @@ private function getGroupsData($id = null) */ private function getStoresData($code = null) { - if ($code) { + if ($code !== null) { $store = $this->storeFactory->create(); - $store->load($code, 'code'); - $data = $store->getData(); + + if (is_numeric($code)) { + $store->load($code); + } else { + $store->load($code, 'code'); + } + + $data[$code] = $store->getData(); } else { $collection = $this->storeCollectionFactory->create(); $collection->setLoadDefault(true); diff --git a/app/code/Magento/Store/App/Config/Type/Scopes.php b/app/code/Magento/Store/App/Config/Type/Scopes.php index 1c9ac59442163..77bc3ed2ccd8a 100644 --- a/app/code/Magento/Store/App/Config/Type/Scopes.php +++ b/app/code/Magento/Store/App/Config/Type/Scopes.php @@ -36,6 +36,7 @@ public function __construct( ConfigSourceInterface $source ) { $this->source = $source; + $this->data = new DataObject(); } /** @@ -43,8 +44,8 @@ public function __construct( */ public function get($path = '') { - if (!$this->data) { - $this->data = new DataObject($this->source->get()); + if (!$this->data->getData($path)) { + $this->data->addData($this->source->get($path)); } return $this->data->getData($path); @@ -57,6 +58,6 @@ public function get($path = '') */ public function clean() { - $this->data = null; + $this->data = new DataObject(); } } diff --git a/app/code/Magento/Store/Model/Config/Processor/Fallback.php b/app/code/Magento/Store/Model/Config/Processor/Fallback.php index 612f0514e77c1..21ce17968646b 100644 --- a/app/code/Magento/Store/Model/Config/Processor/Fallback.php +++ b/app/code/Magento/Store/Model/Config/Processor/Fallback.php @@ -6,7 +6,15 @@ namespace Magento\Store\Model\Config\Processor; use Magento\Framework\App\Config\Spi\PostProcessorInterface; +use Magento\Framework\App\ResourceConnection; +use Magento\Store\Api\Data\StoreInterface; +use Magento\Store\Api\Data\WebsiteInterface; use Magento\Store\App\Config\Type\Scopes; +use Magento\Store\Model\ResourceModel\Store; +use Magento\Store\Model\ResourceModel\Store\AllStoresCollectionFactory; +use Magento\Store\Model\ResourceModel\Website; +use Magento\Store\Model\ResourceModel\Website\AllWebsitesCollection; +use Magento\Store\Model\ResourceModel\Website\AllWebsitesCollectionFactory; /** * Fallback throguh different scopes and merge them @@ -20,13 +28,43 @@ class Fallback implements PostProcessorInterface */ private $scopes; + /** + * @var ResourceConnection + */ + private $resourceConnection; + + /** + * @var array + */ + private $storeData = []; + + /** + * @var array + */ + private $websiteData = []; + /** + * @var Store + */ + private $storeResource; + /** + * @var Website + */ + private $websiteResource; + /** * Fallback constructor. * @param Scopes $scopes */ - public function __construct(Scopes $scopes) - { + public function __construct( + Scopes $scopes, + ResourceConnection $resourceConnection, + Store $storeResource, + Website $websiteResource + ) { $this->scopes = $scopes; + $this->resourceConnection = $resourceConnection; + $this->storeResource = $storeResource; + $this->websiteResource = $websiteResource; } /** @@ -34,6 +72,9 @@ public function __construct(Scopes $scopes) */ public function process(array $data) { + $this->storeData = $this->storeResource->readAllStores(); + $this->websiteData = $this->websiteResource->readAlllWebsites(); + $defaultConfig = isset($data['default']) ? $data['default'] : []; $result = [ 'default' => $defaultConfig, @@ -57,12 +98,15 @@ public function process(array $data) * @param array $websitesConfig * @return array */ - private function prepareWebsitesConfig(array $defaultConfig, array $websitesConfig) - { + private function prepareWebsitesConfig( + array $defaultConfig, + array $websitesConfig + ) { $result = []; - foreach ($this->scopes->get('websites') as $websiteData) { - $code = $websiteData['code']; - $id = $websiteData['website_id']; + /** @var WebsiteInterface $website */ + foreach ($this->websiteData as $website) { + $code = $website['code']; + $id = $website['website_id']; $websiteConfig = isset($websitesConfig[$code]) ? $websitesConfig[$code] : []; $result[$code] = array_replace_recursive($defaultConfig, $websiteConfig); $result[$id] = $result[$code]; @@ -78,15 +122,20 @@ private function prepareWebsitesConfig(array $defaultConfig, array $websitesConf * @param array $storesConfig * @return array */ - private function prepareStoresConfig(array $defaultConfig, array $websitesConfig, array $storesConfig) - { + private function prepareStoresConfig( + array $defaultConfig, + array $websitesConfig, + array $storesConfig + ) { $result = []; - foreach ($this->scopes->get('stores') as $storeData) { - $code = $storeData['code']; - $id = $storeData['store_id']; + + /** @var StoreInterface $store */ + foreach ($this->storeData as $store) { + $code = $store['code']; + $id = $store['store_id']; $websiteConfig = []; - if (isset($storeData['website_id'])) { - $websiteConfig = $this->getWebsiteConfig($websitesConfig, $storeData['website_id']); + if (isset($store['website_id'])) { + $websiteConfig = $this->getWebsiteConfig($websitesConfig, $store['website_id']); } $storeConfig = isset($storesConfig[$code]) ? $storesConfig[$code] : []; $result[$code] = array_replace_recursive($defaultConfig, $websiteConfig, $storeConfig); @@ -104,9 +153,10 @@ private function prepareStoresConfig(array $defaultConfig, array $websitesConfig */ private function getWebsiteConfig(array $websites, $id) { - foreach ($this->scopes->get('websites') as $websiteData) { - if ($websiteData['website_id'] == $id) { - $code = $websiteData['code']; + /** @var WebsiteInterface $website */ + foreach ($this->websiteData as $website) { + if ($website['website_id'] == $id) { + $code = $website['website_id']; return isset($websites[$code]) ? $websites[$code] : []; } } diff --git a/app/code/Magento/Store/Model/GroupRepository.php b/app/code/Magento/Store/Model/GroupRepository.php index dadcc6fb24e68..9443890333e59 100644 --- a/app/code/Magento/Store/Model/GroupRepository.php +++ b/app/code/Magento/Store/Model/GroupRepository.php @@ -62,18 +62,8 @@ public function get($id) return $this->entities[$id]; } - $groupData = []; - $groups = $this->getAppConfig()->get('scopes', 'groups', []); - if ($groups) { - foreach ($groups as $data) { - if (isset($data['group_id']) && $data['group_id'] == $id) { - $groupData = $data; - break; - } - } - } $group = $this->groupFactory->create([ - 'data' => $groupData + 'data' => $this->getAppConfig()->get('scopes', "groups/$id", []) ]); if (null === $group->getId()) { diff --git a/app/code/Magento/Store/Model/ResourceModel/Store.php b/app/code/Magento/Store/Model/ResourceModel/Store.php index 86bfc3e00b28f..d2d3302b0dbdb 100644 --- a/app/code/Magento/Store/Model/ResourceModel/Store.php +++ b/app/code/Magento/Store/Model/ResourceModel/Store.php @@ -155,6 +155,20 @@ protected function _changeGroup(\Magento\Framework\Model\AbstractModel $model) return $this; } + /** + * Read information about all stores + * + * @return array + */ + public function readAllStores() + { + $select = $this->getConnection() + ->select() + ->from($this->getTable('store')); + + return $this->getConnection()->fetchAll($select); + } + /** * Retrieve select object for load object data * diff --git a/app/code/Magento/Store/Model/ResourceModel/Website.php b/app/code/Magento/Store/Model/ResourceModel/Website.php index 47d71427c9b91..b38d90b753949 100644 --- a/app/code/Magento/Store/Model/ResourceModel/Website.php +++ b/app/code/Magento/Store/Model/ResourceModel/Website.php @@ -34,6 +34,20 @@ protected function _initUniqueFields() return $this; } + /** + * Read information about all websites + * + * @return array + */ + public function readAlllWebsites() + { + $select = $this->getConnection() + ->select() + ->from($this->getTable('store_website')); + + return $this->getConnection()->fetchAll($select); + } + /** * Validate website code before object save * diff --git a/app/code/Magento/Store/Model/StoreRepository.php b/app/code/Magento/Store/Model/StoreRepository.php index c9e7a0ebc9d31..d6b8170aaec26 100644 --- a/app/code/Magento/Store/Model/StoreRepository.php +++ b/app/code/Magento/Store/Model/StoreRepository.php @@ -102,14 +102,7 @@ public function getById($id) return $this->entitiesById[$id]; } - $storeData = []; - $stores = $this->getAppConfig()->get('scopes', "stores", []); - foreach ($stores as $data) { - if (isset($data['store_id']) && $data['store_id'] == $id) { - $storeData = $data; - break; - } - } + $storeData = $this->getAppConfig()->get('scopes', "stores/$id", []); $store = $this->storeFactory->create([ 'data' => $storeData ]); diff --git a/app/code/Magento/Store/Model/WebsiteRepository.php b/app/code/Magento/Store/Model/WebsiteRepository.php index dffcef921bc22..2fa752e37461a 100644 --- a/app/code/Magento/Store/Model/WebsiteRepository.php +++ b/app/code/Magento/Store/Model/WebsiteRepository.php @@ -94,14 +94,8 @@ public function getById($id) if (isset($this->entitiesById[$id])) { return $this->entitiesById[$id]; } - $websiteData = []; - $websites = $this->getAppConfig()->get('scopes', 'websites', []); - foreach ($websites as $data) { - if (isset($data['website_id']) && $data['website_id'] == $id) { - $websiteData = $data; - break; - } - } + + $websiteData = $this->getAppConfig()->get('scopes', "websites/$id", []); $website = $this->factory->create([ 'data' => $websiteData ]); @@ -187,7 +181,7 @@ private function getAppConfig() */ private function initDefaultWebsite() { - $websites = (array)$this->getAppConfig()->get('scopes', 'websites', []); + $websites = (array) $this->getAppConfig()->get('scopes', 'websites', []); foreach ($websites as $data) { if (isset($data['is_default']) && $data['is_default'] == 1) { if ($this->default) { From b568e838f2a8512285081cf0ee075eac4d1132b9 Mon Sep 17 00:00:00 2001 From: Sergii Kovalenko Date: Wed, 30 Nov 2016 19:04:48 +0200 Subject: [PATCH 2/8] MAGETWO-60890: Fatal error logging in as admin user with restricted role --- app/code/Magento/Store/App/Config/Type/Scopes.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Store/App/Config/Type/Scopes.php b/app/code/Magento/Store/App/Config/Type/Scopes.php index 77bc3ed2ccd8a..990a90422374f 100644 --- a/app/code/Magento/Store/App/Config/Type/Scopes.php +++ b/app/code/Magento/Store/App/Config/Type/Scopes.php @@ -44,7 +44,7 @@ public function __construct( */ public function get($path = '') { - if (!$this->data->getData($path)) { + if (!$this->data->getData($path) || empty($path)) { $this->data->addData($this->source->get($path)); } From d9e6833eb25f6c9b3d1390131c2e1637a3e9fefd Mon Sep 17 00:00:00 2001 From: Sergii Kovalenko Date: Wed, 30 Nov 2016 19:09:53 +0200 Subject: [PATCH 3/8] MAGETWO-60890: Fatal error logging in as admin user with restricted role --- app/code/Magento/Store/App/Config/Type/Scopes.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Store/App/Config/Type/Scopes.php b/app/code/Magento/Store/App/Config/Type/Scopes.php index 990a90422374f..8feed392a7e3b 100644 --- a/app/code/Magento/Store/App/Config/Type/Scopes.php +++ b/app/code/Magento/Store/App/Config/Type/Scopes.php @@ -44,7 +44,9 @@ public function __construct( */ public function get($path = '') { - if (!$this->data->getData($path) || empty($path)) { + $patchChunks = explode("/", $path); + + if (!$this->data->getData($path) || count($patchChunks) == 1) { $this->data->addData($this->source->get($path)); } From b93433dd7b9f1085d13455e8cc32614711e21922 Mon Sep 17 00:00:00 2001 From: Sergii Kovalenko Date: Thu, 1 Dec 2016 11:02:19 +0200 Subject: [PATCH 4/8] MAGETWO-60890: Fatal error logging in as admin user with restricted role --- .../App/Config/Source/RuntimeConfigSource.php | 6 ++ .../Store/Model/Config/Processor/Fallback.php | 19 ++---- .../Store/Model/ResourceModel/Store.php | 17 +++-- .../Store/Model/ResourceModel/Website.php | 19 ++++-- .../Config/Source/RuntimeConfigSourceTest.php | 65 ++++++++----------- 5 files changed, 63 insertions(+), 63 deletions(-) diff --git a/app/code/Magento/Store/App/Config/Source/RuntimeConfigSource.php b/app/code/Magento/Store/App/Config/Source/RuntimeConfigSource.php index e3672410e9610..3fbd6c3580557 100644 --- a/app/code/Magento/Store/App/Config/Source/RuntimeConfigSource.php +++ b/app/code/Magento/Store/App/Config/Source/RuntimeConfigSource.php @@ -7,9 +7,12 @@ use Magento\Framework\App\Config\ConfigSourceInterface; use Magento\Framework\App\DeploymentConfig; +use Magento\Store\Model\Group; use Magento\Store\Model\ResourceModel\Website\CollectionFactory as WebsiteCollectionFactory; use Magento\Store\Model\ResourceModel\Group\CollectionFactory as GroupCollectionFactory; use Magento\Store\Model\ResourceModel\Store\CollectionFactory as StoreCollectionFactory; +use Magento\Store\Model\Store; +use Magento\Store\Model\Website; use Magento\Store\Model\WebsiteFactory; use Magento\Store\Model\GroupFactory; use Magento\Store\Model\StoreFactory; @@ -135,6 +138,7 @@ private function getWebsitesData($code = null) $collection = $this->websiteCollectionFactory->create(); $collection->setLoadDefault(true); $data = []; + /** @var Website $website */ foreach ($collection as $website) { $data[$website->getCode()] = $website->getData(); } @@ -156,6 +160,7 @@ private function getGroupsData($id = null) $collection = $this->groupCollectionFactory->create(); $collection->setLoadDefault(true); $data = []; + /** @var Group $group */ foreach ($collection as $group) { $data[$group->getId()] = $group->getData(); } @@ -183,6 +188,7 @@ private function getStoresData($code = null) $collection = $this->storeCollectionFactory->create(); $collection->setLoadDefault(true); $data = []; + /** @var Store $store */ foreach ($collection as $store) { $data[$store->getCode()] = $store->getData(); } diff --git a/app/code/Magento/Store/Model/Config/Processor/Fallback.php b/app/code/Magento/Store/Model/Config/Processor/Fallback.php index 21ce17968646b..5499324f10afb 100644 --- a/app/code/Magento/Store/Model/Config/Processor/Fallback.php +++ b/app/code/Magento/Store/Model/Config/Processor/Fallback.php @@ -33,19 +33,11 @@ class Fallback implements PostProcessorInterface */ private $resourceConnection; - /** - * @var array - */ - private $storeData = []; - - /** - * @var array - */ - private $websiteData = []; /** * @var Store */ private $storeResource; + /** * @var Website */ @@ -72,9 +64,6 @@ public function __construct( */ public function process(array $data) { - $this->storeData = $this->storeResource->readAllStores(); - $this->websiteData = $this->websiteResource->readAlllWebsites(); - $defaultConfig = isset($data['default']) ? $data['default'] : []; $result = [ 'default' => $defaultConfig, @@ -104,7 +93,7 @@ private function prepareWebsitesConfig( ) { $result = []; /** @var WebsiteInterface $website */ - foreach ($this->websiteData as $website) { + foreach ($this->websiteResource->readAllWebsites() as $website) { $code = $website['code']; $id = $website['website_id']; $websiteConfig = isset($websitesConfig[$code]) ? $websitesConfig[$code] : []; @@ -130,7 +119,7 @@ private function prepareStoresConfig( $result = []; /** @var StoreInterface $store */ - foreach ($this->storeData as $store) { + foreach ($this->storeResource->readAllStores() as $store) { $code = $store['code']; $id = $store['store_id']; $websiteConfig = []; @@ -154,7 +143,7 @@ private function prepareStoresConfig( private function getWebsiteConfig(array $websites, $id) { /** @var WebsiteInterface $website */ - foreach ($this->websiteData as $website) { + foreach ($this->websiteResource->readAllWebsites() as $website) { if ($website['website_id'] == $id) { $code = $website['website_id']; return isset($websites[$code]) ? $websites[$code] : []; diff --git a/app/code/Magento/Store/Model/ResourceModel/Store.php b/app/code/Magento/Store/Model/ResourceModel/Store.php index d2d3302b0dbdb..fe9da054a5c6f 100644 --- a/app/code/Magento/Store/Model/ResourceModel/Store.php +++ b/app/code/Magento/Store/Model/ResourceModel/Store.php @@ -15,6 +15,11 @@ class Store extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb */ protected $configCache; + /** + * @var array + */ + private $storesCache; + /** * @param \Magento\Framework\Model\ResourceModel\Db\Context $context * @param \Magento\Framework\App\Cache\Type\Config $configCacheType @@ -162,11 +167,15 @@ protected function _changeGroup(\Magento\Framework\Model\AbstractModel $model) */ public function readAllStores() { - $select = $this->getConnection() - ->select() - ->from($this->getTable('store')); + if (!$this->storesCache) { + $select = $this->getConnection() + ->select() + ->from($this->getTable('store')); + + $this->storesCache = $this->getConnection()->fetchAll($select); + } - return $this->getConnection()->fetchAll($select); + return $this->storesCache; } /** diff --git a/app/code/Magento/Store/Model/ResourceModel/Website.php b/app/code/Magento/Store/Model/ResourceModel/Website.php index b38d90b753949..04e3ca70d5d1d 100644 --- a/app/code/Magento/Store/Model/ResourceModel/Website.php +++ b/app/code/Magento/Store/Model/ResourceModel/Website.php @@ -13,6 +13,11 @@ */ class Website extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb { + /** + * @var array + */ + private $websitesCache; + /** * Define main table * @@ -39,13 +44,17 @@ protected function _initUniqueFields() * * @return array */ - public function readAlllWebsites() + public function readAllWebsites() { - $select = $this->getConnection() - ->select() - ->from($this->getTable('store_website')); + if (!$this->websitesCache) { + $select = $this->getConnection() + ->select() + ->from($this->getTable('store_website')); + + $this->websitesCache = $this->getConnection()->fetchAll($select); + } - return $this->getConnection()->fetchAll($select); + return $this->websitesCache; } /** diff --git a/app/code/Magento/Store/Test/Unit/App/Config/Source/RuntimeConfigSourceTest.php b/app/code/Magento/Store/Test/Unit/App/Config/Source/RuntimeConfigSourceTest.php index 8b3ffeafd8b2b..72559e14d1b53 100644 --- a/app/code/Magento/Store/Test/Unit/App/Config/Source/RuntimeConfigSourceTest.php +++ b/app/code/Magento/Store/Test/Unit/App/Config/Source/RuntimeConfigSourceTest.php @@ -104,25 +104,22 @@ class RuntimeConfigSourceTest extends \PHPUnit_Framework_TestCase public function setUp() { $this->data = [ - 'group' => [ - 'code' => 'myGroup', - 'data' => [ + 'groups' => [ + '1' => [ 'name' => 'My Group', - 'group_id' => $this->data['group']['code'] - ] + 'group_id' => 1 + ], ], - 'website' => [ - 'code' => 'myWebsite', - 'data' => [ - 'name' => 'My Website', - 'website_code' => $this->data['website']['code'] + 'stores' => [ + 'myStore' => [ + 'name' => 'My Store', + 'code' => 'myStore' ] ], - 'store' => [ - 'code' => 'myStore', - 'data' => [ - 'name' => 'My Store', - 'store_code' => $this->data['store']['code'] + 'websites' => [ + 'myWebsite' => [ + 'name' => 'My Website', + 'code' => 'myWebsite' ] ], ]; @@ -209,26 +206,16 @@ private function getExpectedResult($path) { switch ($this->getScope($path)) { case 'websites': - $result = $this->data['website']['data']; + $result = ['websites' => $this->data['websites']]; break; case 'groups': - $result = $this->data['group']['data']; + $result = ['groups' => $this->data['groups']]; break; case 'stores': - $result = $this->data['store']['data']; + $result = ['stores' => $this->data['stores']]; break; default: - $result = [ - 'websites' => [ - $this->data['website']['code'] => $this->data['website']['data'] - ], - 'groups' => [ - $this->data['group']['code'] => $this->data['group']['data'] - ], - 'stores' => [ - $this->data['store']['code'] => $this->data['store']['data'] - ], - ]; + $result = $this->data; break; } return $result; @@ -244,7 +231,7 @@ private function prepareStores($path) ->willReturn($this->store); $this->store->expects($this->once()) ->method('load') - ->with($this->data['store']['code'], 'code') + ->with('myStore', 'code') ->willReturnSelf(); } else { $this->storeCollectionFactory->expects($this->once()) @@ -259,11 +246,11 @@ private function prepareStores($path) ->willReturn(new \ArrayIterator([$this->store])); $this->store->expects($this->once()) ->method('getCode') - ->willReturn($this->data['store']['code']); + ->willReturn('myStore'); } $this->store->expects($this->once()) ->method('getData') - ->willReturn($this->data['store']['data']); + ->willReturn($this->data['stores']['myStore']); } } @@ -277,7 +264,7 @@ private function prepareGroups($path) ->willReturn($this->group); $this->group->expects($this->once()) ->method('load') - ->with($this->data['group']['code']) + ->with($this->data['groups']['1']['group_id']) ->willReturnSelf(); } else { $this->groupCollectionFactory->expects($this->once()) @@ -292,11 +279,11 @@ private function prepareGroups($path) ->willReturn(new \ArrayIterator([$this->group])); $this->group->expects($this->once()) ->method('getId') - ->willReturn($this->data['group']['code']); + ->willReturn($this->data['groups']['1']['group_id']); } $this->group->expects($this->once()) ->method('getData') - ->willReturn($this->data['group']['data']); + ->willReturn($this->data['groups']['1']); } } @@ -310,7 +297,7 @@ private function prepareWebsites($path) ->willReturn($this->website); $this->website->expects($this->once()) ->method('load') - ->with($this->data['website']['code']) + ->with($this->data['websites']['myWebsite']['code']) ->willReturnSelf(); } else { $this->websiteCollectionFactory->expects($this->once()) @@ -325,11 +312,11 @@ private function prepareWebsites($path) ->willReturn(new \ArrayIterator([$this->website])); $this->website->expects($this->once()) ->method('getCode') - ->willReturn($this->data['website']['code']); + ->willReturn('myWebsite'); } $this->website->expects($this->once()) ->method('getData') - ->willReturn($this->data['website']['data']); + ->willReturn($this->data['websites']['myWebsite']); } } @@ -350,7 +337,7 @@ public function getDataProvider() { return [ ['websites/myWebsite'], - ['groups/myGroup'], + ['groups/1'], ['stores/myStore'], ['default'] ]; From b7247d46cc3ad239cdd334d0ebe9da5e13bc3a79 Mon Sep 17 00:00:00 2001 From: Sergii Kovalenko Date: Thu, 1 Dec 2016 11:29:09 +0200 Subject: [PATCH 5/8] MAGETWO-60890: Fatal error logging in as admin user with restricted role --- .../Magento/Store/Model/ResourceModel/Store.php | 17 ++++------------- .../Store/Model/ResourceModel/Website.php | 12 ++++-------- 2 files changed, 8 insertions(+), 21 deletions(-) diff --git a/app/code/Magento/Store/Model/ResourceModel/Store.php b/app/code/Magento/Store/Model/ResourceModel/Store.php index fe9da054a5c6f..d2d3302b0dbdb 100644 --- a/app/code/Magento/Store/Model/ResourceModel/Store.php +++ b/app/code/Magento/Store/Model/ResourceModel/Store.php @@ -15,11 +15,6 @@ class Store extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb */ protected $configCache; - /** - * @var array - */ - private $storesCache; - /** * @param \Magento\Framework\Model\ResourceModel\Db\Context $context * @param \Magento\Framework\App\Cache\Type\Config $configCacheType @@ -167,15 +162,11 @@ protected function _changeGroup(\Magento\Framework\Model\AbstractModel $model) */ public function readAllStores() { - if (!$this->storesCache) { - $select = $this->getConnection() - ->select() - ->from($this->getTable('store')); - - $this->storesCache = $this->getConnection()->fetchAll($select); - } + $select = $this->getConnection() + ->select() + ->from($this->getTable('store')); - return $this->storesCache; + return $this->getConnection()->fetchAll($select); } /** diff --git a/app/code/Magento/Store/Model/ResourceModel/Website.php b/app/code/Magento/Store/Model/ResourceModel/Website.php index 04e3ca70d5d1d..5095480aa09ce 100644 --- a/app/code/Magento/Store/Model/ResourceModel/Website.php +++ b/app/code/Magento/Store/Model/ResourceModel/Website.php @@ -46,15 +46,11 @@ protected function _initUniqueFields() */ public function readAllWebsites() { - if (!$this->websitesCache) { - $select = $this->getConnection() - ->select() - ->from($this->getTable('store_website')); + $select = $this->getConnection() + ->select() + ->from($this->getTable('store_website')); - $this->websitesCache = $this->getConnection()->fetchAll($select); - } - - return $this->websitesCache; + return $this->getConnection()->fetchAll($select); } /** From e26d38b3ad46c90c93c03b52643b93134b347c29 Mon Sep 17 00:00:00 2001 From: Sergii Kovalenko Date: Thu, 1 Dec 2016 11:36:34 +0200 Subject: [PATCH 6/8] MAGETWO-60890: Fatal error logging in as admin user with restricted role --- .../Store/Model/Config/Processor/Fallback.php | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/app/code/Magento/Store/Model/Config/Processor/Fallback.php b/app/code/Magento/Store/Model/Config/Processor/Fallback.php index 5499324f10afb..31730db84581c 100644 --- a/app/code/Magento/Store/Model/Config/Processor/Fallback.php +++ b/app/code/Magento/Store/Model/Config/Processor/Fallback.php @@ -33,11 +33,19 @@ class Fallback implements PostProcessorInterface */ private $resourceConnection; + /** + * @var array + */ + private $storeData = []; + + /** + * @var array + */ + private $websiteData = []; /** * @var Store */ private $storeResource; - /** * @var Website */ @@ -64,6 +72,9 @@ public function __construct( */ public function process(array $data) { + $this->storeData = $this->storeResource->readAllStores(); + $this->websiteData = $this->websiteResource->readAllWebsites(); + $defaultConfig = isset($data['default']) ? $data['default'] : []; $result = [ 'default' => $defaultConfig, @@ -92,8 +103,7 @@ private function prepareWebsitesConfig( array $websitesConfig ) { $result = []; - /** @var WebsiteInterface $website */ - foreach ($this->websiteResource->readAllWebsites() as $website) { + foreach ($this->websiteData as $website) { $code = $website['code']; $id = $website['website_id']; $websiteConfig = isset($websitesConfig[$code]) ? $websitesConfig[$code] : []; @@ -118,8 +128,7 @@ private function prepareStoresConfig( ) { $result = []; - /** @var StoreInterface $store */ - foreach ($this->storeResource->readAllStores() as $store) { + foreach ($this->storeData as $store) { $code = $store['code']; $id = $store['store_id']; $websiteConfig = []; @@ -142,8 +151,7 @@ private function prepareStoresConfig( */ private function getWebsiteConfig(array $websites, $id) { - /** @var WebsiteInterface $website */ - foreach ($this->websiteResource->readAllWebsites() as $website) { + foreach ($this->websiteData as $website) { if ($website['website_id'] == $id) { $code = $website['website_id']; return isset($websites[$code]) ? $websites[$code] : []; From 3a06d40c626047f447ef1090f53661586f656bdb Mon Sep 17 00:00:00 2001 From: Sergii Kovalenko Date: Thu, 1 Dec 2016 12:38:31 +0200 Subject: [PATCH 7/8] MAGETWO-60890: Fatal error logging in as admin user with restricted role --- .../Store/Model/Config/Processor/Fallback.php | 21 ++++++++++++++++--- .../App/Config/InitialConfigSource.php | 5 +---- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/app/code/Magento/Store/Model/Config/Processor/Fallback.php b/app/code/Magento/Store/Model/Config/Processor/Fallback.php index 31730db84581c..59b8020d6979b 100644 --- a/app/code/Magento/Store/Model/Config/Processor/Fallback.php +++ b/app/code/Magento/Store/Model/Config/Processor/Fallback.php @@ -6,6 +6,7 @@ namespace Magento\Store\Model\Config\Processor; use Magento\Framework\App\Config\Spi\PostProcessorInterface; +use Magento\Framework\App\DeploymentConfig; use Magento\Framework\App\ResourceConnection; use Magento\Store\Api\Data\StoreInterface; use Magento\Store\Api\Data\WebsiteInterface; @@ -42,15 +43,22 @@ class Fallback implements PostProcessorInterface * @var array */ private $websiteData = []; + /** * @var Store */ private $storeResource; + /** * @var Website */ private $websiteResource; + /** + * @var DeploymentConfig + */ + private $deploymentConfig; + /** * Fallback constructor. * @param Scopes $scopes @@ -59,12 +67,14 @@ public function __construct( Scopes $scopes, ResourceConnection $resourceConnection, Store $storeResource, - Website $websiteResource + Website $websiteResource, + DeploymentConfig $deploymentConfig ) { $this->scopes = $scopes; $this->resourceConnection = $resourceConnection; $this->storeResource = $storeResource; $this->websiteResource = $websiteResource; + $this->deploymentConfig = $deploymentConfig; } /** @@ -72,8 +82,13 @@ public function __construct( */ public function process(array $data) { - $this->storeData = $this->storeResource->readAllStores(); - $this->websiteData = $this->websiteResource->readAllWebsites(); + if ($this->deploymentConfig->isDbAvailable()) {//read only from db + $this->storeData = $this->storeResource->readAllStores(); + $this->websiteData = $this->websiteResource->readAllWebsites(); + } else { + $this->storeData = $this->scopes->get('stores'); + $this->websiteData = $this->scopes->get('websites'); + } $defaultConfig = isset($data['default']) ? $data['default'] : []; $result = [ diff --git a/lib/internal/Magento/Framework/App/Config/InitialConfigSource.php b/lib/internal/Magento/Framework/App/Config/InitialConfigSource.php index 1f75bef92914a..26e0e321eb4a8 100644 --- a/lib/internal/Magento/Framework/App/Config/InitialConfigSource.php +++ b/lib/internal/Magento/Framework/App/Config/InitialConfigSource.php @@ -48,9 +48,6 @@ public function __construct(Reader $reader, $configType, $fileKey) public function get($path = '') { $data = new DataObject($this->reader->load($this->fileKey)); - if ($path !== '' && $path !== null) { - $path = '/' . $path; - } - return $data->getData($this->configType . $path) ?: []; + return $data->getData($this->configType) ?: []; } } From 3faa42155ffa139ce7195f1ce0cddb778ec6a068 Mon Sep 17 00:00:00 2001 From: Sergii Kovalenko Date: Thu, 1 Dec 2016 12:48:19 +0200 Subject: [PATCH 8/8] MAGETWO-60890: Fatal error logging in as admin user with restricted role --- .../Framework/App/Test/Unit/Config/InitialConfigSourceTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/Magento/Framework/App/Test/Unit/Config/InitialConfigSourceTest.php b/lib/internal/Magento/Framework/App/Test/Unit/Config/InitialConfigSourceTest.php index e1b8d1e465141..b6e4dad32e7d1 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/Config/InitialConfigSourceTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/Config/InitialConfigSourceTest.php @@ -49,6 +49,6 @@ public function testGet() ->method('load') ->with($this->fileKey) ->willReturn([$this->configType => [$path => 'value']]); - $this->assertEquals('value', $this->source->get($path)); + $this->assertEquals([$path => 'value'], $this->source->get()); } }