1

and also an role

$roleAuthModel = $objectManager->create('\Magento\Authorization\Model\Role');            
            $roleAuthModel->setName("cool role")
                ->setPid(0) //set parent role id of your role
                ->setRoleType("G")
                ->setUserType(UserContextInterface::USER_TYPE_ADMIN);
            $roleAuthModel->save();

        $userModel->setUserName("bbbb");
        $userModel->setFirstName("cccc");
        $userModel->setLastName("dddd");
        $userModel->setEmail("[email protected]");
        $userModel->setPassword('Adfasdas322323das');
        $userModel->save();

How can I relate and set "cool role" to my new user "bbbb" ?

1 Answer 1

2

You can set role to admin user using role and user models.

/** @var \Magento\Authorization\Model\Role $role */
$role = $this->getRole();
/** @var $model \Magento\User\Model\User */
$user = $this->getUser();
$user->setRoleId($role->getId());

$user->save();

In your case:

$userModel->setRoleId($roleAuthModel->getId());
$userModel->save();

Source: \Magento\User\Controller\Adminhtml\User\Save::execute

Your Answer

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

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