Initial import: property management SaaS + security hardening + admin dashboard

Property Management Network — Next.js 16 (App Router), Better Auth,
Drizzle ORM over PostgreSQL, Stripe, OpenAI, Resend.

Includes:
- Security hardening: access-control/IDOR fixes, TLS-by-default DB layer,
  constant-time cron auth, strict security headers, atomic AI quota gating,
  HTML/email output encoding, demo-backdoor disabled in production.
- Superadmin dashboard at /admin (overview/MRR, server-paginated users with
  ban/impersonate/plan/delete, billing, platform activity + admin audit log,
  AI usage, system health) via the Better Auth admin plugin.
- Seed/migration utility scripts under scripts/.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Leon Serfaty
2026-06-23 20:36:07 -04:00
co-authored by Claude Opus 4.8
commit 857b9a7811
291 changed files with 38996 additions and 0 deletions
+58
View File
@@ -0,0 +1,58 @@
/**
* Idempotent migration for the admin dashboard: adds the Better Auth admin
* plugin columns (user.role/banned/ban_reason/ban_expires, session.impersonated_by)
* and the admin_audit_log table. Safe to run multiple times.
*
* Run: npx tsx scripts/migrate-admin.ts
*/
import { config } from "dotenv"
config({ path: ".env.local" })
process.env.DATABASE_SSL = process.env.DATABASE_SSL ?? "disable"
const SQL = `
ALTER TABLE "user" ADD COLUMN IF NOT EXISTS "role" text DEFAULT 'user';
ALTER TABLE "user" ADD COLUMN IF NOT EXISTS "banned" boolean DEFAULT false;
ALTER TABLE "user" ADD COLUMN IF NOT EXISTS "ban_reason" text;
ALTER TABLE "user" ADD COLUMN IF NOT EXISTS "ban_expires" timestamp;
ALTER TABLE "session" ADD COLUMN IF NOT EXISTS "impersonated_by" text;
CREATE TABLE IF NOT EXISTS "admin_audit_log" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid(),
"admin_id" text,
"action" text NOT NULL,
"target_user_id" text,
"metadata" jsonb NOT NULL DEFAULT '{}'::jsonb,
"ip_address" text,
"created_at" timestamptz NOT NULL DEFAULT now()
);
DO $$ BEGIN
ALTER TABLE "admin_audit_log"
ADD CONSTRAINT "admin_audit_log_admin_id_user_id_fk"
FOREIGN KEY ("admin_id") REFERENCES "user"("id") ON DELETE SET NULL;
EXCEPTION WHEN duplicate_object THEN NULL; END $$;
CREATE INDEX IF NOT EXISTS "admin_audit_log_created_at_idx" ON "admin_audit_log" ("created_at" DESC);
CREATE INDEX IF NOT EXISTS "admin_audit_log_target_idx" ON "admin_audit_log" ("target_user_id");
`
async function main() {
const { pool } = await import("../lib/db")
await pool.query(SQL)
const cols = await pool.query(
`select table_name, column_name from information_schema.columns
where (table_name='user' and column_name in ('role','banned','ban_reason','ban_expires'))
or (table_name='session' and column_name='impersonated_by')
order by table_name, column_name`
)
console.log("Applied admin migration. New columns present:")
for (const r of cols.rows) console.log(` ${r.table_name}.${r.column_name}`)
const t = await pool.query(`select to_regclass('public.admin_audit_log') as t`)
console.log(` admin_audit_log table: ${t.rows[0].t ? "OK" : "MISSING"}`)
await pool.end()
}
main().catch((e) => {
console.error("Migration failed:", e)
process.exit(1)
})