(c *gin.Context, token *model.Token, parts ...string)
| 274 | } |
| 275 | |
| 276 | func SetupContextForToken(c *gin.Context, token *model.Token, parts ...string) error { |
| 277 | if token == nil { |
| 278 | return fmt.Errorf("token is nil") |
| 279 | } |
| 280 | c.Set("id", token.UserId) |
| 281 | c.Set("token_id", token.Id) |
| 282 | c.Set("token_key", token.Key) |
| 283 | c.Set("token_name", token.Name) |
| 284 | c.Set("token_unlimited_quota", token.UnlimitedQuota) |
| 285 | if !token.UnlimitedQuota { |
| 286 | c.Set("token_quota", token.RemainQuota) |
| 287 | } |
| 288 | if token.ModelLimitsEnabled { |
| 289 | c.Set("token_model_limit_enabled", true) |
| 290 | c.Set("token_model_limit", token.GetModelLimitsMap()) |
| 291 | } else { |
| 292 | c.Set("token_model_limit_enabled", false) |
| 293 | } |
| 294 | c.Set("allow_ips", token.GetIpLimitsMap()) |
| 295 | c.Set("token_group", token.Group) |
| 296 | |
| 297 | // 设置令牌渠道标签到上下文中 |
| 298 | if token.ChannelTag != nil && *token.ChannelTag != "" { |
| 299 | c.Set(string(constant.ContextKeyTokenChannelTag), *token.ChannelTag) |
| 300 | } |
| 301 | |
| 302 | if len(parts) > 1 { |
| 303 | if model.IsAdmin(token.UserId) { |
| 304 | c.Set("specific_channel_id", parts[1]) |
| 305 | } else { |
| 306 | abortWithOpenAiMessage(c, http.StatusForbidden, "普通用户不支持指定渠道") |
| 307 | return fmt.Errorf("普通用户不支持指定渠道") |
| 308 | } |
| 309 | } |
| 310 | return nil |
| 311 | } |
no test coverage detected