/** * 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->setPassword($this->password); $user->generateAuthKey(); if ($user->save()) { return $user; } } return null; }
public function fields() { $fields = parent::fields(); // 删除一些包含敏感信息的字段 unset($fields['password_hash']); return $fields; }
/** * Creates a form model given a token. * * @param string $token * @param array $config name-value pairs that will be used to initialize the object properties * @throws \yii\base\InvalidParamException if token is empty or not valid */ public function __construct($token, $config = []) { if (empty($token) || !is_string($token)) { throw new InvalidParamException('Password reset token cannot be blank.'); } $this->_user = User::findByPasswordResetToken($token); if (!$this->_user) { throw new InvalidParamException('Wrong password reset token.'); } parent::__construct($config); }
/** * Sends an email with a link, for resetting the password. * * @return boolean whether the email was send */ public function sendEmail() { /* @var $user User */ $user = User::findOne(['status' => User::STATUS_ACTIVE, 'email' => $this->email]); if ($user) { if (!User::isPasswordResetTokenValid($user->password_reset_token)) { $user->generatePasswordResetToken(); } if ($user->save()) { return \Yii::$app->mailer->compose(['html' => 'passwordResetToken-html', 'text' => 'passwordResetToken-text'], ['user' => $user])->setFrom([\Yii::$app->params['supportEmail'] => \Yii::$app->name . ' robot'])->setTo($this->email)->setSubject('Password reset for ' . \Yii::$app->name)->send(); } } return false; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = User::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'status' => $this->status, 'last_login_time' => $this->last_login_time, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at, 'delect' => $this->delect]); $query->andFilterWhere(['like', 'email', $this->email])->andFilterWhere(['like', 'password_hash', $this->password_hash])->andFilterWhere(['like', 'zipcode', $this->zipcode])->andFilterWhere(['like', 'password_reset_token', $this->password_reset_token])->andFilterWhere(['like', 'last_name', $this->last_name])->andFilterWhere(['like', 'client_name', $this->client_name])->andFilterWhere(['like', 'phone', $this->phone])->andFilterWhere(['like', 'state', $this->state])->andFilterWhere(['like', 'city', $this->city])->andFilterWhere(['like', 'street', $this->street])->andFilterWhere(['like', 'time_zone', $this->time_zone])->andFilterWhere(['like', 'channels', $this->channels])->andFilterWhere(['like', 'register_ip', $this->register_ip])->andFilterWhere(['like', 'last_login_ip', $this->last_login_ip])->andFilterWhere(['like', 'avatar_img', $this->avatar_img])->andFilterWhere(['like', 'username', $this->username])->andFilterWhere(['like', 'auth_key', $this->auth_key])->andFilterWhere(['like', 'accessToken', $this->accessToken]); return $dataProvider; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = User::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'money' => $this->money, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at, 'status' => $this->status]); $query->andFilterWhere(['like', 'cash_id', $this->cash_id])->andFilterWhere(['like', 'email', $this->email])->andFilterWhere(['like', 'password', $this->password])->andFilterWhere(['like', 'salt', $this->salt])->andFilterWhere(['like', 'username', $this->username])->andFilterWhere(['like', 'auth_key', $this->auth_key])->andFilterWhere(['like', 'photo', $this->photo]); return $dataProvider; }
public function actionForgot() { $this->layout = "no_login"; if (Yii::$app->request->post()) { $user = \common\models\db\User::findByEmail(Yii::$app->request->post()['email']); if (!empty($user)) { $pass = $user->generateRandomPassword(); $user->update(); $msg = "<h3>Новый пароль выслан Вам на указанный Email</h3>"; Email::sendForgotPass(Yii::$app->request->post()['email'], $pass); } else { $msg = "<h3>Пользователь с таким паролем не найден</h3>"; } return $this->render('new_pass', ['msg' => $msg]); } else { return $this->render('forgot'); } }
public function rules() { return array_merge(parent::rules(), [[['email', 'password_hash', 'password2'], 'required'], ['password2', 'compare', "compareAttribute" => "password_hash", 'message' => '两次密码输入不一致.请重新输入'], ['password2', 'required', 'message' => '确认密码不能为空'], ['email', 'email', 'message' => '邮箱格式不对'], ['email', 'unique'], ['phone', 'match', 'pattern' => '/^[0-9]{10}$/']]); }
/** * @return \yii\db\ActiveQuery */ public function getUser() { return $this->hasOne(User::className(), ['id' => 'user_id']); }
public function addTask() { $db = Yii::$app->db; $user = User::findOne(['id' => $this->user_id]); $user->debitMoney($this->sum); $transaction = $db->beginTransaction(); try { $user->save(); $this->save(); $transaction->commit(); } catch (\Exception $e) { $transaction->rollBack(); return false; } return true; }
public function actionSetMoney($id) { $setMoney = new SetMoney(); if ($setMoney->load(Yii::$app->request->post()) && $setMoney->validate()) { $setMoney->apply(); return $this->redirect('index'); } return $this->render('set_money', ['user' => User::findOne($id), 'model' => new SetMoney()]); }
/** * @return \yii\db\ActiveQuery */ public function getCash() { return $this->hasOne(User::className(), ['id' => 'cash_id']); }