|
1 | | -/** |
2 | | - * Accounts API Route |
3 | | - * Returns account list (names + roles, no passwords) for admin visibility |
4 | | - */ |
5 | | - |
6 | | -import { NextResponse } from 'next/server'; |
| 1 | +import { NextRequest, NextResponse } from 'next/server'; |
| 2 | +import { |
| 3 | + createManagedAccount, |
| 4 | + getPublicAuthConfig, |
| 5 | + getServerSession, |
| 6 | + isSuperAdminSession, |
| 7 | + listAccountInfo, |
| 8 | +} from '@/lib/server/auth'; |
7 | 9 |
|
8 | 10 | export const runtime = 'edge'; |
9 | 11 |
|
10 | | -const ADMIN_PASSWORD = process.env.ADMIN_PASSWORD || ''; |
11 | | -const ACCESS_PASSWORD = process.env.ACCESS_PASSWORD || ''; |
12 | | -const ACCOUNTS = process.env.ACCOUNTS || ''; |
13 | | - |
14 | | -const effectiveAdminPassword = ADMIN_PASSWORD || ACCESS_PASSWORD; |
15 | | - |
16 | | -interface AccountInfo { |
17 | | - name: string; |
18 | | - role: 'super_admin' | 'admin' | 'viewer'; |
19 | | - customPermissions?: string[]; |
20 | | -} |
21 | | - |
22 | | -function getAccountList(): AccountInfo[] { |
23 | | - const accounts: AccountInfo[] = []; |
24 | | - |
25 | | - // Add admin from ADMIN_PASSWORD |
26 | | - if (effectiveAdminPassword) { |
27 | | - accounts.push({ name: '超级管理员', role: 'super_admin' }); |
| 12 | +async function requireSuperAdmin(request: NextRequest) { |
| 13 | + const session = await getServerSession(request); |
| 14 | + if (!session) { |
| 15 | + return { error: NextResponse.json({ error: 'Authentication required' }, { status: 401 }) }; |
28 | 16 | } |
29 | 17 |
|
30 | | - // Add accounts from ACCOUNTS env var |
31 | | - if (ACCOUNTS) { |
32 | | - ACCOUNTS.split(',') |
33 | | - .map(entry => entry.trim()) |
34 | | - .filter(entry => entry.length > 0) |
35 | | - .forEach(entry => { |
36 | | - const parts = entry.split(':'); |
37 | | - if (parts.length >= 2) { |
38 | | - const name = parts[1].trim(); |
39 | | - const parsedRole = parts[2]?.trim(); |
40 | | - const role = parsedRole === 'super_admin' ? 'super_admin' : parsedRole === 'admin' ? 'admin' : 'viewer'; |
41 | | - const perms = parts[3]?.trim(); |
42 | | - const customPermissions = perms |
43 | | - ? perms.split('|').map(p => p.trim()).filter(p => p.length > 0) |
44 | | - : undefined; |
45 | | - if (name) { |
46 | | - accounts.push({ name, role, ...(customPermissions && customPermissions.length > 0 ? { customPermissions } : {}) }); |
47 | | - } |
48 | | - } |
49 | | - }); |
| 18 | + if (!isSuperAdminSession(session)) { |
| 19 | + return { error: NextResponse.json({ error: 'Super admin required' }, { status: 403 }) }; |
50 | 20 | } |
51 | 21 |
|
52 | | - return accounts; |
| 22 | + return { session }; |
53 | 23 | } |
54 | 24 |
|
55 | | -export async function GET() { |
56 | | - const accounts = getAccountList(); |
| 25 | +export async function GET(request: NextRequest) { |
| 26 | + const auth = await requireSuperAdmin(request); |
| 27 | + if ('error' in auth) { |
| 28 | + return auth.error; |
| 29 | + } |
| 30 | + |
| 31 | + const config = await getPublicAuthConfig(); |
| 32 | + const accounts = await listAccountInfo(); |
57 | 33 |
|
58 | 34 | return NextResponse.json({ |
| 35 | + loginMode: config.loginMode, |
| 36 | + managed: config.loginMode === 'managed', |
59 | 37 | accounts, |
60 | | - hasAdminPassword: !!effectiveAdminPassword, |
61 | | - hasAccounts: !!ACCOUNTS, |
62 | 38 | totalCount: accounts.length, |
63 | 39 | }); |
64 | 40 | } |
| 41 | + |
| 42 | +export async function POST(request: NextRequest) { |
| 43 | + const auth = await requireSuperAdmin(request); |
| 44 | + if ('error' in auth) { |
| 45 | + return auth.error; |
| 46 | + } |
| 47 | + |
| 48 | + const config = await getPublicAuthConfig(); |
| 49 | + if (config.loginMode !== 'managed') { |
| 50 | + return NextResponse.json({ error: 'Managed account mode is not enabled' }, { status: 400 }); |
| 51 | + } |
| 52 | + |
| 53 | + try { |
| 54 | + const body = await request.json(); |
| 55 | + const account = await createManagedAccount(body); |
| 56 | + return NextResponse.json({ account }, { status: 201 }); |
| 57 | + } catch (error) { |
| 58 | + return NextResponse.json( |
| 59 | + { error: error instanceof Error ? error.message : 'Failed to create account' }, |
| 60 | + { status: 400 } |
| 61 | + ); |
| 62 | + } |
| 63 | +} |
0 commit comments