Skip to content

feat(roles): replace is_superuser with admin/manager/member - #2535

Closed
zachross90210 wants to merge 3 commits into
fastapi:masterfrom
zachross90210:feat/user-roles
Closed

zachross90210 wants to merge 3 commits into
fastapi:masterfrom
zachross90210:feat/user-roles

Conversation

@zachross90210

Copy link
Copy Markdown

Why

Add admin, manager, and member access levels so user management and metrics can be role-gated without putting role claims in JWTs.

What changed

  • Replace is_superuser with UserRole (admin/manager/member), including a populated-DB Alembic backfill.
  • Gate routes with require_roles (AdminUser / StaffUser); add GET /metrics/.
  • Frontend mirrors staff/admin rules on /admin and /metrics, and fixes handleApiError so authz 403 no longer logs users out while inactive users do.
  • Add focused security and migration tests, plus python -m app.seed_demo_users for three demo accounts.

Scope

Backend auth/users/items/metrics, one migration, regenerated OpenAPI client, admin/metrics UI, seed CLI. No unrelated refactors.

Tradeoffs

  • Dropped native Postgres ENUM for VARCHAR + check constraint.
  • Deleted is_superuser in the same wave instead of a dual-field compatibility layer.
  • Kept FIRST_SUPERUSER* setting names.

Blast Radius

Breaking OpenAPI change (is_superuser → role). Existing sessions keep working because tokens stay sub-only and roles load from the DB each request.

Verification

  • Backend: 72 pytest passed (local uv + throwaway Postgres).
  • Migration test: upgrade/downgrade on populated scratch DB.
  • Playwright: 62 passed; Mailpit-dependent reset-password specs skipped here; session specs confirmed against old vs new handleApiError.
  • Seed: idempotent create of three demo users.

zachross90210 and others added 3 commits November 28, 2026 20:22
Add UserRole and a role column backed by a check constraint. The
migration backfills admin from is_superuser and member for everyone
else, then drops is_superuser. Routes authorize through require_roles
with AdminUser and StaffUser aliases. Staff can read users and the new
/metrics endpoint. Only admins mutate users or bypass item ownership.
Roles are read from the database on each request, so changes apply to
existing tokens.

Co-authored-by: Cursor <cursoragent@cursor.com>
Regenerate the client from OpenAPI. Staff reach /admin and the new
/metrics page; members are redirected. Only admins see Add User and
row actions. The superuser checkbox becomes a role select.

handleApiError now logs out only on session errors (401, Inactive
user, invalid credentials). Authorization 403s refresh the current
user instead of wiping the token.

Co-authored-by: Cursor <cursoragent@cursor.com>
python -m app.seed_demo_users creates demo-admin, demo-manager, and
demo-member users and converges them to their role, active state,
and password on rerun. It refuses to run unless FASTAPI_ENV is
development and leaves FIRST_SUPERUSER untouched.

Co-authored-by: Cursor <cursoragent@cursor.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant