feat: 更新授权码服务以从上下文中获取用户ID,并添加缺少认证信息的错误处理

This commit is contained in:
wood911010 committed 2025-10-29 23:01:42 +08:00
1 parent 454a474a36
commit ab50f1151c
2 files changed
+75 -10

No files matched your search

@@ -59,7 +59,10 @@ func (s *authorizationCodeService) CreateAuthorizationCode(ctx context.Context,
endDate := startDate.AddDate(0, 0, req.ValidityDays-1).Add(23*time.Hour + 59*time.Minute + 59*time.Second) // 有效期最后一天的23:59:59结束
// 获取当前用户ID
currentUserID := "admin_uuid" // TODO: 从JWT token中获取
currentUserID := pkgcontext.GetUserIDFromContext(ctx)
if currentUserID == "" {
return nil, i18n.NewI18nError("100004", lang) // 缺少认证信息
}
// 生成授权码
authCode, err := s.generateAuthorizationCode(req.CustomerID)
@@ -268,7 +271,10 @@ func (s *authorizationCodeService) UpdateAuthorizationCode(ctx context.Context,
}
// 获取当前用户ID
currentUserID := "admin_uuid" // TODO: 从JWT token中获取
currentUserID := pkgcontext.GetUserIDFromContext(ctx)
if currentUserID == "" {
return nil, i18n.NewI18nError("100004", lang)
}
// 记录变更前的配置
oldConfig := s.buildConfigSnapshot(existingAuthCode)
@@ -363,7 +369,10 @@ func (s *authorizationCodeService) LockUnlockAuthorizationCode(ctx context.Conte
}
// 获取当前用户ID
currentUserID := "admin_uuid" // TODO: 从JWT token中获取
currentUserID := pkgcontext.GetUserIDFromContext(ctx)
if currentUserID == "" {
return nil, i18n.NewI18nError("100004", lang)
}
// 记录变更前的配置
oldConfig := s.buildConfigSnapshot(existingAuthCode)
@@ -415,7 +424,10 @@ func (s *authorizationCodeService) DeleteAuthorizationCode(ctx context.Context,
}
// 获取当前用户ID
currentUserID := "admin_uuid" // TODO: 从JWT token中获取
currentUserID := pkgcontext.GetUserIDFromContext(ctx)
if currentUserID == "" {
return i18n.NewI18nError("100004", lang)
}
// 先查询现有授权码,确保存在
existingAuthCode, err := s.authCodeRepo.GetAuthorizationCodeByID(ctx, id)
+59 -6
View File
@@ -2,6 +2,8 @@ package context
import (
"context"
"github.com/gin-gonic/gin"
)
// 定义Context键的类型,避免键冲突
@@ -55,11 +57,62 @@ func WithUserID(ctx context.Context, userID string) context.Context {
return context.WithValue(ctx, UserIDKey, userID)
}
// GetUserIDFromContext 从Context中获取用户ID
// GetUserIDFromContext 从context中获取当前用户ID
func GetUserIDFromContext(ctx context.Context) string {
if userID, ok := ctx.Value(UserIDKey).(string); ok {
return userID
// 尝试从gin.Context获取
if ginCtx, ok := ctx.(*gin.Context); ok {
if userID, exists := ginCtx.Get("user_id"); exists {
if id, ok := userID.(string); ok {
return id
}
}
}
// 尝试从标准context.Context获取
if userID := ctx.Value("user_id"); userID != nil {
if id, ok := userID.(string); ok {
return id
}
}
return ""
}
// GetUsernameFromContext 从context中获取当前用户名
func GetUsernameFromContext(ctx context.Context) string {
if ginCtx, ok := ctx.(*gin.Context); ok {
if username, exists := ginCtx.Get("username"); exists {
if name, ok := username.(string); ok {
return name
}
}
}
if username := ctx.Value("username"); username != nil {
if name, ok := username.(string); ok {
return name
}
}
return ""
}
// GetUserRoleFromContext 从context中获取当前用户角色
func GetUserRoleFromContext(ctx context.Context) string {
if ginCtx, ok := ctx.(*gin.Context); ok {
if role, exists := ginCtx.Get("role"); exists {
if r, ok := role.(string); ok {
return r
}
}
}
if role := ctx.Value("role"); role != nil {
if r, ok := role.(string); ok {
return r
}
}
return ""
}
@@ -99,11 +152,11 @@ func WithContextInfo(ctx context.Context, info *ContextInfo) context.Context {
if info == nil {
return ctx
}
ctx = WithLanguage(ctx, info.Language)
ctx = WithRequestID(ctx, info.RequestID)
ctx = WithUserID(ctx, info.UserID)
ctx = WithTraceID(ctx, info.TraceID)
return ctx
}
}