mirror of
https://github.com/beilunyang/moemail.git
synced 2026-09-04 23:06:37 +08:00
feat: /api/config endpoint can be called using APIKey
This commit is contained in:
+10
-1
@@ -1,6 +1,7 @@
|
|||||||
import { Role, ROLES } from "@/lib/permissions"
|
import { PERMISSIONS, Role, ROLES } from "@/lib/permissions"
|
||||||
import { getRequestContext } from "@cloudflare/next-on-pages"
|
import { getRequestContext } from "@cloudflare/next-on-pages"
|
||||||
import { EMAIL_CONFIG } from "@/config"
|
import { EMAIL_CONFIG } from "@/config"
|
||||||
|
import { checkPermission } from "@/lib/auth"
|
||||||
|
|
||||||
export const runtime = "edge"
|
export const runtime = "edge"
|
||||||
|
|
||||||
@@ -22,6 +23,14 @@ export async function GET() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function POST(request: Request) {
|
export async function POST(request: Request) {
|
||||||
|
const canAccess = await checkPermission(PERMISSIONS.MANAGE_CONFIG)
|
||||||
|
|
||||||
|
if (!canAccess) {
|
||||||
|
return Response.json({
|
||||||
|
error: "权限不足"
|
||||||
|
}, { status: 403 })
|
||||||
|
}
|
||||||
|
|
||||||
const { defaultRole, emailDomains, adminContact, maxEmails } = await request.json() as {
|
const { defaultRole, emailDomains, adminContact, maxEmails } = await request.json() as {
|
||||||
defaultRole: Exclude<Role, typeof ROLES.EMPEROR>,
|
defaultRole: Exclude<Role, typeof ROLES.EMPEROR>,
|
||||||
emailDomains: string,
|
emailDomains: string,
|
||||||
|
|||||||
+1
-1
@@ -25,7 +25,7 @@ async function getUserByApiKey(key: string): Promise<User | null> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function handleApiKeyAuth(apiKey: string, pathname: string) {
|
export async function handleApiKeyAuth(apiKey: string, pathname: string) {
|
||||||
if (!pathname.startsWith('/api/emails')) {
|
if (!pathname.startsWith('/api/emails') && !pathname.startsWith('/api/config')) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ error: "无权限查看" },
|
{ error: "无权限查看" },
|
||||||
{ status: 403 }
|
{ status: 403 }
|
||||||
|
|||||||
+5
-3
@@ -10,6 +10,7 @@ import CredentialsProvider from "next-auth/providers/credentials"
|
|||||||
import { hashPassword, comparePassword } from "@/lib/utils"
|
import { hashPassword, comparePassword } from "@/lib/utils"
|
||||||
import { authSchema } from "@/lib/validation"
|
import { authSchema } from "@/lib/validation"
|
||||||
import { generateAvatarUrl } from "./avatar"
|
import { generateAvatarUrl } from "./avatar"
|
||||||
|
import { getUserId } from "./apiKey"
|
||||||
|
|
||||||
const ROLE_DESCRIPTIONS: Record<Role, string> = {
|
const ROLE_DESCRIPTIONS: Record<Role, string> = {
|
||||||
[ROLES.EMPEROR]: "皇帝(网站所有者)",
|
[ROLES.EMPEROR]: "皇帝(网站所有者)",
|
||||||
@@ -62,12 +63,13 @@ export async function getUserRole(userId: string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function checkPermission(permission: Permission) {
|
export async function checkPermission(permission: Permission) {
|
||||||
const session = await auth()
|
const userId = await getUserId()
|
||||||
if (!session?.user?.id) return false
|
|
||||||
|
if (!userId) return false
|
||||||
|
|
||||||
const db = createDb()
|
const db = createDb()
|
||||||
const userRoleRecords = await db.query.userRoles.findMany({
|
const userRoleRecords = await db.query.userRoles.findMany({
|
||||||
where: eq(userRoles.userId, session.user.id),
|
where: eq(userRoles.userId, userId),
|
||||||
with: { role: true },
|
with: { role: true },
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user