/** * @return User */ public function getUser() { if ($this->_user === null) { $this->_user = User::findUserByEmail($this->email); } return $this->_user; }
/** @inheritdoc */ public function beforeValidate() { if (parent::beforeValidate()) { $this->user = User::findIdentityByUsernameOrEmail(trim($this->login)); return true; } else { return false; } }
/** @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->passwordHash)) { $this->addError($attribute, Yii::t('users', '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('users', 'You need to confirm your email address')); } if ($this->user->getIsBlocked()) { $this->addError($attribute, Yii::t('users', 'Your account has been blocked')); } } }], 'rememberMe' => ['rememberMe', 'boolean']]; }
/** @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('users', 'There is no user with this email address')], 'emailUnconfirmed' => ['email', function ($attribute) { $this->user = User::findUserByEmail($this->email); if ($this->user !== null && $this->module->enableConfirmation && !$this->user->isConfirmed) { $this->addError($attribute, Yii::t('users', 'You need to confirm your email address')); } if ($this->user->isBlocked) { $this->addError($attribute, Yii::t('users', 'Your account has been blocked')); } }], 'passwordRequired' => ['password', 'required'], 'passwordLength' => ['password', 'string', 'min' => 6]]; }
/** * @param $params * * @return ActiveDataProvider */ public function search($params) { $query = User::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } if ($this->createTime !== null) { $date = strtotime($this->createTime); $query->andFilterWhere(['between', 'createTime', $date, $date + 3600 * 24]); } $query->andFilterWhere(['like', 'username', $this->username])->andFilterWhere(['like', 'email', $this->email])->andFilterWhere(['registrationIp' => $this->registrationIp]); return $dataProvider; }
/** * Confirms user's account. If confirmation was successful logs the user and shows success message. Otherwise * shows error message. * * @param int $id * @param string $code * * @return string * @throws \yii\web\HttpException */ public function actionConfirm($id, $code) { $user = User::findIdentity($id); if ($user === null || $this->module->enableConfirmation == false) { throw new NotFoundHttpException(); } $event = $this->getUserEvent($user); $this->trigger(self::EVENT_BEFORE_CONFIRM, $event); $user->attemptConfirmation($code); $this->trigger(self::EVENT_AFTER_CONFIRM, $event); return $this->render('/message', ['title' => Yii::t('users', 'Account confirmation'), 'module' => $this->module]); }
/** * Finds a user by the given username or email. * * @param string $usernameOrEmail Username or email to be used on search. * * @return User */ public static function findIdentityByUsernameOrEmail($usernameOrEmail) { if (filter_var($usernameOrEmail, FILTER_VALIDATE_EMAIL)) { return self::findUserByEmail($usernameOrEmail); } return User::findIdentityByUsername($usernameOrEmail); }
/** * Назначение пользователю прав администратора * * @param $search */ public function actionAdmin($search) { $user = User::findIdentityByUsernameOrEmail($search); if ($user) { $rbac = new Rbac(); $rbac->setAdminRole($user); $this->stdout(Yii::t('users', 'Admin rights granted') . "\n", Console::FG_GREEN); } else { $this->stdout(Yii::t('users', 'User is not found') . "\n", Console::FG_RED); } }
/** * 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 = User::findUserByEmail($account->email); if (null !== $user) { return $user; } $user = Yii::createObject(['class' => User::className(), 'scenario' => User::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; }
/** * 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); }
/** * Finds the User model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * * @param int $id * * @return User the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { $user = User::findIdentity($id); if ($user === null) { throw new NotFoundHttpException('The requested page does not exist'); } return $user; }
/** * Attempts changing user's email address. * * @param int $id * @param string $code * * @return string * @throws \yii\web\HttpException */ public function actionConfirm($id, $code) { $user = User::findIdentity($id); if ($user === null || $this->module->emailChangeStrategy == Module::STRATEGY_INSECURE) { throw new NotFoundHttpException(); } $event = $this->getUserEvent($user); $this->trigger(self::EVENT_BEFORE_CONFIRM, $event); $user->attemptEmailChange($code); $this->trigger(self::EVENT_AFTER_CONFIRM, $event); return $this->redirect(['account']); }