feat(admin): platform_admin flag + guard + read-only Overview/Workspaces (MN-104)#100
Open
ikrasovytskyi wants to merge 3 commits into
Open
feat(admin): platform_admin flag + guard + read-only Overview/Workspaces (MN-104)#100ikrasovytskyi wants to merge 3 commits into
ikrasovytskyi wants to merge 3 commits into
Conversation
ikrasovytskyi
enabled auto-merge
July 18, 2026 11:22
Deploying with
|
| Status | Name | Latest Commit | Preview URL | Updated (UTC) |
|---|---|---|---|---|
| ✅ Deployment successful! View logs |
storyos-docs | 9c6f26c | Commit Preview URL Branch Preview URL |
Jul 18 2026, 03:35 PM |
…ces (MN-104)
First cut of the superadmin panel's backend foundation — deliberately
narrow, matching the ticket's own design notes ("start with Overview +
Users + Workspaces..."). Everything here is read-only.
platform_admins is a separate table, NOT a column on `user` — better-auth
owns that table (see auth-schema.ts's existing comment forbidding app
columns on it). Presence of a row = admin. Seeded via PLATFORM_ADMIN_EMAIL
at boot: looks up the user by email and grants if found, logs and no-ops
(retried every restart) if they haven't signed up yet — safe to set the env
var before the first operator's sign-up.
PlatformAdminGuard follows the exact WorkspaceAccessGuard convention
(@UseGuards(AuthGuard, PlatformAdminGuard)), 403 not 404 since /admin's
existence isn't a secret. AdminOverviewService reads the existing
entitlements/plan catalogue rather than recomputing pricing (per the
ticket's own instruction) — estimatedMrrUsd is plan price + $12/seat
overage summed across workspaces, explicitly labeled an estimate since it
can drift from Stripe's actual invoiced total on proration/timing.
Known limitation, documented rather than hidden: per-workspace billable-seat
lookups are one query per workspace (AccessService has no bulk variant) —
fine at today's scale, needs revisiting well before thousands of
workspaces.
Deliberately NOT built in this pass — each is substantial, safety-sensitive
work in its own right, not a bolt-on to a read-only overview:
- Impersonation (needs the "never trigger a charge/mutation while
impersonating" guarantee designed and tested, not assumed)
- Token revoke, GDPR export/erase, security audit log
- Billing ops UI (comp/refund/plan-change) — MN-196's setOverride/
clearOverride are the seam this will call
- Agent operability, moderation, growth metrics — all explicitly
forward-looking in the ticket itself
Tests: 8 unit tests (platform-admin.service.test.ts,
platform-admin.guard.test.ts) covering isPlatformAdmin, seedFromEnv
(grants when found / no-ops when not), grant, revoke, and the guard's
allow/reject paths. AdminOverviewService's multi-join aggregate queries are
better proven by a real integration test than a hand-rolled drizzle-chain
stub — deferred to land alongside this ticket's migration, once the
one-migration-in-flight lane clears (MN-195/MN-196 are ahead of it).
Migration deliberately not yet generated, same reason.
…rface (MN-104) Real HTTP calls against a booted app: 403 for a non-platform-admin, 401 with no auth, and a platform admin (granted directly via a DB insert — there's no HTTP grant surface yet) seeing counts and workspace rows across accounts they don't belong to, proving this is genuinely cross-workspace and not scoped by membership like every other endpoint in the app. Not yet run against a live migration — platform_admins' migration is deliberately deferred until the one-migration-in-flight lane clears (MN-196 is ahead of it). Typechecks clean.
Regenerated after a genuine slot collision with an unrelated PR (#98, sort-by-formula-fields) that also claimed 0046 and merged first.
ikrasovytskyi
force-pushed
the
api/mn104-admin-panel
branch
from
July 18, 2026 15:34
42fcb90 to
9c6f26c
Compare
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
First cut of the superadmin panel's backend foundation — deliberately narrow, matching the ticket's own design notes ("start with Overview + Users + Workspaces..."). Everything here is read-only.
platform_adminsis a separate table, not a column onuser— better-auth owns that table (there's an existing warning comment inauth-schema.tsforbidding app columns on it, easy to have missed). Presence of a row = admin.PLATFORM_ADMIN_EMAILat boot: looks up the user by email and grants if found; logs and no-ops (retried every restart) if they haven't signed up yet — safe to set the env var before the first operator's sign-up.PlatformAdminGuardfollows the exactWorkspaceAccessGuardconvention (@UseGuards(AuthGuard, PlatformAdminGuard)), 403 not 404 since/admin's existence isn't a secret.AdminOverviewServicereads the existing entitlements/plan catalogue rather than recomputing pricing (per the ticket's own instruction) —estimatedMrrUsdis plan price + $12/seat overage summed across workspaces, explicitly labeled an estimate since it can drift from Stripe's actual invoiced total on proration/timing.AccessServicehas no bulk variant) — fine at today's scale, needs revisiting well before thousands of workspaces.Deliberately NOT built in this pass — each is substantial, safety-sensitive work in its own right, not a bolt-on to a read-only overview: impersonation (needs the "never trigger a charge/mutation while impersonating" guarantee designed and tested, not assumed), token revoke, GDPR export/erase, security audit log, billing ops UI (comp/refund/plan-change — MN-196's
setOverride/clearOverrideare the seam this will call), agent operability, moderation, growth metrics.Test plan
platform-admin.service.test.ts,platform-admin.guard.test.ts) coveringisPlatformAdmin,seedFromEnv(grants when found / no-ops when not),grant,revoke, and the guard's allow/reject paths.test/admin.test.ts): 401 with no auth, 403 for an authenticated non-admin, and a granted platform admin seeing counts/workspace rows across accounts they don't belong to — proving this is genuinely cross-workspace, not scoped by membership like every other endpoint.main:pnpm lint && pnpm typecheck && pnpm test && pnpm build && pnpm docs:check && pnpm install --frozen-lockfile— all green, 820 tests passed (real testcontainer Postgres, migration0046_puzzling_harrier.sqlapplied cleanly).GET /admin/overviewunauthenticated (401), authenticated-but-not-admin (403), grantedplatform_admindirectly in Postgres, then got real cross-workspace data back —totalWorkspaces: 11, totalUsers: 13, totalRecords: 10103— andGET /admin/workspacescorrectly listed workspaces the test operator had zero membership in (e.g. pre-existing dev-stack workspaces from earlier sessions), proving the cross-tenant read path actually works, not just against fixtures the test created itself.🤖 Generated with Claude Code