Phase 7 Task 3: 数据安全与合规
- 创建 security_manager.py 安全模块
- SecurityManager: 安全管理主类
- 审计日志系统 - 记录所有数据操作
- 端到端加密 - AES-256-GCM 加密项目数据
- 数据脱敏 - 支持手机号、邮箱、身份证等敏感信息脱敏
- 数据访问策略 - 基于用户、角色、IP、时间的访问控制
- 访问审批流程 - 敏感数据访问需要审批
- 更新 schema.sql 添加安全相关数据库表
- audit_logs: 审计日志表
- encryption_configs: 加密配置表
- masking_rules: 脱敏规则表
- data_access_policies: 数据访问策略表
- access_requests: 访问请求表
- 更新 main.py 添加安全相关 API 端点
- GET /api/v1/audit-logs - 查询审计日志
- GET /api/v1/audit-logs/stats - 审计统计
- POST /api/v1/projects/{id}/encryption/enable - 启用加密
- POST /api/v1/projects/{id}/encryption/disable - 禁用加密
- POST /api/v1/projects/{id}/encryption/verify - 验证密码
- GET /api/v1/projects/{id}/encryption - 获取加密配置
- POST /api/v1/projects/{id}/masking-rules - 创建脱敏规则
- GET /api/v1/projects/{id}/masking-rules - 获取脱敏规则
- PUT /api/v1/masking-rules/{id} - 更新脱敏规则
- DELETE /api/v1/masking-rules/{id} - 删除脱敏规则
- POST /api/v1/projects/{id}/masking/apply - 应用脱敏
- POST /api/v1/projects/{id}/access-policies - 创建访问策略
- GET /api/v1/projects/{id}/access-policies - 获取访问策略
- POST /api/v1/access-policies/{id}/check - 检查访问权限
- POST /api/v1/access-requests - 创建访问请求
- POST /api/v1/access-requests/{id}/approve - 批准访问
- POST /api/v1/access-requests/{id}/reject - 拒绝访问
- 更新 requirements.txt 添加 cryptography 依赖
- 更新 STATUS.md 和 README.md 记录完成状态
This commit is contained in:
@@ -1,195 +1,154 @@
|
||||
// InsightFlow Chrome Extension - Popup Script
|
||||
|
||||
document.addEventListener('DOMContentLoaded', async () => {
|
||||
const clipBtn = document.getElementById('clipBtn');
|
||||
const settingsBtn = document.getElementById('settingsBtn');
|
||||
const projectSelect = document.getElementById('projectSelect');
|
||||
const statusDot = document.getElementById('statusDot');
|
||||
const statusText = document.getElementById('statusText');
|
||||
const messageEl = document.getElementById('message');
|
||||
const openDashboard = document.getElementById('openDashboard');
|
||||
|
||||
// 加载配置和项目列表
|
||||
await loadConfig();
|
||||
|
||||
// 保存当前页面按钮
|
||||
clipBtn.addEventListener('click', async () => {
|
||||
// 获取当前标签页信息
|
||||
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
|
||||
|
||||
// 更新按钮状态
|
||||
clipBtn.disabled = true;
|
||||
clipBtn.innerHTML = '<span class="loading"></span> 保存中...';
|
||||
// 更新页面信息
|
||||
document.getElementById('pageTitle').textContent = tab.title || '未知标题';
|
||||
document.getElementById('pageUrl').textContent = tab.url || '';
|
||||
|
||||
// 保存选中的项目
|
||||
const projectId = projectSelect.value;
|
||||
if (projectId) {
|
||||
const config = await getConfig();
|
||||
config.defaultProjectId = projectId;
|
||||
await saveConfig(config);
|
||||
}
|
||||
// 获取页面统计
|
||||
updateStats();
|
||||
|
||||
// 发送剪藏请求
|
||||
chrome.runtime.sendMessage({
|
||||
action: 'clipPage'
|
||||
}, (response) => {
|
||||
clipBtn.disabled = false;
|
||||
clipBtn.innerHTML = `
|
||||
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
|
||||
<path d="M12 5v14M5 12h14"/>
|
||||
</svg>
|
||||
保存当前页面
|
||||
`;
|
||||
|
||||
if (response && response.success) {
|
||||
showMessage('保存成功!', 'success');
|
||||
updateStats();
|
||||
} else {
|
||||
showMessage(response?.error || '保存失败', 'error');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// 设置按钮
|
||||
settingsBtn.addEventListener('click', () => {
|
||||
chrome.runtime.openOptionsPage();
|
||||
});
|
||||
|
||||
// 打开控制台
|
||||
openDashboard.addEventListener('click', async (e) => {
|
||||
e.preventDefault();
|
||||
const config = await getConfig();
|
||||
chrome.tabs.create({ url: config.serverUrl });
|
||||
});
|
||||
// 加载最近的剪辑
|
||||
loadRecentClips();
|
||||
|
||||
// 绑定按钮事件
|
||||
document.getElementById('clipPageBtn').addEventListener('click', clipPage);
|
||||
document.getElementById('clipSelectionBtn').addEventListener('click', clipSelection);
|
||||
document.getElementById('openOptions').addEventListener('click', openOptions);
|
||||
});
|
||||
|
||||
// 加载配置
|
||||
async function loadConfig() {
|
||||
const config = await getConfig();
|
||||
|
||||
// 检查连接状态
|
||||
checkConnection(config);
|
||||
|
||||
// 加载项目列表
|
||||
loadProjects(config);
|
||||
|
||||
// 更新统计
|
||||
updateStats();
|
||||
}
|
||||
|
||||
// 检查连接状态
|
||||
async function checkConnection(config) {
|
||||
const statusDot = document.getElementById('statusDot');
|
||||
const statusText = document.getElementById('statusText');
|
||||
|
||||
if (!config.apiKey) {
|
||||
statusDot.classList.add('error');
|
||||
statusText.textContent = '未配置 API Key';
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(`${config.serverUrl}/api/v1/projects`, {
|
||||
headers: { 'X-API-Key': config.apiKey }
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
statusText.textContent = '已连接';
|
||||
} else {
|
||||
statusDot.classList.add('error');
|
||||
statusText.textContent = '连接失败';
|
||||
}
|
||||
} catch (error) {
|
||||
statusDot.classList.add('error');
|
||||
statusText.textContent = '连接错误';
|
||||
}
|
||||
}
|
||||
|
||||
// 加载项目列表
|
||||
async function loadProjects(config) {
|
||||
const projectSelect = document.getElementById('projectSelect');
|
||||
|
||||
if (!config.apiKey) {
|
||||
projectSelect.innerHTML = '<option>请先配置 API Key</option>';
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(`${config.serverUrl}/api/v1/projects`, {
|
||||
headers: { 'X-API-Key': config.apiKey }
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
const data = await response.json();
|
||||
const projects = data.projects || [];
|
||||
|
||||
// 更新项目数统计
|
||||
document.getElementById('projectCount').textContent = projects.length;
|
||||
|
||||
// 填充下拉框
|
||||
let html = '<option value="">选择保存项目...</option>';
|
||||
projects.forEach(project => {
|
||||
const selected = project.id === config.defaultProjectId ? 'selected' : '';
|
||||
html += `<option value="${project.id}" ${selected}>${escapeHtml(project.name)}</option>`;
|
||||
});
|
||||
projectSelect.innerHTML = html;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to load projects:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// 更新统计
|
||||
// 更新统计信息
|
||||
async function updateStats() {
|
||||
// 从存储中获取统计数据
|
||||
const result = await chrome.storage.local.get(['clipStats']);
|
||||
const stats = result.clipStats || { total: 0, today: 0, lastDate: null };
|
||||
|
||||
// 检查是否需要重置今日计数
|
||||
const today = new Date().toDateString();
|
||||
if (stats.lastDate !== today) {
|
||||
stats.today = 0;
|
||||
stats.lastDate = today;
|
||||
await chrome.storage.local.set({ clipStats: stats });
|
||||
}
|
||||
|
||||
document.getElementById('clipCount').textContent = stats.total;
|
||||
document.getElementById('todayCount').textContent = stats.today;
|
||||
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
|
||||
|
||||
// 获取字数统计
|
||||
try {
|
||||
const response = await chrome.tabs.sendMessage(tab.id, { action: 'extractContent' });
|
||||
if (response.success) {
|
||||
document.getElementById('wordCount').textContent = response.data.wordCount || 0;
|
||||
}
|
||||
} catch (error) {
|
||||
console.log('Content script not available');
|
||||
}
|
||||
|
||||
// 获取待同步数量
|
||||
const result = await chrome.storage.local.get(['clips']);
|
||||
const clips = result.clips || [];
|
||||
const pendingCount = clips.filter(c => !c.synced).length;
|
||||
document.getElementById('pendingCount').textContent = pendingCount;
|
||||
}
|
||||
|
||||
// 显示消息
|
||||
function showMessage(text, type) {
|
||||
const messageEl = document.getElementById('message');
|
||||
messageEl.textContent = text;
|
||||
messageEl.className = `message ${type}`;
|
||||
|
||||
setTimeout(() => {
|
||||
messageEl.className = 'message';
|
||||
}, 3000);
|
||||
// 保存整个页面
|
||||
async function clipPage() {
|
||||
setLoading(true);
|
||||
|
||||
try {
|
||||
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
|
||||
|
||||
// 发送消息给 background script
|
||||
const response = await chrome.runtime.sendMessage({ action: 'clipPage' });
|
||||
|
||||
if (response.success) {
|
||||
showStatus('页面已保存!', 'success');
|
||||
loadRecentClips();
|
||||
updateStats();
|
||||
} else {
|
||||
showStatus(response.error || '保存失败', 'error');
|
||||
}
|
||||
} catch (error) {
|
||||
showStatus('保存失败: ' + error.message, 'error');
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
// 获取配置
|
||||
function getConfig() {
|
||||
return new Promise((resolve) => {
|
||||
chrome.storage.sync.get(['insightflowConfig'], (result) => {
|
||||
resolve(result.insightflowConfig || {
|
||||
serverUrl: 'http://122.51.127.111:18000',
|
||||
apiKey: '',
|
||||
defaultProjectId: ''
|
||||
});
|
||||
});
|
||||
});
|
||||
// 保存选中内容
|
||||
async function clipSelection() {
|
||||
setLoading(true);
|
||||
|
||||
try {
|
||||
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
|
||||
|
||||
const response = await chrome.runtime.sendMessage({ action: 'clipSelection' });
|
||||
|
||||
if (response.success) {
|
||||
showStatus('选中内容已保存!', 'success');
|
||||
loadRecentClips();
|
||||
updateStats();
|
||||
} else {
|
||||
showStatus(response.error || '保存失败', 'error');
|
||||
}
|
||||
} catch (error) {
|
||||
showStatus('保存失败: ' + error.message, 'error');
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
// 保存配置
|
||||
function saveConfig(config) {
|
||||
return new Promise((resolve) => {
|
||||
chrome.storage.sync.set({ insightflowConfig: config }, resolve);
|
||||
});
|
||||
// 加载最近的剪辑
|
||||
async function loadRecentClips() {
|
||||
const result = await chrome.storage.local.get(['clips']);
|
||||
const clips = result.clips || [];
|
||||
|
||||
const clipsList = document.getElementById('clipsList');
|
||||
clipsList.innerHTML = '';
|
||||
|
||||
// 只显示最近5条
|
||||
const recentClips = clips.slice(0, 5);
|
||||
|
||||
for (const clip of recentClips) {
|
||||
const clipEl = document.createElement('div');
|
||||
clipEl.className = 'clip-item';
|
||||
|
||||
const title = clip.title || '未命名';
|
||||
const time = new Date(clip.extractedAt).toLocaleString('zh-CN');
|
||||
const statusClass = clip.synced ? 'synced' : 'pending';
|
||||
const statusText = clip.synced ? '已同步' : '待同步';
|
||||
|
||||
clipEl.innerHTML = `
|
||||
<div class="clip-title">${escapeHtml(title)}</div>
|
||||
<div class="clip-time">${time}</div>
|
||||
<span class="clip-status ${statusClass}">${statusText}</span>
|
||||
`;
|
||||
|
||||
clipsList.appendChild(clipEl);
|
||||
}
|
||||
}
|
||||
|
||||
// 打开设置页面
|
||||
function openOptions(e) {
|
||||
e.preventDefault();
|
||||
chrome.runtime.openOptionsPage();
|
||||
}
|
||||
|
||||
// 显示状态消息
|
||||
function showStatus(message, type) {
|
||||
const statusEl = document.getElementById('status');
|
||||
statusEl.textContent = message;
|
||||
statusEl.className = 'status ' + type;
|
||||
|
||||
setTimeout(() => {
|
||||
statusEl.textContent = '';
|
||||
statusEl.className = 'status';
|
||||
}, 3000);
|
||||
}
|
||||
|
||||
// 设置加载状态
|
||||
function setLoading(loading) {
|
||||
const loadingEl = document.getElementById('loading');
|
||||
if (loading) {
|
||||
loadingEl.classList.add('active');
|
||||
} else {
|
||||
loadingEl.classList.remove('active');
|
||||
}
|
||||
}
|
||||
|
||||
// HTML 转义
|
||||
function escapeHtml(text) {
|
||||
const div = document.createElement('div');
|
||||
div.textContent = text;
|
||||
return div.innerHTML;
|
||||
const div = document.createElement('div');
|
||||
div.textContent = text;
|
||||
return div.innerHTML;
|
||||
}
|
||||
Reference in New Issue
Block a user