分享访问日志的一部分后端实现

*该功能的日志记录仍未实装
This commit is contained in:
Chenx221 2024-04-05 17:22:18 +08:00
parent b0e4867b68
commit 8996158fb5
Signed by: chenx221
GPG Key ID: D7A9EC07024C3021
2 changed files with 54 additions and 1 deletions

View File

@ -2,6 +2,7 @@
namespace app\controllers; namespace app\controllers;
use app\models\DownloadLogs;
use app\models\LoginLogs; use app\models\LoginLogs;
use app\models\SiteConfig; use app\models\SiteConfig;
use app\models\User; use app\models\User;
@ -34,7 +35,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', 'login-log'], 'actions' => ['index', 'system', 'user', 'info', 'user-view', 'user-create', 'user-update', 'user-delete', 'user-totpoff', 'user-pwdreset', 'login-log','access-log'],
'roles' => ['admin'], // only admin can do these 'roles' => ['admin'], // only admin can do these
] ]
], ],
@ -53,6 +54,7 @@ class AdminController extends Controller
'user-totpoff' => ['POST'], 'user-totpoff' => ['POST'],
'user-pwdreset' => ['POST'], 'user-pwdreset' => ['POST'],
'login-log' => ['GET'], 'login-log' => ['GET'],
'access-log' => ['GET'],
], ],
], ],
] ]
@ -395,4 +397,16 @@ class AdminController extends Controller
]); ]);
} }
/**
* @return string
*/
public function actionAccessLog(): string
{
$downloadLogs = new DownloadLogs();
$dataProvider = $downloadLogs->search($this->request->queryParams);
return $this->render('access_log', [
'searchModel' => $downloadLogs,
'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;
@ -96,4 +97,42 @@ class DownloadLogs extends ActiveRecord
$log->save(); $log->save();
} }
public function search($params): ActiveDataProvider
{
$query = DownloadLogs::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,
'share_id' => $this->share_id,
'access_time' => $this->access_time,
]);
$query->andFilterWhere(['like', 'ip_address', $this->ip_address])
->andFilterWhere(['like', 'user_agent', $this->user_agent]);
return $dataProvider;
}
} }