mirror of
https://github.com/acepanel/panel.git
synced 2026-02-04 13:47:15 +08:00
feat: 添加操作日志记录功能和前端日志查看页面 (#1227)
* Initial plan * feat: 添加操作日志记录功能和前端日志查看页面 Co-authored-by: devhaozi <115467771+devhaozi@users.noreply.github.com> * fix: 修复 HttpLog 中 IP 字符串处理的类型安全问题 Co-authored-by: devhaozi <115467771+devhaozi@users.noreply.github.com> * fix: lint * feat: 完善面板内日志记录,添加网站/数据库/项目/计划任务/备份CRUD日志 Co-authored-by: devhaozi <115467771+devhaozi@users.noreply.github.com> * feat: 完善更多模块的日志记录(证书/SSH/Webhook/用户/设置/安全) Co-authored-by: devhaozi <115467771+devhaozi@users.noreply.github.com> * fix: 改进cert_dns删除方法,在删除前获取实体信息用于日志记录 Co-authored-by: devhaozi <115467771+devhaozi@users.noreply.github.com> * fix: lint * feat: 从session获取操作员ID并在前端显示用户名 Co-authored-by: devhaozi <115467771+devhaozi@users.noreply.github.com> * fix: lint --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: devhaozi <115467771+devhaozi@users.noreply.github.com> Co-authored-by: 耗子 <haozi@loli.email>
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"slices"
|
||||
|
||||
"github.com/leonelquinteros/gotext"
|
||||
@@ -15,13 +17,15 @@ import (
|
||||
type databaseUserRepo struct {
|
||||
t *gotext.Locale
|
||||
db *gorm.DB
|
||||
log *slog.Logger
|
||||
server biz.DatabaseServerRepo
|
||||
}
|
||||
|
||||
func NewDatabaseUserRepo(t *gotext.Locale, db *gorm.DB, server biz.DatabaseServerRepo) biz.DatabaseUserRepo {
|
||||
func NewDatabaseUserRepo(t *gotext.Locale, db *gorm.DB, log *slog.Logger, server biz.DatabaseServerRepo) biz.DatabaseUserRepo {
|
||||
return &databaseUserRepo{
|
||||
t: t,
|
||||
db: db,
|
||||
log: log,
|
||||
server: server,
|
||||
}
|
||||
}
|
||||
@@ -58,7 +62,7 @@ func (r *databaseUserRepo) Get(id uint) (*biz.DatabaseUser, error) {
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (r *databaseUserRepo) Create(req *request.DatabaseUserCreate) error {
|
||||
func (r *databaseUserRepo) Create(ctx context.Context, req *request.DatabaseUserCreate) error {
|
||||
server, err := r.server.Get(req.ServerID)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -97,7 +101,14 @@ func (r *databaseUserRepo) Create(req *request.DatabaseUserCreate) error {
|
||||
return err
|
||||
}
|
||||
|
||||
return r.db.Save(user).Error
|
||||
if err = r.db.Save(user).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 记录日志
|
||||
r.log.Info("database user created", slog.String("type", biz.OperationTypeDatabaseUser), slog.Uint64("operator_id", getOperatorID(ctx)), slog.String("username", req.Username), slog.Uint64("server_id", uint64(req.ServerID)))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *databaseUserRepo) Update(req *request.DatabaseUserUpdate) error {
|
||||
@@ -151,7 +162,7 @@ func (r *databaseUserRepo) UpdateRemark(req *request.DatabaseUserUpdateRemark) e
|
||||
return r.db.Save(user).Error
|
||||
}
|
||||
|
||||
func (r *databaseUserRepo) Delete(id uint) error {
|
||||
func (r *databaseUserRepo) Delete(ctx context.Context, id uint) error {
|
||||
user, err := r.Get(id)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -170,7 +181,14 @@ func (r *databaseUserRepo) Delete(id uint) error {
|
||||
|
||||
_ = operator.UserDrop(user.Username, user.Host)
|
||||
|
||||
return r.db.Where("id = ?", id).Delete(&biz.DatabaseUser{}).Error
|
||||
if err = r.db.Where("id = ?", id).Delete(&biz.DatabaseUser{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 记录日志
|
||||
r.log.Info("database user deleted", slog.String("type", biz.OperationTypeDatabaseUser), slog.Uint64("operator_id", getOperatorID(ctx)), slog.Uint64("id", uint64(id)), slog.String("username", user.Username))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *databaseUserRepo) DeleteByNames(serverID uint, names []string) error {
|
||||
|
||||
Reference in New Issue
Block a user