N98\Magento\Command\Admin\User\CreateUserCommand::execute PHP Method

execute() protected method

protected execute ( Symfony\Component\Console\Input\InputInterface $input, Symfony\Component\Console\Output\OutputInterface $output ) : integer | void
$input Symfony\Component\Console\Input\InputInterface
$output Symfony\Component\Console\Output\OutputInterface
return integer | void
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $this->detectMagento($output, true);
        if ($this->initMagento()) {
            $username = $this->getOrAskForArgument('username', $input, $output);
            $email = $this->getOrAskForArgument('email', $input, $output);
            if (($password = $input->getArgument('password')) === null) {
                /* @var $dialog DialogHelper */
                $dialog = $this->getHelper('dialog');
                $password = $dialog->askHiddenResponse($output, '<question>Password:</question>');
            }
            $firstname = $this->getOrAskForArgument('firstname', $input, $output);
            $lastname = $this->getOrAskForArgument('lastname', $input, $output);
            if (($roleName = $input->getArgument('role')) != null) {
                $role = $this->getRoleModel()->load($roleName, 'role_name');
                if (!$role->getId()) {
                    $output->writeln('<error>Role was not found</error>');
                    return;
                }
            } else {
                // create new role if not yet existing
                $role = $this->getRoleModel()->load('Development', 'role_name');
                if (!$role->getId()) {
                    $role->setName('Development')->setRoleType('G')->save();
                    $resourceAll = $this->_magentoMajorVersion == self::MAGENTO_MAJOR_VERSION_2 ? \Mage_Backend_Model_Acl_Config::ACL_RESOURCE_ALL : 'all';
                    // give "all" privileges to role
                    $this->getRulesModel()->setRoleId($role->getId())->setResources(array($resourceAll))->saveRel();
                    $output->writeln('<info>The role <comment>Development</comment> was automatically created.</info>');
                }
            }
            // create new user
            $user = $this->getUserModel()->setData(array('username' => $username, 'firstname' => $firstname, 'lastname' => $lastname, 'email' => $email, 'password' => $password, 'is_active' => 1))->save();
            if ($this->_magentoMajorVersion == self::MAGENTO_MAJOR_VERSION_2) {
                $user->setRoleId($role->getId())->save();
            } else {
                $user->setRoleIds(array($role->getId()))->setRoleUserId($user->getUserId())->saveRelations();
            }
            $output->writeln('<info>User <comment>' . $username . '</comment> successfully created</info>');
        }
    }
CreateUserCommand