1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Ui\Controller\Adminhtml\Index\Render;
use Magento\Framework\App\Action\HttpGetActionInterface as HttpGetActionInterface;
use Magento\Framework\View\Element\Template;
use Magento\Ui\Component\Control\ActionPool;
use Magento\Ui\Component\Wrapper\UiComponent;
use Magento\Ui\Controller\Adminhtml\AbstractAction;
use Magento\Backend\App\Action\Context;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Framework\View\Element\UiComponent\ContextFactory;
use Magento\Framework\App\ObjectManager;
/**
* Class Handle
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Handle extends AbstractAction implements HttpGetActionInterface
{
/**
* @var ContextFactory
*/
private $contextFactory;
/**
* @param Context $context
* @param UiComponentFactory $factory
* @param ContextFactory|null $contextFactory
*/
public function __construct(
Context $context,
UiComponentFactory $factory,
ContextFactory $contextFactory = null
) {
parent::__construct($context, $factory);
$this->contextFactory = $contextFactory
?: ObjectManager::getInstance()->get(ContextFactory::class);
}
/**
* Render UI component by namespace in handle context
*
* @return void
*/
public function execute()
{
$response = '';
$handle = $this->_request->getParam('handle');
$namespace = $this->_request->getParam('namespace');
$buttons = $this->_request->getParam('buttons', false);
$this->_view->loadLayout(['default', $handle], true, true, false);
$layout = $this->_view->getLayout();
$context = $this->contextFactory->create(
[
'namespace' => $namespace,
'pageLayout' => $layout
]
);
$component = $this->factory->create($namespace, null, ['context' => $context]);
if ($this->validateAclResource($component->getContext()->getDataProvider()->getConfigData())) {
$uiComponent = $layout->getBlock($namespace);
$response = $uiComponent instanceof UiComponent ? $uiComponent->toHtml() : '';
}
if ($buttons) {
$actionsToolbar = $layout->getBlock(ActionPool::ACTIONS_PAGE_TOOLBAR);
$response .= $actionsToolbar instanceof Template ? $actionsToolbar->toHtml() : '';
}
$this->_response->appendBody($response);
}
/**
* Optionally validate ACL resource of components with a DataSource/DataProvider
*
* @param mixed $dataProviderConfigData
* @return bool
*/
private function validateAclResource($dataProviderConfigData)
{
if (isset($dataProviderConfigData['aclResource'])
&& !$this->_authorization->isAllowed($dataProviderConfigData['aclResource'])
) {
if (!$this->_request->isAjax()) {
$this->_redirect('admin/denied');
}
return false;
}
return true;
}
}