I want to create admin role and assign a user to it with only sales and catalog resources by installer is any one having idea for this.

1 Answer 1


Here we create admin user role programmatically by custom module installer with specific resources.

First you need to create a installer file name InstallData.php in our custom module Setup folder

complete path:


    namespace ModuleNameSpace\YourModuleName\Setup; 

    use Magento\Framework\Setup\InstallDataInterface; 

    use Magento\Framework\Setup\ModuleContextInterface; 

    use Magento\Framework\Setup\ModuleDataSetupInterface; 

    /* For get RoleType and UserType for create Role */; 
    use Magento\Authorization\Model\Acl\Role\Group as RoleGroup; 

    use Magento\Authorization\Model\UserContextInterface; 

    /** * @codeCoverageIgnore */ 

    class InstallData implements InstallDataInterface { 

    /** * RoleFactory * * @var roleFactory */ 

    private $roleFactory; 

    /** * RulesFactory * * @var rulesFactory */ 

    private $rulesFactory; 

    /** * Init * * @param \Magento\Authorization\Model\RoleFactory $roleFactory * @param \Magento\Authorization\Model\RulesFactory $rulesFactory */ 

    public function __construct( 
    \Magento\Authorization\Model\RoleFactory $roleFactory, /* Instance of Role*/
 \Magento\Authorization\Model\RulesFactory $rulesFactory /* Instance of Rule */ 
/*this define that which resource permitted to wich role */ ) { 
    $this->roleFactory = $roleFactory; 
    $this->rulesFactory = $rulesFactory; 

    /** * {@inheritdoc} * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ 

    public function install(
    ModuleDataSetupInterface $setup,
    ModuleContextInterface $context) { 
    /** * Create Warehouse role */
   $role->setName('YourRoleName') //Set Role Name Which you want to create 

   ->setPid(0) //set parent role id of your role 

    /* Now we set that which resources we allow to this role */ $resource=[
                    'Magento_Sales::actions_view',  //you will use resource id which you want to allow


    /* Array of resource ids which we want to allow this role*/ 

You can get resource ids from the following grep (run this in your magento root folder)

egrep -hiIro --exclude='*.php' --exclude='.bash_history' --exclude='./src/vendor/magento/magento2-base/dev/tests' --exclude='./src/dev/tests/*' '<resource ref="([A-Za-z\_]*::[A-Za-z\_]*)"' ./vendor 2> /dev/null |less

After adding these file install your custom module in your magento2 instance

then by terminal run following command in your magento2 root directory

php bin/magento setup:upgrade

Now  your admin user role created  you can check it  from System > Permission > User Role.

Reference: Create user role programmatically using custom module installer

  • It is not working
    – Tanmay
    Oct 6, 2020 at 9:27

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.