实现登录日志查看

*后端部分
This commit is contained in:
Chenx221 2024-04-03 16:03:33 +08:00
parent 3188782590
commit 71b63d3a2a
Signed by: chenx221
GPG Key ID: D7A9EC07024C3021
2 changed files with 58 additions and 2 deletions

View File

@ -2,6 +2,7 @@
namespace app\controllers; namespace app\controllers;
use app\models\LoginLogs;
use app\models\SiteConfig; use app\models\SiteConfig;
use app\models\User; use app\models\User;
use app\models\UserSearch; use app\models\UserSearch;
@ -33,7 +34,7 @@ class AdminController extends Controller
'rules' => [ 'rules' => [
[ [
'allow' => true, '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 'roles' => ['admin'], // only admin can do these
] ]
], ],
@ -42,7 +43,7 @@ class AdminController extends Controller
'class' => VerbFilter::class, 'class' => VerbFilter::class,
'actions' => [ 'actions' => [
'index' => ['GET'], 'index' => ['GET'],
'system' => ['GET','POST'], 'system' => ['GET', 'POST'],
'user' => ['GET'], 'user' => ['GET'],
'user-view' => ['GET', 'POST'], 'user-view' => ['GET', 'POST'],
'user-create' => ['GET', 'POST'], 'user-create' => ['GET', 'POST'],
@ -51,6 +52,7 @@ class AdminController extends Controller
'info' => ['GET', 'POST'], 'info' => ['GET', 'POST'],
'user-totpoff' => ['POST'], 'user-totpoff' => ['POST'],
'user-pwdreset' => ['POST'], 'user-pwdreset' => ['POST'],
'login-log' => ['GET'],
], ],
], ],
] ]
@ -378,4 +380,19 @@ class AdminController extends Controller
'is_otp_enabled' => $model->is_otp_enabled == 1 '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,
]);
}
} }

View File

@ -2,6 +2,7 @@
namespace app\models; namespace app\models;
use yii\data\ActiveDataProvider;
use yii\db\ActiveQuery; use yii\db\ActiveQuery;
use yii\db\ActiveRecord; use yii\db\ActiveRecord;
@ -85,4 +86,42 @@ class LoginLogs extends ActiveRecord
$log->save(); $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;
}
} }