Webapi.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Integration\Block\Adminhtml\Integration\Edit\Tab;
  7. use Magento\Integration\Controller\Adminhtml\Integration as IntegrationController;
  8. use Magento\Integration\Model\Integration as IntegrationModel;
  9. /**
  10. * Class for handling API section within integration.
  11. *
  12. * @api
  13. * @since 100.0.2
  14. */
  15. class Webapi extends \Magento\Backend\Block\Widget\Form\Generic implements
  16. \Magento\Backend\Block\Widget\Tab\TabInterface
  17. {
  18. /**
  19. * Root ACL Resource
  20. *
  21. * @var \Magento\Framework\Acl\RootResource
  22. */
  23. protected $rootResource;
  24. /**
  25. * Acl resource provider
  26. *
  27. * @var \Magento\Framework\Acl\AclResource\ProviderInterface
  28. */
  29. protected $aclResourceProvider;
  30. /**
  31. * @var \Magento\Integration\Helper\Data
  32. */
  33. protected $integrationData;
  34. /**
  35. * @var \Magento\Integration\Api\IntegrationServiceInterface
  36. */
  37. protected $integrationService;
  38. /**
  39. * Initialize dependencies.
  40. *
  41. * @param \Magento\Backend\Block\Template\Context $context
  42. * @param \Magento\Framework\Registry $registry
  43. * @param \Magento\Framework\Data\FormFactory $formFactory
  44. * @param \Magento\Framework\Acl\RootResource $rootResource
  45. * @param \Magento\Framework\Acl\AclResource\ProviderInterface $aclResourceProvider
  46. * @param \Magento\Integration\Helper\Data $integrationData
  47. * @param \Magento\Integration\Api\IntegrationServiceInterface $integrationService
  48. * @param array $data
  49. *
  50. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  51. */
  52. public function __construct(
  53. \Magento\Backend\Block\Template\Context $context,
  54. \Magento\Framework\Registry $registry,
  55. \Magento\Framework\Data\FormFactory $formFactory,
  56. \Magento\Framework\Acl\RootResource $rootResource,
  57. \Magento\Framework\Acl\AclResource\ProviderInterface $aclResourceProvider,
  58. \Magento\Integration\Helper\Data $integrationData,
  59. \Magento\Integration\Api\IntegrationServiceInterface $integrationService,
  60. array $data = []
  61. ) {
  62. $this->rootResource = $rootResource;
  63. $this->aclResourceProvider = $aclResourceProvider;
  64. $this->integrationData = $integrationData;
  65. $this->integrationService = $integrationService;
  66. parent::__construct($context, $registry, $formFactory, $data);
  67. }
  68. /**
  69. * Get tab label
  70. *
  71. * @return \Magento\Framework\Phrase
  72. * @codeCoverageIgnore
  73. */
  74. public function getTabLabel()
  75. {
  76. return __('API');
  77. }
  78. /**
  79. * Get tab title
  80. *
  81. * @return string
  82. * @codeCoverageIgnore
  83. */
  84. public function getTabTitle()
  85. {
  86. return $this->getTabLabel();
  87. }
  88. /**
  89. * Whether tab is available
  90. *
  91. * @return bool
  92. */
  93. public function canShowTab()
  94. {
  95. $integrationData = $this->_coreRegistry->registry(IntegrationController::REGISTRY_KEY_CURRENT_INTEGRATION);
  96. return !isset(
  97. $integrationData[Info::DATA_SETUP_TYPE]
  98. ) || $integrationData[Info::DATA_SETUP_TYPE] != IntegrationModel::TYPE_CONFIG;
  99. }
  100. /**
  101. * Whether tab is visible
  102. *
  103. * @return bool
  104. * @codeCoverageIgnore
  105. */
  106. public function isHidden()
  107. {
  108. return false;
  109. }
  110. /**
  111. * Class constructor
  112. *
  113. * @return void
  114. */
  115. protected function _construct()
  116. {
  117. parent::_construct();
  118. $savedFromData = $this->retrieveFormResources();
  119. if (false !== $savedFromData) {
  120. $this->setSelectedResources($savedFromData);
  121. return;
  122. }
  123. $integrationData = $this->_coreRegistry->registry(IntegrationController::REGISTRY_KEY_CURRENT_INTEGRATION);
  124. if (is_array($integrationData)
  125. && isset($integrationData['integration_id'])
  126. && $integrationData['integration_id']
  127. ) {
  128. $this->setSelectedResources(
  129. $this->integrationService->getSelectedResources($integrationData['integration_id'])
  130. );
  131. } else {
  132. $this->setSelectedResources([]);
  133. }
  134. }
  135. /**
  136. * Retrieve saved resource
  137. *
  138. * @return array|bool
  139. * @since 100.1.0
  140. */
  141. protected function retrieveFormResources()
  142. {
  143. $savedData = $this->_coreRegistry->registry(
  144. \Magento\Integration\Controller\Adminhtml\Integration::REGISTRY_KEY_CURRENT_RESOURCE
  145. );
  146. if (is_array($savedData)) {
  147. if ($savedData['all_resources']) {
  148. return [$this->rootResource->getId()];
  149. }
  150. return $savedData['resource'];
  151. }
  152. return false;
  153. }
  154. /**
  155. * Check if everything is allowed
  156. *
  157. * @return bool
  158. */
  159. public function isEverythingAllowed()
  160. {
  161. return in_array($this->rootResource->getId(), $this->getSelectedResources());
  162. }
  163. /**
  164. * Get Json Representation of Resource Tree
  165. *
  166. * @return array
  167. */
  168. public function getTree()
  169. {
  170. return $this->integrationData->mapResources($this->getAclResources());
  171. }
  172. /**
  173. * Get lit of all ACL resources declared in the system.
  174. *
  175. * @return array
  176. */
  177. private function getAclResources()
  178. {
  179. $resources = $this->aclResourceProvider->getAclResources();
  180. $configResource = array_filter(
  181. $resources,
  182. function ($node) {
  183. return isset($node['id'])
  184. && $node['id'] == 'Magento_Backend::admin';
  185. }
  186. );
  187. $configResource = reset($configResource);
  188. return isset($configResource['children']) ? $configResource['children'] : [];
  189. }
  190. }