Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<url path="sales/order_create/start"/>
<class>primary</class>
<label translate="true">Create New Order</label>
<aclResource>Magento_Sales::create</aclResource>
</button>
</buttons>
<spinner>sales_order_columns</spinner>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,13 @@
</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="aclResource" type="xs:string" minOccurs="0" maxOccurs="1">
<xs:annotation>
<xs:documentation>
ACL Resource used to validate access to UI Component data
</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element ref="param"/>
</xs:choice>
<xs:attribute name="name" type="xs:string" use="required">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
*/
namespace Magento\Framework\View\Element\UiComponent;

use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\AuthorizationInterface;
use Magento\Framework\UrlInterface;
use Magento\Framework\View\Element\UiComponent\ContentType\ContentTypeFactory;
use Magento\Framework\View\Element\UiComponent\Control\ActionPoolFactory;
Expand Down Expand Up @@ -94,6 +96,11 @@ class Context implements ContextInterface
*/
protected $uiComponentFactory;

/**
* @var AuthorizationInterface
*/
private $authorization;

/**
* @param PageLayoutInterface $pageLayout
* @param RequestInterface $request
Expand All @@ -104,7 +111,8 @@ class Context implements ContextInterface
* @param Processor $processor
* @param UiComponentFactory $uiComponentFactory
* @param DataProviderInterface|null $dataProvider
* @param string|null $namespace
* @param string $namespace
* @param AuthorizationInterface|null $authorization
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
Expand All @@ -117,7 +125,8 @@ public function __construct(
Processor $processor,
UiComponentFactory $uiComponentFactory,
DataProviderInterface $dataProvider = null,
$namespace = null
$namespace = null,
AuthorizationInterface $authorization = null
) {
$this->namespace = $namespace;
$this->request = $request;
Expand All @@ -129,6 +138,9 @@ public function __construct(
$this->urlBuilder = $urlBuilder;
$this->processor = $processor;
$this->uiComponentFactory = $uiComponentFactory;
$this->authorization = $authorization ?: ObjectManager::getInstance()->get(
AuthorizationInterface::class
);
$this->setAcceptType();
}

Expand Down Expand Up @@ -280,6 +292,9 @@ public function addButtons(array $buttons, UiComponentInterface $component)
uasort($buttons, [$this, 'sortButtons']);

foreach ($buttons as $buttonId => $buttonData) {
if (isset($buttonData['aclResource']) && !$this->authorization->isAllowed($buttonData['aclResource'])) {
continue;
}
if (isset($buttonData['url'])) {
$buttonData['url'] = $this->getUrl($buttonData['url']);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,28 @@

use Magento\Framework\View\Element\UiComponent\Context;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
use Magento\Framework\View\Element\UiComponent\Control\ActionPoolInterface;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class ContextTest extends \PHPUnit\Framework\TestCase
{
/**
* @var Context
*/
protected $context;

/**
* @var ActionPoolInterface
*/
private $actionPool;

/**
* @var \Magento\Framework\AuthorizationInterface
*/
private $authorization;

protected function setUp()
{
$pageLayout = $this->getMockBuilder(\Magento\Framework\View\LayoutInterface::class)->getMock();
Expand All @@ -33,6 +47,10 @@ protected function setUp()
$this->getMockBuilder(\Magento\Framework\View\Element\UiComponent\Control\ActionPoolFactory::class)
->disableOriginalConstructor()
->getMock();
$this->actionPool = $this->getMockBuilder(ActionPoolInterface::class)
->disableOriginalConstructor()
->getMock();
$actionPoolFactory->method('create')->willReturn($this->actionPool);
$contentTypeFactory =
$this->getMockBuilder(\Magento\Framework\View\Element\UiComponent\ContentType\ContentTypeFactory::class)
->disableOriginalConstructor()
Expand All @@ -43,6 +61,9 @@ protected function setUp()
$this->getMockBuilder(\Magento\Framework\View\Element\UiComponentFactory::class)
->disableOriginalConstructor()
->getMock();
$this->authorization = $this->getMockBuilder(\Magento\Framework\AuthorizationInterface::class)
->disableOriginalConstructor()
->getMock();

$objectManagerHelper = new ObjectManagerHelper($this);
$this->context = $objectManagerHelper->getObject(
Expand All @@ -55,11 +76,62 @@ protected function setUp()
'contentTypeFactory' => $contentTypeFactory,
'urlBuilder' => $urlBuilder,
'processor' => $processor,
'uiComponentFactory' => $uiComponentFactory
'uiComponentFactory' => $uiComponentFactory,
'authorization' => $this->authorization,
]
);
}

public function testAddButtonWithoutAclResource()
{
$component = $this->getMockBuilder(\Magento\Framework\View\Element\UiComponentInterface::class)
->disableOriginalConstructor()
->getMock();

$this->actionPool->expects($this->once())->method('add');
$this->authorization->expects($this->never())->method('isAllowed');

$this->context->addButtons([
'button_1' => [
'name' => 'button_1',
],
], $component);
}

public function testAddButtonWithAclResourceAllowed()
{
$component = $this->getMockBuilder(\Magento\Framework\View\Element\UiComponentInterface::class)
->disableOriginalConstructor()
->getMock();

$this->actionPool->expects($this->once())->method('add');
$this->authorization->expects($this->once())->method('isAllowed')->willReturn(true);

$this->context->addButtons([
'button_1' => [
'name' => 'button_1',
'aclResource' => 'Magento_Framwork::acl',
],
], $component);
}

public function testAddButtonWithAclResourceDenied()
{
$component = $this->getMockBuilder(\Magento\Framework\View\Element\UiComponentInterface::class)
->disableOriginalConstructor()
->getMock();

$this->actionPool->expects($this->never())->method('add');
$this->authorization->expects($this->once())->method('isAllowed')->willReturn(false);

$this->context->addButtons([
'button_1' => [
'name' => 'button_1',
'aclResource' => 'Magento_Framwork::acl',
],
], $component);
}

/**
* @dataProvider addComponentDefinitionDataProvider
* @param array $components
Expand Down