Skip to content

Commit c292d11

Browse files
authored
Merge pull request #4165 from magento-tsg/2.1.18-develop-pr73
[TSG] Backporting for 2.1 (pr73) (2.1.18-develop)
2 parents c78c130 + 7beee14 commit c292d11

File tree

19 files changed

+606
-27
lines changed

19 files changed

+606
-27
lines changed

app/code/Magento/Catalog/Model/Category.php

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
*/
66
namespace Magento\Catalog\Model;
77

8+
use Magento\Authorization\Model\UserContextInterface;
89
use Magento\Catalog\Api\CategoryRepositoryInterface;
910
use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator;
1011
use Magento\Framework\Api\AttributeValueFactory;
12+
use Magento\Framework\App\ObjectManager;
13+
use Magento\Framework\AuthorizationInterface;
1114
use Magento\Framework\Convert\ConvertArray;
1215
use Magento\Framework\Exception\NoSuchEntityException;
1316
use Magento\Framework\Profiler;
@@ -232,6 +235,16 @@ class Category extends \Magento\Catalog\Model\AbstractModel implements
232235
*/
233236
protected $metadataService;
234237

238+
/**
239+
* @var UserContextInterface
240+
*/
241+
private $userContext;
242+
243+
/**
244+
* @var AuthorizationInterface
245+
*/
246+
private $authorization;
247+
235248
/**
236249
* @param \Magento\Framework\Model\Context $context
237250
* @param \Magento\Framework\Registry $registry
@@ -254,6 +267,8 @@ class Category extends \Magento\Catalog\Model\AbstractModel implements
254267
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
255268
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
256269
* @param array $data
270+
* @param UserContextInterface|null $userContext
271+
* @param AuthorizationInterface|null $authorization
257272
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
258273
*/
259274
public function __construct(
@@ -277,7 +292,9 @@ public function __construct(
277292
CategoryRepositoryInterface $categoryRepository,
278293
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
279294
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
280-
array $data = []
295+
array $data = [],
296+
UserContextInterface $userContext = null,
297+
AuthorizationInterface $authorization = null
281298
) {
282299
$this->metadataService = $metadataService;
283300
$this->_treeModel = $categoryTreeResource;
@@ -302,6 +319,8 @@ public function __construct(
302319
$resourceCollection,
303320
$data
304321
);
322+
$this->userContext = $userContext ?: ObjectManager::getInstance()->get(UserContextInterface::class);
323+
$this->authorization = $authorization ?: ObjectManager::getInstance()->get(AuthorizationInterface::class);
305324
}
306325

307326
/**
@@ -313,10 +332,10 @@ protected function _construct()
313332
{
314333
// If Flat Index enabled then use it but only on frontend
315334
if ($this->flatState->isAvailable()) {
316-
$this->_init('Magento\Catalog\Model\ResourceModel\Category\Flat');
335+
$this->_init(\Magento\Catalog\Model\ResourceModel\Category\Flat::class);
317336
$this->_useFlatResource = true;
318337
} else {
319-
$this->_init('Magento\Catalog\Model\ResourceModel\Category');
338+
$this->_init(\Magento\Catalog\Model\ResourceModel\Category::class);
320339
}
321340
}
322341

@@ -916,6 +935,31 @@ public function beforeDelete()
916935
return parent::beforeDelete();
917936
}
918937

938+
/**
939+
* @inheritdoc
940+
*/
941+
public function beforeSave()
942+
{
943+
//Validate changing of design.
944+
$userType = $this->userContext->getUserType();
945+
if (($userType === UserContextInterface::USER_TYPE_ADMIN
946+
|| $userType === UserContextInterface::USER_TYPE_INTEGRATION)
947+
&& !$this->authorization->isAllowed('Magento_Catalog::edit_category_design')
948+
) {
949+
foreach ($this->_designAttributes as $attributeCode) {
950+
$this->setData($attributeCode, $value = $this->getOrigData($attributeCode));
951+
if (!empty($this->_data[self::CUSTOM_ATTRIBUTES])
952+
&& array_key_exists($attributeCode, $this->_data[self::CUSTOM_ATTRIBUTES])
953+
) {
954+
//In case custom attribute were used to update the entity.
955+
$this->_data[self::CUSTOM_ATTRIBUTES][$attributeCode]->setValue($value);
956+
}
957+
}
958+
}
959+
960+
return parent::beforeSave();
961+
}
962+
919963
/**
920964
* Retrieve anchors above
921965
*

app/code/Magento/Catalog/Model/Category/DataProvider.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111
use Magento\Eav\Model\Config;
1212
use Magento\Eav\Model\Entity\Type;
1313
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
14+
use Magento\Framework\App\ObjectManager;
1415
use Magento\Store\Model\Store;
1516
use Magento\Store\Model\StoreManagerInterface;
1617
use Magento\Ui\Component\Form\Field;
1718
use Magento\Ui\DataProvider\EavValidationRules;
1819
use Magento\Catalog\Model\CategoryFactory;
1920
use Magento\Framework\Exception\NoSuchEntityException;
2021
use Magento\Catalog\Model\Category\Attribute\Backend\Image as ImageBackendModel;
22+
use Magento\Framework\AuthorizationInterface;
2123

2224
/**
2325
* Class DataProvider
@@ -112,6 +114,11 @@ class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
112114
*/
113115
private $categoryFactory;
114116

117+
/**
118+
* @var AuthorizationInterface
119+
*/
120+
private $authorization;
121+
115122
/**
116123
* DataProvider constructor
117124
*
@@ -127,6 +134,7 @@ class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
127134
* @param CategoryFactory $categoryFactory
128135
* @param array $meta
129136
* @param array $data
137+
* @param AuthorizationInterface|null $authorization
130138
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
131139
*/
132140
public function __construct(
@@ -141,7 +149,8 @@ public function __construct(
141149
\Magento\Framework\App\RequestInterface $request,
142150
CategoryFactory $categoryFactory,
143151
array $meta = [],
144-
array $data = []
152+
array $data = [],
153+
AuthorizationInterface $authorization = null
145154
) {
146155
$this->eavValidationRules = $eavValidationRules;
147156
$this->collection = $categoryCollectionFactory->create();
@@ -151,6 +160,8 @@ public function __construct(
151160
$this->storeManager = $storeManager;
152161
$this->request = $request;
153162
$this->categoryFactory = $categoryFactory;
163+
$this->authorization = $authorization ?: ObjectManager::getInstance()->get(AuthorizationInterface::class);
164+
154165
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
155166
$this->meta = $this->prepareMeta($this->meta);
156167
}
@@ -180,11 +191,20 @@ public function prepareMeta($meta)
180191
*/
181192
private function prepareFieldsMeta($fieldsMap, $fieldsMeta)
182193
{
194+
$canEditDesign = $this->authorization->isAllowed('Magento_Catalog::edit_category_design');
195+
183196
$result = [];
184197
foreach ($fieldsMap as $fieldSet => $fields) {
185198
foreach ($fields as $field) {
186199
if (isset($fieldsMeta[$field])) {
187-
$result[$fieldSet]['children'][$field]['arguments']['data']['config'] = $fieldsMeta[$field];
200+
$config = $fieldsMeta[$field];
201+
if (($fieldSet === 'design' || $fieldSet === 'schedule_design_update') && !$canEditDesign) {
202+
$config['required'] = 1;
203+
$config['disabled'] = 1;
204+
$config['serviceDisabled'] = true;
205+
}
206+
207+
$result[$fieldSet]['children'][$field]['arguments']['data']['config'] = $config;
188208
}
189209
}
190210
}

app/code/Magento/Catalog/Model/Product.php

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
*/
66
namespace Magento\Catalog\Model;
77

8+
use Magento\Authorization\Model\UserContextInterface;
89
use Magento\Catalog\Api\CategoryRepositoryInterface;
910
use Magento\Catalog\Api\Data\ProductInterface;
1011
use Magento\Catalog\Api\ProductLinkRepositoryInterface;
1112
use Magento\Framework\Api\AttributeValueFactory;
1213
use Magento\Framework\App\Filesystem\DirectoryList;
14+
use Magento\Framework\App\ObjectManager;
15+
use Magento\Framework\AuthorizationInterface;
1316
use Magento\Framework\DataObject\IdentityInterface;
1417
use Magento\Framework\Pricing\SaleableInterface;
1518
use Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface;
@@ -347,6 +350,16 @@ class Product extends \Magento\Catalog\Model\AbstractModel implements
347350
*/
348351
protected $linkTypeProvider;
349352

353+
/**
354+
* @var UserContextInterface
355+
*/
356+
private $userContext;
357+
358+
/**
359+
* @var AuthorizationInterface
360+
*/
361+
private $authorization;
362+
350363
/**
351364
* Product constructor.
352365
* @param \Magento\Framework\Model\Context $context
@@ -384,6 +397,8 @@ class Product extends \Magento\Catalog\Model\AbstractModel implements
384397
* @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
385398
* @param \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $joinProcessor
386399
* @param array $data
400+
* @param UserContextInterface|null $userContext
401+
* @param AuthorizationInterface|null $authorization
387402
*
388403
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
389404
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
@@ -423,7 +438,9 @@ public function __construct(
423438
EntryConverterPool $mediaGalleryEntryConverterPool,
424439
\Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
425440
\Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $joinProcessor,
426-
array $data = []
441+
array $data = [],
442+
UserContextInterface $userContext = null,
443+
AuthorizationInterface $authorization = null
427444
) {
428445
$this->metadataService = $metadataService;
429446
$this->_itemOptionFactory = $itemOptionFactory;
@@ -462,6 +479,8 @@ public function __construct(
462479
$resourceCollection,
463480
$data
464481
);
482+
$this->userContext = $userContext ?: ObjectManager::getInstance()->get(UserContextInterface::class);
483+
$this->authorization = $authorization ?: ObjectManager::getInstance()->get(AuthorizationInterface::class);
465484
}
466485

467486
/**
@@ -471,7 +490,7 @@ public function __construct(
471490
*/
472491
protected function _construct()
473492
{
474-
$this->_init('Magento\Catalog\Model\ResourceModel\Product');
493+
$this->_init(\Magento\Catalog\Model\ResourceModel\Product::class);
475494
}
476495

477496
/**
@@ -852,6 +871,21 @@ public function beforeSave()
852871

853872
$this->getTypeInstance()->beforeSave($this);
854873

874+
//Validate changing of design.
875+
$userType = $this->userContext->getUserType();
876+
if (($userType === UserContextInterface::USER_TYPE_ADMIN
877+
|| $userType === UserContextInterface::USER_TYPE_INTEGRATION)
878+
&& !$this->authorization->isAllowed('Magento_Catalog::edit_product_design')
879+
) {
880+
$this->setData('custom_design', $this->getOrigData('custom_design'));
881+
$this->setData('page_layout', $this->getOrigData('page_layout'));
882+
$this->setData('options_container', $this->getOrigData('options_container'));
883+
$this->setData('custom_layout_update', $this->getOrigData('custom_layout_update'));
884+
$this->setData('custom_design_from', $this->getOrigData('custom_design_from'));
885+
$this->setData('custom_design_to', $this->getOrigData('custom_design_to'));
886+
$this->setData('custom_layout', $this->getOrigData('custom_layout'));
887+
}
888+
855889
$hasOptions = false;
856890
$hasRequiredOptions = false;
857891

app/code/Magento/Catalog/Model/ResourceModel/Category.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,7 @@ protected function _processPositions($category, $newParent, $afterCategoryId)
983983
if ($afterCategoryId) {
984984
$select = $connection->select()->from($table, 'position')->where('entity_id = :entity_id');
985985
$position = $connection->fetchOne($select, ['entity_id' => $afterCategoryId]);
986-
$position += 1;
986+
$position++;
987987
} else {
988988
$position = 1;
989989
}

app/code/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/Eav.php

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory as GroupCollectionFactory;
1919
use Magento\Framework\Api\SearchCriteriaBuilder;
2020
use Magento\Framework\Api\SortOrderBuilder;
21+
use Magento\Framework\App\ObjectManager;
2122
use Magento\Framework\App\Request\DataPersistorInterface;
2223
use Magento\Framework\App\RequestInterface;
24+
use Magento\Framework\AuthorizationInterface;
2325
use Magento\Framework\Filter\Translit;
2426
use Magento\Framework\Stdlib\ArrayManager;
2527
use Magento\Store\Model\StoreManagerInterface;
@@ -167,6 +169,26 @@ class Eav extends AbstractModifier
167169
*/
168170
private $localeCurrency;
169171

172+
/**
173+
* @var AuthorizationInterface
174+
*/
175+
private $authorization;
176+
177+
/**
178+
* Product design attribute codes.
179+
*
180+
* @var array
181+
*/
182+
private $designAttributeCodes = [
183+
'custom_design',
184+
'page_layout',
185+
'options_container',
186+
'custom_layout_update',
187+
'custom_design_from',
188+
'custom_design_to',
189+
'custom_layout',
190+
];
191+
170192
/**
171193
* @param LocatorInterface $locator
172194
* @param CatalogEavValidationRules $catalogEavValidationRules
@@ -187,6 +209,8 @@ class Eav extends AbstractModifier
187209
* @param DataPersistorInterface $dataPersistor
188210
* @param array $attributesToDisable
189211
* @param array $attributesToEliminate
212+
* @param AuthorizationInterface|null $authorization
213+
*
190214
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
191215
*/
192216
public function __construct(
@@ -208,7 +232,8 @@ public function __construct(
208232
ScopeOverriddenValue $scopeOverriddenValue,
209233
DataPersistorInterface $dataPersistor,
210234
$attributesToDisable = [],
211-
$attributesToEliminate = []
235+
$attributesToEliminate = [],
236+
AuthorizationInterface $authorization = null
212237
) {
213238
$this->locator = $locator;
214239
$this->catalogEavValidationRules = $catalogEavValidationRules;
@@ -229,6 +254,7 @@ public function __construct(
229254
$this->dataPersistor = $dataPersistor;
230255
$this->attributesToDisable = $attributesToDisable;
231256
$this->attributesToEliminate = $attributesToEliminate;
257+
$this->authorization = $authorization ?: ObjectManager::getInstance()->get(AuthorizationInterface::class);
232258
}
233259

234260
/**
@@ -549,6 +575,7 @@ private function isProductExists()
549575
public function setupAttributeMeta(ProductAttributeInterface $attribute, $groupCode, $sortOrder)
550576
{
551577
$configPath = ltrim(static::META_CONFIG_PATH, ArrayManager::DEFAULT_PATH_DELIMITER);
578+
$attributeCode = $attribute->getAttributeCode();
552579

553580
$meta = $this->arrayManager->set($configPath, [], [
554581
'dataType' => $attribute->getFrontendInput(),
@@ -558,7 +585,7 @@ public function setupAttributeMeta(ProductAttributeInterface $attribute, $groupC
558585
'notice' => $attribute->getNote(),
559586
'default' => (!$this->isProductExists()) ? $attribute->getDefaultValue() : null,
560587
'label' => __($attribute->getDefaultFrontendLabel()),
561-
'code' => $attribute->getAttributeCode(),
588+
'code' => $attributeCode,
562589
'source' => $groupCode,
563590
'scopeLabel' => $this->getScopeLabel($attribute),
564591
'globalScope' => $this->isScopeGlobal($attribute),
@@ -591,7 +618,7 @@ public function setupAttributeMeta(ProductAttributeInterface $attribute, $groupC
591618
]);
592619
}
593620

594-
if (in_array($attribute->getAttributeCode(), $this->attributesToDisable)) {
621+
if (in_array($attributeCode, $this->attributesToDisable)) {
595622
$meta = $this->arrayManager->merge($configPath, $meta, [
596623
'disabled' => true,
597624
]);
@@ -623,6 +650,22 @@ public function setupAttributeMeta(ProductAttributeInterface $attribute, $groupC
623650
break;
624651
}
625652

653+
//Checking access to design config.
654+
if (in_array($attributeCode, $this->designAttributeCodes, true)
655+
&& !$this->authorization->isAllowed('Magento_Catalog::edit_product_design')
656+
) {
657+
$meta = $this->arrayManager->merge(
658+
$configPath,
659+
$meta,
660+
[
661+
'disabled' => true,
662+
'validation' => ['required' => false],
663+
'required' => false,
664+
'serviceDisabled' => true,
665+
]
666+
);
667+
}
668+
626669
return $meta;
627670
}
628671

0 commit comments

Comments
 (0)