luya\console\commands\SetupController::actionUser PHP Method

actionUser() public method

Create a new user and append them to an existing group.
public actionUser ( ) : boolean
return boolean
    public function actionUser()
    {
        while (true) {
            $email = $this->prompt('User E-Mail:');
            if (!empty(User::findByEmail($email))) {
                $this->outputError('The provided E-Mail already exsists in the System.');
            } else {
                break;
            }
        }
        $titleArray = ['Mr' => 1, 'Mrs' => 2];
        $title = $this->select('Title:', $titleArray);
        $firstname = $this->prompt('Firstname:');
        $lastname = $this->prompt('Lastname:');
        $password = $this->prompt('User Password:');
        if ($this->confirm("Are you sure to create the User '{$email}'?") !== true) {
            return $this->outputError('Abort user creation process.');
        }
        $user = new User();
        $user->email = $email;
        $user->password_salt = Yii::$app->getSecurity()->generateRandomString();
        $user->password = Yii::$app->getSecurity()->generatePasswordHash($password . $user->password_salt);
        $user->firstname = $firstname;
        $user->lastname = $lastname;
        $user->title = $titleArray[$title];
        if (!$user->save()) {
            throw new Exception("Unable to create new user.");
        }
        $groupSelect = [];
        foreach (Group::find()->all() as $entry) {
            $groupSelect[$entry->id] = $entry->name . ' (' . $entry->text . ')';
            $this->output($entry->id . ' - ' . $groupSelect[$entry->id]);
        }
        $groupId = $this->select('Select Group the user should belong to:', $groupSelect);
        $this->insert('admin_user_group', ['user_id' => $user->id, 'group_id' => $groupId]);
        return $this->outputSuccess("The user ({$email}) has been created.");
    }