123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\User\Block\User\Edit\Tab;
- use Magento\Backend\Block\Widget\Grid\Column;
- /**
- * @api
- * @since 100.0.2
- */
- class Roles extends \Magento\Backend\Block\Widget\Grid\Extended
- {
- /**
- * Core registry
- *
- * @var \Magento\Framework\Registry
- */
- protected $_coreRegistry = null;
- /**
- * @var \Magento\Authorization\Model\ResourceModel\Role\CollectionFactory
- */
- protected $_userRolesFactory;
- /**
- * @var \Magento\Framework\Json\EncoderInterface
- */
- protected $_jsonEncoder;
- /**
- * @param \Magento\Backend\Block\Template\Context $context
- * @param \Magento\Backend\Helper\Data $backendHelper
- * @param \Magento\Framework\Json\EncoderInterface $jsonEncoder
- * @param \Magento\Authorization\Model\ResourceModel\Role\CollectionFactory $userRolesFactory
- * @param \Magento\Framework\Registry $coreRegistry
- * @param array $data
- */
- public function __construct(
- \Magento\Backend\Block\Template\Context $context,
- \Magento\Backend\Helper\Data $backendHelper,
- \Magento\Framework\Json\EncoderInterface $jsonEncoder,
- \Magento\Authorization\Model\ResourceModel\Role\CollectionFactory $userRolesFactory,
- \Magento\Framework\Registry $coreRegistry,
- array $data = []
- ) {
- $this->_jsonEncoder = $jsonEncoder;
- $this->_userRolesFactory = $userRolesFactory;
- $this->_coreRegistry = $coreRegistry;
- parent::__construct($context, $backendHelper, $data);
- }
- /**
- * Class constructor
- *
- * @return void
- */
- protected function _construct()
- {
- parent::_construct();
- $this->setId('permissionsUserRolesGrid');
- $this->setDefaultSort('sort_order');
- $this->setDefaultDir('asc');
- $this->setTitle(__('User Roles Information'));
- $this->setUseAjax(true);
- }
- /**
- * @param Column $column
- * @return $this
- */
- protected function _addColumnFilterToCollection($column)
- {
- if ($column->getId() == 'assigned_user_role') {
- $userRoles = $this->getSelectedRoles();
- if (empty($userRoles)) {
- $userRoles = 0;
- }
- if ($column->getFilter()->getValue()) {
- $this->getCollection()->addFieldToFilter('role_id', ['in' => $userRoles]);
- } else {
- if ($userRoles) {
- $this->getCollection()->addFieldToFilter('role_id', ['nin' => $userRoles]);
- }
- }
- } else {
- parent::_addColumnFilterToCollection($column);
- }
- return $this;
- }
- /**
- * @return $this
- */
- protected function _prepareCollection()
- {
- $collection = $this->_userRolesFactory->create();
- $collection->setRolesFilter();
- $this->setCollection($collection);
- return parent::_prepareCollection();
- }
- /**
- * @return $this
- */
- protected function _prepareColumns()
- {
- $this->addColumn(
- 'assigned_user_role',
- [
- 'header_css_class' => 'data-grid-actions-cell',
- 'header' => __('Assigned'),
- 'type' => 'radio',
- 'html_name' => 'roles[]',
- 'values' => $this->getSelectedRoles(),
- 'align' => 'center',
- 'index' => 'role_id'
- ]
- );
- $this->addColumn('role_name', ['header' => __('Role'), 'index' => 'role_name']);
- return parent::_prepareColumns();
- }
- /**
- * @return string
- */
- public function getGridUrl()
- {
- $userPermissions = $this->_coreRegistry->registry('permissions_user');
- return $this->getUrl('*/*/rolesGrid', ['user_id' => $userPermissions->getUserId()]);
- }
- /**
- * @param bool $json
- * @return array|string
- */
- public function getSelectedRoles($json = false)
- {
- if ($this->getRequest()->getParam('user_roles') != "") {
- return $this->getRequest()->getParam('user_roles');
- }
- /* @var $user \Magento\User\Model\User */
- $user = $this->_coreRegistry->registry('permissions_user');
- //checking if we have this data and we
- //don't need load it through resource model
- if ($user->hasData('roles')) {
- $userRoles = $user->getData('roles');
- } else {
- $userRoles = $user->getRoles();
- }
- if ($json) {
- $jsonRoles = [];
- foreach ($userRoles as $roleId) {
- $jsonRoles[$roleId] = 0;
- }
- return $this->_jsonEncoder->encode((object)$jsonRoles);
- } else {
- return $userRoles;
- }
- }
- }
|