User.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\User\Block\Role\Grid;
  7. use Magento\Backend\Block\Widget\Grid\Column;
  8. /**
  9. * Acl role user grid.
  10. *
  11. * @api
  12. * @since 100.0.2
  13. */
  14. class User extends \Magento\Backend\Block\Widget\Grid\Extended
  15. {
  16. /**
  17. * Core registry
  18. *
  19. * @var \Magento\Framework\Registry
  20. */
  21. protected $_coreRegistry = null;
  22. /**
  23. * Factory for user role model
  24. *
  25. * @var \Magento\Authorization\Model\RoleFactory
  26. */
  27. protected $_roleFactory;
  28. /**
  29. * @var \Magento\Framework\Json\EncoderInterface
  30. */
  31. protected $_jsonEncoder;
  32. /**
  33. * @var \Magento\User\Model\ResourceModel\Role\User\CollectionFactory
  34. */
  35. protected $_userRolesFactory;
  36. /**
  37. * @var bool|array
  38. * @since 100.1.0
  39. */
  40. protected $restoredUsersFormData;
  41. /**
  42. * @param \Magento\Backend\Block\Template\Context $context
  43. * @param \Magento\Backend\Helper\Data $backendHelper
  44. * @param \Magento\Framework\Json\EncoderInterface $jsonEncoder
  45. * @param \Magento\Framework\Registry $coreRegistry
  46. * @param \Magento\Authorization\Model\RoleFactory $roleFactory
  47. * @param \Magento\User\Model\ResourceModel\Role\User\CollectionFactory $userRolesFactory
  48. * @param array $data
  49. */
  50. public function __construct(
  51. \Magento\Backend\Block\Template\Context $context,
  52. \Magento\Backend\Helper\Data $backendHelper,
  53. \Magento\Framework\Json\EncoderInterface $jsonEncoder,
  54. \Magento\Framework\Registry $coreRegistry,
  55. \Magento\Authorization\Model\RoleFactory $roleFactory,
  56. \Magento\User\Model\ResourceModel\Role\User\CollectionFactory $userRolesFactory,
  57. array $data = []
  58. ) {
  59. parent::__construct($context, $backendHelper, $data);
  60. $this->_jsonEncoder = $jsonEncoder;
  61. $this->_coreRegistry = $coreRegistry;
  62. $this->_roleFactory = $roleFactory;
  63. $this->_userRolesFactory = $userRolesFactory;
  64. }
  65. /**
  66. * Class constructor
  67. *
  68. * @return void
  69. */
  70. protected function _construct()
  71. {
  72. parent::_construct();
  73. $this->setDefaultSort('role_user_id');
  74. $this->setDefaultDir('asc');
  75. $this->setId('roleUserGrid');
  76. $this->setUseAjax(true);
  77. }
  78. /**
  79. * @param Column $column
  80. * @return $this
  81. */
  82. protected function _addColumnFilterToCollection($column)
  83. {
  84. if ($column->getId() == 'in_role_users') {
  85. $inRoleIds = $this->getUsers();
  86. if (empty($inRoleIds)) {
  87. $inRoleIds = 0;
  88. }
  89. if ($column->getFilter()->getValue()) {
  90. $this->getCollection()->addFieldToFilter('user_id', ['in' => $inRoleIds]);
  91. } else {
  92. if ($inRoleIds) {
  93. $this->getCollection()->addFieldToFilter('user_id', ['nin' => $inRoleIds]);
  94. }
  95. }
  96. } else {
  97. parent::_addColumnFilterToCollection($column);
  98. }
  99. return $this;
  100. }
  101. /**
  102. * @return $this
  103. */
  104. protected function _prepareCollection()
  105. {
  106. $roleId = $this->getRequest()->getParam('rid');
  107. $this->_coreRegistry->register('RID', $roleId);
  108. $collection = $this->_userRolesFactory->create();
  109. $this->setCollection($collection);
  110. return parent::_prepareCollection();
  111. }
  112. /**
  113. * @return $this
  114. */
  115. protected function _prepareColumns()
  116. {
  117. $this->addColumn(
  118. 'in_role_users',
  119. [
  120. 'header_css_class' => 'a-center',
  121. 'type' => 'checkbox',
  122. 'name' => 'in_role_users',
  123. 'values' => $this->getUsers(),
  124. 'align' => 'center',
  125. 'index' => 'user_id'
  126. ]
  127. );
  128. $this->addColumn(
  129. 'role_user_id',
  130. ['header' => __('User ID'), 'width' => 5, 'align' => 'left', 'sortable' => true, 'index' => 'user_id']
  131. );
  132. $this->addColumn(
  133. 'role_user_username',
  134. ['header' => __('User Name'), 'align' => 'left', 'index' => 'username']
  135. );
  136. $this->addColumn(
  137. 'role_user_firstname',
  138. ['header' => __('First Name'), 'align' => 'left', 'index' => 'firstname']
  139. );
  140. $this->addColumn(
  141. 'role_user_lastname',
  142. ['header' => __('Last Name'), 'align' => 'left', 'index' => 'lastname']
  143. );
  144. $this->addColumn(
  145. 'role_user_email',
  146. ['header' => __('Email'), 'width' => 40, 'align' => 'left', 'index' => 'email']
  147. );
  148. $this->addColumn(
  149. 'role_user_is_active',
  150. [
  151. 'header' => __('Status'),
  152. 'index' => 'is_active',
  153. 'align' => 'left',
  154. 'type' => 'options',
  155. 'options' => ['1' => __('Active'), '0' => __('Inactive')]
  156. ]
  157. );
  158. return parent::_prepareColumns();
  159. }
  160. /**
  161. * @return string
  162. */
  163. public function getGridUrl()
  164. {
  165. $roleId = $this->getRequest()->getParam('rid');
  166. return $this->getUrl('*/*/editrolegrid', ['rid' => $roleId]);
  167. }
  168. /**
  169. * @param bool $json
  170. * @return string|array
  171. */
  172. public function getUsers($json = false)
  173. {
  174. if ($this->getRequest()->getParam('in_role_user') != "") {
  175. return $this->getRequest()->getParam('in_role_user');
  176. }
  177. $roleId = $this->getRequest()->getParam(
  178. 'rid'
  179. ) > 0 ? $this->getRequest()->getParam(
  180. 'rid'
  181. ) : $this->_coreRegistry->registry(
  182. 'RID'
  183. );
  184. $users = $this->getUsersFormData();
  185. if (false === $users) {
  186. $users = $this->_roleFactory->create()->setId($roleId)->getRoleUsers();
  187. }
  188. if (sizeof($users) > 0) {
  189. if ($json) {
  190. $jsonUsers = [];
  191. foreach ($users as $usrid) {
  192. $jsonUsers[$usrid] = 0;
  193. }
  194. return $this->_jsonEncoder->encode((object)$jsonUsers);
  195. } else {
  196. return array_values($users);
  197. }
  198. } else {
  199. if ($json) {
  200. return '{}';
  201. } else {
  202. return [];
  203. }
  204. }
  205. }
  206. /**
  207. * Get Form Data if exist
  208. *
  209. * @return array|bool
  210. * @since 100.1.0
  211. */
  212. protected function getUsersFormData()
  213. {
  214. if (false !== $this->restoredUsersFormData && null === $this->restoredUsersFormData) {
  215. $this->restoredUsersFormData = $this->restoreUsersFormData();
  216. }
  217. return $this->restoredUsersFormData;
  218. }
  219. /**
  220. * Restore Users Form Data from the registry
  221. *
  222. * @return array|bool
  223. * @since 100.1.0
  224. */
  225. protected function restoreUsersFormData()
  226. {
  227. $sessionData = $this->_coreRegistry->registry(
  228. \Magento\User\Controller\Adminhtml\User\Role\SaveRole::IN_ROLE_USER_FORM_DATA_SESSION_KEY
  229. );
  230. if (null !== $sessionData) {
  231. parse_str($sessionData, $sessionData);
  232. return array_keys($sessionData);
  233. }
  234. return false;
  235. }
  236. }