DeleteAPIKey 删除API密钥 @Summary 删除API密钥 @Description 删除指定ID的API密钥。需要管理员权限或对关联知识库的管理权限。 @Tags API密钥管理 @Accept json @Produce json @Security BearerAuth @Param library_id path int false "知识库ID(通过路径传递)" @Param id path string true "API密钥ID" @Success 200 {object} model.CommonResponse "成功删除API密钥" @Failure 400
(c *gin.Context)
| 199 | // @Router /api/api-keys/{id} [delete] |
| 200 | // @Router /api/libraries/{library_id}/api-keys/{key_id} [delete] # 知识库相关路由 |
| 201 | func (ctrl *APIKeyController) DeleteAPIKey(c *gin.Context) { |
| 202 | // 检查路径中是否包含library_id参数 |
| 203 | libraryIDParam := c.Param("library_id") |
| 204 | |
| 205 | var keyID int64 |
| 206 | var err error |
| 207 | |
| 208 | // 根据路径格式确定API密钥ID的参数名 |
| 209 | if libraryIDParam != "" { |
| 210 | // 如果路径中有library_id参数,API密钥ID参数名为key_id |
| 211 | keyIDStr := c.Param("key_id") |
| 212 | if keyIDStr == "" { |
| 213 | c.JSON(http.StatusBadRequest, model.ParamError.ToNewErrorResponse("缺少API密钥ID参数")) |
| 214 | return |
| 215 | } |
| 216 | |
| 217 | if decoded, err := hashids.TryParseID(keyIDStr); err == nil { |
| 218 | keyID = decoded |
| 219 | } |
| 220 | if keyID == 0 { |
| 221 | c.JSON(http.StatusBadRequest, model.ParamError.ToNewErrorResponse("无效的API密钥ID参数")) |
| 222 | return |
| 223 | } |
| 224 | } else { |
| 225 | // 否则API密钥ID参数名为id |
| 226 | id := c.Param("id") |
| 227 | if id == "" { |
| 228 | c.JSON(http.StatusBadRequest, model.ParamError.ToNewErrorResponse("缺少API密钥ID参数")) |
| 229 | return |
| 230 | } |
| 231 | |
| 232 | _, err = fmt.Sscanf(id, "%d", &keyID) |
| 233 | if err != nil { |
| 234 | c.JSON(http.StatusBadRequest, model.ParamError.ToNewErrorResponse("无效的API密钥ID参数")) |
| 235 | return |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | eid := config.GetEID(c) |
| 240 | creatorID := config.GetUserId(c) |
| 241 | role := config.GetUserRole(c) |
| 242 | apiKeyService := mcpsvc.NewAPIKeyService() |
| 243 | |
| 244 | var pathLibraryID *int64 |
| 245 | if libraryIDParam != "" { |
| 246 | parsed, err := strconv.ParseInt(libraryIDParam, 10, 64) |
| 247 | if err == nil { |
| 248 | pathLibraryID = &parsed |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | if err := apiKeyService.DeleteAPIKey(c.Request.Context(), eid, creatorID, role, keyID, pathLibraryID); err != nil { |
| 253 | if isPermissionRelatedError(err) { |
| 254 | c.JSON(http.StatusForbidden, model.ForbiddenError.ToResponse(err)) |
| 255 | return |
| 256 | } |
| 257 | c.JSON(http.StatusInternalServerError, model.DBError.ToResponse(err)) |
| 258 | return |
nothing calls this directly
no test coverage detected