2025-09-28 00:13:47 +08:00
|
|
|
|
package middleware
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"bytes"
|
|
|
|
|
|
"encoding/json"
|
|
|
|
|
|
"io"
|
|
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
|
|
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/app/service/audit"
|
2025-09-28 01:02:29 +08:00
|
|
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
2025-09-28 00:54:19 +08:00
|
|
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/repository"
|
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 00:54:19 +08:00
|
|
|
|
actionType, exists := c.Get(repository.ContextAuditActionType)
|
2025-09-28 00:13:47 +08:00
|
|
|
|
if !exists {
|
|
|
|
|
|
// 如果上下文中没有 actionType,说明此接口无需记录审计日志,直接返回
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-28 01:02:29 +08:00
|
|
|
|
// 从 Gin Context 中获取用户对象
|
|
|
|
|
|
userCtx, userExists := c.Get(repository.ContextUserKey)
|
|
|
|
|
|
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 00:54:19 +08:00
|
|
|
|
description, _ := c.Get(repository.ContextAuditDescription)
|
|
|
|
|
|
targetResource, _ := c.Get(repository.ContextAuditTargetResource)
|
2025-09-28 00:13:47 +08:00
|
|
|
|
|
2025-09-28 00:54:19 +08:00
|
|
|
|
// 默认操作状态为成功
|
|
|
|
|
|
status := repository.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 {
|
|
|
|
|
|
status = repository.AuditStatusFailed
|
|
|
|
|
|
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 {
|
|
|
|
|
|
status = repository.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
|
|
|
|
|
|
}
|