feat: developer access permission gates webhooks/API keys/BYOK (v0.71.0)
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>
This commit is contained in:
@@ -3,8 +3,9 @@ import { headers } from "next/headers";
|
||||
import Link from "next/link";
|
||||
import { ExternalLink } from "lucide-react";
|
||||
import { auth } from "@/lib/auth/server";
|
||||
import { db, apiKeys, eq } from "@epicure/db";
|
||||
import { db, apiKeys, users, eq } from "@epicure/db";
|
||||
import { ApiKeysManager } from "@/components/settings/api-keys-manager";
|
||||
import { DeveloperLockedNotice } from "@/components/settings/developer-locked-notice";
|
||||
import { getMessages } from "@/lib/i18n/server";
|
||||
|
||||
export const metadata: Metadata = {};
|
||||
@@ -14,16 +15,20 @@ export default async function ApiKeysPage() {
|
||||
if (!session) return null;
|
||||
const m = getMessages((session.user as { locale?: string }).locale);
|
||||
|
||||
const keys = await db
|
||||
.select({
|
||||
id: apiKeys.id,
|
||||
name: apiKeys.name,
|
||||
scope: apiKeys.scope,
|
||||
lastUsedAt: apiKeys.lastUsedAt,
|
||||
createdAt: apiKeys.createdAt,
|
||||
})
|
||||
.from(apiKeys)
|
||||
.where(eq(apiKeys.userId, session.user.id));
|
||||
const dbUser = (await db.select({ isDeveloper: users.isDeveloper }).from(users).where(eq(users.id, session.user.id)).limit(1))[0];
|
||||
|
||||
const keys = dbUser?.isDeveloper
|
||||
? await db
|
||||
.select({
|
||||
id: apiKeys.id,
|
||||
name: apiKeys.name,
|
||||
scope: apiKeys.scope,
|
||||
lastUsedAt: apiKeys.lastUsedAt,
|
||||
createdAt: apiKeys.createdAt,
|
||||
})
|
||||
.from(apiKeys)
|
||||
.where(eq(apiKeys.userId, session.user.id))
|
||||
: [];
|
||||
|
||||
return (
|
||||
<div className="space-y-8">
|
||||
@@ -42,15 +47,18 @@ export default async function ApiKeysPage() {
|
||||
{m.settings.apiKeysPage.docsLink}
|
||||
</Link>
|
||||
</div>
|
||||
<ApiKeysManager
|
||||
initialKeys={keys.map((k) => ({
|
||||
id: k.id,
|
||||
name: k.name,
|
||||
scope: k.scope,
|
||||
lastUsedAt: k.lastUsedAt ? k.lastUsedAt.toISOString() : null,
|
||||
createdAt: k.createdAt.toISOString(),
|
||||
}))}
|
||||
/>
|
||||
{!dbUser?.isDeveloper && <DeveloperLockedNotice message={m.settings.developerLockedNotice} />}
|
||||
{dbUser?.isDeveloper && (
|
||||
<ApiKeysManager
|
||||
initialKeys={keys.map((k) => ({
|
||||
id: k.id,
|
||||
name: k.name,
|
||||
scope: k.scope,
|
||||
lastUsedAt: k.lastUsedAt ? k.lastUsedAt.toISOString() : null,
|
||||
createdAt: k.createdAt.toISOString(),
|
||||
}))}
|
||||
/>
|
||||
)}
|
||||
</section>
|
||||
</div>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user