Add multi-tenant organizations, memberships, org-scoped items and RBAC #11
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.
What this PR does
Key implementation notes
Database migration (backend/app/alembic/versions/fe12b3c4a567_add_organizations_memberships_and_item_org_id.py): creates organization and membership tables, adds org_id to item, backfills items by creating a default org per existing user, and makes item.org_id NOT NULL.
Models (backend/app/models.py): new Organization and Membership models, Role enum, membership relationships, and TokenPayload.active_org_id.
Security / tokens (backend/app/core/security.py): create_access_token accepts active_org_id and encodes it into the JWT.
Dependencies (backend/app/api/deps.py): new helpers to decode token payload, require_org_member (verifies membership and acceptance), and require_admin (enforces admin role). get_current_user now builds from the token payload. get_active_org_id helper is provided.
Orgs API (backend/app/api/routes/orgs.py): endpoints to list user orgs, create org (auto-adds creator as admin), switch active org (returns new token with active_org_id), list members, invite member (creates pending membership), accept invite, and remove member. All endpoints validate the active org against the token and membership.
Items API (backend/app/api/routes/items.py): all item endpoints (list, get, create, update, delete) are scoped to the active organization. Non-admins see only their own items within the org.
CRUD changes (backend/app/crud.py): creating users now creates a default org + admin membership; create_item uses a user’s accepted membership to assign the org (creates a default org if none exists).
Seed script (backend/scripts/seed.py): creates two orgs (Acme Inc, Umbrella Corp), users (superuser from settings, alice, bob), and memberships (superuser admin of both, Alice member of Acme, Bob member of Umbrella).
Frontend
Tests
Docs (development.md): added instructions for running the seed script in Docker Compose.
How to try locally
Notes and considerations
Files of interest (non-exhaustive)
This PR implements the multi-tenant foundation (DB, models, API, frontend bits, seed data and tests). Follow-up work can include improving UX for selecting the active org, listing available orgs in the members page, stronger invite emails/accept flows, and more comprehensive e2e coverage.
Original Task: full-stack-demo/q83jqyoy6fk9
Author: James White