Files
wrong-notebook/scripts/seed-admin.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

59 lines
1.8 KiB
JavaScript

/**
* Docker 启动、安装包构建时调用:只维护已有的默认管理员 admin@localhost(恢复管理员角色并启用)。
* 全新安装不再创建固定密码的管理员,改由服务启动时在日志中打印一次性初始化链接创建(见 src/lib/initialization.ts)。
*/
const { PrismaClient } = require('@prisma/client');
const DEFAULT_ADMIN = {
email: 'admin@localhost',
role: 'admin',
isActive: true,
educationStage: 'junior_high',
enrollmentYear: 2025,
};
async function seedAdmin({ prisma }) {
const existingUser = await prisma.user.findUnique({
where: { email: DEFAULT_ADMIN.email },
});
if (!existingUser) {
return { action: 'skipped', email: DEFAULT_ADMIN.email };
}
await prisma.user.update({
where: { email: DEFAULT_ADMIN.email },
data: {
role: DEFAULT_ADMIN.role,
isActive: DEFAULT_ADMIN.isActive,
educationStage: existingUser.educationStage ?? DEFAULT_ADMIN.educationStage,
enrollmentYear: existingUser.enrollmentYear ?? DEFAULT_ADMIN.enrollmentYear,
},
});
return { action: 'updated', email: DEFAULT_ADMIN.email };
}
async function main() {
const prisma = new PrismaClient();
try {
const result = await seedAdmin({ prisma });
if (result.action === 'updated') {
console.log('Existing admin user found. Restored admin role.');
} else {
console.log('No default admin user. Create the admin with the one-time setup link printed when the server starts.');
}
} finally {
await prisma.$disconnect();
}
}
if (require.main === module) {
main().catch((error) => {
console.error(error);
process.exit(1);
});
}
module.exports = { seedAdmin, DEFAULT_ADMIN };