Constraint: 切换 main 前必须保存当前七月分支全部项目进展,套餐生效提案仅属于 Iteration/7-11。 Rejected: 将七月套餐修复直接移植到 main | 两个分支的可靠投递架构不同。 Confidence: medium Scope-risk: broad Directive: 不得将本提交整体 cherry-pick 到 main;main 套餐热修必须基于其纯 Asynq 代码独立实施。 Tested: git diff --check;openspec validate fix-package-activation-starvation --strict。 Not-tested: 按用户要求未运行自动化测试;go build ./... 因当前审计改造中的 Enterprise 模型字面量和 role.recordFailure 参数类型错误未通过。
85 lines
2.1 KiB
Go
85 lines
2.1 KiB
Go
// Package auditcontext 提供跨 HTTP、异步任务和外部回调传播的审计上下文。
|
|
package auditcontext
|
|
|
|
import "context"
|
|
|
|
type contextKey struct{}
|
|
|
|
// Context 保存入口提供的真实操作者与链路信息,不包含业务动作或资源事实。
|
|
type Context struct {
|
|
ActorKind string
|
|
ActorID string
|
|
ActorName string
|
|
ActorShopID *uint
|
|
ActorEnterpriseID *uint
|
|
Source string
|
|
RequestID string
|
|
CorrelationID string
|
|
ParentEventID string
|
|
RequestPath string
|
|
RequestMethod string
|
|
IPAddress string
|
|
UserAgent string
|
|
}
|
|
|
|
// With 合并审计上下文;非空新值覆盖旧值,便于认证中间件覆盖 HTTP 基础信息。
|
|
func With(ctx context.Context, value Context) context.Context {
|
|
if ctx == nil {
|
|
ctx = context.Background()
|
|
}
|
|
merged := From(ctx)
|
|
merge(&merged, value)
|
|
return context.WithValue(ctx, contextKey{}, merged)
|
|
}
|
|
|
|
// From 读取审计上下文;未设置时返回零值。
|
|
func From(ctx context.Context) Context {
|
|
if ctx == nil {
|
|
return Context{}
|
|
}
|
|
value, _ := ctx.Value(contextKey{}).(Context)
|
|
return value
|
|
}
|
|
|
|
func merge(target *Context, value Context) {
|
|
if value.ActorKind != "" {
|
|
target.ActorKind = value.ActorKind
|
|
}
|
|
if value.ActorID != "" {
|
|
target.ActorID = value.ActorID
|
|
}
|
|
if value.ActorName != "" {
|
|
target.ActorName = value.ActorName
|
|
}
|
|
if value.ActorShopID != nil {
|
|
target.ActorShopID = value.ActorShopID
|
|
}
|
|
if value.ActorEnterpriseID != nil {
|
|
target.ActorEnterpriseID = value.ActorEnterpriseID
|
|
}
|
|
if value.Source != "" {
|
|
target.Source = value.Source
|
|
}
|
|
if value.RequestID != "" {
|
|
target.RequestID = value.RequestID
|
|
}
|
|
if value.CorrelationID != "" {
|
|
target.CorrelationID = value.CorrelationID
|
|
}
|
|
if value.ParentEventID != "" {
|
|
target.ParentEventID = value.ParentEventID
|
|
}
|
|
if value.RequestPath != "" {
|
|
target.RequestPath = value.RequestPath
|
|
}
|
|
if value.RequestMethod != "" {
|
|
target.RequestMethod = value.RequestMethod
|
|
}
|
|
if value.IPAddress != "" {
|
|
target.IPAddress = value.IPAddress
|
|
}
|
|
if value.UserAgent != "" {
|
|
target.UserAgent = value.UserAgent
|
|
}
|
|
}
|