优化代码
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"strconv"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/app/service/audit"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/repository"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
@@ -35,6 +36,20 @@ func AuditLogMiddleware(auditService audit.Service) gin.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
// 从 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,
|
||||
}
|
||||
|
||||
// 获取其他审计信息
|
||||
description, _ := c.Get(repository.ContextAuditDescription)
|
||||
targetResource, _ := c.Get(repository.ContextAuditTargetResource)
|
||||
@@ -63,7 +78,8 @@ func AuditLogMiddleware(auditService audit.Service) gin.HandlerFunc {
|
||||
|
||||
// 调用审计服务记录日志(异步)
|
||||
auditService.LogAction(
|
||||
c,
|
||||
user,
|
||||
reqCtx,
|
||||
actionType.(string),
|
||||
description.(string),
|
||||
targetResource,
|
||||
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/app/service/audit"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/app/service/token"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/repository"
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -53,7 +52,7 @@ func AuthMiddleware(tokenService token.TokenService, userRepo repository.UserRep
|
||||
}
|
||||
|
||||
// 将完整的用户对象存储在 context 中,以便后续的处理函数使用
|
||||
c.Set(audit.ContextUserKey, user)
|
||||
c.Set(repository.ContextUserKey, user)
|
||||
|
||||
// 继续处理请求链中的下一个处理程序
|
||||
c.Next()
|
||||
|
||||
Reference in New Issue
Block a user