From 558ba174fef21d3d50dbbbe25db99b9bf0d64835 Mon Sep 17 00:00:00 2001 From: mtvpls Date: Wed, 24 Dec 2025 20:13:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E7=89=88=E7=94=A8=E6=88=B7=E8=BF=81?= =?UTF-8?q?=E7=A7=BB=E5=B0=86=E7=AB=99=E9=95=BF=E8=BD=AC=E6=8D=A2=E4=B8=BA?= =?UTF-8?q?=E6=99=AE=E9=80=9A=E8=B4=A6=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/admin/data_migration/import/route.ts | 13 ++++++++++--- src/lib/db.ts | 17 +++++++++-------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/app/api/admin/data_migration/import/route.ts b/src/app/api/admin/data_migration/import/route.ts index 7b246ac..5d2651b 100644 --- a/src/app/api/admin/data_migration/import/route.ts +++ b/src/app/api/admin/data_migration/import/route.ts @@ -98,8 +98,9 @@ export async function POST(req: NextRequest) { if (importData.data.usersV2 && Array.isArray(importData.data.usersV2)) { for (const userV2 of importData.data.usersV2) { try { - // 跳过站长(站长使用环境变量认证) - if (userV2.role === 'owner') { + // 跳过环境变量中的站长(站长使用环境变量认证) + if (userV2.username === process.env.USERNAME) { + console.log(`跳过站长 ${userV2.username} 的导入`); continue; } @@ -108,6 +109,12 @@ export async function POST(req: NextRequest) { const passwordV2 = userData?.passwordV2; if (passwordV2) { + // 将站长角色转换为普通角色 + const importedRole = userV2.role === 'owner' ? 'user' : userV2.role; + if (userV2.role === 'owner') { + console.log(`用户 ${userV2.username} 的角色从 owner 转换为 user`); + } + // 直接使用加密后的密码创建用户 const storage = (db as any).storage; if (storage && typeof storage.client?.hset === 'function') { @@ -115,7 +122,7 @@ export async function POST(req: NextRequest) { const createdAt = userV2.created_at || Date.now(); const userInfo: any = { - role: userV2.role, + role: importedRole, banned: userV2.banned, password: passwordV2, created_at: createdAt.toString(), diff --git a/src/lib/db.ts b/src/lib/db.ts index 06c12ce..7bcfb2b 100644 --- a/src/lib/db.ts +++ b/src/lib/db.ts @@ -274,8 +274,8 @@ export class DbManager { for (const user of users) { try { - // 跳过站长(站长使用环境变量认证,不需要迁移) - if (user.role === 'owner') { + // 跳过环境变量中的站长(站长使用环境变量认证,不需要迁移) + if (user.username === process.env.USERNAME) { console.log(`跳过站长 ${user.username} 的迁移`); continue; } @@ -295,11 +295,6 @@ export class DbManager { password = crypto.randomUUID(); console.log(`用户 ${user.username} (OIDC用户) 使用随机密码迁移`); } - // 如果是站长,使用环境变量中的密码 - else if (user.username === process.env.USERNAME && process.env.PASSWORD) { - password = process.env.PASSWORD; - console.log(`用户 ${user.username} (站长) 使用环境变量密码迁移`); - } // 尝试从旧的存储中获取密码 else { try { @@ -322,11 +317,17 @@ export class DbManager { } } + // 将站长角色转换为普通角色 + const migratedRole = user.role === 'owner' ? 'user' : user.role; + if (user.role === 'owner') { + console.log(`用户 ${user.username} 的角色从 owner 转换为 user`); + } + // 创建新用户 await this.createUserV2( user.username, password, - user.role, + migratedRole, user.tags, (user as any).oidcSub, user.enabledApis