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
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;
|