Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
User.php
Go to the documentation of this file.
1 <?php
7 
9 
16 class User extends \Magento\Backend\Block\Widget\Grid\Extended
17 {
23  protected $_coreRegistry = null;
24 
30  protected $_roleFactory;
31 
35  protected $_jsonEncoder;
36 
40  protected $_userRolesFactory;
41 
47 
57  public function __construct(
58  \Magento\Backend\Block\Template\Context $context,
59  \Magento\Backend\Helper\Data $backendHelper,
60  \Magento\Framework\Json\EncoderInterface $jsonEncoder,
61  \Magento\Framework\Registry $coreRegistry,
62  \Magento\Authorization\Model\RoleFactory $roleFactory,
63  \Magento\User\Model\ResourceModel\Role\User\CollectionFactory $userRolesFactory,
64  array $data = []
65  ) {
66  parent::__construct($context, $backendHelper, $data);
67  $this->_jsonEncoder = $jsonEncoder;
68  $this->_coreRegistry = $coreRegistry;
69  $this->_roleFactory = $roleFactory;
70  $this->_userRolesFactory = $userRolesFactory;
71  }
72 
78  protected function _construct()
79  {
80  parent::_construct();
81  $this->setDefaultSort('role_user_id');
82  $this->setDefaultDir('asc');
83  $this->setId('roleUserGrid');
84  $this->setUseAjax(true);
85  }
86 
91  protected function _addColumnFilterToCollection($column)
92  {
93  if ($column->getId() == 'in_role_users') {
94  $inRoleIds = $this->getUsers();
95  if (empty($inRoleIds)) {
96  $inRoleIds = 0;
97  }
98  if ($column->getFilter()->getValue()) {
99  $this->getCollection()->addFieldToFilter('user_id', ['in' => $inRoleIds]);
100  } else {
101  if ($inRoleIds) {
102  $this->getCollection()->addFieldToFilter('user_id', ['nin' => $inRoleIds]);
103  }
104  }
105  } else {
106  parent::_addColumnFilterToCollection($column);
107  }
108  return $this;
109  }
110 
114  protected function _prepareCollection()
115  {
116  $roleId = $this->getRequest()->getParam('rid');
117  $this->_coreRegistry->register('RID', $roleId);
118  $collection = $this->_userRolesFactory->create();
119  $this->setCollection($collection);
120  return parent::_prepareCollection();
121  }
122 
126  protected function _prepareColumns()
127  {
128  $this->addColumn(
129  'in_role_users',
130  [
131  'header_css_class' => 'a-center',
132  'type' => 'checkbox',
133  'name' => 'in_role_users',
134  'values' => $this->getUsers(),
135  'align' => 'center',
136  'index' => 'user_id'
137  ]
138  );
139 
140  $this->addColumn(
141  'role_user_id',
142  ['header' => __('User ID'), 'width' => 5, 'align' => 'left', 'sortable' => true, 'index' => 'user_id']
143  );
144 
145  $this->addColumn(
146  'role_user_username',
147  ['header' => __('User Name'), 'align' => 'left', 'index' => 'username']
148  );
149 
150  $this->addColumn(
151  'role_user_firstname',
152  ['header' => __('First Name'), 'align' => 'left', 'index' => 'firstname']
153  );
154 
155  $this->addColumn(
156  'role_user_lastname',
157  ['header' => __('Last Name'), 'align' => 'left', 'index' => 'lastname']
158  );
159 
160  $this->addColumn(
161  'role_user_email',
162  ['header' => __('Email'), 'width' => 40, 'align' => 'left', 'index' => 'email']
163  );
164 
165  $this->addColumn(
166  'role_user_is_active',
167  [
168  'header' => __('Status'),
169  'index' => 'is_active',
170  'align' => 'left',
171  'type' => 'options',
172  'options' => ['1' => __('Active'), '0' => __('Inactive')]
173  ]
174  );
175 
176  return parent::_prepareColumns();
177  }
178 
182  public function getGridUrl()
183  {
184  $roleId = $this->getRequest()->getParam('rid');
185  return $this->getUrl('*/*/editrolegrid', ['rid' => $roleId]);
186  }
187 
192  public function getUsers($json = false)
193  {
194  if ($this->getRequest()->getParam('in_role_user') != "") {
195  return $this->getRequest()->getParam('in_role_user');
196  }
197  $roleId = $this->getRequest()->getParam(
198  'rid'
199  ) > 0 ? $this->getRequest()->getParam(
200  'rid'
201  ) : $this->_coreRegistry->registry(
202  'RID'
203  );
204 
205  $users = $this->getUsersFormData();
206  if (false === $users) {
207  $users = $this->_roleFactory->create()->setId($roleId)->getRoleUsers();
208  }
209  if (sizeof($users) > 0) {
210  if ($json) {
211  $jsonUsers = [];
212  foreach ($users as $usrid) {
213  $jsonUsers[$usrid] = 0;
214  }
215  return $this->_jsonEncoder->encode((object)$jsonUsers);
216  } else {
217  return array_values($users);
218  }
219  } else {
220  if ($json) {
221  return '{}';
222  } else {
223  return [];
224  }
225  }
226  }
227 
234  protected function getUsersFormData()
235  {
236  if (false !== $this->restoredUsersFormData && null === $this->restoredUsersFormData) {
237  $this->restoredUsersFormData = $this->restoreUsersFormData();
238  }
239 
241  }
242 
249  protected function restoreUsersFormData()
250  {
251  $sessionData = $this->_coreRegistry->registry(
252  \Magento\User\Controller\Adminhtml\User\Role\SaveRole::IN_ROLE_USER_FORM_DATA_SESSION_KEY
253  );
254  if (null !== $sessionData) {
255  parse_str($sessionData, $sessionData);
256  return array_keys($sessionData);
257  }
258 
259  return false;
260  }
261 }
__construct(\Magento\Backend\Block\Template\Context $context, \Magento\Backend\Helper\Data $backendHelper, \Magento\Framework\Json\EncoderInterface $jsonEncoder, \Magento\Framework\Registry $coreRegistry, \Magento\Authorization\Model\RoleFactory $roleFactory, \Magento\User\Model\ResourceModel\Role\User\CollectionFactory $userRolesFactory, array $data=[])
Definition: User.php:57
__()
Definition: __.php:13
_addColumnFilterToCollection($column)
Definition: User.php:91
$roleId
Definition: webapi_user.php:22
getParam($paramName, $default=null)
Definition: Grid.php:729