/** * This command creates new user account with admin role. If password is not set, this command will generate new 8-char password. * After saving user to database, this command uses mailer component to send credentials (username and password) to * user via email. * * @param string $email Email address * @param string $username Username * @param null|string $password Password (if null it will be generated automatically) */ public function actionAdmin($email, $username, $password = null) { $user = \Yii::createObject(['class' => User::className(), 'scenario' => 'create', 'email' => $email, 'username' => $username, 'password' => $password]); if ($user->create()) { $auth = \Yii::$app->authManager; $role = $auth->getRole('admin'); $assignment = $auth->assign($role, $user->getId()); if (empty($assignment)) { $this->stdout(\Yii::t('user', 'User has been created') . "!\n", Console::FG_GREEN); $this->stdout('Could not assign user for admin role: ' . \yii\helpers\CVarDumper::dumpAsString($assignment) . "\n", Console::FG_RED); } else { $this->stdout(\Yii::t('user', 'User with admin role has been created') . "!\n", Console::FG_GREEN); } } else { $this->stdout(\Yii::t('user', 'Please fix following errors:') . "\n", Console::FG_RED); foreach ($user->errors as $errors) { foreach ($errors as $error) { $this->stdout(" - " . $error . "\n", Console::FG_RED); } } } }
/** * Creates a new User model. * If creation is successful, the browser will be redirected to the 'index' page. * @return mixed */ public function actionCreate() { /** @var User $user */ $user = \Yii::createObject(['class' => User::className(), 'scenario' => 'create']); $profile = \Yii::createObject(['class' => Profile::className()]); $r = \Yii::$app->request; $this->performAjaxValidation($user); if ($user->load($r->post()) && $user->create()) { $profile = $this->finder->findProfileById($user->id); $profile->load($r->post()); $profile->save(); Event::trigger(BaseController::class, BaseController::EVENT_MODEL_CREATED, new MessageEvent($profile)); $this->trigger(BaseController::EVENT_MODEL_CREATED, new MessageEvent($user)); \Yii::$app->getSession()->setFlash('success', \Yii::t('user', 'User has been created')); return $this->redirect(['index']); } return $this->render('create', ['user' => $user, 'profile' => $profile]); }