/** @inheritdoc */ public function rules() { return [['email', 'filter', 'filter' => 'trim'], ['email', 'required'], ['email', 'email'], ['email', 'exist', 'targetClass' => $this->module->modelMap['User'], 'message' => \Yii::t('user', 'There is no user with this email address')], ['email', function ($attribute) { $this->user = $this->finder->findUserByEmail($this->email); if ($this->user !== null && $this->module->enableConfirmation && !$this->user->getIsConfirmed()) { $this->addError($attribute, \Yii::t('user', 'You need to confirm your email address')); } }], ['password', 'required'], ['password', 'string', 'min' => 6]]; }
/** * Validates form and logs the user in. * @return boolean whether the user is logged in successfully */ public function login() { if ($this->validate()) { $this->user->setOffsetFromUTC($this->offsetFromUTC); return \Yii::$app->getUser()->login($this->user, $this->rememberMe ? $this->module->rememberFor : 0); } else { return false; } }
/** * 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]); }