用户数据结构变更

This commit is contained in:
mtvpls
2025-12-24 00:24:50 +08:00
parent 1a0ec53452
commit d63192ef7a
20 changed files with 1680 additions and 245 deletions

View File

@@ -148,15 +148,41 @@ export async function GET(request: NextRequest) {
}
// 检查用户是否已存在(通过OIDC sub查找)
const existingUser = config.UserConfig.Users.find((u: any) => u.oidcSub === oidcSub);
// 优先使用新版本查找
let username = await db.getUserByOidcSub(oidcSub);
let userRole: 'owner' | 'admin' | 'user' = 'user';
if (existingUser) {
if (username) {
// 从新版本获取用户信息
const userInfoV2 = await db.getUserInfoV2(username);
if (userInfoV2) {
userRole = userInfoV2.role;
// 检查用户是否被封禁
if (userInfoV2.banned) {
return NextResponse.redirect(
new URL('/login?error=' + encodeURIComponent('用户被封禁'), origin)
);
}
}
} else {
// 回退到配置中查找
const existingUser = config.UserConfig.Users.find((u: any) => u.oidcSub === oidcSub);
if (existingUser) {
username = existingUser.username;
userRole = existingUser.role || 'user';
// 检查用户是否被封禁
if (existingUser.banned) {
return NextResponse.redirect(
new URL('/login?error=' + encodeURIComponent('用户被封禁'), origin)
);
}
}
}
if (username) {
// 用户已存在,直接登录
const response = NextResponse.redirect(new URL('/', origin));
const cookieValue = await generateAuthCookie(
existingUser.username,
existingUser.role || 'user'
);
const cookieValue = await generateAuthCookie(username, userRole);
const expires = new Date();
expires.setDate(expires.getDate() + 7);

View File

@@ -110,7 +110,20 @@ export async function POST(request: NextRequest) {
);
}
// 检查用户名是否已存在
// 检查用户名是否已存在(优先使用新版本)
let userExists = await db.checkUserExistV2(username);
if (!userExists) {
// 回退到旧版本检查
userExists = await db.checkUserExist(username);
}
if (userExists) {
return NextResponse.json(
{ error: '用户名已存在' },
{ status: 409 }
);
}
// 检查配置中是否已存在
const existingUser = config.UserConfig.Users.find((u) => u.username === username);
if (existingUser) {
return NextResponse.json(
@@ -119,9 +132,16 @@ export async function POST(request: NextRequest) {
);
}
// 检查OIDC sub是否已被使用
const existingOIDCUser = config.UserConfig.Users.find((u: any) => u.oidcSub === oidcSession.sub);
if (existingOIDCUser) {
// 检查OIDC sub是否已被使用(优先使用新版本)
let existingOIDCUsername = await db.getUserByOidcSub(oidcSession.sub);
if (!existingOIDCUsername) {
// 回退到配置中查找
const existingOIDCUser = config.UserConfig.Users.find((u: any) => u.oidcSub === oidcSession.sub);
if (existingOIDCUser) {
existingOIDCUsername = existingOIDCUser.username;
}
}
if (existingOIDCUsername) {
return NextResponse.json(
{ error: '该OIDC账号已被注册' },
{ status: 409 }
@@ -132,9 +152,19 @@ export async function POST(request: NextRequest) {
try {
// 生成随机密码(OIDC用户不需要密码登录)
const randomPassword = crypto.randomUUID();
// 获取默认用户组
const defaultTags = siteConfig.DefaultUserTags && siteConfig.DefaultUserTags.length > 0
? siteConfig.DefaultUserTags
: undefined;
// 使用新版本创建用户带SHA256加密和OIDC绑定
await db.createUserV2(username, randomPassword, 'user', defaultTags, oidcSession.sub);
// 同时在旧版本存储中创建(保持兼容性)
await db.registerUser(username, randomPassword);
// 将用户添加到配置中
// 将用户添加到配置中(保持兼容性)
const newUser: any = {
username: username,
role: 'user',
@@ -143,8 +173,8 @@ export async function POST(request: NextRequest) {
};
// 如果配置了默认用户组,分配给新用户
if (siteConfig.DefaultUserTags && siteConfig.DefaultUserTags.length > 0) {
newUser.tags = siteConfig.DefaultUserTags;
if (defaultTags) {
newUser.tags = defaultTags;
}
config.UserConfig.Users.push(newUser);