新版用户迁移将站长转换为普通账号

This commit is contained in:
mtvpls
2025-12-24 20:13:09 +08:00
parent 187e6f1bd4
commit 558ba174fe
2 changed files with 19 additions and 11 deletions

View File

@@ -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(),

View File

@@ -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