Skip to content

Commit 34617d9

Browse files
author
OlgaVasyltsun
committed
MAGETWO-98202: Additional Permissions for Design settings
1 parent e29f4c5 commit 34617d9

File tree

19 files changed

+534
-25
lines changed

19 files changed

+534
-25
lines changed

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

Lines changed: 46 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,30 @@ 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 ((
946+
$userType === UserContextInterface::USER_TYPE_ADMIN
947+
|| $userType === UserContextInterface::USER_TYPE_INTEGRATION
948+
)
949+
&& !$this->authorization->isAllowed('Magento_Catalog::edit_category_design')
950+
) {
951+
$this->setData('custom_design', $this->getOrigData('custom_design'));
952+
$this->setData('custom_design_from', $this->getOrigData('custom_design_from'));
953+
$this->setData('custom_design_to', $this->getOrigData('custom_design_to'));
954+
$this->setData('page_layout', $this->getOrigData('page_layout'));
955+
$this->setData('custom_layout_update', $this->getOrigData('custom_layout_update'));
956+
$this->setData('custom_apply_to_products', $this->getOrigData('custom_apply_to_products'));
957+
}
958+
959+
return parent::beforeSave();
960+
}
961+
919962
/**
920963
* Retrieve anchors above
921964
*

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Magento\Catalog\Model\CategoryFactory;
1919
use Magento\Framework\Exception\NoSuchEntityException;
2020
use Magento\Catalog\Model\Category\Attribute\Backend\Image as ImageBackendModel;
21+
use Magento\Framework\AuthorizationInterface;
2122

2223
/**
2324
* Class DataProvider
@@ -112,6 +113,11 @@ class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
112113
*/
113114
private $categoryFactory;
114115

116+
/**
117+
* @var AuthorizationInterface
118+
*/
119+
private $auth;
120+
115121
/**
116122
* DataProvider constructor
117123
*
@@ -127,6 +133,7 @@ class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
127133
* @param CategoryFactory $categoryFactory
128134
* @param array $meta
129135
* @param array $data
136+
* @param AuthorizationInterface|null $auth
130137
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
131138
*/
132139
public function __construct(
@@ -141,7 +148,8 @@ public function __construct(
141148
\Magento\Framework\App\RequestInterface $request,
142149
CategoryFactory $categoryFactory,
143150
array $meta = [],
144-
array $data = []
151+
array $data = [],
152+
AuthorizationInterface $auth = null
145153
) {
146154
$this->eavValidationRules = $eavValidationRules;
147155
$this->collection = $categoryCollectionFactory->create();
@@ -151,6 +159,8 @@ public function __construct(
151159
$this->storeManager = $storeManager;
152160
$this->request = $request;
153161
$this->categoryFactory = $categoryFactory;
162+
$this->auth = $auth ?? \Magento\Framework\App\ObjectManager::getInstance()->get(AuthorizationInterface::class);
163+
154164
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
155165
$this->meta = $this->prepareMeta($this->meta);
156166
}
@@ -180,11 +190,20 @@ public function prepareMeta($meta)
180190
*/
181191
private function prepareFieldsMeta($fieldsMap, $fieldsMeta)
182192
{
193+
$canEditDesign = $this->auth->isAllowed('Magento_Catalog::edit_category_design');
194+
183195
$result = [];
184196
foreach ($fieldsMap as $fieldSet => $fields) {
185197
foreach ($fields as $field) {
186198
if (isset($fieldsMeta[$field])) {
187-
$result[$fieldSet]['children'][$field]['arguments']['data']['config'] = $fieldsMeta[$field];
199+
$config = $fieldsMeta[$field];
200+
if (($fieldSet === 'design' || $fieldSet === 'schedule_design_update') && !$canEditDesign) {
201+
$config['required'] = 1;
202+
$config['disabled'] = 1;
203+
$config['serviceDisabled'] = true;
204+
}
205+
206+
$result[$fieldSet]['children'][$field]['arguments']['data']['config'] = $config;
188207
}
189208
}
190209
}

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

Lines changed: 35 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,20 @@ public function beforeSave()
852871

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

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

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Magento\Framework\EntityManager\EntityManager;
1515
use Magento\Catalog\Api\Data\CategoryInterface;
16+
use Magento\Catalog\Model\Category as CategoryEntity;
1617

1718
/**
1819
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -983,7 +984,7 @@ protected function _processPositions($category, $newParent, $afterCategoryId)
983984
if ($afterCategoryId) {
984985
$select = $connection->select()->from($table, 'position')->where('entity_id = :entity_id');
985986
$position = $connection->fetchOne($select, ['entity_id' => $afterCategoryId]);
986-
$position += 1;
987+
$position++;
987988
} else {
988989
$position = 1;
989990
}
@@ -1076,4 +1077,19 @@ private function getAggregateCount()
10761077
}
10771078
return $this->aggregateCount;
10781079
}
1080+
1081+
/**
1082+
* @inheritdoc
1083+
*
1084+
* @param CategoryEntity|\Magento\Framework\DataObject $object
1085+
*/
1086+
public function validate($object)
1087+
{
1088+
$isValid = parent::validate($object);
1089+
if ($isValid !== true) {
1090+
return $isValid;
1091+
}
1092+
1093+
return true;
1094+
}
10791095
}

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

Lines changed: 32 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,11 @@ class Eav extends AbstractModifier
167169
*/
168170
private $localeCurrency;
169171

172+
/**
173+
* @var AuthorizationInterface
174+
*/
175+
private $auth;
176+
170177
/**
171178
* @param LocatorInterface $locator
172179
* @param CatalogEavValidationRules $catalogEavValidationRules
@@ -187,6 +194,8 @@ class Eav extends AbstractModifier
187194
* @param DataPersistorInterface $dataPersistor
188195
* @param array $attributesToDisable
189196
* @param array $attributesToEliminate
197+
* @param AuthorizationInterface|null $auth
198+
*
190199
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
191200
*/
192201
public function __construct(
@@ -208,7 +217,8 @@ public function __construct(
208217
ScopeOverriddenValue $scopeOverriddenValue,
209218
DataPersistorInterface $dataPersistor,
210219
$attributesToDisable = [],
211-
$attributesToEliminate = []
220+
$attributesToEliminate = [],
221+
AuthorizationInterface $auth = null
212222
) {
213223
$this->locator = $locator;
214224
$this->catalogEavValidationRules = $catalogEavValidationRules;
@@ -229,6 +239,7 @@ public function __construct(
229239
$this->dataPersistor = $dataPersistor;
230240
$this->attributesToDisable = $attributesToDisable;
231241
$this->attributesToEliminate = $attributesToEliminate;
242+
$this->auth = $auth ?: ObjectManager::getInstance()->get(AuthorizationInterface::class);
232243
}
233244

234245
/**
@@ -549,6 +560,7 @@ private function isProductExists()
549560
public function setupAttributeMeta(ProductAttributeInterface $attribute, $groupCode, $sortOrder)
550561
{
551562
$configPath = ltrim(static::META_CONFIG_PATH, ArrayManager::DEFAULT_PATH_DELIMITER);
563+
$attributeCode = $attribute->getAttributeCode();
552564

553565
$meta = $this->arrayManager->set($configPath, [], [
554566
'dataType' => $attribute->getFrontendInput(),
@@ -558,7 +570,7 @@ public function setupAttributeMeta(ProductAttributeInterface $attribute, $groupC
558570
'notice' => $attribute->getNote(),
559571
'default' => (!$this->isProductExists()) ? $attribute->getDefaultValue() : null,
560572
'label' => __($attribute->getDefaultFrontendLabel()),
561-
'code' => $attribute->getAttributeCode(),
573+
'code' => $attributeCode,
562574
'source' => $groupCode,
563575
'scopeLabel' => $this->getScopeLabel($attribute),
564576
'globalScope' => $this->isScopeGlobal($attribute),
@@ -591,7 +603,7 @@ public function setupAttributeMeta(ProductAttributeInterface $attribute, $groupC
591603
]);
592604
}
593605

594-
if (in_array($attribute->getAttributeCode(), $this->attributesToDisable)) {
606+
if (in_array($attributeCode, $this->attributesToDisable)) {
595607
$meta = $this->arrayManager->merge($configPath, $meta, [
596608
'disabled' => true,
597609
]);
@@ -623,6 +635,23 @@ public function setupAttributeMeta(ProductAttributeInterface $attribute, $groupC
623635
break;
624636
}
625637

638+
//Checking access to design config.
639+
$designAttributeCodes = ['custom_design', 'page_layout', 'options_container', 'custom_layout_update'];
640+
if (in_array($attributeCode, $designAttributeCodes, true)) {
641+
if (!$this->auth->isAllowed('Magento_Catalog::edit_product_design')) {
642+
$meta = $this->arrayManager->merge(
643+
$configPath,
644+
$meta,
645+
[
646+
'disabled' => true,
647+
'validation' => ['required' => false],
648+
'required' => false,
649+
'serviceDisabled' => true,
650+
]
651+
);
652+
}
653+
}
654+
626655
return $meta;
627656
}
628657

0 commit comments

Comments
 (0)