feat(settings): 汇总全部数据源配置操作审计
build / build (push) Successful in 1m13s

This commit is contained in:
姚章浩
2026-10-27 10:42:30 +08:00
parent f0b12412a1
commit 7448d130ec
17 changed files with 453 additions and 56 deletions
@@ -0,0 +1,3 @@
DROP TRIGGER IF EXISTS source_configuration_audit ON source_connections;
DROP FUNCTION IF EXISTS audit_source_configuration();
DROP TABLE IF EXISTS source_configuration_events;
@@ -0,0 +1,50 @@
-- Only safe configuration metadata is audited; no URLs or credential references.
CREATE TABLE source_configuration_events (
id BIGSERIAL PRIMARY KEY,
connection_id VARCHAR(36) NOT NULL,
source_name VARCHAR(255) NOT NULL,
action VARCHAR(32) NOT NULL,
actor VARCHAR(128) NOT NULL,
created_at TIMESTAMPTZ NOT NULL,
detail TEXT NOT NULL
);
CREATE INDEX source_configuration_events_time ON source_configuration_events(created_at DESC,id DESC);
-- Evidence-backed history, explicitly labelled rather than invented user actions.
INSERT INTO source_configuration_events(connection_id,source_name,action,actor,created_at,detail)
SELECT connection_id,name,'historical_settings',created_by,activated_at,
'历史生效设置:' || CASE WHEN enabled THEN '自动刷新已启用' ELSE '自动刷新已暂停' END ||
';刷新周期(秒):' || COALESCE(schedule->>'refresh_interval_seconds','0') || '(0 为仅手动);来自已有修订记录'
FROM source_connection_revisions WHERE activated_at IS NOT NULL ORDER BY activated_at,id;
INSERT INTO source_configuration_events(connection_id,source_name,action,actor,created_at,detail)
SELECT id,name,'archived',archived_by,archived_at,'历史移除连接记录;源端文件与已入库知识保留'
FROM source_connections WHERE archived_at IS NOT NULL ORDER BY archived_at,id;
CREATE FUNCTION audit_source_configuration() RETURNS TRIGGER LANGUAGE plpgsql AS $$
DECLARE event_action TEXT; event_detail TEXT; event_actor TEXT;
BEGIN
event_actor := COALESCE(NULLIF(NEW.updated_by,''),'system');
IF TG_OP='INSERT' THEN
event_action:='created'; event_detail:='添加数据源连接';
ELSIF OLD.archived_at IS NULL AND NEW.archived_at IS NOT NULL THEN
event_action:='archived'; event_detail:='移除连接;源端文件与已入库知识保留';
event_actor:=COALESCE(NULLIF(NEW.archived_by,''),event_actor);
ELSIF OLD.archived_at IS NOT NULL AND NEW.archived_at IS NULL THEN
event_action:='restored'; event_detail:='恢复数据源连接';
ELSIF ROW(OLD.name,OLD.enabled,OLD.schedule,OLD.base_url,OLD.secret_ref) IS NOT DISTINCT FROM
ROW(NEW.name,NEW.enabled,NEW.schedule,NEW.base_url,NEW.secret_ref) THEN
RETURN NEW;
ELSE
event_action:=CASE WHEN OLD.enabled AND NOT NEW.enabled THEN 'paused' WHEN NOT OLD.enabled AND NEW.enabled THEN 'resumed' ELSE 'settings_updated' END;
event_detail:='';
IF OLD.name IS DISTINCT FROM NEW.name THEN event_detail:='名称:' || OLD.name || ' → ' || NEW.name || ';'; END IF;
IF OLD.enabled IS DISTINCT FROM NEW.enabled THEN event_detail:=event_detail || CASE WHEN NEW.enabled THEN '恢复自动刷新;' ELSE '暂停自动刷新;' END; END IF;
IF OLD.schedule IS DISTINCT FROM NEW.schedule THEN event_detail:=event_detail || '刷新周期(秒):' || COALESCE(OLD.schedule->>'refresh_interval_seconds','0') || ' → ' || COALESCE(NEW.schedule->>'refresh_interval_seconds','0') || '(0 为仅手动);'; END IF;
IF OLD.base_url IS DISTINCT FROM NEW.base_url THEN event_detail:=event_detail || '修改连接地址;'; END IF;
IF OLD.secret_ref IS DISTINCT FROM NEW.secret_ref THEN event_detail:=event_detail || '修改凭据引用;'; END IF;
END IF;
INSERT INTO source_configuration_events(connection_id,source_name,action,actor,created_at,detail)
VALUES(NEW.id,NEW.name,event_action,event_actor,clock_timestamp(),event_detail);
RETURN NEW;
END $$;
CREATE TRIGGER source_configuration_audit AFTER INSERT OR UPDATE ON source_connections FOR EACH ROW EXECUTE FUNCTION audit_source_configuration();