0

I want to show user role list in my custom module edit page my code is as below

$fieldset->addField(
       'admin_role_ids',
       'multiselect',
       [
         'name'     => 'permission[admin_role_ids]',
         'label'    => __('Allowed for Roles'),
         'title'    => __('Allowed for Roles'),
         'values'   => $this->_adminSession->getUser()->getRole(),
       ]
    );

1 Answer 1

2

1) You can use class \Magento\Authorization\Model\ResourceModel\Role\Grid\Collection to get admin roles:

protected $roleCollectionFactory;

public function __construct(
  \Magento\Authorization\Model\ResourceModel\Role\Grid\CollectionFactory $roleCollectionFactory,
) {
  $this->roleCollectionFactory = $roleCollectionFactory;
}

public function getRoles()
{
    $role = $this->roleCollectionFactory->create();
    print_r($role->getData());
}

2) Use class Magento\Authorization\Model\ResourceModel\Role\Collection to get admin role with all admin user:

protected $roleCollectionFactory;

public function __construct(
  \Magento\Authorization\Model\ResourceModel\Role\CollectionFactory $roleCollectionFactory,
) {
  $this->roleCollectionFactory = $roleCollectionFactory;
}

public function getRoles()
{
    $role = $this->roleCollectionFactory->create();
    print_r($role->getData());
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.