Edit.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\User\Block\Role\Tab;
  7. use Magento\User\Controller\Adminhtml\User\Role\SaveRole;
  8. /**
  9. * Rolesedit Tab Display Block.
  10. *
  11. * @api
  12. * @since 100.0.2
  13. */
  14. class Edit extends \Magento\Backend\Block\Widget\Form implements \Magento\Backend\Block\Widget\Tab\TabInterface
  15. {
  16. /**
  17. * @var string
  18. */
  19. protected $_template = 'Magento_User::role/edit.phtml';
  20. /**
  21. * Root ACL Resource
  22. *
  23. * @var \Magento\Framework\Acl\RootResource
  24. */
  25. protected $_rootResource;
  26. /**
  27. * Rules collection factory
  28. *
  29. * @var \Magento\Authorization\Model\ResourceModel\Rules\CollectionFactory
  30. */
  31. protected $_rulesCollectionFactory;
  32. /**
  33. * Acl builder
  34. *
  35. * @var \Magento\Authorization\Model\Acl\AclRetriever
  36. */
  37. protected $_aclRetriever;
  38. /**
  39. * Acl resource provider
  40. *
  41. * @var \Magento\Framework\Acl\AclResource\ProviderInterface
  42. */
  43. protected $_aclResourceProvider;
  44. /**
  45. * @var \Magento\Integration\Helper\Data
  46. */
  47. protected $_integrationData;
  48. /**
  49. * Core registry
  50. *
  51. * @var \Magento\Framework\Registry
  52. * @since 100.1.0
  53. */
  54. protected $coreRegistry = null;
  55. /**
  56. * @param \Magento\Backend\Block\Template\Context $context
  57. * @param \Magento\Framework\Acl\RootResource $rootResource
  58. * @param \Magento\Authorization\Model\ResourceModel\Rules\CollectionFactory $rulesCollectionFactory
  59. * @param \Magento\Authorization\Model\Acl\AclRetriever $aclRetriever
  60. * @param \Magento\Framework\Acl\AclResource\ProviderInterface $aclResourceProvider
  61. * @param \Magento\Integration\Helper\Data $integrationData
  62. * @param array $data
  63. */
  64. public function __construct(
  65. \Magento\Backend\Block\Template\Context $context,
  66. \Magento\Authorization\Model\Acl\AclRetriever $aclRetriever,
  67. \Magento\Framework\Acl\RootResource $rootResource,
  68. \Magento\Authorization\Model\ResourceModel\Rules\CollectionFactory $rulesCollectionFactory,
  69. \Magento\Framework\Acl\AclResource\ProviderInterface $aclResourceProvider,
  70. \Magento\Integration\Helper\Data $integrationData,
  71. array $data = []
  72. ) {
  73. $this->_aclRetriever = $aclRetriever;
  74. $this->_rootResource = $rootResource;
  75. $this->_rulesCollectionFactory = $rulesCollectionFactory;
  76. $this->_aclResourceProvider = $aclResourceProvider;
  77. $this->_integrationData = $integrationData;
  78. parent::__construct($context, $data);
  79. }
  80. /**
  81. * Set core registry
  82. *
  83. * @param \Magento\Framework\Registry $coreRegistry
  84. * @return void
  85. * @deprecated 100.1.0
  86. * @since 100.1.0
  87. */
  88. public function setCoreRegistry(\Magento\Framework\Registry $coreRegistry)
  89. {
  90. $this->coreRegistry = $coreRegistry;
  91. }
  92. /**
  93. * Get core registry
  94. *
  95. * @return \Magento\Framework\Registry
  96. * @deprecated 100.1.0
  97. * @since 100.1.0
  98. */
  99. public function getCoreRegistry()
  100. {
  101. if (!($this->coreRegistry instanceof \Magento\Framework\Registry)) {
  102. return \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Framework\Registry::class);
  103. } else {
  104. return $this->coreRegistry;
  105. }
  106. }
  107. /**
  108. * Get tab label
  109. *
  110. * @return \Magento\Framework\Phrase
  111. */
  112. public function getTabLabel()
  113. {
  114. return __('Role Resources');
  115. }
  116. /**
  117. * Get tab title
  118. *
  119. * @return string
  120. */
  121. public function getTabTitle()
  122. {
  123. return $this->getTabLabel();
  124. }
  125. /**
  126. * Whether tab is available
  127. *
  128. * @return bool
  129. */
  130. public function canShowTab()
  131. {
  132. return true;
  133. }
  134. /**
  135. * Whether tab is visible
  136. *
  137. * @return bool
  138. */
  139. public function isHidden()
  140. {
  141. return false;
  142. }
  143. /**
  144. * Check if everything is allowed
  145. *
  146. * @return bool
  147. */
  148. public function isEverythingAllowed()
  149. {
  150. $selectedResources = $this->getSelectedResources();
  151. $id = $this->_rootResource->getId();
  152. return in_array($id, $selectedResources);
  153. }
  154. /**
  155. * Get selected resources
  156. *
  157. * @return array|mixed|\string[]
  158. * @since 100.1.0
  159. */
  160. public function getSelectedResources()
  161. {
  162. $selectedResources = $this->getData('selected_resources');
  163. if (empty($selectedResources)) {
  164. $allResource = $this->getCoreRegistry()->registry(SaveRole::RESOURCE_ALL_FORM_DATA_SESSION_KEY);
  165. if ($allResource) {
  166. $selectedResources = [$this->_rootResource->getId()];
  167. } else {
  168. $selectedResources = $this->getCoreRegistry()->registry(SaveRole::RESOURCE_FORM_DATA_SESSION_KEY);
  169. }
  170. if (null === $selectedResources) {
  171. $rid = $this->_request->getParam('rid', false);
  172. $selectedResources = $this->_aclRetriever->getAllowedResourcesByRole($rid);
  173. }
  174. $this->setData('selected_resources', $selectedResources);
  175. }
  176. return $selectedResources;
  177. }
  178. /**
  179. * Get Json Representation of Resource Tree
  180. *
  181. * @return array
  182. */
  183. public function getTree()
  184. {
  185. return $this->_integrationData->mapResources($this->getAclResources());
  186. }
  187. /**
  188. * Get lit of all ACL resources declared in the system.
  189. *
  190. * @return array
  191. */
  192. private function getAclResources()
  193. {
  194. $resources = $this->_aclResourceProvider->getAclResources();
  195. $configResource = array_filter(
  196. $resources,
  197. function ($node) {
  198. return isset($node['id'])
  199. && $node['id'] == 'Magento_Backend::admin';
  200. }
  201. );
  202. $configResource = reset($configResource);
  203. return isset($configResource['children']) ? $configResource['children'] : [];
  204. }
  205. }