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
Co-authored-by: Sharang Parnerkar <30073382+mighty840@users.noreply.github.com>
Reviewed-on: #28
38 lines
1.5 KiB
SQL
38 lines
1.5 KiB
SQL
-- D1: the portal's Settings/Users page shows who the tenant's first admin
|
|
-- is. Stored at creation (POST /v1/tenants admin_email) or on the first
|
|
-- IT_ADMIN/OWNER invite through POST /v1/tenants/{id}/invites. Nullable —
|
|
-- sales-led tenants may not have an admin yet.
|
|
ALTER TABLE tenants ADD COLUMN admin_email TEXT;
|
|
|
|
-- Backfill from the audit trail: the first successful Keycloak invite per
|
|
-- tenant is the first admin (createTenant emits keycloak.invite_sent with
|
|
-- target_id = admin email) …
|
|
UPDATE tenants t
|
|
SET admin_email = a.target_id
|
|
FROM (
|
|
SELECT DISTINCT ON (tenant_id) tenant_id, target_id
|
|
FROM audit_log
|
|
WHERE action = 'keycloak.invite_sent'
|
|
AND target_type = 'user'
|
|
AND tenant_id IS NOT NULL
|
|
AND target_id <> ''
|
|
ORDER BY tenant_id, created_at ASC, id ASC
|
|
) a
|
|
WHERE a.tenant_id = t.id
|
|
AND t.admin_email IS NULL;
|
|
|
|
-- … and, where provisioning failed, from the admin_email the failure
|
|
-- event recorded (the operator re-sent the invite by hand).
|
|
UPDATE tenants t
|
|
SET admin_email = a.admin_email
|
|
FROM (
|
|
SELECT DISTINCT ON (tenant_id) tenant_id, metadata->>'admin_email' AS admin_email
|
|
FROM audit_log
|
|
WHERE action = 'keycloak.provision_failed'
|
|
AND tenant_id IS NOT NULL
|
|
AND COALESCE(metadata->>'admin_email', '') <> ''
|
|
ORDER BY tenant_id, created_at ASC, id ASC
|
|
) a
|
|
WHERE a.tenant_id = t.id
|
|
AND t.admin_email IS NULL;
|