MCPcopy Create free account
hub / github.com/53AI/53AIHub / EnableAPIKey

Method EnableAPIKey

api/controller/api_key.go:355–414  ·  view source on GitHub ↗

EnableAPIKey 启用API密钥 @Summary 启用API密钥 @Description 启用指定ID的API密钥。需要管理员权限或对关联知识库的管理权限。 @Tags API密钥管理 @Accept json @Produce json @Security BearerAuth @Param library_id path int false "知识库ID(通过路径传递)" @Param key_id path string true "API密钥ID" @Success 200 {object} model.CommonResponse "成功启用API密钥" @Failure

(c *gin.Context)

Source from the content-addressed store, hash-verified

353// @Router /api/api-keys/{id}/enable [post]
354// @Router /api/libraries/{library_id}/api-keys/{key_id}/enable [post] # 知识库相关路由
355func (ctrl *APIKeyController) EnableAPIKey(c *gin.Context) {
356 // 检查路径中是否包含library_id参数
357 libraryIDParam := c.Param("library_id")
358
359 var keyID int64
360 var err error
361
362 // 根据路径格式确定API密钥ID的参数名
363 if libraryIDParam != "" {
364 // 如果路径中有library_id参数,API密钥ID参数名为key_id
365 keyIDStr := c.Param("key_id")
366 if keyIDStr == "" {
367 c.JSON(http.StatusBadRequest, model.ParamError.ToNewErrorResponse("缺少API密钥ID参数"))
368 return
369 }
370
371 keyID, err = strconv.ParseInt(keyIDStr, 10, 64)
372 if err != nil {
373 c.JSON(http.StatusBadRequest, model.ParamError.ToNewErrorResponse("无效的API密钥ID参数"))
374 return
375 }
376 } else {
377 // 否则API密钥ID参数名为id
378 id := c.Param("id")
379 if id == "" {
380 c.JSON(http.StatusBadRequest, model.ParamError.ToNewErrorResponse("缺少API密钥ID参数"))
381 return
382 }
383
384 _, err = fmt.Sscanf(id, "%d", &keyID)
385 if err != nil {
386 c.JSON(http.StatusBadRequest, model.ParamError.ToNewErrorResponse("无效的API密钥ID参数"))
387 return
388 }
389 }
390
391 eid := config.GetEID(c)
392 creatorID := config.GetUserId(c)
393 role := config.GetUserRole(c)
394 apiKeyService := mcpsvc.NewAPIKeyService()
395
396 var pathLibraryID *int64
397 if libraryIDParam != "" {
398 parsed, err := strconv.ParseInt(libraryIDParam, 10, 64)
399 if err == nil {
400 pathLibraryID = &parsed
401 }
402 }
403
404 if err := apiKeyService.SetAPIKeyStatus(c.Request.Context(), eid, creatorID, role, keyID, pathLibraryID, true); err != nil {
405 if isPermissionRelatedError(err) {
406 c.JSON(http.StatusForbidden, model.ForbiddenError.ToResponse(err))
407 return
408 }
409 c.JSON(http.StatusInternalServerError, model.DBError.ToResponse(err))
410 return
411 }
412

Callers 1

SetAPIKeyStatusMethod · 0.80

Calls 5

SetAPIKeyStatusMethod · 0.95
isPermissionRelatedErrorFunction · 0.85
ToNewErrorResponseMethod · 0.80
GetEIDMethod · 0.80
ToResponseMethod · 0.80

Tested by

no test coverage detected