forked from beilunyang/moemail
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add configuration management for default user roles and permiss…
…ions
- Loading branch information
1 parent
798def1
commit 6420cd7
Showing
10 changed files
with
145 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Role, ROLES } from "@/lib/permissions" | ||
import { getRequestContext } from "@cloudflare/next-on-pages" | ||
|
||
export const runtime = "edge" | ||
|
||
export async function GET() { | ||
const config = await getRequestContext().env.SITE_CONFIG.get("DEFAULT_ROLE") | ||
|
||
return Response.json({ defaultRole: config || ROLES.CIVILIAN }) | ||
} | ||
|
||
export async function POST(request: Request) { | ||
const { defaultRole } = await request.json() as { defaultRole: Exclude<Role, typeof ROLES.EMPEROR> } | ||
|
||
if (![ROLES.KNIGHT, ROLES.CIVILIAN].includes(defaultRole)) { | ||
return Response.json({ error: "无效的角色" }, { status: 400 }) | ||
} | ||
|
||
await getRequestContext().env.SITE_CONFIG.put("DEFAULT_ROLE", defaultRole) | ||
return Response.json({ success: true }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
"use client" | ||
|
||
import { Button } from "@/components/ui/button" | ||
import { Settings } from "lucide-react" | ||
import { useToast } from "@/components/ui/use-toast" | ||
import { useState, useEffect } from "react" | ||
import { Role, ROLES } from "@/lib/permissions" | ||
import { | ||
Select, | ||
SelectContent, | ||
SelectItem, | ||
SelectTrigger, | ||
SelectValue, | ||
} from "@/components/ui/select" | ||
|
||
export function ConfigPanel() { | ||
const [defaultRole, setDefaultRole] = useState<string>("") | ||
const [loading, setLoading] = useState(false) | ||
const { toast } = useToast() | ||
|
||
useEffect(() => { | ||
fetchConfig() | ||
}, []) | ||
|
||
const fetchConfig = async () => { | ||
const res = await fetch("/api/config") | ||
if (res.ok) { | ||
const data = await res.json() as { defaultRole: Exclude<Role, typeof ROLES.EMPEROR> } | ||
setDefaultRole(data.defaultRole) | ||
} | ||
} | ||
|
||
const handleSave = async () => { | ||
setLoading(true) | ||
try { | ||
const res = await fetch("/api/config", { | ||
method: "POST", | ||
headers: { "Content-Type": "application/json" }, | ||
body: JSON.stringify({ defaultRole }), | ||
}) | ||
|
||
if (!res.ok) throw new Error("保存失败") | ||
|
||
toast({ | ||
title: "保存成功", | ||
description: "默认角色设置已更新", | ||
}) | ||
} catch (error) { | ||
toast({ | ||
title: "保存失败", | ||
description: error instanceof Error ? error.message : "请稍后重试", | ||
variant: "destructive", | ||
}) | ||
} finally { | ||
setLoading(false) | ||
} | ||
} | ||
|
||
return ( | ||
<div className="bg-background rounded-lg border-2 border-primary/20 p-6"> | ||
<div className="flex items-center gap-2 mb-6"> | ||
<Settings className="w-5 h-5 text-primary" /> | ||
<h2 className="text-lg font-semibold">网站设置</h2> | ||
</div> | ||
|
||
<div className="space-y-4"> | ||
<div className="flex items-center gap-4"> | ||
<span className="text-sm">新用户默认角色:</span> | ||
<Select value={defaultRole} onValueChange={setDefaultRole}> | ||
<SelectTrigger className="w-32"> | ||
<SelectValue /> | ||
</SelectTrigger> | ||
<SelectContent> | ||
<SelectItem value={ROLES.KNIGHT}>骑士</SelectItem> | ||
<SelectItem value={ROLES.CIVILIAN}>平民</SelectItem> | ||
</SelectContent> | ||
</Select> | ||
<Button | ||
onClick={handleSave} | ||
disabled={loading} | ||
> | ||
保存 | ||
</Button> | ||
</div> | ||
</div> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters