feat(permissions,clients,ai,system,server,web,client,shared)!: 权限组 + 客户端设备远控与自动更新 + 大模型助手 + 系统诊断(1.3.0)
功能级准入改为「权限键」,`requireRole` 退成默认模板。 权限组 - 权限目录(39 键:键 / 中文名 / 分类 / 默认角色)收口到 shared,路由挂 requirePermission / requireAnyPermission - 有效权限每请求实时算,改完分组立刻生效:ADMIN 全集且 permission:manage、system:manage 不可剥夺; CLASS_DEVICE 空集(不受分组影响);有分组 = 并集(因此也能用来**收回**权限);无分组 = 角色默认模板 - 新增「权限组」页:组增删改、成员分配、生效权限预览 - 班级维度的科目规则(assertCanManageSubjectContent)故意不可被权限组覆盖 客户端设备(ClassHelper 桌面端) - 设备身份是 deviceKey(首次启动生成的 UUID,saveConfig 拒绝改写、退出登录也保留) - 心跳走「DB 记状态 + 客户端定时拉」,不引入新的长连接:30 秒一次,在线窗口 90 秒 - 远控命令 shutdown / restart / logoff / lock:服务端有效期 10 分钟,客户端弹默认选中「取消」的对话框、 60 秒未确认按取消处理,执行后必须回执(重复回执幂等),同一台机器同一条命令只弹一次 - 新增「客户端设备」页:版本一览、一键推送更新、下发远控 客户端安装包由服务端下载并缓存(教室机常常没有外网,浏览器也受 CSP 限制) - POST /api/update/client/fetch 由服务端下载到 data/client-releases/<版本>/ 并登记 sha256 + size - 自动安装三条护栏:没有 sha256 就拒收、必须等到非上课且主窗口隐藏满 5 分钟、冒烟模式永不安装 大模型助手:只解析、不落库 - POST /api/ai/interpret 返回**待确认**的结构化动作(建作业 / 发通知 / 叫人), 真正落库仍调既有端点 —— 权限断言、上课时段紧急通知保护、ClassIsland 推送全部自动复用 - 模型给的 params 用与手动入口**同一份** zod 再校验(模型输出不可信) - API Key 走 AES-256-GCM(密钥由 JWT_SECRET 经 scrypt 派生)加密存 AppSetting,只进不出 - provider=mock 是离线确定性解析,自动化验收一律用它;每次解析写一条 AiAudit(不含密钥) 系统与运维 - 开发者模式(服务端内存缓存开关,热路径不查库;开启后 500 才附 error.stack)、运行诊断(绝不回传密钥) - 开机自启动运行期可改:Windows 改 HKCU\...\Run、Linux systemctl enable/disable, **非安装形态一律返回 supported:false**,不往开发机的注册表里写自启项 - 服务端侧栏折叠只用 CSS 收拢(不重建 DOM),关怀模式只放大字号与控件、不动任何几何令牌 客户端 - 崩溃自愈:渲染进程挂掉 / 超 10 秒无响应先静默重建主窗口(10 分钟最多 3 次), 未捕获异常只在「没在上课、没人看着」时才重启进程;冒烟模式绝不重启(会杀掉验证实例) - 上线补弹离线期间收到的消息只做一次:起点只在「可达 → 不可达」的边界写,恢复时取最近 5 条按时间升序推 发布内容批量 - POST /notifications/bulk、POST /homeworks/bulk:**单班失败不影响整批**,逐班返回结果 - 作业的科目名跨班**按名匹配**已有课程,找不到就整班记失败,绝不自动建课 (自动建课会把创建者写成任课老师,等于越权改任教关系) - 通知与作业同口径按「所属日期」归档,新增 /notifications/days 与 /notifications/creators BREAKING CHANGE: 能不能做某件事不再看角色名,而是看权限键(GET /api/permissions/me)。 未分组的账号按角色默认模板生效,行为与 1.2.0 一致;分组之后生效集合只由组的并集决定。 服务端首次启动会自动应用 1 个数据库迁移(权限组 3 表 / AppSetting / 客户端设备·命令·安装包 / AiAudit / Notification.assignDate / User.careMode,历史通知按本地日期回填),升级前请先备份。
This commit is contained in:
1 parent
e51879667a
commit
817af02357
131 files changed
+12995
-538
No files matched your search
@@ -1,3 +1,4 @@
|
||||
import crypto from 'node:crypto';
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { app, safeStorage } from 'electron';
|
||||
@@ -44,6 +45,20 @@ interface PersistedConfig {
|
||||
sidebarCollapsed?: boolean;
|
||||
/** 用户选择「忽略此版本」的版本号(向后兼容:旧配置没有该字段视为没忽略过任何版本) */
|
||||
ignoredUpdateVersion?: string;
|
||||
/** 本机设备标识(向后兼容:旧配置没有该字段时首次读取生成并落盘,之后永不变) */
|
||||
deviceKey?: string;
|
||||
/** 是否开机自启(向后兼容:旧配置没有该字段视为关闭 —— 由用户在设置页自己打开) */
|
||||
autoLaunch?: boolean;
|
||||
/** 是否接受服务端统一下发的自动更新(向后兼容:旧配置没有该字段视为开启) */
|
||||
autoUpdate?: boolean;
|
||||
/** 开发者模式:日志调到 debug 级 + 设置里多出「开发者」页(向后兼容:旧配置没有该字段视为关闭) */
|
||||
developerMode?: boolean;
|
||||
/** 关怀模式:整体放大正文与控件字号,供视力不佳的用户使用(向后兼容:旧配置没有该字段视为关闭) */
|
||||
careMode?: boolean;
|
||||
/** 已下载待安装的更新版本(空串 = 无;跨重启保留) */
|
||||
pendingUpdateVersion?: string;
|
||||
/** 已下载待安装的安装包绝对路径(空串 = 无;跨重启保留) */
|
||||
pendingUpdateFile?: string;
|
||||
}
|
||||
|
||||
const DEFAULT_CONFIG: PersistedConfig = {
|
||||
@@ -59,6 +74,13 @@ const DEFAULT_CONFIG: PersistedConfig = {
|
||||
theme: 'light',
|
||||
sidebarCollapsed: false,
|
||||
ignoredUpdateVersion: '',
|
||||
deviceKey: '',
|
||||
autoLaunch: false,
|
||||
autoUpdate: true,
|
||||
developerMode: false,
|
||||
careMode: false,
|
||||
pendingUpdateVersion: '',
|
||||
pendingUpdateFile: '',
|
||||
};
|
||||
|
||||
/** 主题白名单:非法值一律回落浅色 */
|
||||
@@ -203,6 +225,15 @@ function readPersistedFromDisk(): PersistedConfig {
|
||||
sidebarCollapsed: parsed.sidebarCollapsed === true,
|
||||
ignoredUpdateVersion:
|
||||
typeof parsed.ignoredUpdateVersion === 'string' ? parsed.ignoredUpdateVersion : '',
|
||||
deviceKey: typeof parsed.deviceKey === 'string' ? parsed.deviceKey : '',
|
||||
autoLaunch: parsed.autoLaunch === true,
|
||||
// 默认开启:老配置文件里没有这个字段时按"接受服务端统一下发的更新"处理
|
||||
autoUpdate: parsed.autoUpdate !== false,
|
||||
developerMode: parsed.developerMode === true,
|
||||
careMode: parsed.careMode === true,
|
||||
pendingUpdateVersion:
|
||||
typeof parsed.pendingUpdateVersion === 'string' ? parsed.pendingUpdateVersion : '',
|
||||
pendingUpdateFile: typeof parsed.pendingUpdateFile === 'string' ? parsed.pendingUpdateFile : '',
|
||||
};
|
||||
} catch {
|
||||
return { ...DEFAULT_CONFIG };
|
||||
@@ -356,9 +387,52 @@ function toDesktopConfig(state: ConfigCache): DesktopStoredConfig {
|
||||
theme: normalizeTheme(persisted.theme),
|
||||
sidebarCollapsed: persisted.sidebarCollapsed === true,
|
||||
ignoredUpdateVersion: persisted.ignoredUpdateVersion ?? '',
|
||||
deviceKey: ensureDeviceKey(persisted),
|
||||
autoLaunch: persisted.autoLaunch === true,
|
||||
autoUpdate: persisted.autoUpdate !== false,
|
||||
developerMode: persisted.developerMode === true,
|
||||
careMode: persisted.careMode === true,
|
||||
pendingUpdateVersion: persisted.pendingUpdateVersion ?? '',
|
||||
pendingUpdateFile: persisted.pendingUpdateFile ?? '',
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* `crypto.randomUUID()`:Node 18+ / Chromium 里都有,但**不能假设它一定存在**
|
||||
* (打包后的运行时或某些受限环境里 `crypto.randomUUID` 为 undefined,直接调用会抛)。
|
||||
* 兜底用时间戳 + 随机字节拼一个同形状的 UUID v4。
|
||||
*/
|
||||
function generateDeviceKey(): string {
|
||||
try {
|
||||
if (typeof crypto.randomUUID === 'function') return crypto.randomUUID();
|
||||
} catch {
|
||||
// 落到下面的兜底
|
||||
}
|
||||
const hex = crypto.randomBytes(16).toString('hex');
|
||||
return (
|
||||
`${hex.slice(0, 8)}-${hex.slice(8, 12)}-4${hex.slice(12, 15)}` +
|
||||
`-a${hex.slice(15, 18)}-${hex.slice(18, 30)}`
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设备标识:**首次读取时生成并落盘,之后永不变**(它是这台机器在服务端的身份,
|
||||
* 服务端按 `deviceKey` upsert 设备记录,换了它就等于换了一台机器)。
|
||||
*
|
||||
* 为什么放在这里生成:`getConfig()` 是所有读取路径的唯一出口(主进程各处 + 渲染进程 IPC),
|
||||
* 且此时必然已经过了 `app.setPath('userData', …)`(见 `loadCache()` 的懒初始化说明),
|
||||
* 写的就是当前 profile 的 config.json。落盘走既有的 120ms 合并窗口,退出时 `flushConfigSync()` 兜底。
|
||||
*/
|
||||
function ensureDeviceKey(persisted: PersistedConfig): string {
|
||||
const existing = typeof persisted.deviceKey === 'string' ? persisted.deviceKey.trim() : '';
|
||||
if (existing) return existing;
|
||||
const created = generateDeviceKey();
|
||||
persisted.deviceKey = created;
|
||||
scheduleWrite(persisted);
|
||||
logger.info(`已为本机生成设备标识(服务端据此识别这台电脑):${created}`);
|
||||
return created;
|
||||
}
|
||||
|
||||
/** 读取配置(token 已解密),供渲染进程使用 */
|
||||
export function getConfig(): DesktopStoredConfig {
|
||||
return toDesktopConfig(loadCache());
|
||||
@@ -407,6 +481,30 @@ export function saveConfig(patch: Partial<DesktopStoredConfig>): DesktopStoredCo
|
||||
if (typeof patch.ignoredUpdateVersion === 'string') {
|
||||
persisted.ignoredUpdateVersion = patch.ignoredUpdateVersion;
|
||||
}
|
||||
if (patch.autoLaunch !== undefined) {
|
||||
persisted.autoLaunch = patch.autoLaunch === true;
|
||||
}
|
||||
if (patch.autoUpdate !== undefined) {
|
||||
persisted.autoUpdate = patch.autoUpdate === true;
|
||||
}
|
||||
if (patch.developerMode !== undefined) {
|
||||
persisted.developerMode = patch.developerMode === true;
|
||||
}
|
||||
if (patch.careMode !== undefined) {
|
||||
persisted.careMode = patch.careMode === true;
|
||||
}
|
||||
// 待安装的更新只由主进程的 auto-update 模块写入;渲染进程传进来也照收(退出登录/重置走 clearConfig)
|
||||
if (typeof patch.pendingUpdateVersion === 'string') {
|
||||
persisted.pendingUpdateVersion = patch.pendingUpdateVersion;
|
||||
}
|
||||
if (typeof patch.pendingUpdateFile === 'string') {
|
||||
persisted.pendingUpdateFile = patch.pendingUpdateFile;
|
||||
}
|
||||
// deviceKey **不接受外部改写**:它是这台机器在服务端的身份,改了服务端就会当成一台新机器
|
||||
// (旧记录永远停在上一次心跳、新记录从零开始)。要重置身份只能手工删掉 config.json 里的该字段。
|
||||
if (typeof patch.deviceKey === 'string' && patch.deviceKey && patch.deviceKey !== persisted.deviceKey) {
|
||||
logger.warn('忽略对设备标识(deviceKey)的改写:它是这台机器在服务端的身份,不允许变更');
|
||||
}
|
||||
if (patch.token !== undefined) {
|
||||
const encrypted = encryptToken(patch.token);
|
||||
persisted.token = encrypted.token;
|
||||
@@ -419,9 +517,27 @@ export function saveConfig(patch: Partial<DesktopStoredConfig>): DesktopStoredCo
|
||||
return toDesktopConfig(state);
|
||||
}
|
||||
|
||||
/** 清空为默认配置(退出登录 / 重置)。立即落盘:它对应退出登录,不该被合并窗口延迟 */
|
||||
/**
|
||||
* 清空为默认配置(退出登录 / 重置)。立即落盘:它对应退出登录,不该被合并窗口延迟。
|
||||
*
|
||||
* **保留"这台机器"的身份与本地开关**:`deviceKey`(服务端按它识别设备,换了就等于换一台机器)、
|
||||
* 开机自启 / 自动更新 / 开发者模式 / 关怀模式,以及已下载待安装的更新记录 —— 退出登录不该在服务端
|
||||
* 多出一台陌生设备,也不该把用户刚打开的开关悄悄关回默认值。
|
||||
*/
|
||||
export function clearConfig(): DesktopStoredConfig {
|
||||
const state: ConfigCache = { config: { ...DEFAULT_CONFIG }, token: null };
|
||||
const current = cache?.config ?? DEFAULT_CONFIG;
|
||||
const kept: PersistedConfig = {
|
||||
...DEFAULT_CONFIG,
|
||||
deviceKey: current.deviceKey ?? '',
|
||||
autoLaunch: current.autoLaunch === true,
|
||||
autoUpdate: current.autoUpdate !== false,
|
||||
developerMode: current.developerMode === true,
|
||||
careMode: current.careMode === true,
|
||||
pendingUpdateVersion: current.pendingUpdateVersion ?? '',
|
||||
pendingUpdateFile: current.pendingUpdateFile ?? '',
|
||||
};
|
||||
ensureDeviceKey(kept);
|
||||
const state: ConfigCache = { config: kept, token: null };
|
||||
cache = state;
|
||||
scheduleWrite(state.config, true);
|
||||
return toDesktopConfig(state);
|
||||
|
||||
Reference in new issue
Block a user