Files
hi-server/internal/middleware/adminMetaMiddleware.go
shanshanzhong147 92cf2921dd 修复(#3): 抽奖 Stage 1 后台审计 IP/UA 丢失 + 冒烟脚本 Bearer 前缀
Closes HIF-3 (Stage 1 P1 defects from QA smoke report)

F2: admin_action_log.ip / user_agent 恒为空
- 根因:requestMeta 从 ctx 读裸字符串 key,全库无 writer 塞
- 修复:新增 AdminMetaMiddleware 用 typed constant.CtxKeyIP / CtxKeyUserAgent
- 挂载:lottery admin 组末尾(不 gate access)
- 回归护栏:UsesTypedKey + IgnoresBareStringKeys 双向断言 typed key,防止未来退化回裸字符串

F3: qa/lottery/stage1_curl.sh Bearer 前缀
- 删除两处 Bearer 前缀 + 加注释说明 ppanel AuthMiddleware 不 strip

单测 5 用例全绿;scope 严格限于 lottery admin 组,其它路径零改动;无 DB 变更。

Merged: architect review 后,将触发 staging 二次部署 — 期望这次能一并解决 shanshanzhong147 手工 SSH 后 Lottery.Enable=true 未生效的问题(若真是 mount/restart 未正确 pick up)。
2026-07-09 07:45:58 -07:00

27 lines
890 B
Go

package middleware
import (
"context"
"github.com/gin-gonic/gin"
"github.com/perfect-panel/server/pkg/constant"
)
// AdminMetaMiddleware pins the request's client IP and User-Agent onto the
// context under constant.CtxKeyIP / constant.CtxKeyUserAgent so downstream
// audit writers (admin_action_log) can capture them without threading the
// gin.Context through every logic layer.
//
// This middleware is a no-op for auth: it does NOT gate access; wire it
// after AuthMiddleware so ctx.Value(CtxKeyUser) is already populated by the
// time an admin logic writes audit rows.
func AdminMetaMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
ctx := c.Request.Context()
ctx = context.WithValue(ctx, constant.CtxKeyIP, c.ClientIP())
ctx = context.WithValue(ctx, constant.CtxKeyUserAgent, c.Request.UserAgent())
c.Request = c.Request.WithContext(ctx)
c.Next()
}
}