Skip to content

Commit e9f33d0

Browse files
committed
feat(rbac): backend models, enforcement, override, audit (WIP)
1 parent 5af1c38 commit e9f33d0

47 files changed

Lines changed: 3841 additions & 1033 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.env.example

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Local (example)
2+
DATABASE_URL="postgresql://postgres:postgrespw@localhost:5432/hospital_db?schema=public"
3+
4+
# Neon (example format) - DO NOT COMMIT real credentials
5+
# DATABASE_URL="postgresql://<USER>:<PASSWORD>@<HOST>/<DB>?pgbouncer=true&sslmode=require&schema=public"
6+
7+
# NextAuth
8+
NEXTAUTH_SECRET="your-super-secret-key-change-this-in-production"
9+
NEXTAUTH_URL="http://localhost:3000"
10+
11+
# RBAC seed (only used when you run prisma:seed in staging/prod)
12+
# RBAC_ADMIN_EMAIL=admin@yourorg.example
13+
# RBAC_ADMIN_PASSWORD=<strong-password>

README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,58 @@ Built with ❤️ by [Your Name]
134134
## Support
135135

136136
For support, email your-email@example.com or open an issue in the repository.
137+
138+
---
139+
140+
## Authentication & Access Control (RBAC, emergency override, audit) 🔐
141+
142+
This repository now includes a production-ready backend implementation for:
143+
144+
- `Users`, `RoleEntity` (roles), `Permission` (atomic permissions)
145+
- JWT augmentation with `permissions` claim
146+
- Emergency override workflow (time-limited, single-use tokens)
147+
- Immutable `AuditLog` records for sensitive actions
148+
149+
How to apply schema + seed (local dev):
150+
151+
1. Ensure your `.env` has a valid `DATABASE_URL` and `NEXTAUTH_SECRET`.
152+
2. Run migrations and seed:
153+
154+
```bash
155+
# create migration and apply to your DB
156+
npx prisma migrate dev --name add-rbac-audit-override
157+
# seed RBAC + initial admin user
158+
npm run prisma:seed
159+
```
160+
161+
API endpoints (backend only — admin permissions required):
162+
163+
- `POST /api/auth/override` — request an emergency override (body: `{ reason, targetUserId?, ttlMinutes? }`) → returns `{ token, expiresAt }`
164+
- `GET /api/admin/roles/:id` — get single role (requires `roles.manage`)
165+
- `PUT /api/admin/roles/:id` — update role name and permissions (requires `roles.manage`) — body: `{ name?, permissions?: string[] }`
166+
- `DELETE /api/admin/roles/:id` — delete role (requires `roles.manage`, cannot delete `ADMIN` or roles with users)`
167+
- `GET /api/admin/roles` — list roles (requires `roles.manage`)
168+
- `POST /api/admin/roles` — create role with permissions (requires `roles.manage`)
169+
- `GET /api/admin/permissions` — list permissions (requires `roles.manage`)
170+
- `POST /api/admin/permissions` — bulk create permissions (requires `roles.manage`)
171+
- `GET /api/admin/users` — list users (requires `users.manage`)
172+
- `POST /api/admin/users` — create user (requires `users.manage`)
173+
- `GET /api/admin/audit` — read audit log (requires `audit.read`)
174+
175+
Security notes:
176+
- Emergency override token must be supplied in the `x-override-token` header for actions that allow overrides.
177+
- Socket.IO connections require a valid session JWT (passed as `auth.token` or `Authorization: Bearer <jwt>`); server enforces origin via `NEXT_PUBLIC_APP_URL`. (See `server.ts`.)
178+
- All overrides and permission changes are written to `AuditLog`.
179+
180+
If you'd like, I can open a PR that runs the migration against a disposable test database and add CI to run the new unit/integration tests.
181+
182+
---
183+
184+
Neon (Postgres) — quick notes
185+
- Set `DATABASE_URL` to your Neon branch URL, include `?pgbouncer=true&sslmode=require&schema=public`.
186+
- To repair local Prisma client issues on Windows/OneDrive: `npm run prisma:regen` — it removes temp files and regenerates the client.
187+
- To verify a Neon DB after migrations: set `DATABASE_URL` and run `npm run verify:neon`.
188+
189+
CI
190+
- The repository includes a GitHub Actions workflow that will run migrations against a Neon staging DB (when `NEON_DATABASE_URL` secret is provided) and run integration tests.
191+

0 commit comments

Comments
 (0)