Files
attacker 77c234b060 feat(audit): F1+F2 告警静默规则标记完成 + 审计日志CSV导出实现
F1 告警静默规则 [done]:
- 后端 alert_silence.go: CRUD + Toggle (onetime/daily/weekly 三模式)
- 告警引擎 isSilenced 集成 (alert_engine.go)
- 前端 SilenceRuleTab + SilenceDialog
- 路由 /alerts/silences 已注册

F2 审计日志导出 [done]:
- 重写 ExportAuditLogs: 真正的 CSV 导出 (encoding/csv + UTF-8 BOM)
- 支持 module/action/username/startTime/endTime 筛选
- 导出限制 10000 条防溢出,自动记录审计日志
- JSON 格式向后兼容 (format=json)
- 前端 exportLogs 传递查询参数

同时包含累积的前期工作:
- 目录重构 internal/ -> controller/model/router (v0.55-v0.57)
- 后端分层架构 dto/repository/service/utils
- 审计模块全功能 (终端审计/操作审计/登录日志)
- asciicast v2 录制格式
- SFTP 文件浏览器
- StatStrip 紧凑统计条
- 在线会话自动刷新
- LiveMonitor WebSocket 诊断修复

验证: go build ✅ | npm run build ✅ | Docker 部署 ✅ | 10 回归端点 ✅ | CSV 导出 ✅ | 静默 CRUD ✅
2026-06-25 20:16:30 +08:00

219 lines
5.4 KiB
Go

package controller
import (
"net/http"
"strconv"
"ezops/model"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
// ApprovalHandler 审批处理器
type ApprovalHandler struct {
db *gorm.DB
}
// NewApprovalHandler 创建审批处理器
func NewApprovalHandler(db *gorm.DB) *ApprovalHandler {
return &ApprovalHandler{db: db}
}
// ListTemplates 获取审批模板列表
// @Summary 获取审批模板列表
// @Description 获取所有审批模板列表
// @Tags 审批流程
// @Produce json
// @Security Bearer
// @Param page query int false "页码"
// @Param page_size query int false "每页数量"
// @Success 200 {object} model.Response{data=model.ListResponse}
// @Router /approvals/templates [get]
func (h *ApprovalHandler) ListTemplates(c *gin.Context) {
var query model.PageQuery
c.ShouldBindQuery(&query)
db := h.db.Model(&model.ApprovalTemplate{})
if category := c.Query("category"); category != "" {
db = db.Where("category = ?", category)
}
if qType := c.Query("type"); qType != "" {
db = db.Where("type = ?", qType)
}
var total int64
db.Count(&total)
var templates []model.ApprovalTemplate
db.Offset(query.GetOffset()).Limit(query.GetLimit()).Find(&templates)
c.JSON(http.StatusOK, model.Response{
Code: 200,
Message: "success",
Data: model.ListResponse{
List: templates,
Total: total,
},
})
}
// GetTemplate 获取审批模板详情
// @Summary 获取审批模板详情
// @Description 获取指定审批模板的详细信息
// @Tags 审批流程
// @Produce json
// @Security Bearer
// @Param id path int true "模板ID"
// @Success 200 {object} model.Response{data=model.ApprovalTemplate}
// @Router /approvals/templates/{id} [get]
func (h *ApprovalHandler) GetTemplate(c *gin.Context) {
id, _ := strconv.Atoi(c.Param("id"))
var template model.ApprovalTemplate
if err := h.db.First(&template, id).Error; err != nil {
c.JSON(http.StatusNotFound, model.Response{
Code: 404,
Message: "模板不存在",
})
return
}
c.JSON(http.StatusOK, model.Response{
Code: 200,
Message: "success",
Data: template,
})
}
// CreateTemplate 创建审批模板
// @Summary 创建审批模板
// @Description 创建新审批模板
// @Tags 审批流程
// @Accept json
// @Produce json
// @Security Bearer
// @Param request body model.CreateApprovalTemplateRequest true "创建请求"
// @Success 200 {object} model.Response{data=model.ApprovalTemplate}
// @Router /approvals/templates [post]
func (h *ApprovalHandler) CreateTemplate(c *gin.Context) {
var req model.CreateApprovalTemplateRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, model.Response{
Code: 400,
Message: "请求参数错误: " + err.Error(),
})
return
}
template := model.ApprovalTemplate{
Name: req.Name,
Code: req.Code,
Description: req.Description,
Type: req.Type,
FormSchema: req.FormSchema,
FlowConfig: req.FlowConfig,
Category: req.Category,
Enabled: true,
}
if err := h.db.Create(&template).Error; err != nil {
c.JSON(http.StatusInternalServerError, model.Response{
Code: 500,
Message: "创建失败",
})
return
}
c.JSON(http.StatusOK, model.Response{
Code: 200,
Message: "创建成功",
Data: template,
})
}
// UpdateTemplate 更新审批模板
// @Summary 更新审批模板
// @Description 更新指定审批模板
// @Tags 审批流程
// @Accept json
// @Produce json
// @Security Bearer
// @Param id path int true "模板ID"
// @Param request body model.CreateApprovalTemplateRequest true "更新请求"
// @Success 200 {object} model.Response{data=model.ApprovalTemplate}
// @Router /approvals/templates/{id} [put]
func (h *ApprovalHandler) UpdateTemplate(c *gin.Context) {
id, _ := strconv.Atoi(c.Param("id"))
var req model.CreateApprovalTemplateRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, model.Response{
Code: 400,
Message: "请求参数错误: " + err.Error(),
})
return
}
var template model.ApprovalTemplate
if err := h.db.First(&template, id).Error; err != nil {
c.JSON(http.StatusNotFound, model.Response{
Code: 404,
Message: "模板不存在",
})
return
}
updates := map[string]interface{}{
"name": req.Name,
"description": req.Description,
"type": req.Type,
"form_schema": req.FormSchema,
"flow_config": req.FlowConfig,
}
if err := h.db.Model(&template).Updates(updates).Error; err != nil {
c.JSON(http.StatusInternalServerError, model.Response{
Code: 500,
Message: "更新失败",
})
return
}
h.db.First(&template, id)
c.JSON(http.StatusOK, model.Response{
Code: 200,
Message: "更新成功",
Data: template,
})
}
// DeleteTemplate 删除审批模板
// @Summary 删除审批模板
// @Description 删除指定审批模板
// @Tags 审批流程
// @Produce json
// @Security Bearer
// @Param id path int true "模板ID"
// @Success 200 {object} model.Response
// @Router /approvals/templates/{id} [delete]
func (h *ApprovalHandler) DeleteTemplate(c *gin.Context) {
id, _ := strconv.Atoi(c.Param("id"))
if err := h.db.Delete(&model.ApprovalTemplate{}, id).Error; err != nil {
c.JSON(http.StatusInternalServerError, model.Response{
Code: 500,
Message: "删除失败",
})
return
}
c.JSON(http.StatusOK, model.Response{
Code: 200,
Message: "删除成功",
})
}
// Process 相关 handler 见 approval_process.go