Roles.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\User\Block\User\Edit\Tab;
  7. use Magento\Backend\Block\Widget\Grid\Column;
  8. /**
  9. * @api
  10. * @since 100.0.2
  11. */
  12. class Roles extends \Magento\Backend\Block\Widget\Grid\Extended
  13. {
  14. /**
  15. * Core registry
  16. *
  17. * @var \Magento\Framework\Registry
  18. */
  19. protected $_coreRegistry = null;
  20. /**
  21. * @var \Magento\Authorization\Model\ResourceModel\Role\CollectionFactory
  22. */
  23. protected $_userRolesFactory;
  24. /**
  25. * @var \Magento\Framework\Json\EncoderInterface
  26. */
  27. protected $_jsonEncoder;
  28. /**
  29. * @param \Magento\Backend\Block\Template\Context $context
  30. * @param \Magento\Backend\Helper\Data $backendHelper
  31. * @param \Magento\Framework\Json\EncoderInterface $jsonEncoder
  32. * @param \Magento\Authorization\Model\ResourceModel\Role\CollectionFactory $userRolesFactory
  33. * @param \Magento\Framework\Registry $coreRegistry
  34. * @param array $data
  35. */
  36. public function __construct(
  37. \Magento\Backend\Block\Template\Context $context,
  38. \Magento\Backend\Helper\Data $backendHelper,
  39. \Magento\Framework\Json\EncoderInterface $jsonEncoder,
  40. \Magento\Authorization\Model\ResourceModel\Role\CollectionFactory $userRolesFactory,
  41. \Magento\Framework\Registry $coreRegistry,
  42. array $data = []
  43. ) {
  44. $this->_jsonEncoder = $jsonEncoder;
  45. $this->_userRolesFactory = $userRolesFactory;
  46. $this->_coreRegistry = $coreRegistry;
  47. parent::__construct($context, $backendHelper, $data);
  48. }
  49. /**
  50. * Class constructor
  51. *
  52. * @return void
  53. */
  54. protected function _construct()
  55. {
  56. parent::_construct();
  57. $this->setId('permissionsUserRolesGrid');
  58. $this->setDefaultSort('sort_order');
  59. $this->setDefaultDir('asc');
  60. $this->setTitle(__('User Roles Information'));
  61. $this->setUseAjax(true);
  62. }
  63. /**
  64. * @param Column $column
  65. * @return $this
  66. */
  67. protected function _addColumnFilterToCollection($column)
  68. {
  69. if ($column->getId() == 'assigned_user_role') {
  70. $userRoles = $this->getSelectedRoles();
  71. if (empty($userRoles)) {
  72. $userRoles = 0;
  73. }
  74. if ($column->getFilter()->getValue()) {
  75. $this->getCollection()->addFieldToFilter('role_id', ['in' => $userRoles]);
  76. } else {
  77. if ($userRoles) {
  78. $this->getCollection()->addFieldToFilter('role_id', ['nin' => $userRoles]);
  79. }
  80. }
  81. } else {
  82. parent::_addColumnFilterToCollection($column);
  83. }
  84. return $this;
  85. }
  86. /**
  87. * @return $this
  88. */
  89. protected function _prepareCollection()
  90. {
  91. $collection = $this->_userRolesFactory->create();
  92. $collection->setRolesFilter();
  93. $this->setCollection($collection);
  94. return parent::_prepareCollection();
  95. }
  96. /**
  97. * @return $this
  98. */
  99. protected function _prepareColumns()
  100. {
  101. $this->addColumn(
  102. 'assigned_user_role',
  103. [
  104. 'header_css_class' => 'data-grid-actions-cell',
  105. 'header' => __('Assigned'),
  106. 'type' => 'radio',
  107. 'html_name' => 'roles[]',
  108. 'values' => $this->getSelectedRoles(),
  109. 'align' => 'center',
  110. 'index' => 'role_id'
  111. ]
  112. );
  113. $this->addColumn('role_name', ['header' => __('Role'), 'index' => 'role_name']);
  114. return parent::_prepareColumns();
  115. }
  116. /**
  117. * @return string
  118. */
  119. public function getGridUrl()
  120. {
  121. $userPermissions = $this->_coreRegistry->registry('permissions_user');
  122. return $this->getUrl('*/*/rolesGrid', ['user_id' => $userPermissions->getUserId()]);
  123. }
  124. /**
  125. * @param bool $json
  126. * @return array|string
  127. */
  128. public function getSelectedRoles($json = false)
  129. {
  130. if ($this->getRequest()->getParam('user_roles') != "") {
  131. return $this->getRequest()->getParam('user_roles');
  132. }
  133. /* @var $user \Magento\User\Model\User */
  134. $user = $this->_coreRegistry->registry('permissions_user');
  135. //checking if we have this data and we
  136. //don't need load it through resource model
  137. if ($user->hasData('roles')) {
  138. $userRoles = $user->getData('roles');
  139. } else {
  140. $userRoles = $user->getRoles();
  141. }
  142. if ($json) {
  143. $jsonRoles = [];
  144. foreach ($userRoles as $roleId) {
  145. $jsonRoles[$roleId] = 0;
  146. }
  147. return $this->_jsonEncoder->encode((object)$jsonRoles);
  148. } else {
  149. return $userRoles;
  150. }
  151. }
  152. }