Schema migrations: - users.is_admin boolean - users.password_hash text (scrypt N=16384, 16-byte salt) - users.last_login_at timestamp - organizations.suspended + suspended_reason - admin_settings table (DB-stored prompt override + future settings) Auth (@bmm/auth): - hashPassword + verifyPassword via node:crypto scrypt (no extra dep) - loginWithPassword: scrypt-verifies, issues 30-day session, updates last_login_at - seedAdmin: idempotent upsert keyed on email; creates org + membership on first run - AuthedUser now carries isAdmin flag API: - POST /v1/auth/admin/login (email + password) — 300ms throttle on failure - requireAdmin preHandler — 401 if no session, 403 if non-admin - Bootstrap: api on boot calls seedAdmin(ADMIN_EMAIL, ADMIN_PASSWORD, ADMIN_NAME) if env present. Idempotent. Admin API routes (all gated by requireAdmin): - GET /v1/admin/overview (totals, trends 7d, server-status breakdown, builds 24h, recent activity) - GET /v1/admin/users (search, per-row org + plan + serverCount) - PATCH /v1/admin/users/:id (isAdmin, name) - DELETE /v1/admin/users/:id (self-delete blocked) - GET /v1/admin/orgs (member + server counts) - PATCH /v1/admin/orgs/:id (plan, quota, suspended; cascades to mcp_servers.status=paused on suspend) - GET /v1/admin/servers (cross-org with status filter) - POST /v1/admin/servers/:id/rebuild (re-queues build using last prompt) - DELETE /v1/admin/servers/:id - GET /v1/admin/builds (status filter, error messages, prompt previews) - GET /v1/admin/builds/:id/logs - GET /v1/admin/audit (system-wide with user email join) - GET /v1/admin/system (DB ping, Redis ping, BullMQ queue depth, docker ps count) - GET /v1/admin/prompt (builtin + override + updatedAt) - PATCH /v1/admin/prompt (value: string | null) — saves DB override or drops it UI (apps/web/app/admin/*): - /admin/login — password form, separate from /login magic-link - AdminLayout — Linear-style sidebar (8 nav items), bottom panel with user email + 'user view' shortcut + logout, client-side requireAdmin guard with redirect - /admin — overview dashboard with 4 metric cards, 2 panels (status + 24h builds), recent activity table linking to full audit - /admin/users — search + admin toggle + delete (self-delete blocked) - /admin/orgs — plan/quota/suspend actions via prompts - /admin/servers — cross-org table with rebuild + delete actions, status filter - /admin/builds — every build cross-fleet with error vs prompt preview - /admin/audit — system-wide log + CSV export + filter dropdowns - /admin/system — auto-refreshing 5s health probes for Postgres, Redis, queue, Docker - /admin/prompt — live editor for the LLM system prompt with built-in baseline, override-state badge, drop-override action, diff preview, save-as-override End-to-end verified: login as marco.frangiskatos@gmail.com + Melusa112233.*, every admin page returns 200, admin login + overview tested via screenshot, docker probe returns true count of running MCP containers.
145 lines
5.5 KiB
TypeScript
145 lines
5.5 KiB
TypeScript
'use client';
|
|
|
|
import { useEffect, useState } from 'react';
|
|
import { apiFetch } from '@/lib/api';
|
|
import { Input } from '@/components/input';
|
|
import { Button } from '@/components/ui/button';
|
|
import { ShieldCheck, Trash2 } from 'lucide-react';
|
|
|
|
interface AdminUser {
|
|
id: string;
|
|
email: string;
|
|
name: string | null;
|
|
isAdmin: boolean;
|
|
emailVerified: boolean;
|
|
lastLoginAt: string | null;
|
|
createdAt: string;
|
|
org: { orgId: string; orgName: string; orgSlug: string; plan: string; role: string } | null;
|
|
serverCount: number;
|
|
}
|
|
|
|
export default function AdminUsersPage() {
|
|
const [users, setUsers] = useState<AdminUser[] | null>(null);
|
|
const [search, setSearch] = useState('');
|
|
|
|
async function reload() {
|
|
const r = await apiFetch<{ users: AdminUser[] }>(
|
|
`/v1/admin/users${search ? `?search=${encodeURIComponent(search)}` : ''}`,
|
|
);
|
|
setUsers(r.users);
|
|
}
|
|
|
|
useEffect(() => {
|
|
reload();
|
|
}, [search]);
|
|
|
|
async function toggleAdmin(u: AdminUser) {
|
|
if (!confirm(`${u.isAdmin ? 'Revoke' : 'Grant'} admin for ${u.email}?`)) return;
|
|
await apiFetch(`/v1/admin/users/${u.id}`, {
|
|
method: 'PATCH',
|
|
body: JSON.stringify({ isAdmin: !u.isAdmin }),
|
|
});
|
|
reload();
|
|
}
|
|
|
|
async function remove(u: AdminUser) {
|
|
if (!confirm(`Delete user ${u.email}? This cascades to their org and servers.`)) return;
|
|
try {
|
|
await apiFetch(`/v1/admin/users/${u.id}`, { method: 'DELETE' });
|
|
reload();
|
|
} catch (e) {
|
|
const detail = (e as { detail?: { error?: string } }).detail;
|
|
alert(`Failed: ${detail?.error ?? (e as Error).message}`);
|
|
}
|
|
}
|
|
|
|
return (
|
|
<div className="px-8 py-8">
|
|
<header className="mb-6">
|
|
<h1 className="text-[22px] font-semibold tracking-tight">Users</h1>
|
|
<p className="mt-1 text-[13px] text-[--color-fg-muted]">
|
|
{users?.length ?? 0} total. Click admin toggle to elevate or revoke privileges.
|
|
</p>
|
|
</header>
|
|
|
|
<div className="mb-4">
|
|
<Input
|
|
value={search}
|
|
onChange={(e) => setSearch(e.target.value)}
|
|
placeholder="Search by email or name…"
|
|
className="w-80"
|
|
/>
|
|
</div>
|
|
|
|
<div className="panel">
|
|
{users === null && (
|
|
<p className="px-4 py-3 text-[12.5px] text-[--color-fg-muted]">Loading…</p>
|
|
)}
|
|
{users && users.length === 0 && (
|
|
<p className="px-4 py-12 text-center text-[13px] text-[--color-fg-muted]">No matches.</p>
|
|
)}
|
|
{users && users.length > 0 && (
|
|
<table className="w-full text-[12.5px]">
|
|
<thead className="border-b border-[--color-border] text-[--color-fg-subtle]">
|
|
<tr>
|
|
<th className="px-4 py-2 text-left font-medium">Email</th>
|
|
<th className="px-4 py-2 text-left font-medium">Org</th>
|
|
<th className="px-4 py-2 text-left font-medium">Plan</th>
|
|
<th className="px-4 py-2 text-left font-medium">Servers</th>
|
|
<th className="px-4 py-2 text-left font-medium">Last login</th>
|
|
<th className="px-4 py-2 text-left font-medium">Joined</th>
|
|
<th className="px-4 py-2 text-right font-medium">Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{users.map((u) => (
|
|
<tr key={u.id} className="border-b border-[--color-border] last:border-0">
|
|
<td className="px-4 py-2.5">
|
|
<div className="flex items-center gap-2">
|
|
{u.isAdmin && (
|
|
<span
|
|
className="inline-flex h-4 items-center gap-0.5 rounded-full border border-[--color-accent]/40 bg-[--color-accent]/10 px-1.5 text-[10px] font-medium text-[--color-accent]"
|
|
title="admin"
|
|
>
|
|
<ShieldCheck size={9} /> admin
|
|
</span>
|
|
)}
|
|
<span className="mono">{u.email}</span>
|
|
</div>
|
|
{u.name && (
|
|
<div className="text-[11px] text-[--color-fg-subtle]">{u.name}</div>
|
|
)}
|
|
</td>
|
|
<td className="px-4 py-2.5 text-[--color-fg-muted]">{u.org?.orgName ?? '—'}</td>
|
|
<td className="px-4 py-2.5">
|
|
<span className="mono rounded-full border border-[--color-border] bg-[--color-bg-subtle] px-2 py-0.5 text-[11px]">
|
|
{u.org?.plan ?? '—'}
|
|
</span>
|
|
</td>
|
|
<td className="px-4 py-2.5 mono text-[--color-fg-muted]">{u.serverCount}</td>
|
|
<td className="px-4 py-2.5 mono text-[--color-fg-muted]">
|
|
{u.lastLoginAt ? new Date(u.lastLoginAt).toLocaleString() : '—'}
|
|
</td>
|
|
<td className="px-4 py-2.5 mono text-[--color-fg-muted]">
|
|
{new Date(u.createdAt).toLocaleDateString()}
|
|
</td>
|
|
<td className="px-4 py-2.5 text-right">
|
|
<div className="inline-flex gap-1">
|
|
<Button variant="ghost" size="sm" onClick={() => toggleAdmin(u)}>
|
|
{u.isAdmin ? 'revoke admin' : 'make admin'}
|
|
</Button>
|
|
<Button variant="danger" size="sm" onClick={() => remove(u)}>
|
|
<Trash2 size={11} />
|
|
</Button>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
)}
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|