mirror of
https://github.com/beilunyang/moemail.git
synced 2026-09-05 23:36:37 +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 { EMAIL_CONFIG } from "@/config"
|
||||||
import { getRequestContext } from "@cloudflare/next-on-pages"
|
import { getRequestContext } from "@cloudflare/next-on-pages"
|
||||||
import { getUserId } from "@/lib/apiKey"
|
import { getUserId } from "@/lib/apiKey"
|
||||||
|
import { getUserRole } from "@/lib/auth"
|
||||||
|
import { ROLES } from "@/lib/permissions"
|
||||||
|
|
||||||
export const runtime = "edge"
|
export const runtime = "edge"
|
||||||
|
|
||||||
export async function POST(request: Request) {
|
export async function POST(request: Request) {
|
||||||
const db = createDb()
|
const db = createDb()
|
||||||
|
|
||||||
const userId = await getUserId()
|
const userId = await getUserId()
|
||||||
|
const userRole = await getUserRole(userId!)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const activeEmailsCount = await db
|
if (userRole !== ROLES.EMPEROR) {
|
||||||
.select({ count: sql<number>`count(*)` })
|
const activeEmailsCount = await db
|
||||||
.from(emails)
|
.select({ count: sql<number>`count(*)` })
|
||||||
.where(
|
.from(emails)
|
||||||
and(
|
.where(
|
||||||
eq(emails.userId, userId!),
|
and(
|
||||||
gt(emails.expiresAt, new Date())
|
eq(emails.userId, userId!),
|
||||||
|
gt(emails.expiresAt, new Date())
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
|
||||||
|
if (Number(activeEmailsCount[0].count) >= EMAIL_CONFIG.MAX_ACTIVE_EMAILS) {
|
||||||
if (Number(activeEmailsCount[0].count) >= EMAIL_CONFIG.MAX_ACTIVE_EMAILS) {
|
return NextResponse.json(
|
||||||
return NextResponse.json(
|
{ error: `已达到最大邮箱数量限制 (${EMAIL_CONFIG.MAX_ACTIVE_EMAILS})` },
|
||||||
{ error: `已达到最大邮箱数量限制 (${EMAIL_CONFIG.MAX_ACTIVE_EMAILS})` },
|
{ status: 403 }
|
||||||
{ status: 403 }
|
)
|
||||||
)
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const { name, expiryTime, domain } = await request.json<{
|
const { name, expiryTime, domain } = await request.json<{
|
||||||
|
|||||||
@@ -19,6 +19,8 @@ import {
|
|||||||
AlertDialogHeader,
|
AlertDialogHeader,
|
||||||
AlertDialogTitle,
|
AlertDialogTitle,
|
||||||
} from "@/components/ui/alert-dialog"
|
} from "@/components/ui/alert-dialog"
|
||||||
|
import { ROLES } from "@/lib/permissions"
|
||||||
|
import { useUserRole } from "@/hooks/use-user-role"
|
||||||
|
|
||||||
interface Email {
|
interface Email {
|
||||||
id: string
|
id: string
|
||||||
@@ -40,6 +42,7 @@ interface EmailResponse {
|
|||||||
|
|
||||||
export function EmailList({ onEmailSelect, selectedEmailId }: EmailListProps) {
|
export function EmailList({ onEmailSelect, selectedEmailId }: EmailListProps) {
|
||||||
const { data: session } = useSession()
|
const { data: session } = useSession()
|
||||||
|
const { role } = useUserRole()
|
||||||
const [emails, setEmails] = useState<Email[]>([])
|
const [emails, setEmails] = useState<Email[]>([])
|
||||||
const [loading, setLoading] = useState(true)
|
const [loading, setLoading] = useState(true)
|
||||||
const [refreshing, setRefreshing] = useState(false)
|
const [refreshing, setRefreshing] = useState(false)
|
||||||
@@ -167,7 +170,11 @@ export function EmailList({ onEmailSelect, selectedEmailId }: EmailListProps) {
|
|||||||
<RefreshCw className="h-4 w-4" />
|
<RefreshCw className="h-4 w-4" />
|
||||||
</Button>
|
</Button>
|
||||||
<span className="text-xs text-gray-500">
|
<span className="text-xs text-gray-500">
|
||||||
{total}/{EMAIL_CONFIG.MAX_ACTIVE_EMAILS} 个邮箱
|
{role === ROLES.EMPEROR ? (
|
||||||
|
`${total}/∞ 个邮箱`
|
||||||
|
) : (
|
||||||
|
`${total}/${EMAIL_CONFIG.MAX_ACTIVE_EMAILS} 个邮箱`
|
||||||
|
)}
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
<CreateDialog onEmailCreated={handleRefresh} />
|
<CreateDialog onEmailCreated={handleRefresh} />
|
||||||
|
|||||||
@@ -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) {
|
export async function checkPermission(permission: Permission) {
|
||||||
const session = await auth()
|
const session = await auth()
|
||||||
if (!session?.user?.id) return false
|
if (!session?.user?.id) return false
|
||||||
|
|||||||
Reference in New Issue
Block a user