/** @inheritdoc */ public function rules() { return ['emailTrim' => ['email', 'filter', 'filter' => 'trim'], 'emailRequired' => ['email', 'required'], 'emailPattern' => ['email', 'email'], 'emailExist' => ['email', 'exist', 'targetClass' => $this->module->modelMap['User'], 'message' => Yii::t('user', 'There is no user with this email address')], 'emailUnconfirmed' => ['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')); } }], 'passwordRequired' => ['password', 'required'], 'passwordLength' => ['password', 'string', 'min' => 6]]; }
/** @inheritdoc */ public function rules() { return ['requiredFields' => [['login', 'password'], 'required'], 'loginTrim' => ['login', 'trim'], 'passwordValidate' => ['password', function ($attribute) { if ($this->user === null || !Password::validate($this->password, $this->user->password_hash)) { $this->addError($attribute, Yii::t('user', 'Invalid login or password')); } }], 'confirmationValidate' => ['login', function ($attribute) { if ($this->user !== null) { $confirmationRequired = $this->module->enableConfirmation && !$this->module->enableUnconfirmedLogin; if ($confirmationRequired && !$this->user->getIsConfirmed()) { $this->addError($attribute, Yii::t('user', 'You need to confirm your email address')); } if ($this->user->getIsBlocked()) { $this->addError($attribute, Yii::t('user', 'Your account has been blocked')); } } }], 'rememberMe' => ['rememberMe', 'boolean']]; }
/** * 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']); $this->performAjaxValidation($user); if ($user->load(Yii::$app->request->post()) && $user->create()) { Yii::$app->getSession()->setFlash('success', Yii::t('user', 'User has been created')); return $this->redirect(['update', 'id' => $user->id]); } return $this->render('create', ['user' => $user]); }
/** * This command creates new user account. 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 actionIndex($email, $username, $password = null) { $user = Yii::createObject(['class' => User::className(), 'scenario' => 'create', 'email' => $email, 'username' => $username, 'password' => $password]); if ($user->create()) { $this->stdout(Yii::t('user', 'User 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); } } } }
/** * Displays page where user can create new account that will be connected to social account. * * @param string $code * * @return string * @throws NotFoundHttpException */ public function actionConnect($code) { $account = $this->finder->findAccount()->byCode($code)->one(); if ($account === null || $account->getIsConnected()) { throw new NotFoundHttpException(); } /** @var User $user */ $user = Yii::createObject(['class' => User::className(), 'scenario' => 'connect', 'username' => $account->username, 'email' => $account->email]); if ($user->load(Yii::$app->request->post()) && $user->create()) { $account->connect($user); Yii::$app->user->login($user, $this->module->rememberFor); return $this->goBack(); } return $this->render('connect', ['model' => $user, 'account' => $account]); }
/** * Loads attributes to the user model. You should override this method if you are going to add new fields to the * registration form. You can read more in special guide. * * By default this method set all attributes of this model to the attributes of User model, so you should properly * configure safe attributes of your User model. * * @param User $user */ protected function loadAttributes(User $user) { $user->setAttributes($this->attributes); }
/** * Tries to find user or create a new one. * * @param Account $account * * @return User|bool False when can't create user. */ protected static function fetchUser(Account $account) { $user = static::getFinder()->findUserByEmail($account->email); if (null !== $user) { return $user; } $user = Yii::createObject(['class' => User::className(), 'scenario' => 'connect', 'username' => $account->username, 'email' => $account->email]); if (!$user->validate(['email'])) { $account->email = null; } if (!$user->validate(['username'])) { $account->username = null; } return $user->create() ? $user : false; }