初步实现审计

This commit is contained in:
2025-09-28 00:13:47 +08:00
parent b177781fa1
commit 1c7e13b965
9 changed files with 324 additions and 27 deletions

View File

@@ -3,6 +3,7 @@ package models
import (
"time"
"gorm.io/datatypes"
"gorm.io/gorm"
)
@@ -140,3 +141,32 @@ type PendingCollection struct {
func (PendingCollection) TableName() string {
return "pending_collections"
}
// --- 用户审计日志 ---
// UserActionLog 记录用户的操作历史,用于审计
type UserActionLog struct {
// Time 是操作发生的时间,作为主键和超表的时间分区键
Time time.Time `gorm:"primaryKey" json:"time"`
// --- Who (谁) ---
UserID uint `gorm:"index" json:"user_id,omitempty"`
Username string `json:"username,omitempty"` // 操作发生时用户名的快照
// --- Where (何地) ---
SourceIP string `json:"source_ip,omitempty"`
// --- What (什么) & How (如何) ---
ActionType string `gorm:"index" json:"action_type,omitempty"` // 标准化的操作类型,如 "CREATE_DEVICE"
TargetResource datatypes.JSON `gorm:"type:jsonb" json:"target_resource,omitempty"` // 被操作的资源, e.g., {"type": "device", "id": 123}
Description string `json:"description,omitempty"` // 人类可读的操作描述
Status string `json:"status,omitempty"` // success 或 failed
HTTPPath string `json:"http_path,omitempty"` // 请求的API路径
HTTPMethod string `json:"http_method,omitempty"` // 请求的HTTP方法
ResultDetails string `json:"result_details,omitempty"` // 结果详情,如失败时的错误信息
}
// TableName 自定义 GORM 使用的数据库表名
func (UserActionLog) TableName() string {
return "user_action_logs"
}