Reading list Switch to dark mode

    Magento 2- How to create user role programmatically by custom module installer

    Updated 21 February 2024

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

    1=>First you need to create an installer file named InstallData.php in our custom module Setup folder

    complete path: app/code/ModuleNameSpace/YourModuleName/Setup

    <?php
    
    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=$this->roleFactory->create();
            $role->setName('YourRoleName') //Set Role Name Which you want to create 
                    ->setPid(0) //set parent role id of your role
                    ->setRoleType(RoleGroup::ROLE_TYPE) 
                    ->setUserType(UserContextInterface::USER_TYPE_ADMIN);
            $role->save();
            /* Now we set that which resources we allow to this role */
            $resource=['Magento_Backend::admin',
    					'Magento_Sales::sales',
    					'Magento_Sales::create',
    					'Magento_Sales::actions_view', //you will use resource id which you want to allow
    					'Magento_Sales::cancel'
    				  ];
            /* Array of resource ids which we want to allow this role*/
            $this->rulesFactory->create()->setRoleId($role->getId())->setResources($resource)->saveRel();
        }
    }

    You can get resources id as explain in following snap
    ACL

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

    Searching for an experienced
    Magento Company ?
    Find out More

    then by terminal run the 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 as follows 🙂

    Selection_057
    Selection_058

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home