Skip to content

Commit d459d0c

Browse files
committed
ACP2E-277: treat missing rules from authorization_rule table as deny permission rules; fix static errors / warnings
1 parent 49744ae commit d459d0c

File tree

2 files changed

+47
-17
lines changed

2 files changed

+47
-17
lines changed

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

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Rule implements LoaderInterface
2121
/**
2222
* Rules array cache key
2323
*/
24-
const ACL_RULE_CACHE_KEY = 'authorization_rule_cached_data';
24+
public const ACL_RULE_CACHE_KEY = 'authorization_rule_cached_data';
2525

2626
/**
2727
* @var ResourceConnection
@@ -79,10 +79,19 @@ public function __construct(
7979
* @return void
8080
*/
8181
public function populateAcl(\Magento\Framework\Acl $acl)
82+
{
83+
$result = $this->applyPermissionsAccordingToRules($acl);
84+
$this->applyDenyPermissionsForMissingRules($acl, ...$result);
85+
}
86+
87+
/**
88+
* @param \Magento\Framework\Acl $acl
89+
* @return array[]
90+
*/
91+
private function applyPermissionsAccordingToRules(\Magento\Framework\Acl $acl): array
8292
{
8393
$foundResources = [];
8494
$foundRoles = [];
85-
8695
foreach ($this->getRulesArray() as $rule) {
8796
$role = $rule['role_id'];
8897
$resource = $rule['resource_id'];
@@ -101,15 +110,25 @@ public function populateAcl(\Magento\Framework\Acl $acl)
101110
}
102111
}
103112
}
113+
return [$foundResources, $foundRoles];
114+
}
104115

105-
/**
106-
* for all rules that were not regenerated in authorization_rule table,
107-
* when adding a new module and without re-saving all roles,7
108-
* consider not present rules with deny permissions
109-
* */
116+
/**
117+
*
118+
* For all rules that were not regenerated in authorization_rule table,
119+
* when adding a new module and without re-saving all roles,
120+
* consider not present rules with deny permissions
121+
*
122+
* @param \Magento\Framework\Acl $acl
123+
* @param array $resources
124+
* @param array $roles
125+
* @return void
126+
*/
127+
private function applyDenyPermissionsForMissingRules(\Magento\Framework\Acl $acl, array $resources, array $roles)
128+
{
110129
foreach ($acl->getResources() as $resource) {
111-
if (!isset($foundResources[$resource])) {
112-
foreach ($foundRoles as $role) {
130+
if (!isset($resources[$resource])) {
131+
foreach ($roles as $role) {
113132
$acl->deny($role, $resource, null);
114133
}
115134
}

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+
126+
$aclMock
127+
->method('getResources')
128+
->willReturn([
129+
'Vendor_MyModule::menu'
130+
]);
131+
126132
$aclMock
127133
->method('deny')
128-
->with('3', 1, null);
134+
->withConsecutive(
135+
['3', 1, null],
136+
['1', 'Vendor_MyModule::menu', null],
137+
['2', 'Vendor_MyModule::menu', null],
138+
['3', 'Vendor_MyModule::menu', null]
139+
);
129140

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

0 commit comments

Comments
 (0)