chore: 恢复 demo 演示与设计文档入库
This commit is contained in:
@@ -0,0 +1,439 @@
|
||||
/**
|
||||
* 操作审计 Console UI Demo · 交互层(无框架,模拟 Halo Console 插件页)
|
||||
*
|
||||
* 数据流模拟真实插件的捕获管线:
|
||||
* 请求(method/path/user) → AuditCore.shouldCapture 判定 → AuditCore.resolveResource 解析
|
||||
* → 生成审计记录 → 渲染到列表
|
||||
* 全部数据保存在页面内存中,刷新即重置。
|
||||
*/
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
var $ = function (sel) { return document.querySelector(sel); };
|
||||
|
||||
// ---------- 状态 ----------
|
||||
var state = {
|
||||
logs: [],
|
||||
filters: { operator: '', resourceType: '', method: '', result: '', timeRange: 'all', keyword: '' },
|
||||
page: 1,
|
||||
size: 10,
|
||||
tab: 'logs',
|
||||
settings: { enabled: true, retentionDays: 90 },
|
||||
detailId: null,
|
||||
highlightId: null
|
||||
};
|
||||
|
||||
var DEFAULT_FILTERS = { operator: '', resourceType: '', method: '', result: '', timeRange: 'all', keyword: '' };
|
||||
|
||||
// ---------- 工具 ----------
|
||||
function esc(s) {
|
||||
return String(s).replace(/[&<>"']/g, function (c) {
|
||||
return { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c];
|
||||
});
|
||||
}
|
||||
|
||||
function fmtTime(iso) {
|
||||
var d = new Date(iso);
|
||||
function p(n) { return n < 10 ? '0' + n : '' + n; }
|
||||
return d.getFullYear() + '-' + p(d.getMonth() + 1) + '-' + p(d.getDate()) +
|
||||
' ' + p(d.getHours()) + ':' + p(d.getMinutes()) + ':' + p(d.getSeconds());
|
||||
}
|
||||
|
||||
function fmtDate(iso) {
|
||||
return fmtTime(iso).slice(0, 10);
|
||||
}
|
||||
|
||||
function avCls(name) {
|
||||
var sum = 0;
|
||||
String(name).split('').forEach(function (c) { sum += c.charCodeAt(0); });
|
||||
return 'c-' + 'abcde'[sum % 5];
|
||||
}
|
||||
|
||||
function initial(name) {
|
||||
return String(name).charAt(0).toUpperCase();
|
||||
}
|
||||
|
||||
var toastTimer = null;
|
||||
function toast(msg, type) {
|
||||
var el = $('#toast');
|
||||
el.textContent = msg;
|
||||
el.className = 'toast show ' + (type || '');
|
||||
clearTimeout(toastTimer);
|
||||
toastTimer = setTimeout(function () { el.classList.remove('show'); }, 2800);
|
||||
}
|
||||
|
||||
function methodBadge(method) {
|
||||
return '<span class="method m-' + esc(method.toLowerCase()) + '">' + esc(method) + '</span>';
|
||||
}
|
||||
|
||||
function resultBadge(code) {
|
||||
var ok = AuditCore.isSuccess(code);
|
||||
return '<span class="result ' + (ok ? 'ok' : 'fail') + '"><i></i>' + code + ' ' + (ok ? '成功' : '失败') + '</span>';
|
||||
}
|
||||
|
||||
// ---------- 渲染 ----------
|
||||
function filteredLogs() {
|
||||
var f = state.filters;
|
||||
return AuditCore.applyFilters(state.logs, {
|
||||
operator: f.operator,
|
||||
resourceType: f.resourceType,
|
||||
method: f.method,
|
||||
result: f.result,
|
||||
timeRange: f.timeRange,
|
||||
keyword: f.keyword
|
||||
});
|
||||
}
|
||||
|
||||
function activeFilterCount() {
|
||||
var f = state.filters;
|
||||
var n = 0;
|
||||
['operator', 'resourceType', 'method', 'result'].forEach(function (k) {
|
||||
if (f[k]) n++;
|
||||
});
|
||||
if (f.timeRange && f.timeRange !== 'all') n++; // 'all' 为默认值,不算激活的筛选
|
||||
if (String(f.keyword).trim()) n++;
|
||||
return n;
|
||||
}
|
||||
|
||||
function renderFilterOptions() {
|
||||
var operators = {};
|
||||
var types = {};
|
||||
state.logs.forEach(function (l) {
|
||||
operators[l.username] = true;
|
||||
types[l.resourceType] = true;
|
||||
});
|
||||
var opSel = $('#f-operator');
|
||||
var typeSel = $('#f-type');
|
||||
opSel.innerHTML = '<option value="">全部</option>' + Object.keys(operators).sort().map(function (u) {
|
||||
return '<option value="' + esc(u) + '"' + (state.filters.operator === u ? ' selected' : '') + '>' + esc(u) + '</option>';
|
||||
}).join('');
|
||||
typeSel.innerHTML = '<option value="">全部</option>' + Object.keys(types).sort().map(function (t) {
|
||||
return '<option value="' + esc(t) + '"' + (state.filters.resourceType === t ? ' selected' : '') + '>' + esc(t) + '</option>';
|
||||
}).join('');
|
||||
// 动作选项与捕获规则一致:POST/PUT/DELETE/PATCH(AuditCore.WRITE_METHODS)
|
||||
$('#f-method').innerHTML = '<option value="">全部</option>' + AuditCore.WRITE_METHODS.map(function (m) {
|
||||
return '<option value="' + m + '"' + (state.filters.method === m ? ' selected' : '') + '>' + m + '</option>';
|
||||
}).join('');
|
||||
}
|
||||
|
||||
function renderBanner() {
|
||||
$('#audit-off-banner').hidden = state.settings.enabled;
|
||||
}
|
||||
|
||||
function renderTable() {
|
||||
var filtered = filteredLogs();
|
||||
var paged = AuditCore.paginate(filtered, state.page, state.size);
|
||||
var tbody = $('#tbody');
|
||||
|
||||
if (paged.total === 0) {
|
||||
tbody.innerHTML = '<tr><td colspan="6"><div class="empty-state">' +
|
||||
'<div class="es-icon">🔍</div>' +
|
||||
'<p>未找到匹配的审计记录</p>' +
|
||||
(activeFilterCount() ? '<button class="btn-link" id="empty-clear">清除全部筛选</button>' : '') +
|
||||
'</div></td></tr>';
|
||||
var emptyClear = $('#empty-clear');
|
||||
if (emptyClear) emptyClear.addEventListener('click', clearFilters);
|
||||
} else {
|
||||
tbody.innerHTML = paged.items.map(function (rec) {
|
||||
var cls = [];
|
||||
if (state.detailId === rec.id) cls.push('selected');
|
||||
if (state.highlightId === rec.id) cls.push('row-new');
|
||||
return '<tr data-id="' + rec.id + '" class="' + cls.join(' ') + '">' +
|
||||
'<td class="td-time">' + fmtTime(rec.timestamp) + '</td>' +
|
||||
'<td><span class="cell-user"><span class="avatar sm ' + avCls(rec.username) + '">' + esc(initial(rec.username)) + '</span>' + esc(rec.username) + '</span></td>' +
|
||||
'<td>' + methodBadge(rec.method) + '</td>' +
|
||||
'<td><span class="rt">' + esc(rec.resourceType) + '</span>' +
|
||||
(rec.resourceName ? '<span class="rn">' + esc(rec.resourceName) + '</span>' : '<span class="muted">(集合级操作)</span>') +
|
||||
'</td>' +
|
||||
'<td>' + resultBadge(rec.statusCode) + '</td>' +
|
||||
'<td class="td-ip">' + esc(rec.remoteIp) + '</td>' +
|
||||
'</tr>';
|
||||
}).join('');
|
||||
tbody.querySelectorAll('tr[data-id]').forEach(function (tr) {
|
||||
tr.addEventListener('click', function () { openDetail(tr.getAttribute('data-id')); });
|
||||
});
|
||||
}
|
||||
|
||||
$('#pg-info').textContent = '共 ' + paged.total + ' 条 · 第 ' + paged.page + ' / ' + paged.pages + ' 页';
|
||||
$('#pg-prev').disabled = paged.page <= 1;
|
||||
$('#pg-next').disabled = paged.page >= paged.pages;
|
||||
|
||||
$('#btn-clear').hidden = activeFilterCount() === 0;
|
||||
}
|
||||
|
||||
function renderSettings() {
|
||||
$('#set-enabled').checked = state.settings.enabled;
|
||||
$('#set-retention').value = state.settings.retentionDays;
|
||||
|
||||
var logs = state.logs;
|
||||
var expired = AuditCore.countExpired(logs, state.settings.retentionDays, Date.now());
|
||||
var earliest = logs.length ? logs[logs.length - 1].timestamp : null;
|
||||
$('#clean-stats').innerHTML =
|
||||
'当前共 <b>' + logs.length + '</b> 条记录,最早一条:<b>' + (earliest ? fmtDate(earliest) : '—') + '</b>。<br>' +
|
||||
'按保留期 <b>' + state.settings.retentionDays + '</b> 天计算,下次定时清理预计删除 ' +
|
||||
(expired > 0 ? '<span class="num-warn">' + expired + ' 条</span>' : '<b>0 条</b>') + '。';
|
||||
}
|
||||
|
||||
function renderTabs() {
|
||||
document.querySelectorAll('.tab').forEach(function (t) {
|
||||
t.classList.toggle('active', t.getAttribute('data-tab') === state.tab);
|
||||
});
|
||||
$('#panel-logs').hidden = state.tab !== 'logs';
|
||||
$('#panel-settings').hidden = state.tab !== 'settings';
|
||||
}
|
||||
|
||||
function renderAll() {
|
||||
renderBanner();
|
||||
renderFilterOptions();
|
||||
renderTable();
|
||||
renderSettings();
|
||||
renderTabs();
|
||||
}
|
||||
|
||||
// ---------- 详情抽屉 ----------
|
||||
function openDetail(id) {
|
||||
var rec = null;
|
||||
for (var i = 0; i < state.logs.length; i++) {
|
||||
if (state.logs[i].id === id) { rec = state.logs[i]; break; }
|
||||
}
|
||||
if (!rec) return;
|
||||
state.detailId = id;
|
||||
|
||||
var row = function (dt, dd) {
|
||||
return '<div class="d-row"><dt>' + dt + '</dt><dd>' + dd + '</dd></div>';
|
||||
};
|
||||
|
||||
var drawer = $('#drawer');
|
||||
drawer.innerHTML =
|
||||
'<div class="drawer-head">' +
|
||||
'<div><div class="drawer-title">审计记录详情</div><div class="drawer-sub">' + esc(rec.id) + '</div></div>' +
|
||||
'<button class="icon-btn" id="drawer-close" title="关闭">✕</button>' +
|
||||
'</div>' +
|
||||
'<div class="readonly-banner">🛡️ 审计记录为只读,不提供任何修改或删除入口(设计原则 3 · 技术方案 D4)</div>' +
|
||||
'<dl class="detail-list">' +
|
||||
row('时间', esc(fmtTime(rec.timestamp))) +
|
||||
row('操作者', '<span class="avatar sm ' + avCls(rec.username) + '">' + esc(initial(rec.username)) + '</span>' + esc(rec.username)) +
|
||||
row('动作', methodBadge(rec.method)) +
|
||||
row('资源类型', '<span class="rt">' + esc(rec.resourceType) + '</span>') +
|
||||
row('资源名称', rec.resourceName ? esc(rec.resourceName) : '<span class="muted">(集合级操作,无具体资源名)</span>') +
|
||||
row('完整路径', '<code class="path-code">' + esc(rec.path) + '</code>') +
|
||||
row('结果', resultBadge(rec.statusCode)) +
|
||||
row('来源 IP', esc(rec.remoteIp)) +
|
||||
row('User-Agent', '<span class="ua">' + esc(rec.userAgent) + '</span>') +
|
||||
'</dl>' +
|
||||
'<details class="raw-json"><summary>原始记录(JSON)</summary><pre>' + esc(JSON.stringify(rec, null, 2)) + '</pre></details>';
|
||||
|
||||
drawer.hidden = false;
|
||||
$('#drawer-overlay').hidden = false;
|
||||
renderTable();
|
||||
$('#drawer-close').addEventListener('click', closeDetail);
|
||||
}
|
||||
|
||||
function closeDetail() {
|
||||
state.detailId = null;
|
||||
$('#drawer').hidden = true;
|
||||
$('#drawer-overlay').hidden = true;
|
||||
renderTable();
|
||||
}
|
||||
|
||||
// ---------- 模拟一次操作 ----------
|
||||
function nextLogId() {
|
||||
var max = 0;
|
||||
state.logs.forEach(function (l) {
|
||||
var n = parseInt(String(l.id).replace('audit-', ''), 10);
|
||||
if (n > max) max = n;
|
||||
});
|
||||
return 'audit-' + String(max + 1).padStart(4, '0');
|
||||
}
|
||||
|
||||
function simulateSend() {
|
||||
var user = $('#sim-user').value;
|
||||
var method = $('#sim-method').value;
|
||||
var path = String($('#sim-path').value).trim();
|
||||
var status = parseInt($('#sim-status').value, 10);
|
||||
|
||||
if (!path) { toast('请填写请求路径', 'warn'); return; }
|
||||
|
||||
var authenticated = user !== '__anon__';
|
||||
|
||||
if (!state.settings.enabled) {
|
||||
toast('审计开关已关闭,本次操作未记录', 'warn');
|
||||
return;
|
||||
}
|
||||
|
||||
var rule = AuditCore.shouldCapture({ method: method, path: path, authenticated: authenticated });
|
||||
if (!rule.captured) {
|
||||
toast('未记录:' + rule.reason, 'warn');
|
||||
return;
|
||||
}
|
||||
|
||||
// 组装审计记录(与真实管线一致:规则判定 → 资源解析 → 落一条记录)
|
||||
var isAuthPath = path === '/login' || path === '/logout';
|
||||
var username = authenticated ? user : '未知用户';
|
||||
if (isAuthPath && status >= 400) username = '未知用户'; // 登录失败记"未知用户"(产品设计已知局限)
|
||||
|
||||
var record = {
|
||||
id: nextLogId(),
|
||||
timestamp: new Date().toISOString(),
|
||||
username: username,
|
||||
method: AuditCore.normalizeMethod(method),
|
||||
path: path,
|
||||
resourceType: AuditCore.resolveResource(path).resourceType,
|
||||
resourceName: AuditCore.resolveResource(path).resourceName,
|
||||
statusCode: status,
|
||||
remoteIp: '192.168.1.66',
|
||||
userAgent: 'OperationAuditDemo/1.0 (Playground)'
|
||||
};
|
||||
|
||||
state.logs.unshift(record);
|
||||
state.highlightId = record.id;
|
||||
setTimeout(function () { state.highlightId = null; }, 3000);
|
||||
|
||||
// 重置筛选与页码,确保新记录可见(同时恢复筛选控件的显示值)
|
||||
clearFilters();
|
||||
if (state.tab !== 'logs') switchTab('logs');
|
||||
renderAll();
|
||||
|
||||
closeModal();
|
||||
toast('已捕获并记录审计日志:' + record.resourceType + (record.resourceName ? ' / ' + record.resourceName : ''), 'ok');
|
||||
}
|
||||
|
||||
function openModal() {
|
||||
$('#modal').hidden = false;
|
||||
$('#modal-overlay').hidden = false;
|
||||
$('#sim-path').focus();
|
||||
}
|
||||
|
||||
function closeModal() {
|
||||
$('#modal').hidden = true;
|
||||
$('#modal-overlay').hidden = true;
|
||||
}
|
||||
|
||||
// ---------- 事件绑定 ----------
|
||||
function switchTab(tab) {
|
||||
state.tab = tab;
|
||||
renderTabs();
|
||||
}
|
||||
|
||||
function clearFilters() {
|
||||
state.filters = Object.assign({}, DEFAULT_FILTERS);
|
||||
$('#f-operator').value = '';
|
||||
$('#f-type').value = '';
|
||||
$('#f-method').value = '';
|
||||
$('#f-result').value = '';
|
||||
$('#f-time').value = 'all';
|
||||
$('#f-keyword').value = '';
|
||||
state.page = 1;
|
||||
renderTable();
|
||||
}
|
||||
|
||||
function bindEvents() {
|
||||
// 侧边栏占位项
|
||||
document.querySelectorAll('[data-demo]').forEach(function (el) {
|
||||
el.addEventListener('click', function (e) {
|
||||
e.preventDefault();
|
||||
toast('Demo 演示:此页面为 Halo Console 既有页面,未实现');
|
||||
});
|
||||
});
|
||||
|
||||
// Tab 切换
|
||||
document.querySelectorAll('.tab').forEach(function (t) {
|
||||
t.addEventListener('click', function () { switchTab(t.getAttribute('data-tab')); });
|
||||
});
|
||||
|
||||
// 筛选
|
||||
$('#f-operator').addEventListener('change', function (e) {
|
||||
state.filters.operator = e.target.value; state.page = 1; renderTable();
|
||||
});
|
||||
$('#f-type').addEventListener('change', function (e) {
|
||||
state.filters.resourceType = e.target.value; state.page = 1; renderTable();
|
||||
});
|
||||
$('#f-method').addEventListener('change', function (e) {
|
||||
state.filters.method = e.target.value; state.page = 1; renderTable();
|
||||
});
|
||||
$('#f-result').addEventListener('change', function (e) {
|
||||
state.filters.result = e.target.value; state.page = 1; renderTable();
|
||||
});
|
||||
$('#f-time').addEventListener('change', function (e) {
|
||||
state.filters.timeRange = e.target.value; state.page = 1; renderTable();
|
||||
});
|
||||
$('#f-keyword').addEventListener('input', function (e) {
|
||||
state.filters.keyword = e.target.value; state.page = 1; renderTable();
|
||||
});
|
||||
$('#btn-clear').addEventListener('click', clearFilters);
|
||||
|
||||
// 分页
|
||||
$('#pg-size').addEventListener('change', function (e) {
|
||||
state.size = parseInt(e.target.value, 10); state.page = 1; renderTable();
|
||||
});
|
||||
$('#pg-prev').addEventListener('click', function () { state.page--; renderTable(); });
|
||||
$('#pg-next').addEventListener('click', function () { state.page++; renderTable(); });
|
||||
|
||||
// 详情抽屉
|
||||
$('#drawer-overlay').addEventListener('click', closeDetail);
|
||||
|
||||
// 设置
|
||||
$('#btn-save-settings').addEventListener('click', function () {
|
||||
var days = parseInt($('#set-retention').value, 10);
|
||||
if (isNaN(days) || days < 7 || days > 365) {
|
||||
toast('保留天数需在 7 ~ 365 之间', 'warn');
|
||||
return;
|
||||
}
|
||||
state.settings.enabled = $('#set-enabled').checked;
|
||||
state.settings.retentionDays = days;
|
||||
renderAll();
|
||||
toast('设置已保存(Demo 模拟,对应 ConfigMap 写入)', 'ok');
|
||||
});
|
||||
$('#set-retention').addEventListener('input', function (e) {
|
||||
var days = parseInt(e.target.value, 10);
|
||||
if (!isNaN(days) && days >= 7 && days <= 365) {
|
||||
state.settings.retentionDays = days;
|
||||
renderSettings();
|
||||
}
|
||||
});
|
||||
$('#btn-clean').addEventListener('click', function () {
|
||||
var before = state.logs.length;
|
||||
var cutoff = Date.now() - state.settings.retentionDays * 86400 * 1000;
|
||||
state.logs = state.logs.filter(function (l) {
|
||||
return new Date(l.timestamp).getTime() >= cutoff;
|
||||
});
|
||||
var removed = before - state.logs.length;
|
||||
state.page = 1;
|
||||
renderAll();
|
||||
toast(removed > 0 ? '清理完成:已删除 ' + removed + ' 条过期记录' : '没有需要清理的过期记录', 'ok');
|
||||
});
|
||||
|
||||
// 模拟操作
|
||||
$('#btn-simulate').addEventListener('click', openModal);
|
||||
$('#modal-close').addEventListener('click', closeModal);
|
||||
$('#sim-cancel').addEventListener('click', closeModal);
|
||||
$('#modal-overlay').addEventListener('click', closeModal);
|
||||
$('#sim-send').addEventListener('click', simulateSend);
|
||||
$('#sim-path').addEventListener('keydown', function (e) {
|
||||
if (e.key === 'Enter') simulateSend();
|
||||
});
|
||||
document.querySelectorAll('#modal .chip').forEach(function (chip) {
|
||||
chip.addEventListener('click', function () {
|
||||
var p = JSON.parse(chip.getAttribute('data-preset'));
|
||||
$('#sim-user').value = p.user;
|
||||
$('#sim-method').value = p.method;
|
||||
$('#sim-path').value = p.path;
|
||||
$('#sim-status').value = String(p.status);
|
||||
$('#sim-path').focus();
|
||||
});
|
||||
});
|
||||
|
||||
// Esc 关闭抽屉/弹窗
|
||||
document.addEventListener('keydown', function (e) {
|
||||
if (e.key === 'Escape') {
|
||||
if (!$('#modal').hidden) closeModal();
|
||||
else if (!$('#drawer').hidden) closeDetail();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// ---------- 启动 ----------
|
||||
state.logs = MockData.generateLogs();
|
||||
bindEvents();
|
||||
renderAll();
|
||||
})();
|
||||
@@ -0,0 +1,158 @@
|
||||
/**
|
||||
* 操作审计 · 核心纯函数(浏览器与 Node 测试共用)
|
||||
*
|
||||
* 对应文档:
|
||||
* - 技术方案 D1 捕获规则(shouldCapture)
|
||||
* - 技术方案 5.2 资源解析规则(resolveResource)
|
||||
* - 产品设计 4.2 筛选维度(applyFilters)
|
||||
* - 技术方案 D5 保留期清理(countExpired)
|
||||
*
|
||||
* 本文件不依赖任何 DOM / 网络能力,仅做纯计算,便于单元测试。
|
||||
*/
|
||||
(function (root, factory) {
|
||||
if (typeof module === 'object' && module.exports) {
|
||||
module.exports = factory();
|
||||
} else {
|
||||
root.AuditCore = factory();
|
||||
}
|
||||
})(typeof self !== 'undefined' ? self : this, function () {
|
||||
'use strict';
|
||||
|
||||
var WRITE_METHODS = ['POST', 'PUT', 'DELETE', 'PATCH'];
|
||||
var AUTH_PATHS = ['/login', '/logout'];
|
||||
|
||||
function normalizeMethod(method) {
|
||||
return String(method || '').trim().toUpperCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* 捕获规则(技术方案 D1):
|
||||
* - /apis/** 下已登录用户的写操作(POST/PUT/DELETE/PATCH)
|
||||
* - 或路径精确匹配 /login、/logout(登录事件始终记录,即使匿名)
|
||||
* - /apis/** 下认证后仍为匿名主体的请求不记录(401/403 场景)
|
||||
*
|
||||
* @param {{method: string, path: string, authenticated: boolean}} options
|
||||
* @returns {{captured: boolean, reason: string}} reason 用于 Demo 中向用户解释判定依据
|
||||
*/
|
||||
function shouldCapture(options) {
|
||||
var method = normalizeMethod(options && options.method);
|
||||
var path = String((options && options.path) || '').trim();
|
||||
var authenticated = !!(options && options.authenticated);
|
||||
|
||||
if (AUTH_PATHS.indexOf(path) !== -1) {
|
||||
return { captured: true, reason: '登录/登出事件,始终记录' };
|
||||
}
|
||||
if (path.indexOf('/apis/') !== 0) {
|
||||
return { captured: false, reason: '路径不在捕获范围(仅 /apis/** 与 /login、/logout)' };
|
||||
}
|
||||
if (!authenticated) {
|
||||
return { captured: false, reason: '匿名请求不记录(认证后仍为匿名主体的 401/403 场景)' };
|
||||
}
|
||||
if (WRITE_METHODS.indexOf(method) === -1) {
|
||||
return { captured: false, reason: '仅记录写操作(POST/PUT/DELETE/PATCH),读操作不记录' };
|
||||
}
|
||||
return { captured: true, reason: '已登录用户对 /apis/** 的写操作,予以记录' };
|
||||
}
|
||||
|
||||
/**
|
||||
* 资源解析(技术方案 5.2,通用规则,不追求语义完美):
|
||||
* /apis/{group}/{version}/{plural} → resourceType = plural,resourceName 为空
|
||||
* /apis/{group}/{version}/{plural}/{name} → resourceType = plural,resourceName = name
|
||||
* 更深层子资源 → 取前两段(完整路径由 path 字段兜底)
|
||||
* /login、/logout → resourceType = "auth"
|
||||
* 其他 → resourceType = "other"
|
||||
*/
|
||||
function resolveResource(path) {
|
||||
var p = String(path || '').trim();
|
||||
if (AUTH_PATHS.indexOf(p) !== -1) {
|
||||
return { resourceType: 'auth', resourceName: '' };
|
||||
}
|
||||
if (p.indexOf('/apis/') !== 0) {
|
||||
return { resourceType: 'other', resourceName: '' };
|
||||
}
|
||||
// segs[0]='apis',segs[1]=group,segs[2]=version,segs[3]=plural,segs[4]=name
|
||||
var segs = p.split('/').filter(function (s) { return s.length > 0; });
|
||||
if (segs.length < 4) {
|
||||
return { resourceType: 'other', resourceName: '' };
|
||||
}
|
||||
return { resourceType: segs[3], resourceName: segs[4] || '' };
|
||||
}
|
||||
|
||||
/** 结果判定:2xx/3xx 视为成功 */
|
||||
function isSuccess(statusCode) {
|
||||
return statusCode > 0 && statusCode < 400;
|
||||
}
|
||||
|
||||
/**
|
||||
* 筛选(产品设计 4.2 + 动作维度):按操作者、资源类型、动作、结果、时间范围;
|
||||
* keyword 对资源名称/路径模糊匹配。
|
||||
* 返回按时间倒序的新数组,不修改入参。
|
||||
*
|
||||
* @param {Array} logs
|
||||
* @param {{operator?: string, resourceType?: string, method?: string, result?: 'success'|'failure'|'',
|
||||
* timeRange?: '24h'|'7d'|'30d'|'all', keyword?: string, now?: number}} f
|
||||
*/
|
||||
function applyFilters(logs, f) {
|
||||
f = f || {};
|
||||
var now = typeof f.now === 'number' ? f.now : Date.now();
|
||||
var keyword = String(f.keyword || '').trim().toLowerCase();
|
||||
var method = normalizeMethod(f.method);
|
||||
var cutoff = null;
|
||||
if (f.timeRange === '24h') cutoff = now - 24 * 3600 * 1000;
|
||||
else if (f.timeRange === '7d') cutoff = now - 7 * 86400 * 1000;
|
||||
else if (f.timeRange === '30d') cutoff = now - 30 * 86400 * 1000;
|
||||
|
||||
return logs.filter(function (log) {
|
||||
if (f.operator && log.username !== f.operator) return false;
|
||||
if (f.resourceType && log.resourceType !== f.resourceType) return false;
|
||||
if (method && log.method !== method) return false;
|
||||
if (f.result === 'success' && !isSuccess(log.statusCode)) return false;
|
||||
if (f.result === 'failure' && isSuccess(log.statusCode)) return false;
|
||||
if (cutoff !== null && new Date(log.timestamp).getTime() < cutoff) return false;
|
||||
if (keyword) {
|
||||
var hay = (log.resourceName + ' ' + log.path).toLowerCase();
|
||||
if (hay.indexOf(keyword) === -1) return false;
|
||||
}
|
||||
return true;
|
||||
}).sort(function (a, b) {
|
||||
return new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页(page 从 1 开始,越界自动收敛到合法页码)
|
||||
* @returns {{items: Array, total: number, pages: number, page: number}}
|
||||
*/
|
||||
function paginate(items, page, size) {
|
||||
var total = items.length;
|
||||
var pages = Math.max(1, Math.ceil(total / size));
|
||||
var current = Math.min(Math.max(1, page || 1), pages);
|
||||
var start = (current - 1) * size;
|
||||
return {
|
||||
items: items.slice(start, start + size),
|
||||
total: total,
|
||||
pages: pages,
|
||||
page: current
|
||||
};
|
||||
}
|
||||
|
||||
/** 保留期清理(技术方案 D5):统计超过保留天数的记录条数 */
|
||||
function countExpired(logs, retentionDays, now) {
|
||||
var cutoff = now - retentionDays * 86400 * 1000;
|
||||
return logs.filter(function (log) {
|
||||
return new Date(log.timestamp).getTime() < cutoff;
|
||||
}).length;
|
||||
}
|
||||
|
||||
return {
|
||||
WRITE_METHODS: WRITE_METHODS,
|
||||
AUTH_PATHS: AUTH_PATHS,
|
||||
normalizeMethod: normalizeMethod,
|
||||
shouldCapture: shouldCapture,
|
||||
resolveResource: resolveResource,
|
||||
isSuccess: isSuccess,
|
||||
applyFilters: applyFilters,
|
||||
paginate: paginate,
|
||||
countExpired: countExpired
|
||||
};
|
||||
});
|
||||
@@ -0,0 +1,737 @@
|
||||
/* =========================================================
|
||||
* 操作审计 Console UI Demo · 样式
|
||||
* 视觉基调取自 Halo Console(@halo-dev/components):
|
||||
* primary #4ccba0 / secondary #0e1731 / danger #d71d1d
|
||||
* ========================================================= */
|
||||
|
||||
:root {
|
||||
--primary: #4ccba0;
|
||||
--primary-dark: #2ea27f;
|
||||
--primary-tint: rgba(76, 203, 160, 0.12);
|
||||
--secondary: #0e1731;
|
||||
--danger: #d71d1d;
|
||||
--danger-tint: rgba(215, 29, 29, 0.08);
|
||||
--warn: #b45309;
|
||||
--warn-tint: rgba(217, 119, 6, 0.1);
|
||||
--info: #2563eb;
|
||||
--info-tint: rgba(37, 99, 235, 0.1);
|
||||
--gray-50: #f9fafb;
|
||||
--gray-100: #f3f4f6;
|
||||
--gray-200: #e5e7eb;
|
||||
--gray-300: #d1d5db;
|
||||
--gray-400: #9ca3af;
|
||||
--gray-500: #6b7280;
|
||||
--gray-700: #374151;
|
||||
--gray-900: #111827;
|
||||
--radius: 8px;
|
||||
--shadow: 0 1px 3px rgba(14, 23, 49, 0.06), 0 1px 2px rgba(14, 23, 49, 0.04);
|
||||
--shadow-lg: 0 10px 30px rgba(14, 23, 49, 0.18);
|
||||
}
|
||||
|
||||
* { box-sizing: border-box; }
|
||||
|
||||
html, body { height: 100%; }
|
||||
|
||||
body {
|
||||
margin: 0;
|
||||
display: flex;
|
||||
min-width: 1080px;
|
||||
font-size: 14px;
|
||||
color: var(--gray-900);
|
||||
background: var(--gray-100);
|
||||
font-family: system-ui, -apple-system, 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', sans-serif;
|
||||
}
|
||||
|
||||
/* ============ 左侧导航 ============ */
|
||||
.sidebar {
|
||||
width: 232px;
|
||||
flex-shrink: 0;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
background: #fff;
|
||||
border-right: 1px solid var(--gray-200);
|
||||
position: sticky;
|
||||
top: 0;
|
||||
height: 100vh;
|
||||
}
|
||||
|
||||
.logo-row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
padding: 14px 16px;
|
||||
height: 56px;
|
||||
border-bottom: 1px solid var(--gray-100);
|
||||
}
|
||||
|
||||
.logo-mark { width: 26px; height: 26px; }
|
||||
|
||||
.logo-text {
|
||||
font-size: 18px;
|
||||
font-weight: 700;
|
||||
letter-spacing: 0.5px;
|
||||
}
|
||||
|
||||
.demo-badge {
|
||||
font-size: 10px;
|
||||
font-weight: 600;
|
||||
color: var(--primary-dark);
|
||||
background: var(--primary-tint);
|
||||
border-radius: 999px;
|
||||
padding: 2px 8px;
|
||||
margin-left: auto;
|
||||
}
|
||||
|
||||
.nav-scroll { flex: 1; overflow-y: auto; padding: 8px; }
|
||||
|
||||
.nav-group {
|
||||
font-size: 11px;
|
||||
color: var(--gray-400);
|
||||
padding: 14px 12px 4px;
|
||||
font-weight: 600;
|
||||
letter-spacing: 1px;
|
||||
}
|
||||
|
||||
.nav-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
height: 38px;
|
||||
padding: 0 12px;
|
||||
margin: 1px 0;
|
||||
border-radius: 6px;
|
||||
color: var(--gray-700);
|
||||
text-decoration: none;
|
||||
font-weight: 500;
|
||||
cursor: pointer;
|
||||
transition: background 0.15s, color 0.15s;
|
||||
}
|
||||
|
||||
.nav-item svg {
|
||||
width: 17px;
|
||||
height: 17px;
|
||||
fill: none;
|
||||
stroke: currentColor;
|
||||
stroke-width: 1.7;
|
||||
stroke-linecap: round;
|
||||
stroke-linejoin: round;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.nav-item:hover { background: var(--gray-100); }
|
||||
|
||||
.nav-item.active {
|
||||
background: var(--primary-tint);
|
||||
color: var(--primary-dark);
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.sidebar-footer {
|
||||
border-top: 1px solid var(--gray-100);
|
||||
padding: 10px 12px;
|
||||
}
|
||||
|
||||
.user-chip { display: flex; align-items: center; gap: 10px; }
|
||||
|
||||
.user-meta { display: flex; flex-direction: column; line-height: 1.3; }
|
||||
.user-meta b { font-size: 13px; }
|
||||
.user-meta small { font-size: 11px; color: var(--gray-500); }
|
||||
|
||||
/* ============ 主内容区 ============ */
|
||||
.main {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
padding: 24px 28px 40px;
|
||||
}
|
||||
|
||||
.page-header {
|
||||
display: flex;
|
||||
align-items: flex-start;
|
||||
justify-content: space-between;
|
||||
gap: 16px;
|
||||
}
|
||||
|
||||
.page-header h1 {
|
||||
margin: 0;
|
||||
font-size: 20px;
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
.page-sub {
|
||||
margin: 4px 0 0;
|
||||
color: var(--gray-500);
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.readonly-hint { color: var(--primary-dark); }
|
||||
|
||||
.tabs {
|
||||
display: flex;
|
||||
gap: 4px;
|
||||
margin: 18px 0 14px;
|
||||
border-bottom: 1px solid var(--gray-200);
|
||||
}
|
||||
|
||||
.tab {
|
||||
appearance: none;
|
||||
border: none;
|
||||
background: none;
|
||||
padding: 8px 14px;
|
||||
font-size: 14px;
|
||||
font-weight: 500;
|
||||
color: var(--gray-500);
|
||||
cursor: pointer;
|
||||
border-bottom: 2px solid transparent;
|
||||
margin-bottom: -1px;
|
||||
}
|
||||
|
||||
.tab:hover { color: var(--gray-900); }
|
||||
|
||||
.tab.active {
|
||||
color: var(--primary-dark);
|
||||
border-bottom-color: var(--primary);
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
/* ============ 卡片 / 通用 ============ */
|
||||
.card {
|
||||
background: #fff;
|
||||
border: 1px solid var(--gray-200);
|
||||
border-radius: var(--radius);
|
||||
box-shadow: var(--shadow);
|
||||
}
|
||||
|
||||
.card-title {
|
||||
font-size: 15px;
|
||||
font-weight: 600;
|
||||
padding: 14px 18px 0;
|
||||
}
|
||||
|
||||
.btn {
|
||||
appearance: none;
|
||||
border: none;
|
||||
border-radius: 6px;
|
||||
padding: 8px 16px;
|
||||
font-size: 13px;
|
||||
font-weight: 500;
|
||||
cursor: pointer;
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
transition: filter 0.15s, background 0.15s;
|
||||
}
|
||||
|
||||
.btn svg {
|
||||
width: 13px;
|
||||
height: 13px;
|
||||
fill: currentColor;
|
||||
}
|
||||
|
||||
.btn-primary { background: var(--primary); color: #fff; }
|
||||
.btn-primary:hover { filter: brightness(0.94); }
|
||||
|
||||
.btn-outline {
|
||||
background: #fff;
|
||||
color: var(--gray-700);
|
||||
border: 1px solid var(--gray-300);
|
||||
}
|
||||
.btn-outline:hover { border-color: var(--primary); color: var(--primary-dark); }
|
||||
|
||||
.btn-link {
|
||||
appearance: none;
|
||||
border: none;
|
||||
background: none;
|
||||
color: var(--primary-dark);
|
||||
font-size: 13px;
|
||||
cursor: pointer;
|
||||
padding: 8px 4px;
|
||||
align-self: flex-end;
|
||||
}
|
||||
|
||||
.btn-link:hover { text-decoration: underline; }
|
||||
|
||||
.icon-btn {
|
||||
appearance: none;
|
||||
border: none;
|
||||
background: none;
|
||||
width: 28px;
|
||||
height: 28px;
|
||||
border-radius: 6px;
|
||||
font-size: 16px;
|
||||
color: var(--gray-500);
|
||||
cursor: pointer;
|
||||
line-height: 1;
|
||||
}
|
||||
|
||||
.icon-btn:hover { background: var(--gray-100); color: var(--gray-900); }
|
||||
|
||||
.input {
|
||||
border: 1px solid var(--gray-300);
|
||||
border-radius: 6px;
|
||||
padding: 7px 10px;
|
||||
font-size: 13px;
|
||||
font-family: inherit;
|
||||
outline: none;
|
||||
transition: border-color 0.15s, box-shadow 0.15s;
|
||||
background: #fff;
|
||||
}
|
||||
|
||||
.input:focus,
|
||||
select:focus {
|
||||
border-color: var(--primary);
|
||||
box-shadow: 0 0 0 2px var(--primary-tint);
|
||||
}
|
||||
|
||||
select, .input { min-width: 0; }
|
||||
|
||||
select {
|
||||
appearance: none;
|
||||
border: 1px solid var(--gray-300);
|
||||
border-radius: 6px;
|
||||
padding: 7px 28px 7px 10px;
|
||||
font-size: 13px;
|
||||
font-family: inherit;
|
||||
background: #fff url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6' viewBox='0 0 10 6'%3E%3Cpath d='M1 1l4 4 4-4' stroke='%236b7280' stroke-width='1.5' fill='none' stroke-linecap='round'/%3E%3C/svg%3E") no-repeat right 10px center;
|
||||
outline: none;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.hint { font-size: 12px; color: var(--gray-400); }
|
||||
.muted { color: var(--gray-400); font-size: 12px; }
|
||||
|
||||
/* ============ 横幅 ============ */
|
||||
.banner {
|
||||
border-radius: var(--radius);
|
||||
padding: 10px 14px;
|
||||
font-size: 13px;
|
||||
margin-bottom: 14px;
|
||||
}
|
||||
|
||||
.banner-warn { background: var(--warn-tint); color: var(--warn); border: 1px solid rgba(217, 119, 6, 0.25); }
|
||||
|
||||
/* ============ 筛选栏 ============ */
|
||||
.filter-bar {
|
||||
display: flex;
|
||||
align-items: flex-end;
|
||||
gap: 12px;
|
||||
padding: 12px 16px;
|
||||
margin-bottom: 14px;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.filter-item { display: flex; flex-direction: column; gap: 4px; }
|
||||
.filter-item.grow { flex: 1; min-width: 180px; }
|
||||
.filter-item label { font-size: 11px; color: var(--gray-500); font-weight: 600; }
|
||||
.filter-item select, .filter-item .input { min-width: 120px; width: 100%; }
|
||||
|
||||
/* ============ 表格 ============ */
|
||||
.table-card { overflow: hidden; }
|
||||
|
||||
.audit-table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
}
|
||||
|
||||
.audit-table th {
|
||||
text-align: left;
|
||||
font-size: 12px;
|
||||
font-weight: 600;
|
||||
color: var(--gray-500);
|
||||
padding: 10px 16px;
|
||||
background: var(--gray-50);
|
||||
border-bottom: 1px solid var(--gray-200);
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.audit-table td {
|
||||
padding: 10px 16px;
|
||||
border-bottom: 1px solid var(--gray-100);
|
||||
font-size: 13px;
|
||||
vertical-align: middle;
|
||||
}
|
||||
|
||||
.audit-table tbody tr {
|
||||
cursor: pointer;
|
||||
transition: background 0.12s;
|
||||
}
|
||||
|
||||
.audit-table tbody tr:hover { background: var(--gray-50); }
|
||||
|
||||
.audit-table tbody tr.selected td { background: var(--primary-tint); }
|
||||
|
||||
@keyframes rowFlash {
|
||||
0% { background: var(--primary-tint); }
|
||||
100% { background: transparent; }
|
||||
}
|
||||
|
||||
tr.row-new td { animation: rowFlash 2.6s ease-out; }
|
||||
|
||||
.td-time { color: var(--gray-700); font-variant-numeric: tabular-nums; white-space: nowrap; }
|
||||
.td-ip { color: var(--gray-500); font-variant-numeric: tabular-nums; }
|
||||
|
||||
.cell-user { display: flex; align-items: center; gap: 8px; }
|
||||
|
||||
/* 动作徽标 */
|
||||
.method {
|
||||
display: inline-block;
|
||||
font-size: 11px;
|
||||
font-weight: 700;
|
||||
font-family: ui-monospace, Consolas, monospace;
|
||||
padding: 2px 8px;
|
||||
border-radius: 5px;
|
||||
letter-spacing: 0.4px;
|
||||
}
|
||||
.m-post { background: var(--info-tint); color: var(--info); }
|
||||
.m-put { background: var(--warn-tint); color: var(--warn); }
|
||||
.m-delete { background: var(--danger-tint); color: var(--danger); }
|
||||
.m-patch { background: rgba(124, 58, 237, 0.1); color: #6d28d9; }
|
||||
.m-get { background: var(--gray-100); color: var(--gray-500); }
|
||||
|
||||
/* 资源列 */
|
||||
.rt {
|
||||
display: inline-block;
|
||||
font-size: 11px;
|
||||
font-weight: 600;
|
||||
color: var(--gray-700);
|
||||
background: var(--gray-100);
|
||||
border-radius: 4px;
|
||||
padding: 2px 7px;
|
||||
margin-right: 8px;
|
||||
}
|
||||
.rn { color: var(--gray-500); font-size: 12.5px; word-break: break-all; }
|
||||
|
||||
/* 结果徽标 */
|
||||
.result {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
font-size: 12.5px;
|
||||
font-weight: 500;
|
||||
font-variant-numeric: tabular-nums;
|
||||
}
|
||||
.result i {
|
||||
width: 7px;
|
||||
height: 7px;
|
||||
border-radius: 50%;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
.result.ok { color: #16a34a; }
|
||||
.result.ok i { background: #16a34a; }
|
||||
.result.fail { color: var(--danger); }
|
||||
.result.fail i { background: var(--danger); }
|
||||
|
||||
/* 空状态 */
|
||||
.empty-state {
|
||||
text-align: center;
|
||||
padding: 48px 0 40px;
|
||||
color: var(--gray-500);
|
||||
}
|
||||
.empty-state .es-icon { font-size: 30px; margin-bottom: 8px; }
|
||||
.empty-state p { margin: 4px 0; }
|
||||
.empty-state .btn-link { padding: 4px 8px; }
|
||||
|
||||
/* 分页 */
|
||||
.pagination {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
padding: 10px 16px;
|
||||
border-top: 1px solid var(--gray-200);
|
||||
background: #fff;
|
||||
}
|
||||
|
||||
.pg-info { font-size: 12.5px; color: var(--gray-500); }
|
||||
|
||||
.pg-right { display: flex; align-items: center; gap: 8px; }
|
||||
.pg-right select { padding: 5px 24px 5px 8px; font-size: 12.5px; }
|
||||
.icon-btn[disabled] { opacity: 0.35; cursor: not-allowed; }
|
||||
|
||||
/* ============ 设置页 ============ */
|
||||
#panel-settings .settings-card { max-width: 780px; }
|
||||
.settings-card { margin-bottom: 16px; padding-bottom: 6px; }
|
||||
|
||||
.setting-row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 24px;
|
||||
padding: 16px 18px;
|
||||
border-bottom: 1px solid var(--gray-100);
|
||||
}
|
||||
|
||||
.setting-text { max-width: 640px; }
|
||||
.s-name { font-size: 14px; font-weight: 600; margin-bottom: 4px; }
|
||||
.s-desc { font-size: 12.5px; color: var(--gray-500); line-height: 1.5; }
|
||||
|
||||
.input.num { width: 90px; text-align: center; }
|
||||
|
||||
.settings-actions {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 14px;
|
||||
padding: 14px 18px 16px;
|
||||
}
|
||||
|
||||
.clean-stats {
|
||||
padding: 14px 18px;
|
||||
font-size: 13px;
|
||||
color: var(--gray-700);
|
||||
line-height: 1.7;
|
||||
}
|
||||
.clean-stats .num-warn { color: var(--danger); font-weight: 600; }
|
||||
|
||||
.settings-card .btn { margin: 0 18px 16px; }
|
||||
|
||||
/* 开关 */
|
||||
.switch { position: relative; display: inline-block; width: 40px; height: 22px; flex-shrink: 0; }
|
||||
.switch input { opacity: 0; width: 0; height: 0; }
|
||||
|
||||
.slider {
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
background: var(--gray-300);
|
||||
border-radius: 999px;
|
||||
transition: background 0.2s;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.slider::before {
|
||||
content: '';
|
||||
position: absolute;
|
||||
width: 16px;
|
||||
height: 16px;
|
||||
left: 3px;
|
||||
top: 3px;
|
||||
border-radius: 50%;
|
||||
background: #fff;
|
||||
transition: transform 0.2s;
|
||||
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
|
||||
}
|
||||
|
||||
.switch input:checked + .slider { background: var(--primary); }
|
||||
.switch input:checked + .slider::before { transform: translateX(18px); }
|
||||
|
||||
/* ============ 头像 ============ */
|
||||
.avatar {
|
||||
width: 28px;
|
||||
height: 28px;
|
||||
border-radius: 50%;
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
font-size: 12px;
|
||||
font-weight: 700;
|
||||
color: #fff;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.avatar.sm { width: 22px; height: 22px; font-size: 10px; }
|
||||
|
||||
.c-a { background: #4ccba0; }
|
||||
.c-b { background: #6366f1; }
|
||||
.c-c { background: #f59e0b; }
|
||||
.c-d { background: #ec4899; }
|
||||
.c-e { background: #0ea5e9; }
|
||||
|
||||
/* ============ 详情抽屉 ============ */
|
||||
.overlay {
|
||||
position: fixed;
|
||||
inset: 0;
|
||||
background: rgba(14, 23, 49, 0.4);
|
||||
z-index: 40;
|
||||
}
|
||||
|
||||
.drawer {
|
||||
position: fixed;
|
||||
top: 0;
|
||||
right: 0;
|
||||
width: 440px;
|
||||
max-width: 92vw;
|
||||
height: 100vh;
|
||||
background: #fff;
|
||||
z-index: 50;
|
||||
box-shadow: var(--shadow-lg);
|
||||
overflow-y: auto;
|
||||
padding: 20px 22px 28px;
|
||||
}
|
||||
|
||||
@keyframes slideIn {
|
||||
from { transform: translateX(60px); opacity: 0; }
|
||||
to { transform: translateX(0); opacity: 1; }
|
||||
}
|
||||
|
||||
.drawer[hidden] { display: none; }
|
||||
.drawer { animation: slideIn 0.25s ease-out; }
|
||||
|
||||
.drawer-head {
|
||||
display: flex;
|
||||
align-items: flex-start;
|
||||
justify-content: space-between;
|
||||
margin-bottom: 14px;
|
||||
}
|
||||
|
||||
.drawer-title { font-size: 16px; font-weight: 700; }
|
||||
.drawer-sub { font-size: 12px; color: var(--gray-400); margin-top: 3px; font-family: ui-monospace, Consolas, monospace; }
|
||||
|
||||
.readonly-banner {
|
||||
background: var(--primary-tint);
|
||||
color: var(--primary-dark);
|
||||
font-size: 12.5px;
|
||||
border-radius: 6px;
|
||||
padding: 9px 12px;
|
||||
margin-bottom: 16px;
|
||||
line-height: 1.5;
|
||||
}
|
||||
|
||||
.detail-list { margin: 0; }
|
||||
|
||||
.detail-list .d-row {
|
||||
padding: 9px 0;
|
||||
border-bottom: 1px dashed var(--gray-200);
|
||||
}
|
||||
|
||||
.detail-list dt {
|
||||
font-size: 11.5px;
|
||||
color: var(--gray-500);
|
||||
font-weight: 600;
|
||||
margin-bottom: 5px;
|
||||
}
|
||||
|
||||
.detail-list dd {
|
||||
margin: 0;
|
||||
font-size: 13px;
|
||||
word-break: break-all;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.path-code {
|
||||
display: block;
|
||||
font-family: ui-monospace, Consolas, 'Courier New', monospace;
|
||||
font-size: 12px;
|
||||
background: var(--gray-100);
|
||||
border-radius: 5px;
|
||||
padding: 7px 9px;
|
||||
word-break: break-all;
|
||||
line-height: 1.6;
|
||||
}
|
||||
|
||||
.ua { font-size: 12px; color: var(--gray-500); line-height: 1.6; }
|
||||
|
||||
.raw-json { margin-top: 16px; }
|
||||
.raw-json summary {
|
||||
font-size: 12.5px;
|
||||
color: var(--gray-500);
|
||||
cursor: pointer;
|
||||
user-select: none;
|
||||
}
|
||||
.raw-json summary:hover { color: var(--gray-900); }
|
||||
|
||||
.raw-json pre {
|
||||
background: var(--secondary);
|
||||
color: #d1fae5;
|
||||
font-size: 11.5px;
|
||||
border-radius: 6px;
|
||||
padding: 12px;
|
||||
overflow-x: auto;
|
||||
line-height: 1.6;
|
||||
}
|
||||
|
||||
/* ============ 模拟操作弹窗 ============ */
|
||||
.modal {
|
||||
position: fixed;
|
||||
top: 50%;
|
||||
left: 50%;
|
||||
transform: translate(-50%, -50%);
|
||||
width: 520px;
|
||||
max-width: 92vw;
|
||||
background: #fff;
|
||||
border-radius: 10px;
|
||||
z-index: 50;
|
||||
box-shadow: var(--shadow-lg);
|
||||
padding: 18px 22px 20px;
|
||||
}
|
||||
|
||||
.modal[hidden] { display: none; }
|
||||
|
||||
@keyframes popIn {
|
||||
from { transform: translate(-50%, -50%) scale(0.96); opacity: 0; }
|
||||
to { transform: translate(-50%, -50%) scale(1); opacity: 1; }
|
||||
}
|
||||
|
||||
.modal { animation: popIn 0.2s ease-out; }
|
||||
|
||||
.modal-head {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
font-size: 16px;
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
.modal-sub {
|
||||
font-size: 12.5px;
|
||||
color: var(--gray-500);
|
||||
line-height: 1.6;
|
||||
margin: 8px 0 14px;
|
||||
}
|
||||
|
||||
.modal-sub code {
|
||||
background: var(--gray-100);
|
||||
border-radius: 4px;
|
||||
padding: 1px 5px;
|
||||
font-size: 11.5px;
|
||||
}
|
||||
|
||||
.field { display: flex; flex-direction: column; gap: 5px; margin-bottom: 12px; flex: 1; }
|
||||
.field label { font-size: 12px; color: var(--gray-500); font-weight: 600; }
|
||||
.field select, .field .input { width: 100%; }
|
||||
|
||||
.field-row { display: flex; gap: 12px; }
|
||||
|
||||
.presets { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 14px; }
|
||||
|
||||
.chip {
|
||||
appearance: none;
|
||||
border: 1px solid var(--gray-300);
|
||||
background: #fff;
|
||||
border-radius: 999px;
|
||||
padding: 5px 12px;
|
||||
font-size: 12px;
|
||||
color: var(--gray-700);
|
||||
cursor: pointer;
|
||||
transition: all 0.15s;
|
||||
}
|
||||
|
||||
.chip:hover { border-color: var(--primary); color: var(--primary-dark); background: var(--primary-tint); }
|
||||
|
||||
.modal-actions {
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
gap: 10px;
|
||||
margin-top: 6px;
|
||||
}
|
||||
|
||||
/* ============ Toast ============ */
|
||||
.toast {
|
||||
position: fixed;
|
||||
top: 22px;
|
||||
left: 50%;
|
||||
transform: translate(-50%, -8px);
|
||||
background: var(--secondary);
|
||||
color: #fff;
|
||||
font-size: 13px;
|
||||
padding: 10px 18px;
|
||||
border-radius: 8px;
|
||||
box-shadow: var(--shadow-lg);
|
||||
opacity: 0;
|
||||
pointer-events: none;
|
||||
transition: opacity 0.25s, transform 0.25s;
|
||||
z-index: 100;
|
||||
max-width: 60vw;
|
||||
}
|
||||
|
||||
.toast.show { opacity: 1; transform: translate(-50%, 0); }
|
||||
.toast.ok { background: var(--primary-dark); }
|
||||
.toast.warn { background: #92400e; }
|
||||
@@ -0,0 +1,210 @@
|
||||
/**
|
||||
* 操作审计 · 模拟数据生成器(种子随机,确定性输出)
|
||||
*
|
||||
* 记录字段与产品设计 4.1 表格一一对应;resourceType/resourceName 通过
|
||||
* AuditCore.resolveResource 从路径解析得出(模拟真实捕获管线)。
|
||||
* 内置若干"故事线"数据:登录爆破、越权 403、服务端 500 等,便于验证筛选场景。
|
||||
*/
|
||||
(function (root, factory) {
|
||||
if (typeof module === 'object' && module.exports) {
|
||||
module.exports = factory(require('./audit-core.js'));
|
||||
} else {
|
||||
root.MockData = factory(root.AuditCore);
|
||||
}
|
||||
})(typeof self !== 'undefined' ? self : this, function (AuditCore) {
|
||||
'use strict';
|
||||
|
||||
var DAY = 86400000;
|
||||
var MIN = 60000;
|
||||
var BASE_COUNT = 128; // 随机生成的基础记录条数
|
||||
|
||||
// ---------- 种子随机 ----------
|
||||
function mulberry32(seed) {
|
||||
var a = seed >>> 0;
|
||||
return function () {
|
||||
a |= 0;
|
||||
a = (a + 0x6D2B79F5) | 0;
|
||||
var t = Math.imul(a ^ (a >>> 15), 1 | a);
|
||||
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
|
||||
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
||||
};
|
||||
}
|
||||
function rint(r, min, max) {
|
||||
return Math.floor(r() * (max - min + 1)) + min;
|
||||
}
|
||||
function pick(r, arr) {
|
||||
return arr[rint(r, 0, arr.length - 1)];
|
||||
}
|
||||
function hex(r, n) {
|
||||
var s = '';
|
||||
var chars = '0123456789abcdef';
|
||||
for (var i = 0; i < n; i++) s += chars[rint(r, 0, 15)];
|
||||
return s;
|
||||
}
|
||||
|
||||
// ---------- 基础素材 ----------
|
||||
var UAS = {
|
||||
chrome: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.2151.97 Safari/537.36',
|
||||
edge: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.2151.97 Safari/537.36 Edg/119.0.2151.97',
|
||||
firefox: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:120.0) Gecko/20100101 Firefox/120.0',
|
||||
safari: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.1 Safari/605.1.15',
|
||||
bot: 'python-requests/2.31.0'
|
||||
};
|
||||
|
||||
var USERS = {
|
||||
'guichumo': { ips: ['192.168.1.23', '116.24.65.187'], uas: ['chrome', 'edge'] },
|
||||
'editor.zhang': { ips: ['116.24.65.201'], uas: ['edge', 'firefox'] },
|
||||
'writer.li': { ips: ['120.79.10.45'], uas: ['safari', 'chrome'] }
|
||||
};
|
||||
|
||||
var SLUGS = ['hello-halo', 'self-host-guide', 'weekly-2026-w46', 'reading-notes-11', 'plugin-dev-diary', 'spring-festival-2026'];
|
||||
var PLUGINS = ['plugin-search-widget', 'plugin-comment-widget', 'plugin-sitemap', 'plugin-links'];
|
||||
|
||||
function postPath(r) { return '/apis/content.halo.run/v1alpha1/posts/' + pick(r, SLUGS); }
|
||||
function publishPath(r) { return '/apis/api.content.halo.run/v1alpha1/posts/' + pick(r, SLUGS) + '/publish'; }
|
||||
function commentPath(r) { return '/apis/content.halo.run/v1alpha1/comments/comment-' + hex(r, 6); }
|
||||
function approvePath(r) { return '/apis/api.halo.run/v1alpha1/comments/comment-' + hex(r, 6) + '/approve'; }
|
||||
function replyPath(r) { return '/apis/api.halo.run/v1alpha1/comments/comment-' + hex(r, 6) + '/reply'; }
|
||||
function attachPath(r) { return '/apis/storage.halo.run/v1alpha1/attachments/attachment-' + hex(r, 6); }
|
||||
|
||||
// [权重, 操作者, 方法, 路径函数, 状态码]
|
||||
var ACTIONS = [
|
||||
[16, 'guichumo', 'PUT', postPath, 200],
|
||||
[10, 'guichumo', 'POST', publishPath, 200],
|
||||
[6, 'guichumo', 'POST', function () { return '/apis/content.halo.run/v1alpha1/posts'; }, 201],
|
||||
[5, 'guichumo', 'PUT', function () { return '/apis/v1alpha1/configmaps/system'; }, 200],
|
||||
[4, 'guichumo', 'PUT', function (r) { return '/apis/plugin.halo.run/v1alpha1/plugins/' + pick(r, PLUGINS); }, 200],
|
||||
[4, 'guichumo', 'POST', function () { return '/apis/storage.halo.run/v1alpha1/attachments'; }, 201],
|
||||
[4, 'guichumo', 'DELETE', attachPath, 204],
|
||||
[3, 'guichumo', 'DELETE', commentPath, 204],
|
||||
[3, 'guichumo', 'PUT', function () { return '/apis/v1alpha1/roles/role-template-editor'; }, 200],
|
||||
[2, 'guichumo', 'POST', function () { return '/login'; }, 200],
|
||||
[2, 'guichumo', 'POST', function () { return '/logout'; }, 200],
|
||||
[12, 'editor.zhang', 'PUT', postPath, 200],
|
||||
[6, 'editor.zhang', 'POST', function () { return '/apis/content.halo.run/v1alpha1/posts'; }, 201],
|
||||
[2, 'editor.zhang', 'POST', function () { return '/login'; }, 200],
|
||||
[2, 'editor.zhang', 'POST', function () { return '/apis/storage.halo.run/v1alpha1/attachments'; }, 201],
|
||||
[8, 'writer.li', 'PUT', postPath, 200],
|
||||
[6, 'writer.li', 'POST', approvePath, 200],
|
||||
[3, 'writer.li', 'POST', replyPath, 200],
|
||||
[2, 'writer.li', 'POST', function () { return '/login'; }, 200]
|
||||
];
|
||||
|
||||
var TOTAL_WEIGHT = ACTIONS.reduce(function (sum, a) { return sum + a[0]; }, 0);
|
||||
|
||||
function userCtx(r, name) {
|
||||
var u = USERS[name];
|
||||
return {
|
||||
username: name,
|
||||
remoteIp: pick(r, u.ips),
|
||||
userAgent: UAS[pick(r, u.uas)]
|
||||
};
|
||||
}
|
||||
|
||||
function rec(timestamp, username, method, path, statusCode, remoteIp, userAgent) {
|
||||
var res = AuditCore.resolveResource(path);
|
||||
return {
|
||||
id: null,
|
||||
timestamp: timestamp,
|
||||
username: username,
|
||||
method: AuditCore.normalizeMethod(method),
|
||||
path: path,
|
||||
resourceType: res.resourceType,
|
||||
resourceName: res.resourceName,
|
||||
statusCode: statusCode,
|
||||
remoteIp: remoteIp,
|
||||
userAgent: userAgent
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成模拟审计日志(按时间倒序,id 从旧到新递增)
|
||||
* @param {number} [now] 当前时间戳(默认 Date.now()),测试传固定值保证确定性
|
||||
* @returns {Array}
|
||||
*/
|
||||
function generateLogs(now) {
|
||||
if (typeof now !== 'number') now = Date.now();
|
||||
var r = mulberry32(20261121);
|
||||
var logs = [];
|
||||
|
||||
// 基础记录:均匀散布在近 90 天
|
||||
for (var i = 0; i < BASE_COUNT; i++) {
|
||||
var roll = r() * TOTAL_WEIGHT;
|
||||
var action = ACTIONS[0];
|
||||
for (var j = 0; j < ACTIONS.length; j++) {
|
||||
roll -= ACTIONS[j][0];
|
||||
if (roll <= 0) { action = ACTIONS[j]; break; }
|
||||
}
|
||||
var ctx = userCtx(r, action[1]);
|
||||
var time = now - rint(r, 5 * MIN, 90 * DAY);
|
||||
logs.push(rec(
|
||||
new Date(time).toISOString(), ctx.username,
|
||||
action[2], action[3](r), action[4],
|
||||
ctx.remoteIp, ctx.userAgent
|
||||
));
|
||||
}
|
||||
|
||||
// ---- 故事线(固定时间锚点)----
|
||||
|
||||
// 1. 攻击者爆破登录:20 天前,同一 IP 6 次失败,UA 为 python-requests
|
||||
var burstBase = now - 20 * DAY - 3600 * 1000 * 3; // 约 20 天前的凌晨
|
||||
[0, 41, 88, 135, 210, 400].forEach(function (off) {
|
||||
logs.push(rec(
|
||||
new Date(burstBase + off * 1000).toISOString(), '未知用户',
|
||||
'POST', '/login', 401, '203.0.113.66', UAS.bot
|
||||
));
|
||||
});
|
||||
|
||||
// 2. 站长忘记密码:12 天前输错两次后登录成功
|
||||
var b2 = now - 12 * DAY - 3600 * 1000 * 10;
|
||||
[[0, 401], [97, 401], [180, 200]].forEach(function (pair) {
|
||||
var ctx = { remoteIp: '192.168.1.23', userAgent: UAS.chrome };
|
||||
logs.push(rec(
|
||||
new Date(b2 + pair[0] * 1000).toISOString(),
|
||||
pair[1] === 200 ? 'guichumo' : '未知用户',
|
||||
'POST', '/login', pair[1], ctx.remoteIp, ctx.userAgent
|
||||
));
|
||||
});
|
||||
|
||||
// 3. 越权尝试:8 天前 editor.zhang 尝试修改系统设置被拒绝
|
||||
logs.push(rec(
|
||||
new Date(now - 8 * DAY - 3600 * 1000 * 14).toISOString(), 'editor.zhang',
|
||||
'PUT', '/apis/v1alpha1/configmaps/system', 403, '116.24.65.201', UAS.edge
|
||||
));
|
||||
|
||||
// 4. 权限不足:5 天前 writer.li 尝试删除评论被拒绝
|
||||
logs.push(rec(
|
||||
new Date(now - 5 * DAY - 3600 * 1000 * 9).toISOString(), 'writer.li',
|
||||
'DELETE', '/apis/content.halo.run/v1alpha1/comments/comment-a3f802', 403, '120.79.10.45', UAS.safari
|
||||
));
|
||||
|
||||
// 5. 资源不存在:3 天前删除一个已不存在的附件
|
||||
logs.push(rec(
|
||||
new Date(now - 3 * DAY - 3600 * 1000 * 20).toISOString(), 'guichumo',
|
||||
'DELETE', '/apis/storage.halo.run/v1alpha1/attachments/attachment-dead42', 404, '192.168.1.23', UAS.chrome
|
||||
));
|
||||
|
||||
// 6. 服务端异常:2 天前发布文章返回 500
|
||||
logs.push(rec(
|
||||
new Date(now - 2 * DAY - 3600 * 1000 * 6).toISOString(), 'writer.li',
|
||||
'POST', '/apis/api.content.halo.run/v1alpha1/posts/self-host-guide/publish', 500, '120.79.10.45', UAS.chrome
|
||||
));
|
||||
|
||||
// 7. 误操作定位:1 天前删除了一篇文章
|
||||
logs.push(rec(
|
||||
new Date(now - DAY - 3600 * 1000 * 2).toISOString(), 'guichumo',
|
||||
'DELETE', '/apis/content.halo.run/v1alpha1/posts/weekly-2026-w45', 204, '192.168.1.23', UAS.chrome
|
||||
));
|
||||
|
||||
// 排序 + 分配 id(最新记录 id 数字最大)
|
||||
logs.sort(function (a, b) {
|
||||
return new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime();
|
||||
});
|
||||
logs.forEach(function (l, idx) {
|
||||
l.id = 'audit-' + String(logs.length - idx).padStart(4, '0');
|
||||
});
|
||||
return logs;
|
||||
}
|
||||
|
||||
return { generateLogs: generateLogs };
|
||||
});
|
||||
Reference in New Issue
Block a user