From 71b63d3a2aa9c25d33e02446849988631add8212 Mon Sep 17 00:00:00 2001 From: Chenx221 Date: Wed, 3 Apr 2024 16:03:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E7=99=BB=E5=BD=95=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E6=9F=A5=E7=9C=8B=20*=E5=90=8E=E7=AB=AF=E9=83=A8?= =?UTF-8?q?=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/AdminController.php | 21 ++++++++++++++++-- models/LoginLogs.php | 39 +++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/controllers/AdminController.php b/controllers/AdminController.php index 3d9d2e1..ab7edfd 100644 --- a/controllers/AdminController.php +++ b/controllers/AdminController.php @@ -2,6 +2,7 @@ namespace app\controllers; +use app\models\LoginLogs; use app\models\SiteConfig; use app\models\User; use app\models\UserSearch; @@ -33,7 +34,7 @@ class AdminController extends Controller 'rules' => [ [ 'allow' => true, - 'actions' => ['index', 'system', 'user', 'info', 'user-view', 'user-create', 'user-update', 'user-delete', 'user-totpoff', 'user-pwdreset'], + 'actions' => ['index', 'system', 'user', 'info', 'user-view', 'user-create', 'user-update', 'user-delete', 'user-totpoff', 'user-pwdreset', 'login-log'], 'roles' => ['admin'], // only admin can do these ] ], @@ -42,7 +43,7 @@ class AdminController extends Controller 'class' => VerbFilter::class, 'actions' => [ 'index' => ['GET'], - 'system' => ['GET','POST'], + 'system' => ['GET', 'POST'], 'user' => ['GET'], 'user-view' => ['GET', 'POST'], 'user-create' => ['GET', 'POST'], @@ -51,6 +52,7 @@ class AdminController extends Controller 'info' => ['GET', 'POST'], 'user-totpoff' => ['POST'], 'user-pwdreset' => ['POST'], + 'login-log' => ['GET'], ], ], ] @@ -378,4 +380,19 @@ class AdminController extends Controller 'is_otp_enabled' => $model->is_otp_enabled == 1 ]); } + + /** + * login log + * @return string + */ + public function actionLoginLog(): string + { + $loginLogs = new LoginLogs(); + $dataProvider = $loginLogs->search($this->request->queryParams); + return $this->render('login_log', [ + 'searchModel' => $loginLogs, + 'dataProvider' => $dataProvider, + ]); + } + } diff --git a/models/LoginLogs.php b/models/LoginLogs.php index d16fcdb..a357bf1 100644 --- a/models/LoginLogs.php +++ b/models/LoginLogs.php @@ -2,6 +2,7 @@ namespace app\models; +use yii\data\ActiveDataProvider; use yii\db\ActiveQuery; use yii\db\ActiveRecord; @@ -85,4 +86,42 @@ class LoginLogs extends ActiveRecord $log->save(); } + + public function search($params): ActiveDataProvider + { + $query = LoginLogs::find(); + + // add conditions that should always apply here + + $dataProvider = new ActiveDataProvider([ + 'query' => $query, + 'sort' => [ + 'defaultOrder' => [ + 'id' => SORT_DESC, // 默认按照 'id' 倒序排序 + ] + ], + ]); + + $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; + } + + // grid filtering conditions + $query->andFilterWhere([ + 'id' => $this->id, + 'user_id' => $this->user_id, + 'login_time' => $this->login_time, + 'status' => $this->status, + ]); + + $query->andFilterWhere(['like', 'ip_address', $this->ip_address]) + ->andFilterWhere(['like', 'user_agent', $this->user_agent]); + + + return $dataProvider; + } }