mirror of
https://github.com/beilunyang/moemail.git
synced 2026-06-28 02:42:56 +08:00
feat: Add role-based email limit exemption for emperors
This commit is contained in:
@@ -7,29 +7,35 @@ import { EXPIRY_OPTIONS } from "@/types/email"
|
||||
import { EMAIL_CONFIG } from "@/config"
|
||||
import { getRequestContext } from "@cloudflare/next-on-pages"
|
||||
import { getUserId } from "@/lib/apiKey"
|
||||
import { getUserRole } from "@/lib/auth"
|
||||
import { ROLES } from "@/lib/permissions"
|
||||
|
||||
export const runtime = "edge"
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const db = createDb()
|
||||
|
||||
const userId = await getUserId()
|
||||
const userRole = await getUserRole(userId!)
|
||||
|
||||
try {
|
||||
const activeEmailsCount = await db
|
||||
.select({ count: sql<number>`count(*)` })
|
||||
.from(emails)
|
||||
.where(
|
||||
and(
|
||||
eq(emails.userId, userId!),
|
||||
gt(emails.expiresAt, new Date())
|
||||
if (userRole !== ROLES.EMPEROR) {
|
||||
const activeEmailsCount = await db
|
||||
.select({ count: sql<number>`count(*)` })
|
||||
.from(emails)
|
||||
.where(
|
||||
and(
|
||||
eq(emails.userId, userId!),
|
||||
gt(emails.expiresAt, new Date())
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
if (Number(activeEmailsCount[0].count) >= EMAIL_CONFIG.MAX_ACTIVE_EMAILS) {
|
||||
return NextResponse.json(
|
||||
{ error: `已达到最大邮箱数量限制 (${EMAIL_CONFIG.MAX_ACTIVE_EMAILS})` },
|
||||
{ status: 403 }
|
||||
)
|
||||
|
||||
if (Number(activeEmailsCount[0].count) >= EMAIL_CONFIG.MAX_ACTIVE_EMAILS) {
|
||||
return NextResponse.json(
|
||||
{ error: `已达到最大邮箱数量限制 (${EMAIL_CONFIG.MAX_ACTIVE_EMAILS})` },
|
||||
{ status: 403 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
const { name, expiryTime, domain } = await request.json<{
|
||||
|
||||
@@ -19,6 +19,8 @@ import {
|
||||
AlertDialogHeader,
|
||||
AlertDialogTitle,
|
||||
} from "@/components/ui/alert-dialog"
|
||||
import { ROLES } from "@/lib/permissions"
|
||||
import { useUserRole } from "@/hooks/use-user-role"
|
||||
|
||||
interface Email {
|
||||
id: string
|
||||
@@ -40,6 +42,7 @@ interface EmailResponse {
|
||||
|
||||
export function EmailList({ onEmailSelect, selectedEmailId }: EmailListProps) {
|
||||
const { data: session } = useSession()
|
||||
const { role } = useUserRole()
|
||||
const [emails, setEmails] = useState<Email[]>([])
|
||||
const [loading, setLoading] = useState(true)
|
||||
const [refreshing, setRefreshing] = useState(false)
|
||||
@@ -167,7 +170,11 @@ export function EmailList({ onEmailSelect, selectedEmailId }: EmailListProps) {
|
||||
<RefreshCw className="h-4 w-4" />
|
||||
</Button>
|
||||
<span className="text-xs text-gray-500">
|
||||
{total}/{EMAIL_CONFIG.MAX_ACTIVE_EMAILS} 个邮箱
|
||||
{role === ROLES.EMPEROR ? (
|
||||
`${total}/∞ 个邮箱`
|
||||
) : (
|
||||
`${total}/${EMAIL_CONFIG.MAX_ACTIVE_EMAILS} 个邮箱`
|
||||
)}
|
||||
</span>
|
||||
</div>
|
||||
<CreateDialog onEmailCreated={handleRefresh} />
|
||||
|
||||
21
app/hooks/use-user-role.ts
Normal file
21
app/hooks/use-user-role.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
"use client"
|
||||
|
||||
import { useSession } from "next-auth/react"
|
||||
import { Role } from "@/lib/permissions"
|
||||
import { useEffect, useState } from "react"
|
||||
|
||||
export function useUserRole() {
|
||||
const { data: session } = useSession()
|
||||
const [role, setRole] = useState<Role | null>(null)
|
||||
|
||||
useEffect(() => {
|
||||
if (session?.user?.roles?.[0]?.name) {
|
||||
setRole(session.user.roles[0].name as Role)
|
||||
}
|
||||
}, [session])
|
||||
|
||||
return {
|
||||
role,
|
||||
loading: !session
|
||||
}
|
||||
}
|
||||
@@ -52,6 +52,15 @@ export async function assignRoleToUser(db: Db, userId: string, roleId: string) {
|
||||
})
|
||||
}
|
||||
|
||||
export async function getUserRole(userId: string) {
|
||||
const db = createDb()
|
||||
const userRoleRecords = await db.query.userRoles.findMany({
|
||||
where: eq(userRoles.userId, userId),
|
||||
with: { role: true },
|
||||
})
|
||||
return userRoleRecords[0].role.name
|
||||
}
|
||||
|
||||
export async function checkPermission(permission: Permission) {
|
||||
const session = await auth()
|
||||
if (!session?.user?.id) return false
|
||||
|
||||
Reference in New Issue
Block a user