Files
wrong-notebook/scripts/prepare-e2e.js
margrop bd0befa85b feat: 全新安装改用一次性初始化链接创建管理员,不再预置固定密码的默认管理员
- 服务启动时若还没有管理员,生成一次性令牌保存到配置目录的 setup-token,并在日志中打印 /initialize?token= 链接;已有管理员时删除残留令牌
- 新增 /initialize 页面与 POST /api/initialize:凭令牌设置管理员名称、邮箱和密码(不能用 123456),成功后自动登录进入安装向导;令牌改名占用,并发提交只会创建一个管理员
- 登录页在尚未创建管理员时说明各安装方式到哪里找初始化链接,注册状态接口增加 initialized
- prisma/seed.ts、scripts/seed-admin.js 不再创建 admin@localhost,只为已有的默认管理员恢复角色;E2E 准备脚本改为创建测试管理员
- 已有实例升级保留原账号,旧默认管理员仍走强制改密流程
- 同步中英文 README、Wiki、安装包说明与 npm、飞牛OS 的首次使用提示

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 03:45:48 +08:00

65 lines
2.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* E2E 测试前的数据准备(在 prisma db seed 之后运行):
* - 全新数据库不再预置管理员,创建(或重置)admin@localhost,密码为 E2E_ADMIN_PASSWORD,供各用例登录;
* - 另建一个仍使用默认密码的管理员 setup-admin@localhost,专门测试强制改密流程;
* - 在 app-config.json 中标记安装向导已完成,避免全新数据库让各用例被重定向到向导。
* 每次运行都会重置这两个账号的密码,可重复执行。
*/
const fs = require('fs');
const path = require('path');
const { PrismaClient } = require('@prisma/client');
const { hash } = require('bcryptjs');
const E2E_ADMIN_PASSWORD = process.env.E2E_ADMIN_PASSWORD || 'e2e-admin-pass';
const DEFAULT_ADMIN_PASSWORD = '123456';
const SETUP_ADMIN_EMAIL = 'setup-admin@localhost';
// 与 src/lib/config.ts 一致
const CONFIG_FILE_PATH = path.join(process.env.CONFIG_DIR || path.join(process.cwd(), 'config'), 'app-config.json');
// 直接读取并处理不存在的情况,避免先判断再读写之间文件被替换
function readConfig() {
try {
return JSON.parse(fs.readFileSync(CONFIG_FILE_PATH, 'utf8'));
} catch (error) {
if (error.code === 'ENOENT') return {};
throw error;
}
}
function markSetupCompleted() {
const config = readConfig();
if (config.setupCompletedAt) return;
fs.mkdirSync(path.dirname(CONFIG_FILE_PATH), { recursive: true });
fs.writeFileSync(CONFIG_FILE_PATH, JSON.stringify({ ...config, setupCompletedAt: new Date().toISOString() }, null, 2));
}
async function main() {
const prisma = new PrismaClient();
try {
const adminHash = await hash(E2E_ADMIN_PASSWORD, 10);
await prisma.user.upsert({
where: { email: 'admin@localhost' },
update: { password: adminHash, role: 'admin', isActive: true },
create: { email: 'admin@localhost', name: 'Admin', password: adminHash, role: 'admin', isActive: true, educationStage: 'junior_high', enrollmentYear: 2025 },
});
const defaultHash = await hash(DEFAULT_ADMIN_PASSWORD, 10);
await prisma.user.upsert({
where: { email: SETUP_ADMIN_EMAIL },
update: { password: defaultHash, role: 'admin', isActive: true },
create: { email: SETUP_ADMIN_EMAIL, name: 'Setup Admin', password: defaultHash, role: 'admin', isActive: true },
});
markSetupCompleted();
console.log(`[prepare-e2e] admin@localhost 密码已设为 E2E_ADMIN_PASSWORD,${SETUP_ADMIN_EMAIL} 使用默认密码,安装向导已标记完成`);
} finally {
await prisma.$disconnect();
}
}
main().catch((error) => {
console.error(error);
process.exit(1);
});