Skip to content

Commit ba9f4f8

Browse files
committed
aclResource for UIComponent buttons
1 parent e5c1527 commit ba9f4f8

File tree

4 files changed

+89
-1
lines changed

4 files changed

+89
-1
lines changed

app/code/Magento/Sales/view/adminhtml/ui_component/sales_order_grid.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<url path="sales/order_create/start"/>
1818
<class>primary</class>
1919
<label translate="true">Create New Order</label>
20+
<aclResource>Magento_Sales::create</aclResource>
2021
</button>
2122
</buttons>
2223
<spinner>sales_order_columns</spinner>

app/code/Magento/Ui/view/base/ui_component/etc/definition/ui_settings.xsd

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,13 @@
476476
</xs:documentation>
477477
</xs:annotation>
478478
</xs:element>
479+
<xs:element name="aclResource" type="xs:string" minOccurs="0" maxOccurs="1">
480+
<xs:annotation>
481+
<xs:documentation>
482+
ACL Resource used to validate access to UI Component data
483+
</xs:documentation>
484+
</xs:annotation>
485+
</xs:element>
479486
<xs:element ref="param"/>
480487
</xs:choice>
481488
<xs:attribute name="name" type="xs:string" use="required">

lib/internal/Magento/Framework/View/Element/UiComponent/Context.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Magento\Framework\View\Element\UiComponent;
77

88
use Magento\Framework\App\RequestInterface;
9+
use Magento\Framework\AuthorizationInterface;
910
use Magento\Framework\UrlInterface;
1011
use Magento\Framework\View\Element\UiComponent\ContentType\ContentTypeFactory;
1112
use Magento\Framework\View\Element\UiComponent\Control\ActionPoolFactory;
@@ -94,6 +95,11 @@ class Context implements ContextInterface
9495
*/
9596
protected $uiComponentFactory;
9697

98+
/**
99+
* @var AuthorizationInterface
100+
*/
101+
protected $authorization;
102+
97103
/**
98104
* @param PageLayoutInterface $pageLayout
99105
* @param RequestInterface $request
@@ -103,6 +109,7 @@ class Context implements ContextInterface
103109
* @param UrlInterface $urlBuilder
104110
* @param Processor $processor
105111
* @param UiComponentFactory $uiComponentFactory
112+
* @param AuthorizationInterface $authorization
106113
* @param DataProviderInterface|null $dataProvider
107114
* @param string|null $namespace
108115
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
@@ -116,6 +123,7 @@ public function __construct(
116123
UrlInterface $urlBuilder,
117124
Processor $processor,
118125
UiComponentFactory $uiComponentFactory,
126+
AuthorizationInterface $authorization,
119127
DataProviderInterface $dataProvider = null,
120128
$namespace = null
121129
) {
@@ -129,6 +137,7 @@ public function __construct(
129137
$this->urlBuilder = $urlBuilder;
130138
$this->processor = $processor;
131139
$this->uiComponentFactory = $uiComponentFactory;
140+
$this->authorization = $authorization;
132141
$this->setAcceptType();
133142
}
134143

@@ -280,6 +289,9 @@ public function addButtons(array $buttons, UiComponentInterface $component)
280289
uasort($buttons, [$this, 'sortButtons']);
281290

282291
foreach ($buttons as $buttonId => $buttonData) {
292+
if (isset($buttonData['aclResource']) && !$this->authorization->isAllowed($buttonData['aclResource'])) {
293+
continue;
294+
}
283295
if (isset($buttonData['url'])) {
284296
$buttonData['url'] = $this->getUrl($buttonData['url']);
285297
}

lib/internal/Magento/Framework/View/Test/Unit/Element/UiComponent/ContextTest.php

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ class ContextTest extends \PHPUnit\Framework\TestCase
1919
*/
2020
protected $context;
2121

22+
/**
23+
* @var \Magento\Framework\View\Element\UiComponent\Control\ActionPoolInterface
24+
*/
25+
private $actionPool;
26+
27+
/**
28+
* @var \Magento\Framework\AuthorizationInterface
29+
*/
30+
private $authorization;
31+
2232
protected function setUp()
2333
{
2434
$pageLayout = $this->getMockBuilder(\Magento\Framework\View\LayoutInterface::class)->getMock();
@@ -33,6 +43,10 @@ protected function setUp()
3343
$this->getMockBuilder(\Magento\Framework\View\Element\UiComponent\Control\ActionPoolFactory::class)
3444
->disableOriginalConstructor()
3545
->getMock();
46+
$this->actionPool = $this->getMockBuilder(\Magento\Framework\View\Element\UiComponent\Control\ActionPoolInterface::class)
47+
->disableOriginalConstructor()
48+
->getMock();
49+
$actionPoolFactory->method('create')->willReturn($this->actionPool);
3650
$contentTypeFactory =
3751
$this->getMockBuilder(\Magento\Framework\View\Element\UiComponent\ContentType\ContentTypeFactory::class)
3852
->disableOriginalConstructor()
@@ -43,6 +57,9 @@ protected function setUp()
4357
$this->getMockBuilder(\Magento\Framework\View\Element\UiComponentFactory::class)
4458
->disableOriginalConstructor()
4559
->getMock();
60+
$this->authorization = $this->getMockBuilder(\Magento\Framework\AuthorizationInterface::class)
61+
->disableOriginalConstructor()
62+
->getMock();
4663

4764
$objectManagerHelper = new ObjectManagerHelper($this);
4865
$this->context = $objectManagerHelper->getObject(
@@ -55,11 +72,62 @@ protected function setUp()
5572
'contentTypeFactory' => $contentTypeFactory,
5673
'urlBuilder' => $urlBuilder,
5774
'processor' => $processor,
58-
'uiComponentFactory' => $uiComponentFactory
75+
'uiComponentFactory' => $uiComponentFactory,
76+
'authorization' => $this->authorization,
5977
]
6078
);
6179
}
6280

81+
public function testAddButtonWithoutAclResource()
82+
{
83+
$component = $this->getMockBuilder(\Magento\Framework\View\Element\UiComponentInterface::class)
84+
->disableOriginalConstructor()
85+
->getMock();
86+
87+
$this->actionPool->expects($this->once())->method('add');
88+
$this->authorization->expects($this->never())->method('isAllowed');
89+
90+
$this->context->addButtons([
91+
'button_1' => [
92+
'name' => 'button_1',
93+
],
94+
], $component);
95+
}
96+
97+
public function testAddButtonWithAclResourceAllowed()
98+
{
99+
$component = $this->getMockBuilder(\Magento\Framework\View\Element\UiComponentInterface::class)
100+
->disableOriginalConstructor()
101+
->getMock();
102+
103+
$this->actionPool->expects($this->once())->method('add');
104+
$this->authorization->expects($this->once())->method('isAllowed')->willReturn(true);
105+
106+
$this->context->addButtons([
107+
'button_1' => [
108+
'name' => 'button_1',
109+
'aclResource' => 'Magento_Framwork::acl',
110+
],
111+
], $component);
112+
}
113+
114+
public function testAddButtonWithAclResourceDenied()
115+
{
116+
$component = $this->getMockBuilder(\Magento\Framework\View\Element\UiComponentInterface::class)
117+
->disableOriginalConstructor()
118+
->getMock();
119+
120+
$this->actionPool->expects($this->never())->method('add');
121+
$this->authorization->expects($this->once())->method('isAllowed')->willReturn(false);
122+
123+
$this->context->addButtons([
124+
'button_1' => [
125+
'name' => 'button_1',
126+
'aclResource' => 'Magento_Framwork::acl',
127+
],
128+
], $component);
129+
}
130+
63131
/**
64132
* @dataProvider addComponentDefinitionDataProvider
65133
* @param array $components

0 commit comments

Comments
 (0)