Handle.php 3.1 KB

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