public function actionLogin() { if (Yii::$app->request->isPost) { $request = Yii::$app->request; $user_info = User::findOne(['user_email' => $request->post("email")]); if ($user_info) { if (Yii::$app->getSecurity()->validatePassword($request->post("password"), $user_info->user_password)) { $userAuth = new \app\models\table\User(); $identity = $userAuth->findIdentity($user_info->user_id); if (Yii::$app->user->login($identity, 3600 * 24 * 7)) { //用户登录,cookie7天有效 return $this->redirect(['index/index']); } else { Yii::$app->session->setFlash("info", "登录失败!"); } } else { Yii::$app->session->setFlash("info", "邮箱或密码错误!"); } } else { Yii::$app->session->setFlash("info", "用户名不存在!"); } return $this->redirect(['user/login']); } else { return $this->renderPartial("login"); } }
/** * 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(['user_id' => $this->user_id, 'login_times' => $this->login_times]); $query->andFilterWhere(['like', 'user_name', $this->user_name])->andFilterWhere(['like', 'user_password', $this->user_password])->andFilterWhere(['like', 'user_nickname', $this->user_nickname])->andFilterWhere(['like', 'user_portrait', $this->user_portrait])->andFilterWhere(['like', 'create_time', $this->create_time])->andFilterWhere(['like', 'last_login_time', $this->last_login_time])->andFilterWhere(['like', 'last_login_ip', $this->last_login_ip])->andFilterWhere(['like', 'user_email', $this->user_email])->andFilterWhere(['like', 'user_role', $this->user_role]); return $dataProvider; }
/** * Finds the User model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return User the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = User::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }