Skip to content

Commit

Permalink
MAGETWO-60890: Fatal error logging in as admin user with restricted role
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergii Kovalenko committed Nov 30, 2016
1 parent ec5a7f5 commit c1e806f
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 58 deletions.
26 changes: 16 additions & 10 deletions app/code/Magento/Store/App/Config/Source/RuntimeConfigSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
7 changes: 4 additions & 3 deletions app/code/Magento/Store/App/Config/Type/Scopes.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,16 @@ public function __construct(
ConfigSourceInterface $source
) {
$this->source = $source;
$this->data = new DataObject();
}

/**
* @inheritdoc
*/
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);
Expand All @@ -57,6 +58,6 @@ public function get($path = '')
*/
public function clean()
{
$this->data = null;
$this->data = new DataObject();
}
}
84 changes: 67 additions & 17 deletions app/code/Magento/Store/Model/Config/Processor/Fallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,20 +28,53 @@ 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;
}

/**
* @inheritdoc
*/
public function process(array $data)
{
$this->storeData = $this->storeResource->readAllStores();
$this->websiteData = $this->websiteResource->readAlllWebsites();

$defaultConfig = isset($data['default']) ? $data['default'] : [];
$result = [
'default' => $defaultConfig,
Expand All @@ -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];
Expand All @@ -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);
Expand All @@ -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] : [];
}
}
Expand Down
12 changes: 1 addition & 11 deletions app/code/Magento/Store/Model/GroupRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
14 changes: 14 additions & 0 deletions app/code/Magento/Store/Model/ResourceModel/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
14 changes: 14 additions & 0 deletions app/code/Magento/Store/Model/ResourceModel/Website.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
9 changes: 1 addition & 8 deletions app/code/Magento/Store/Model/StoreRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
]);
Expand Down
12 changes: 3 additions & 9 deletions app/code/Magento/Store/Model/WebsiteRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
]);
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit c1e806f

Please sign in to comment.