feat(members): tenant organization members, invites, admin email #28

Merged
sharang merged 1 commits from feat/tenant-members into main 2026-09-14 08:27:13 +00:00
Owner

Demonstration plan, phase D1 (registry side). The portal's Settings/Users page renders design fixtures today and its "Invite member" button posts to a route that does not exist, because the registry had no membership surface.

GET /v1/tenants/{id}/members lists the tenant's Keycloak organization members — user id, email, first/last name, org_roles, enabled, created_at. A tenant whose organization was never created (sales-led) answers an empty list rather than an error.

POST /v1/tenants/{id}/invites {email, first_name?, last_name?, role?} creates the Keycloak user if missing, adds the organization member (KC 26: the member body is the bare user id), writes the tenant-context user attributes, sends the same execute-actions mail the first admin gets, and audits user.invited with the caller as actor. Idempotent: an existing member answers 200 {status:"member"} and sends no mail; a new one 202 {status:"invited"}.

Scope. A user token may only touch its own tenant — another tenant's id is a 404, never a hint that it exists. Inviting needs the IT_ADMIN or OWNER org role, so Principal now carries the org_roles claim and IsTenantAdmin(). Service clients stay gated by AUTHZ_RULES route groups: members on tenants.read, invites on tenants.write.

admin_email on the tenant (migration 0006, backfilled from the keycloak.invite_sent audit trail, and from keycloak.provision_failed metadata where the first invite failed) so Settings can show a real contact instead of an em dash. The first IT_ADMIN/OWNER invite fills it in when it was empty.

Routing note worth keeping: GET /v1/tenants/{id}/members cannot be registered — Go's ServeMux calls it a conflict with GET /v1/tenants/by-slug/{slug}, since both match /v1/tenants/by-slug/members and neither pattern is more specific. It is registered as GET /v1/tenants/{id}/{sub} instead, which is less specific than the by-slug lookup, so the two coexist; anything but members answers 404.

openapi.yaml documents both endpoints, the OrgMember/Invite schemas, admin_email and a Forbidden response.

Verified locally: go vet ./..., gofmt clean, go test ./... green, CI coverage command at 74.3% (gate 70%).

🤖 Generated with Claude Code

https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB

Demonstration plan, phase D1 (registry side). The portal's Settings/Users page renders design fixtures today and its "Invite member" button posts to a route that does not exist, because the registry had no membership surface. **`GET /v1/tenants/{id}/members`** lists the tenant's Keycloak organization members — user id, email, first/last name, `org_roles`, `enabled`, `created_at`. A tenant whose organization was never created (sales-led) answers an empty list rather than an error. **`POST /v1/tenants/{id}/invites`** `{email, first_name?, last_name?, role?}` creates the Keycloak user if missing, adds the organization member (KC 26: the member body is the bare user id), writes the tenant-context user attributes, sends the same execute-actions mail the first admin gets, and audits `user.invited` with the caller as actor. Idempotent: an existing member answers `200 {status:"member"}` and sends no mail; a new one `202 {status:"invited"}`. **Scope.** A user token may only touch its own tenant — another tenant's id is a 404, never a hint that it exists. Inviting needs the `IT_ADMIN` or `OWNER` org role, so `Principal` now carries the `org_roles` claim and `IsTenantAdmin()`. Service clients stay gated by `AUTHZ_RULES` route groups: members on `tenants.read`, invites on `tenants.write`. **`admin_email` on the tenant** (migration `0006`, backfilled from the `keycloak.invite_sent` audit trail, and from `keycloak.provision_failed` metadata where the first invite failed) so Settings can show a real contact instead of an em dash. The first `IT_ADMIN`/`OWNER` invite fills it in when it was empty. **Routing note worth keeping:** `GET /v1/tenants/{id}/members` cannot be registered — Go's ServeMux calls it a conflict with `GET /v1/tenants/by-slug/{slug}`, since both match `/v1/tenants/by-slug/members` and neither pattern is more specific. It is registered as `GET /v1/tenants/{id}/{sub}` instead, which *is* less specific than the by-slug lookup, so the two coexist; anything but `members` answers 404. `openapi.yaml` documents both endpoints, the `OrgMember`/`Invite` schemas, `admin_email` and a `Forbidden` response. Verified locally: `go vet ./...`, `gofmt` clean, `go test ./...` green, CI coverage command at **74.3%** (gate 70%). 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB
sharang added 1 commit 2026-09-12 18:59:49 +00:00
feat(members): tenant organization members, invites, admin email
ci / shared (pull_request) Successful in 13s
ci / test (pull_request) Successful in 22m19s
ci / image (pull_request) Skipped
9f393b728f
The portal's Settings/Users page showed design fixtures because the
registry had no membership surface. Two endpoints close that:

- GET /v1/tenants/{id}/members lists the Keycloak organization members
  (user id, email, name, org_roles, enabled). A tenant without an
  organization answers an empty list, not an error.
- POST /v1/tenants/{id}/invites creates the user if needed, adds the org
  member (KC 26: bare user id body), writes the tenant-context
  attributes, sends the execute-actions mail and audits user.invited.
  Idempotent: an existing member answers 200 with status=member.

User tokens are confined to their own tenant (cross-tenant is a 404) and
need IT_ADMIN or OWNER to invite; service clients are gated per route
group as before. Principal gains the org_roles claim for that check.

Tenants carry admin_email (migration 0006, backfilled from the invite
audit trail) so Settings can show a real contact instead of an em dash.

The members route is registered as GET /v1/tenants/{id}/{sub}: ServeMux
rejects /v1/tenants/{id}/members as conflicting with the by-slug lookup,
because both match /v1/tenants/by-slug/members and neither pattern is
more specific.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB
sharang merged commit 99214c088b into main 2026-09-14 08:27:13 +00:00
Sign in to join this conversation.