Skip to content

Commit 15c4ed4

Browse files
authored
Merge branch '2.4-develop' into MQE-GL-TicketRoundup-Mar14
2 parents a7f3489 + a96f0f7 commit 15c4ed4

File tree

73 files changed

+4107
-176
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+4107
-176
lines changed

app/code/Magento/Authorization/Model/Acl/Loader/Rule.php

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\Authorization\Model\Acl\Loader;
99

10+
use Magento\Framework\Acl;
1011
use Magento\Framework\Acl\Data\CacheInterface;
1112
use Magento\Framework\Acl\LoaderInterface;
1213
use Magento\Framework\Acl\RootResource;
@@ -21,7 +22,12 @@ class Rule implements LoaderInterface
2122
/**
2223
* Rules array cache key
2324
*/
24-
const ACL_RULE_CACHE_KEY = 'authorization_rule_cached_data';
25+
public const ACL_RULE_CACHE_KEY = 'authorization_rule_cached_data';
26+
27+
/**
28+
* Allow everything resource id
29+
*/
30+
private const ALLOW_EVERYTHING = 'Magento_Backend::all';
2531

2632
/**
2733
* @var ResourceConnection
@@ -75,27 +81,74 @@ public function __construct(
7581
/**
7682
* Populate ACL with rules from external storage
7783
*
78-
* @param \Magento\Framework\Acl $acl
84+
* @param Acl $acl
7985
* @return void
8086
*/
81-
public function populateAcl(\Magento\Framework\Acl $acl)
87+
public function populateAcl(Acl $acl)
8288
{
89+
$result = $this->applyPermissionsAccordingToRules($acl);
90+
$this->applyDenyPermissionsForMissingRules($acl, ...$result);
91+
}
92+
93+
/**
94+
* Apply ACL with rules
95+
*
96+
* @param Acl $acl
97+
* @return array[]
98+
*/
99+
private function applyPermissionsAccordingToRules(Acl $acl): array
100+
{
101+
$foundResources = $foundDeniedRoles = [];
83102
foreach ($this->getRulesArray() as $rule) {
84103
$role = $rule['role_id'];
85104
$resource = $rule['resource_id'];
86105
$privileges = !empty($rule['privileges']) ? explode(',', $rule['privileges']) : null;
87106

88107
if ($acl->has($resource)) {
108+
$foundResources[$resource] = $resource;
89109
if ($rule['permission'] == 'allow') {
90110
if ($resource === $this->_rootResource->getId()) {
91111
$acl->allow($role, null, $privileges);
92112
}
93113
$acl->allow($role, $resource, $privileges);
94114
} elseif ($rule['permission'] == 'deny') {
115+
$foundDeniedRoles[$role] = $role;
95116
$acl->deny($role, $resource, $privileges);
96117
}
97118
}
98119
}
120+
return [$foundResources, $foundDeniedRoles];
121+
}
122+
123+
/**
124+
* Apply deny permissions for missing rules
125+
*
126+
* For all rules that were not regenerated in authorization_rule table,
127+
* when adding a new module and without re-saving all roles,
128+
* consider not present rules with deny permissions
129+
*
130+
* @param Acl $acl
131+
* @param array $resources
132+
* @param array $deniedRoles
133+
* @return void
134+
*/
135+
private function applyDenyPermissionsForMissingRules(
136+
Acl $acl,
137+
array $resources,
138+
array $deniedRoles
139+
) {
140+
if (count($resources) && count($deniedRoles)
141+
//ignore denying missing permission if all are allowed
142+
&& !(count($resources) === 1 && isset($resources[static::ALLOW_EVERYTHING]))
143+
) {
144+
foreach ($acl->getResources() as $resource) {
145+
if (!isset($resources[$resource])) {
146+
foreach ($deniedRoles as $role) {
147+
$acl->deny($role, $resource, null);
148+
}
149+
}
150+
}
151+
}
99152
}
100153

101154
/**

app/code/Magento/Authorization/Test/Unit/Model/Acl/Loader/RuleTest.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ static function ($value) {
9797
*/
9898
public function testPopulateAclFromCache(): void
9999
{
100+
$rules = [
101+
['role_id' => 1, 'resource_id' => 'Magento_Backend::all', 'permission' => 'allow'],
102+
['role_id' => 2, 'resource_id' => 1, 'permission' => 'allow'],
103+
['role_id' => 3, 'resource_id' => 1, 'permission' => 'deny']
104+
];
100105
$this->resourceMock->expects($this->never())->method('getTable');
101106
$this->resourceMock->expects($this->never())
102107
->method('getConnection');
@@ -105,13 +110,7 @@ public function testPopulateAclFromCache(): void
105110
->method('load')
106111
->with(Rule::ACL_RULE_CACHE_KEY)
107112
->willReturn(
108-
json_encode(
109-
[
110-
['role_id' => 1, 'resource_id' => 'Magento_Backend::all', 'permission' => 'allow'],
111-
['role_id' => 2, 'resource_id' => 1, 'permission' => 'allow'],
112-
['role_id' => 3, 'resource_id' => 1, 'permission' => 'deny']
113-
]
114-
)
113+
json_encode($rules)
115114
);
116115

117116
$aclMock = $this->createMock(Acl::class);
@@ -123,9 +122,21 @@ public function testPopulateAclFromCache(): void
123122
['1', 'Magento_Backend::all', null],
124123
['2', 1, null]
125124
);
125+
126126
$aclMock
127127
->method('deny')
128-
->with('3', 1, null);
128+
->withConsecutive(
129+
['3', 1, null]
130+
);
131+
132+
$aclMock
133+
->method('getResources')
134+
->willReturn([
135+
'Magento_Backend::all',
136+
'Magento_Backend::admin',
137+
'Vendor_MyModule::menu',
138+
'Vendor_MyModule::index'
139+
]);
129140

130141
$this->model->populateAcl($aclMock);
131142
}

app/code/Magento/Bundle/Model/ResourceModel/Selection/Collection/FilterApplier.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Magento\Bundle\Model\ResourceModel\Selection\Collection;
77

88
use Magento\Bundle\Model\ResourceModel\Selection\Collection;
9+
use Zend_Db_Select_Exception;
910

1011
/**
1112
* An applier of additional filters to a selection collection.
@@ -32,16 +33,17 @@ class FilterApplier
3233
* @param string $conditionType
3334
*
3435
* @return void
36+
* @throws Zend_Db_Select_Exception
3537
*/
36-
public function apply(Collection $collection, $field, $value, $conditionType = 'eq')
38+
public function apply(Collection $collection, string $field, $value, string $conditionType = 'eq')
3739
{
3840
foreach ($collection->getSelect()->getPart('from') as $tableAlias => $data) {
3941
if ($data['tableName'] == $collection->getTable('catalog_product_bundle_selection')) {
4042
$field = $tableAlias . '.' . $field;
4143
}
4244
}
4345

44-
$collection->getSelect()
46+
$collection->getSelect()->distinct(true)
4547
->where($field . $this->conditionTypesMap[$conditionType], $value);
4648
}
4749
}

app/code/Magento/Bundle/Test/Unit/Model/ResourceModel/Selection/Collection/FilterApplierTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77

88
namespace Magento\Bundle\Test\Unit\Model\ResourceModel\Selection\Collection;
99

10+
use Magento\Bundle\Model\ResourceModel\Selection\Collection;
1011
use Magento\Bundle\Model\ResourceModel\Selection\Collection\FilterApplier;
1112
use Magento\Framework\DB\Select;
1213
use PHPUnit\Framework\TestCase;
14+
use Zend_Db_Select_Exception;
1315

1416
/**
1517
* Test selection collection filter applier
@@ -37,12 +39,13 @@ protected function setUp(): void
3739
* @param $expectedCondition
3840
* @param $expectedValue
3941
* @dataProvider applyDataProvider
42+
* @throws Zend_Db_Select_Exception
4043
*/
4144
public function testApply($field, $value, $conditionType, $expectedCondition, $expectedValue): void
4245
{
4346
$tableName = 'catalog_product_bundle_selection';
4447
$select = $this->createMock(Select::class);
45-
$collection = $this->createMock(\Magento\Bundle\Model\ResourceModel\Selection\Collection::class);
48+
$collection = $this->createMock(Collection::class);
4649
$collection->method('getSelect')
4750
->willReturn($select);
4851
$collection->method('getTable')
@@ -60,6 +63,10 @@ public function testApply($field, $value, $conditionType, $expectedCondition, $e
6063
]
6164
]
6265
);
66+
$select->expects($this->once())
67+
->method('distinct')
68+
->with(true)
69+
->willReturnSelf();
6370
$select->expects($this->once())
6471
->method('where')
6572
->with($expectedCondition, $expectedValue);

app/code/Magento/Catalog/Test/Fixture/Product.php

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\Catalog\Test\Fixture;
99

10+
use Magento\Catalog\Api\Data\ProductInterface;
1011
use Magento\Catalog\Api\ProductRepositoryInterface;
1112
use Magento\Catalog\Model\Product\Attribute\Source\Status;
1213
use Magento\Catalog\Model\Product\Type;
@@ -50,6 +51,12 @@ class Product implements RevertibleDataFixtureInterface
5051
'updated_at' => null,
5152
];
5253

54+
private const DEFAULT_PRODUCT_LINK_DATA = [
55+
'sku' => null,
56+
'type' => 'related',
57+
'position' => 1,
58+
];
59+
5360
/**
5461
* @var ServiceFactory
5562
*/
@@ -65,18 +72,25 @@ class Product implements RevertibleDataFixtureInterface
6572
*/
6673
private $dataMerger;
6774

75+
/**
76+
* @var ProductRepositoryInterface
77+
*/
78+
private $productRepository;
79+
6880
/**
6981
* @param ServiceFactory $serviceFactory
7082
* @param ProcessorInterface $dataProcessor
7183
*/
7284
public function __construct(
7385
ServiceFactory $serviceFactory,
7486
ProcessorInterface $dataProcessor,
75-
DataMerger $dataMerger
87+
DataMerger $dataMerger,
88+
ProductRepositoryInterface $productRepository
7689
) {
7790
$this->serviceFactory = $serviceFactory;
7891
$this->dataProcessor = $dataProcessor;
7992
$this->dataMerger = $dataMerger;
93+
$this->productRepository = $productRepository;
8094
}
8195

8296
/**
@@ -122,6 +136,49 @@ private function prepareData(array $data): array
122136
unset($data['extension_attributes']['category_links']);
123137
}
124138

139+
$data['product_links'] = $this->prepareLinksData($data);
140+
125141
return $this->dataProcessor->process($this, $data);
126142
}
143+
144+
/**
145+
* Prepare links data
146+
*
147+
* @param array $data
148+
* @return array
149+
*/
150+
private function prepareLinksData(array $data): array
151+
{
152+
$links = [];
153+
154+
$position = 1;
155+
foreach ($data['product_links'] as $link) {
156+
$defaultLinkData = self::DEFAULT_PRODUCT_LINK_DATA;
157+
$defaultLinkData['position'] = $position;
158+
$linkData = [];
159+
if (is_numeric($link)) {
160+
$product = $this->productRepository->getById($link);
161+
} elseif (is_string($link)) {
162+
$product = $this->productRepository->get($link);
163+
} elseif ($link instanceof ProductInterface) {
164+
$product = $this->productRepository->get($link->getSku());
165+
} else {
166+
$linkData = $link instanceof DataObject ? $link->toArray() : $link;
167+
$product = $this->productRepository->get($linkData['sku']);
168+
}
169+
170+
$linkData += $defaultLinkData;
171+
$links[] = [
172+
'sku' => $data['sku'],
173+
'link_type' => $linkData['type'],
174+
'linked_product_sku' => $product->getSku(),
175+
'linked_product_type' => $product->getTypeId(),
176+
'position' => $linkData['position'],
177+
'extension_attributes' => array_diff_key($linkData, $defaultLinkData),
178+
];
179+
$position++;
180+
}
181+
182+
return $links;
183+
}
127184
}

0 commit comments

Comments
 (0)