src/commands/SetupController.php
public function actionUser()
    {
        while (true) {
            $email = $this->prompt('User E-Mail:');
            if (!empty(User::findByEmail($email))) {
                $this->outputError('The provided E-Mail already exists 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()) {
            return $this->outputError('User validation error: ' . VarDumper::dumpAsString($user->getErrors()));
        }

        $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.");
    }
public function actionUser()
    {
        while (true) {
            $email = $this->prompt('User E-Mail:');
            if (!empty(User::findByEmail($email))) {
                $this->outputError('The provided E-Mail already exists 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()) {
            return $this->outputError('User validation error: ' . VarDumper::dumpAsString($user->getErrors()));
        }

        $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.");
    }
public function actionUser ( ) { while ( true ) { $ email = $ this -> prompt ( 'User E-Mail:' ) ; if ( ! empty ( User :: findByEmail ( $ email ) ) ) { $ this -> outputError ( 'The provided E-Mail already exists 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 ( ) ) { return $ this -> outputError ( 'User validation error: ' . VarDumper :: dumpAsString ( $ user -> getErrors ( ) ) ) ; } $ 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." ) ; }
Create a new user and append them to an existing group.
@return bool
@throws Exception
Create a new user and append them to an existing group .