Back to AddUserCommand class

Method doExecute

protected int
doExecute
(\Symfony\Component\Console\Input\InputInterface $input, \Symfony\Component\Console\Output\OutputInterface $output)
Internal function to execute the command.
Parameters
  • \Symfony\Component\Console\Input\InputInterface $input The input to inject into the command.
  • \Symfony\Component\Console\Output\OutputInterface $output The output to inject into the command.
Returns
  • int The command exit code
Since
  • 4.0.0

Method doExecute - Source code

/**
 * Internal function to execute the command.
 *
 * @param   InputInterface   $input   The input to inject into the command.
 * @param   OutputInterface  $output  The output to inject into the command.
 *
 * @return  integer  The command exit code
 *
 * @since   4.0.0
 */
protected function doExecute(InputInterface $input, OutputInterface $output) : int
{
    $this->configureIO($input, $output);
    $this->ioStyle->title('Add user');
    $this->user = $this->getStringFromOption('username', 'Please enter a username');
    $this->name = $this->getStringFromOption('name', 'Please enter a name (full name of user)');
    $this->email = $this->getStringFromOption('email', 'Please enter an email address');
    $this->password = $this->getStringFromOption('password', 'Please enter a password');
    $this->userGroups = $this->getUserGroups();
    if (\in_array("error", $this->userGroups)) {
        $this->ioStyle->error("'" . $this->userGroups[1] . "' user group doesn't exist!");
        return Command::FAILURE;
    }
    // Get filter to remove invalid characters
    $filter = new InputFilter();
    $user['username'] = $filter->clean($this->user, 'USERNAME');
    $user['password'] = $this->password;
    $user['name'] = $filter->clean($this->name, 'STRING');
    $user['email'] = $this->email;
    $user['groups'] = $this->userGroups;
    $userObj = User::getInstance();
    $userObj->bind($user);
    if (!$userObj->save()) {
        switch ($userObj->getError()) {
            case "JLIB_DATABASE_ERROR_USERNAME_INUSE":
                $this->ioStyle->error("The username already exists!");
                break;
            case "JLIB_DATABASE_ERROR_EMAIL_INUSE":
                $this->ioStyle->error("The email address already exists!");
                break;
            case "JLIB_DATABASE_ERROR_VALID_MAIL":
                $this->ioStyle->error("The email address is invalid!");
                break;
        }
        return 1;
    }
    $this->ioStyle->success("User created!");
    return Command::SUCCESS;
}