Files
pig-farm-controller/internal/app/middleware/audit.go

118 lines
3.5 KiB
Go
Raw Normal View History

2025-09-28 00:13:47 +08:00
package middleware
import (
"bytes"
"encoding/json"
"io"
"strconv"
2025-10-02 00:18:13 +08:00
"git.huangwc.com/pig/pig-farm-controller/internal/domain/audit"
2025-09-28 01:02:29 +08:00
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
2025-09-28 00:13:47 +08:00
"github.com/gin-gonic/gin"
)
2025-09-28 00:54:19 +08:00
type auditResponse struct {
Code int `json:"code"`
Message string `json:"message"`
}
2025-09-28 00:13:47 +08:00
// AuditLogMiddleware 创建一个Gin中间件用于在请求结束后记录用户操作审计日志
func AuditLogMiddleware(auditService audit.Service) gin.HandlerFunc {
return func(c *gin.Context) {
// 使用自定义的 response body writer 来捕获响应体
blw := &bodyLogWriter{body: bytes.NewBufferString(""), ResponseWriter: c.Writer}
c.Writer = blw
// 首先执行请求链中的后续处理程序(即业务控制器)
c.Next()
// --- 在这里,请求已经处理完毕 ---
// 从上下文中尝试获取由控制器设置的业务审计信息
2025-09-28 01:10:04 +08:00
actionType, exists := c.Get(models.ContextAuditActionType.String())
2025-09-28 00:13:47 +08:00
if !exists {
// 如果上下文中没有 actionType说明此接口无需记录审计日志直接返回
return
}
2025-09-28 01:02:29 +08:00
// 从 Gin Context 中获取用户对象
2025-09-28 01:10:04 +08:00
userCtx, userExists := c.Get(models.ContextUserKey.String())
2025-09-28 01:02:29 +08:00
var user *models.User
if userExists {
user, _ = userCtx.(*models.User)
}
// 构建 RequestContext
reqCtx := audit.RequestContext{
ClientIP: c.ClientIP(),
HTTPPath: c.Request.URL.Path,
HTTPMethod: c.Request.Method,
}
2025-09-28 00:13:47 +08:00
// 获取其他审计信息
2025-09-28 01:10:04 +08:00
description, _ := c.Get(models.ContextAuditDescription.String())
targetResource, _ := c.Get(models.ContextAuditTargetResource.String())
2025-09-28 00:13:47 +08:00
2025-09-28 00:54:19 +08:00
// 默认操作状态为成功
2025-09-28 01:10:04 +08:00
status := models.AuditStatusSuccess
2025-09-28 00:13:47 +08:00
resultDetails := ""
2025-09-28 00:54:19 +08:00
// 尝试从捕获的响应体中解析平台响应
var platformResponse auditResponse
if err := json.Unmarshal(blw.body.Bytes(), &platformResponse); err == nil {
// 如果解析成功,根据平台状态码判断操作是否失败
// 成功状态码范围是 2000-2999
if platformResponse.Code < 2000 || platformResponse.Code >= 3000 {
2025-09-28 01:10:04 +08:00
status = models.AuditStatusFailed
2025-09-28 00:54:19 +08:00
resultDetails = platformResponse.Message
2025-09-28 00:13:47 +08:00
}
2025-09-28 00:54:19 +08:00
} else {
// 如果响应体不是预期的平台响应格式或者解析失败则记录原始HTTP状态码作为详情
// 并且如果HTTP状态码不是2xx则标记为失败
if c.Writer.Status() < 200 || c.Writer.Status() >= 300 {
2025-09-28 01:10:04 +08:00
status = models.AuditStatusFailed
2025-09-28 00:13:47 +08:00
}
2025-09-28 00:54:19 +08:00
resultDetails = "HTTP Status: " + strconv.Itoa(c.Writer.Status()) + ", Body Parse Error: " + err.Error()
2025-09-28 00:13:47 +08:00
}
// 调用审计服务记录日志(异步)
auditService.LogAction(
2025-09-28 01:02:29 +08:00
user,
reqCtx,
2025-09-28 00:13:47 +08:00
actionType.(string),
description.(string),
targetResource,
status,
resultDetails,
)
}
}
// bodyLogWriter 是一个自定义的 gin.ResponseWriter用于捕获响应体
// 这对于在操作失败时记录详细的错误信息非常有用
type bodyLogWriter struct {
gin.ResponseWriter
body *bytes.Buffer
}
func (w bodyLogWriter) Write(b []byte) (int, error) {
w.body.Write(b)
return w.ResponseWriter.Write(b)
}
func (w bodyLogWriter) WriteString(s string) (int, error) {
w.body.WriteString(s)
return w.ResponseWriter.WriteString(s)
}
// ReadBody 用于安全地读取请求体,并防止其被重复读取
func ReadBody(c *gin.Context) ([]byte, error) {
bodyBytes, err := io.ReadAll(c.Request.Body)
if err != nil {
return nil, err
}
// 将读取的内容放回 Body 中,以便后续的处理函数可以再次读取
c.Request.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
return bodyBytes, nil
}