DisableAPIKey 禁用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密钥" @Failur
(c *gin.Context)
| 277 | // @Router /api/api-keys/{id}/disable [post] |
| 278 | // @Router /api/libraries/{library_id}/api-keys/{key_id}/disable [post] # 知识库相关路由 |
| 279 | func (ctrl *APIKeyController) DisableAPIKey(c *gin.Context) { |
| 280 | // 检查路径中是否包含library_id参数 |
| 281 | libraryIDParam := c.Param("library_id") |
| 282 | |
| 283 | var keyID int64 |
| 284 | var err error |
| 285 | |
| 286 | // 根据路径格式确定API密钥ID的参数名 |
| 287 | if libraryIDParam != "" { |
| 288 | // 如果路径中有library_id参数,API密钥ID参数名为key_id |
| 289 | keyIDStr := c.Param("key_id") |
| 290 | if keyIDStr == "" { |
| 291 | c.JSON(http.StatusBadRequest, model.ParamError.ToNewErrorResponse("缺少API密钥ID参数")) |
| 292 | return |
| 293 | } |
| 294 | |
| 295 | keyID, err = strconv.ParseInt(keyIDStr, 10, 64) |
| 296 | if err != nil { |
| 297 | c.JSON(http.StatusBadRequest, model.ParamError.ToNewErrorResponse("无效的API密钥ID参数")) |
| 298 | return |
| 299 | } |
| 300 | } else { |
| 301 | // 否则API密钥ID参数名为id |
| 302 | id := c.Param("id") |
| 303 | if id == "" { |
| 304 | c.JSON(http.StatusBadRequest, model.ParamError.ToNewErrorResponse("缺少API密钥ID参数")) |
| 305 | return |
| 306 | } |
| 307 | |
| 308 | _, err = fmt.Sscanf(id, "%d", &keyID) |
| 309 | if err != nil { |
| 310 | c.JSON(http.StatusBadRequest, model.ParamError.ToNewErrorResponse("无效的API密钥ID参数")) |
| 311 | return |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | eid := config.GetEID(c) |
| 316 | creatorID := config.GetUserId(c) |
| 317 | role := config.GetUserRole(c) |
| 318 | apiKeyService := mcpsvc.NewAPIKeyService() |
| 319 | |
| 320 | var pathLibraryID *int64 |
| 321 | if libraryIDParam != "" { |
| 322 | parsed, err := strconv.ParseInt(libraryIDParam, 10, 64) |
| 323 | if err == nil { |
| 324 | pathLibraryID = &parsed |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | if err := apiKeyService.SetAPIKeyStatus(c.Request.Context(), eid, creatorID, role, keyID, pathLibraryID, false); err != nil { |
| 329 | if isPermissionRelatedError(err) { |
| 330 | c.JSON(http.StatusForbidden, model.ForbiddenError.ToResponse(err)) |
| 331 | return |
| 332 | } |
| 333 | c.JSON(http.StatusInternalServerError, model.DBError.ToResponse(err)) |
| 334 | return |
| 335 | } |
| 336 |
no test coverage detected