eb99faf655
Webhooks, self-serve API keys, and BYOK AI provider keys had zero access gating -- any logged-in user, any tier. Adds users.isDeveloper (boolean, admin-toggled in admin/users/[id] alongside role/tier), checked via a single hasDeveloperAccess() (lib/permissions.ts) so a future subscription-tier auto-grant is a one-line change there, not a redesign across call sites. requireDeveloper() (lib/api-auth.ts) wraps requireSession() with a fresh isDeveloper check (same reasoning as requireAdmin re-querying role: session.user's cookieCache can be up to 5 minutes stale) and replaces requireSession in all 8 gated routes: webhooks CRUD + deliveries + redeliver, api-keys CRUD, ai-keys CRUD. Settings UI: the sidebar hides API Keys/Webhooks nav entries for non-developers; those pages and the BYOK section of Settings -> AI show a locked notice instead of the manager component when accessed directly. Migration grandfathers in anyone who already has a webhook, API key, or BYOK key row -- ships as a new gate on existing features, not a silent lockout of active integrations. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
61 lines
2.2 KiB
TypeScript
61 lines
2.2 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { requireAdmin } from "@/lib/api-auth";
|
|
import { db, users, auditLogs, eq } from "@epicure/db";
|
|
import { randomUUID } from "crypto";
|
|
|
|
interface RouteContext {
|
|
params: Promise<{ id: string }>;
|
|
}
|
|
|
|
export async function PATCH(req: NextRequest, { params }: RouteContext) {
|
|
const { session, response } = await requireAdmin();
|
|
if (response) return response;
|
|
|
|
const { id } = await params;
|
|
const body = await req.json() as { role?: string; tier?: string; isDeveloper?: boolean };
|
|
const { role, tier, isDeveloper } = body;
|
|
|
|
const validRoles = ["user", "moderator", "admin"] as const;
|
|
const validTiers = ["free", "pro", "family"] as const;
|
|
|
|
if (role !== undefined && !validRoles.includes(role as typeof validRoles[number])) {
|
|
return NextResponse.json({ error: "Invalid role" }, { status: 400 });
|
|
}
|
|
if (tier !== undefined && !validTiers.includes(tier as typeof validTiers[number])) {
|
|
return NextResponse.json({ error: "Invalid tier" }, { status: 400 });
|
|
}
|
|
if (isDeveloper !== undefined && typeof isDeveloper !== "boolean") {
|
|
return NextResponse.json({ error: "Invalid isDeveloper" }, { status: 400 });
|
|
}
|
|
|
|
const updateData: Partial<{ role: "user" | "moderator" | "admin"; tier: "free" | "pro" | "family"; isDeveloper: boolean; updatedAt: Date }> = {
|
|
updatedAt: new Date(),
|
|
};
|
|
if (role) updateData.role = role as "user" | "moderator" | "admin";
|
|
if (tier) updateData.tier = tier as "free" | "pro" | "family";
|
|
if (isDeveloper !== undefined) updateData.isDeveloper = isDeveloper;
|
|
|
|
const [updated] = await db
|
|
.update(users)
|
|
.set(updateData)
|
|
.where(eq(users.id, id))
|
|
.returning({ id: users.id, role: users.role, tier: users.tier, isDeveloper: users.isDeveloper });
|
|
|
|
if (!updated) {
|
|
return NextResponse.json({ error: "User not found" }, { status: 404 });
|
|
}
|
|
|
|
// Write audit log
|
|
await db.insert(auditLogs).values({
|
|
id: randomUUID(),
|
|
userId: session!.user.id,
|
|
action: "admin.user.update",
|
|
targetType: "user",
|
|
targetId: id,
|
|
metadata: JSON.stringify({ role, tier, isDeveloper }),
|
|
createdAt: new Date(),
|
|
});
|
|
|
|
return NextResponse.json({ user: updated });
|
|
}
|