feat(tenant): expose API Key reset from the API Info page

Add a danger-themed reset button next to the existing copy/eye buttons
on the API Info settings page. Confirmation dialog warns that the old
key is revoked immediately; on success the new plaintext key replaces
the displayed value and is auto-revealed so the user can copy it.

Backend: new POST /api/v1/tenants/:id/api-key handler that wraps the
existing TenantService.UpdateAPIKey; access is gated by the same
authorizeTenantAccess check as other tenant endpoints. The handler
returns the freshly generated plaintext key, while the encrypted form
is persisted to the database.

Frontend: new resetTenantApiKey API client, reset button + confirm
dialog wiring in ApiInfo.vue, plus matching i18n entries for zh-CN,
en-US, ko-KR, and ru-RU.
This commit is contained in:
wizardchen
2026-04-29 20:00:46 +08:00
committed by lyingbug
parent cbdfba0075
commit 7fcd92994b
8 changed files with 150 additions and 3 deletions
+48
View File
@@ -226,6 +226,54 @@ func (h *TenantHandler) UpdateTenant(c *gin.Context) {
})
}
// ResetAPIKey godoc
// @Summary 重置租户 API Key
// @Description 为指定租户生成一个新的 API Key,旧 Key 立即失效
// @Tags 租户管理
// @Accept json
// @Produce json
// @Param id path int true "租户ID"
// @Success 200 {object} map[string]interface{} "新生成的 API Key"
// @Failure 400 {object} errors.AppError "请求参数错误"
// @Failure 403 {object} errors.AppError "权限不足"
// @Security Bearer
// @Router /tenants/{id}/api-key [post]
func (h *TenantHandler) ResetAPIKey(c *gin.Context) {
ctx := c.Request.Context()
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
if err != nil {
logger.Errorf(ctx, "Invalid tenant ID: %s", secutils.SanitizeForLog(c.Param("id")))
c.Error(errors.NewBadRequestError("Invalid tenant ID"))
return
}
if _, ok := h.authorizeTenantAccess(c, id); !ok {
return
}
logger.Infof(ctx, "Resetting API key for tenant, ID: %d", id)
apiKey, err := h.service.UpdateAPIKey(ctx, id)
if err != nil {
if appErr, ok := errors.IsAppError(err); ok {
logger.Error(ctx, "Failed to reset API key: application error", appErr)
c.Error(appErr)
} else {
logger.ErrorWithFields(ctx, err, nil)
c.Error(errors.NewInternalServerError("Failed to reset API key").WithDetails(err.Error()))
}
return
}
logger.Infof(ctx, "API key reset successfully, tenant ID: %d", id)
c.JSON(http.StatusOK, gin.H{
"success": true,
"data": gin.H{
"api_key": apiKey,
},
})
}
// DeleteTenant godoc
// @Summary 删除租户
// @Description 删除指定的租户