/** * Signs user up. * * @return User|null the saved model or null if saving fails */ public function signup() { if ($this->validate()) { $user = new User(); $user->username = $this->username; $user->email = $this->email; $user->phone = $this->phone; $user->type = $this->type; $user->status = User::STATUS_WAIT; $user->setPassword($this->password); $user->generateAuthKey(); $user->generateEmailConfirmToken(); if ($user->save()) { Yii::$app->mailer->compose(['text' => 'confirmEmail'], ['user' => $user])->setFrom([\Yii::$app->params['supportEmail'] => \Yii::$app->name])->setTo($this->email)->setSubject('Email confirmation for ' . Yii::$app->name)->send(); } return $user; } return null; }
/** * @todo don't write directly in db table (admin_user_group) * @todo reuse encode password function from user model * @todo find better solution for while(true)/break loop * @todo find better solution for title array/selection * @todo error handling (validate, can't save models) * * @param $title * @param $firstname * @param $lastname * @param $email * @param $password */ public function actionUser() { while (true) { $email = $this->prompt('Benutzer E-Mail:'); if (!empty(User::findByEmail($email))) { $this->outputError('Email existiert bereits!'); } else { break; } } $titleArray = ['Herr' => 1, 'Frau' => 2]; $title = $this->select('Anrede:', ['Herr' => '1', 'Frau' => '2']); $firstname = $this->prompt('Vorname:'); $lastname = $this->prompt('Nachname:'); $password = $this->prompt('Benutzer Passwort:'); if (!$this->confirm("Einen neuen Benutzer '{$title} {$firstname} {$lastname}, Email {$email}' mit dem Passwort '{$password}' anlegen?")) { 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]; $user->save(); $userId = $user->id; $groupEntries = Group::find()->all(); $groupSelect = []; $this->output(''); foreach ($groupEntries as $entry) { $groupSelect[$entry->id] = $entry->name . ' (' . $entry->text . ')'; $this->output($entry->id . ' - ' . $groupSelect[$entry->id]); } $groupId = $this->select('Benutzergruppe:', $groupSelect); $this->insert('admin_user_group', ['user_id' => $userId, 'group_id' => $groupId]); return $this->outputSuccess("Der Benutzer {$firstname} {$lastname} wurde erfolgreich angelegt."); }