彻底重构-1
This commit is contained in:
@@ -18,7 +18,7 @@ export async function ensureAdminUsersBootstrapped() {
|
||||
const configuredUsers = Array.isArray(runtimeConfig.admin?.defaultUsers) ? runtimeConfig.admin.defaultUsers : []
|
||||
|
||||
for (const configuredUser of configuredUsers) {
|
||||
const username = String(configuredUser?.username || '').trim()
|
||||
const username = String(configuredUser?.username || '').trim().toLowerCase()
|
||||
const password = String(configuredUser?.password || '').trim()
|
||||
const role = normalizeAdminRole(configuredUser?.role)
|
||||
|
||||
@@ -26,12 +26,12 @@ export async function ensureAdminUsersBootstrapped() {
|
||||
continue
|
||||
}
|
||||
|
||||
if (getAdminUserByUsername(username)) {
|
||||
if (await getAdminUserByUsername(username)) {
|
||||
continue
|
||||
}
|
||||
|
||||
const now = nowIso()
|
||||
createAdminUser({
|
||||
await createAdminUser({
|
||||
username,
|
||||
passwordHash: hashAdminPassword(password),
|
||||
role,
|
||||
@@ -42,10 +42,10 @@ export async function ensureAdminUsersBootstrapped() {
|
||||
}
|
||||
}
|
||||
|
||||
export function loginAdmin(username, password) {
|
||||
export async function loginAdmin(username, password) {
|
||||
ensureAdminAuthConfigured()
|
||||
|
||||
const normalizedUsername = String(username || '').trim()
|
||||
const normalizedUsername = String(username || '').trim().toLowerCase()
|
||||
const normalizedPassword = String(password || '').trim()
|
||||
|
||||
if (!normalizedUsername || !normalizedPassword) {
|
||||
@@ -55,7 +55,7 @@ export function loginAdmin(username, password) {
|
||||
})
|
||||
}
|
||||
|
||||
const user = getAdminUserByUsername(normalizedUsername)
|
||||
const user = await getAdminUserByUsername(normalizedUsername)
|
||||
if (!user || user.status !== 'active' || !verifyAdminPassword(normalizedPassword, user.password_hash)) {
|
||||
throw createHttpError('账号或密码错误', {
|
||||
statusCode: 401,
|
||||
@@ -66,7 +66,7 @@ export function loginAdmin(username, password) {
|
||||
return createAdminSession(user)
|
||||
}
|
||||
|
||||
export function verifyAdminSessionToken(token) {
|
||||
export async function verifyAdminSessionToken(token) {
|
||||
ensureAdminAuthConfigured()
|
||||
|
||||
const normalizedToken = String(token || '').trim()
|
||||
@@ -113,7 +113,7 @@ export function verifyAdminSessionToken(token) {
|
||||
}
|
||||
|
||||
const userId = Number(payload?.uid || 0)
|
||||
const user = getAdminUserById(userId)
|
||||
const user = await getAdminUserById(userId)
|
||||
if (!user || user.status !== 'active') {
|
||||
throw createHttpError('后台账号已不可用,请重新登录', {
|
||||
statusCode: 401,
|
||||
@@ -122,16 +122,16 @@ export function verifyAdminSessionToken(token) {
|
||||
}
|
||||
|
||||
return {
|
||||
sessionId: String(payload.sid || '').trim(),
|
||||
userId: user.id,
|
||||
username: user.username,
|
||||
sessionId: String(payload?.sid || '').trim(),
|
||||
userId: Number(user.id),
|
||||
username: String(user.username || ''),
|
||||
role: normalizeAdminRole(user.role),
|
||||
expiresAt,
|
||||
}
|
||||
}
|
||||
|
||||
export function getAdminSessionSummary(token) {
|
||||
const session = verifyAdminSessionToken(token)
|
||||
export async function getAdminSessionSummary(token) {
|
||||
const session = await verifyAdminSessionToken(token)
|
||||
|
||||
return {
|
||||
authenticated: true,
|
||||
@@ -155,10 +155,10 @@ export function requireAdminRole(session, allowedRoles) {
|
||||
})
|
||||
}
|
||||
|
||||
export function getAdminUserList(query = {}) {
|
||||
export async function getAdminUserList(query = {}) {
|
||||
const page = normalizePage(query.page)
|
||||
const pageSize = normalizePageSize(query.pageSize)
|
||||
const { items, total } = listAdminUsers({
|
||||
const { items, total } = await listAdminUsers({
|
||||
page,
|
||||
pageSize,
|
||||
username: String(query.username || '').trim(),
|
||||
@@ -172,7 +172,7 @@ export function getAdminUserList(query = {}) {
|
||||
}
|
||||
}
|
||||
|
||||
export function createManagedAdminUser(payload = {}) {
|
||||
export async function createManagedAdminUser(payload = {}) {
|
||||
const username = normalizeUsername(payload.username)
|
||||
const password = normalizePassword(payload.password)
|
||||
|
||||
@@ -193,7 +193,7 @@ export function createManagedAdminUser(payload = {}) {
|
||||
validateUsername(username)
|
||||
validatePassword(password)
|
||||
|
||||
if (getAdminUserByUsername(username)) {
|
||||
if (await getAdminUserByUsername(username)) {
|
||||
throw createHttpError('后台账号已存在', {
|
||||
statusCode: 409,
|
||||
errorCode: 'admin_user_exists',
|
||||
@@ -201,7 +201,7 @@ export function createManagedAdminUser(payload = {}) {
|
||||
}
|
||||
|
||||
const now = nowIso()
|
||||
const created = createAdminUser({
|
||||
const created = await createAdminUser({
|
||||
username,
|
||||
passwordHash: hashAdminPassword(password),
|
||||
role: normalizeAdminRole(payload.role),
|
||||
@@ -215,8 +215,8 @@ export function createManagedAdminUser(payload = {}) {
|
||||
}
|
||||
}
|
||||
|
||||
export function updateManagedAdminUserRole(userId, payload = {}, session) {
|
||||
const user = getRequiredAdminUser(userId)
|
||||
export async function updateManagedAdminUserRole(userId, payload = {}, session) {
|
||||
const user = await getRequiredAdminUser(userId)
|
||||
const role = normalizeAdminRole(payload.role)
|
||||
|
||||
if (user.role === role) {
|
||||
@@ -225,8 +225,8 @@ export function updateManagedAdminUserRole(userId, payload = {}, session) {
|
||||
}
|
||||
}
|
||||
|
||||
ensureAdminUserChangeAllowed(user, { nextRole: role }, session)
|
||||
const updated = updateAdminUser(user.id, {
|
||||
await ensureAdminUserChangeAllowed(user, { nextRole: role }, session)
|
||||
const updated = await updateAdminUser(user.id, {
|
||||
role,
|
||||
updated_at: nowIso(),
|
||||
})
|
||||
@@ -236,8 +236,8 @@ export function updateManagedAdminUserRole(userId, payload = {}, session) {
|
||||
}
|
||||
}
|
||||
|
||||
export function updateManagedAdminUserStatus(userId, payload = {}, session) {
|
||||
const user = getRequiredAdminUser(userId)
|
||||
export async function updateManagedAdminUserStatus(userId, payload = {}, session) {
|
||||
const user = await getRequiredAdminUser(userId)
|
||||
const status = normalizeAdminUserStatus(payload.status)
|
||||
|
||||
if (user.status === status) {
|
||||
@@ -246,8 +246,8 @@ export function updateManagedAdminUserStatus(userId, payload = {}, session) {
|
||||
}
|
||||
}
|
||||
|
||||
ensureAdminUserChangeAllowed(user, { nextStatus: status }, session)
|
||||
const updated = updateAdminUser(user.id, {
|
||||
await ensureAdminUserChangeAllowed(user, { nextStatus: status }, session)
|
||||
const updated = await updateAdminUser(user.id, {
|
||||
status,
|
||||
updated_at: nowIso(),
|
||||
})
|
||||
@@ -257,8 +257,8 @@ export function updateManagedAdminUserStatus(userId, payload = {}, session) {
|
||||
}
|
||||
}
|
||||
|
||||
export function resetManagedAdminUserPassword(userId, payload = {}) {
|
||||
const user = getRequiredAdminUser(userId)
|
||||
export async function resetManagedAdminUserPassword(userId, payload = {}) {
|
||||
const user = await getRequiredAdminUser(userId)
|
||||
const password = normalizePassword(payload.password)
|
||||
|
||||
if (!password) {
|
||||
@@ -269,7 +269,7 @@ export function resetManagedAdminUserPassword(userId, payload = {}) {
|
||||
}
|
||||
|
||||
validatePassword(password)
|
||||
const updated = updateAdminUser(user.id, {
|
||||
const updated = await updateAdminUser(user.id, {
|
||||
password_hash: hashAdminPassword(password),
|
||||
updated_at: nowIso(),
|
||||
})
|
||||
@@ -298,8 +298,8 @@ function createAdminSession(user) {
|
||||
const expiresAt = addHours(issuedAt, Number(runtimeConfig.admin.sessionTtlHours || 12))
|
||||
const payload = {
|
||||
sid: crypto.randomBytes(12).toString('hex'),
|
||||
uid: user.id,
|
||||
usr: user.username,
|
||||
uid: Number(user.id),
|
||||
usr: String(user.username || ''),
|
||||
role: normalizeAdminRole(user.role),
|
||||
iat: issuedAt,
|
||||
exp: expiresAt,
|
||||
@@ -311,8 +311,8 @@ function createAdminSession(user) {
|
||||
token: `${encodedPayload}.${signature}`,
|
||||
expiresAt,
|
||||
user: {
|
||||
userId: user.id,
|
||||
username: user.username,
|
||||
userId: Number(user.id),
|
||||
username: String(user.username || ''),
|
||||
role: normalizeAdminRole(user.role),
|
||||
},
|
||||
}
|
||||
@@ -409,8 +409,8 @@ function validatePassword(password) {
|
||||
}
|
||||
}
|
||||
|
||||
function getRequiredAdminUser(userId) {
|
||||
const user = getAdminUserById(Number(userId))
|
||||
async function getRequiredAdminUser(userId) {
|
||||
const user = await getAdminUserById(Number(userId))
|
||||
|
||||
if (!user) {
|
||||
throw createHttpError('后台用户不存在', {
|
||||
@@ -422,18 +422,18 @@ function getRequiredAdminUser(userId) {
|
||||
return user
|
||||
}
|
||||
|
||||
function ensureAdminUserChangeAllowed(user, options = {}, session) {
|
||||
async function ensureAdminUserChangeAllowed(user, options = {}, session) {
|
||||
const nextRole = options.nextRole || user.role
|
||||
const nextStatus = options.nextStatus || user.status
|
||||
|
||||
if (session?.userId === user.id && (nextRole !== 'admin' || nextStatus !== 'active')) {
|
||||
if (session?.userId === Number(user.id) && (nextRole !== 'admin' || nextStatus !== 'active')) {
|
||||
throw createHttpError('不能停用或降级当前登录账号', {
|
||||
statusCode: 409,
|
||||
errorCode: 'admin_user_self_change_not_allowed',
|
||||
})
|
||||
}
|
||||
|
||||
if (user.role === 'admin' && (nextRole !== 'admin' || nextStatus !== 'active') && countActiveAdminUsers() <= 1) {
|
||||
if (user.role === 'admin' && (nextRole !== 'admin' || nextStatus !== 'active') && await countActiveAdminUsers() <= 1) {
|
||||
throw createHttpError('至少保留一个启用中的管理员账号', {
|
||||
statusCode: 409,
|
||||
errorCode: 'admin_user_last_admin_not_allowed',
|
||||
@@ -443,8 +443,8 @@ function ensureAdminUserChangeAllowed(user, options = {}, session) {
|
||||
|
||||
function mapAdminUser(user) {
|
||||
return {
|
||||
userId: user.id,
|
||||
username: user.username,
|
||||
userId: Number(user.id),
|
||||
username: String(user.username || ''),
|
||||
role: normalizeAdminRole(user.role),
|
||||
status: normalizeAdminUserStatus(user.status),
|
||||
createdAt: user.created_at,
|
||||
|
||||
Reference in New Issue
Block a user