feat(members): team management: role, block, remove, revoke, resend #64

Merged
sharang merged 1 commits from feat/team-management into main 2026-10-05 18:43:31 +00:00
Owner

Registry half of platform/portal#74 (Team-Verwaltung). The portal PR follows once this is on staging.

Endpoints

Tenant (IT_ADMIN/OWNER user token, or service client with tenants.write) Staff (route group admin)
Role / block PATCH /v1/tenants/{id}/members/{uid} {"role"} or {"blocked"} PATCH /v1/admin/tenants/{id}/members/{uid}
Remove / revoke DELETE /v1/tenants/{id}/members/{uid} DELETE /v1/admin/tenants/{id}/members/{uid}
Resend POST /v1/tenants/{id}/members/{uid}/resend POST /v1/admin/tenants/{id}/members/{uid}/resend

The portal calls as portal-svc and names the person in X-Portal-User-Subject / X-Portal-User-Email (staff: the existing X-Staff-*). The guards and the audit trail use that person. Members in GET .../members and the admin detail now carry pending and status (invited | active | blocked). "Invited" means no password credential and no identity-provider link.

Guards

  • 409 self: nobody changes, blocks or removes their own membership. Matched by subject or e-mail.
  • 409 last_admin: the last active IT_ADMIN/OWNER cannot be demoted, blocked or removed. Pending and blocked admins don't count.
  • Role must be one of IT_ADMIN, OWNER, CXO, FINANCE, LEGAL, USER.

Block per workspace: design choice

  • No enabled=false. The tenant id goes into the user attribute org_blocked, a list of blocked tenant ids. The realm's unmanaged-attribute policy is ADMIN_EDIT, so users cannot edit it.
  • Memberships() leaves blocked orgs out. That changes GET /v1/users/{id}/memberships, which the portal uses as its access check. This check does not depend on any protocol mapper.
  • New requireMember middleware: user tokens of a member who is blocked in the token's tenant get 403 MEMBER_BLOCKED. It checks the org_blocked claim when a mapper projects it. Otherwise it reads the Keycloak attribute (MemberBlocked), cached for 30 s; a block made through the registry drops the cache entry at once. A deleted account gets 403 USER_UNKNOWN. If Keycloak is down the check fails open and logs a warning, so an IdP outage does not lock everyone out.
  • POST /v1/internal/entitlements/check takes an optional user_id. A blocked member gets allowed=false, reason=member_blocked.

Sessions

Remove and block call Keycloak POST /users/{id}/logout. That ends all of the user's sessions in the realm, not only this workspace's. Keycloak has no per-organization logout. We accept this: the user simply signs in again elsewhere. If the logout fails, the action still succeeds and sessions_ended=false is reported and audited.

Remove vs. revoke

  • Accepted member: the org membership is removed. The tenant-context attributes (tenant_id, tenant_slug, org_roles, tenant_status, products, plan) are cleared when they name this tenant. The tenant is dropped from org_blocked. Sessions are ended.
  • Pending invitation: the membership is removed. The Keycloak account is deleted only when a user.invited audit entry of this tenant records user_created=true for that user id and the account is in no other org. Deleting the account voids the mail link. Otherwise only the membership goes.

API keys

Remove and block return the member's active keys (created_by = e-mail or user id) as api_keys, so the caller can offer them for revocation. They are never revoked silently. Keys created before this change have no created_by, so they can't be attributed; the portal PR starts recording it.

Audit

user.role_changed (metadata from → to), user.blocked, user.unblocked, user.removed, user.invite_revoked (user_deleted), user.invite_resent. Each records the actor.

Role change

Rewrites org_roles to [role] and takes effect at the member's next login or token refresh. Note that org_roles is a user attribute, not a per-org one, so a person in two workspaces has one role for both. This is unchanged from before; signup already allows one workspace per address.

Follow-ups (not in this PR)

  • Realm: add a user-attribute mapper org_blocked → claim to the tenant-context scope. The tenant-context mapper could also drop tenant_id when it is blocked. This lets products reject blocked members from the token alone. Until then, products must ask entitlements/check with user_id, or the memberships endpoint.
  • Prüfwerk: pass user_id to entitlements/check.

Checks

go vet, golangci-lint v2.12.2 (0 issues), go test ./... (memory + postgres) green. OpenAPI spec and route test extended.

🤖 Generated with Claude Code

https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB

Registry half of platform/portal#74 (Team-Verwaltung). The portal PR follows once this is on staging. ## Endpoints | | Tenant (IT_ADMIN/OWNER user token, or service client with `tenants.write`) | Staff (route group `admin`) | |---|---|---| | Role / block | `PATCH /v1/tenants/{id}/members/{uid}` `{"role"}` or `{"blocked"}` | `PATCH /v1/admin/tenants/{id}/members/{uid}` | | Remove / revoke | `DELETE /v1/tenants/{id}/members/{uid}` | `DELETE /v1/admin/tenants/{id}/members/{uid}` | | Resend | `POST /v1/tenants/{id}/members/{uid}/resend` | `POST /v1/admin/tenants/{id}/members/{uid}/resend` | The portal calls as `portal-svc` and names the person in `X-Portal-User-Subject` / `X-Portal-User-Email` (staff: the existing `X-Staff-*`). The guards and the audit trail use that person. Members in `GET .../members` and the admin detail now carry `pending` and `status` (`invited` | `active` | `blocked`). "Invited" means no password credential and no identity-provider link. ## Guards - `409 self`: nobody changes, blocks or removes their own membership. Matched by subject or e-mail. - `409 last_admin`: the last **active** IT_ADMIN/OWNER cannot be demoted, blocked or removed. Pending and blocked admins don't count. - Role must be one of IT_ADMIN, OWNER, CXO, FINANCE, LEGAL, USER. ## Block per workspace: design choice - **No `enabled=false`.** The tenant id goes into the user attribute **`org_blocked`**, a list of blocked tenant ids. The realm's unmanaged-attribute policy is ADMIN_EDIT, so users cannot edit it. - `Memberships()` leaves blocked orgs out. That changes `GET /v1/users/{id}/memberships`, which the portal uses as its access check. This check does not depend on any protocol mapper. - New `requireMember` middleware: user tokens of a member who is blocked in the token's tenant get `403 MEMBER_BLOCKED`. It checks the `org_blocked` claim when a mapper projects it. Otherwise it reads the Keycloak attribute (`MemberBlocked`), cached for 30 s; a block made through the registry drops the cache entry at once. A deleted account gets `403 USER_UNKNOWN`. If Keycloak is down the check **fails open** and logs a warning, so an IdP outage does not lock everyone out. - `POST /v1/internal/entitlements/check` takes an optional `user_id`. A blocked member gets `allowed=false, reason=member_blocked`. ## Sessions Remove and block call Keycloak `POST /users/{id}/logout`. That ends **all** of the user's sessions in the realm, not only this workspace's. Keycloak has no per-organization logout. We accept this: the user simply signs in again elsewhere. If the logout fails, the action still succeeds and `sessions_ended=false` is reported and audited. ## Remove vs. revoke - **Accepted member:** the org membership is removed. The tenant-context attributes (`tenant_id`, `tenant_slug`, `org_roles`, `tenant_status`, `products`, `plan`) are cleared when they name this tenant. The tenant is dropped from `org_blocked`. Sessions are ended. - **Pending invitation:** the membership is removed. The Keycloak account is deleted only when a `user.invited` audit entry of this tenant records `user_created=true` for that user id **and** the account is in no other org. Deleting the account voids the mail link. Otherwise only the membership goes. ## API keys Remove and block return the member's active keys (`created_by` = e-mail or user id) as `api_keys`, so the caller can offer them for revocation. They are never revoked silently. Keys created before this change have no `created_by`, so they can't be attributed; the portal PR starts recording it. ## Audit `user.role_changed` (metadata `from` → `to`), `user.blocked`, `user.unblocked`, `user.removed`, `user.invite_revoked` (`user_deleted`), `user.invite_resent`. Each records the actor. ## Role change Rewrites `org_roles` to `[role]` and takes effect at the member's next login or token refresh. Note that `org_roles` is a user attribute, not a per-org one, so a person in two workspaces has one role for both. This is unchanged from before; signup already allows one workspace per address. ## Follow-ups (not in this PR) - Realm: add a user-attribute mapper `org_blocked` → claim to the tenant-context scope. The tenant-context mapper could also drop `tenant_id` when it is blocked. This lets products reject blocked members from the token alone. Until then, products must ask `entitlements/check` with `user_id`, or the memberships endpoint. - Prüfwerk: pass `user_id` to `entitlements/check`. ## Checks `go vet`, `golangci-lint` v2.12.2 (0 issues), `go test ./...` (memory + postgres) green. OpenAPI spec and route test extended. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB
sharang added 1 commit 2026-10-05 17:53:42 +00:00
feat(members): team management: role, block, remove, revoke, resend
ci / shared (pull_request) Successful in 14s
ci / test (pull_request) Successful in 24m44s
ci / image (pull_request) Skipped
1eea397819
portal#74. PATCH/DELETE /v1/tenants/{id}/members/{uid} and POST
.../resend, the same under /v1/admin/tenants/{id}/members/{uid} for staff.

- Role change rewrites org_roles (allowed set only); effective at the
  member's next login / token refresh.
- Block is per workspace: tenant id in the user attribute org_blocked,
  never enabled=false. Memberships() leaves blocked orgs out, a new
  requireMember middleware rejects user tokens of blocked members (claim
  when mapped, else a cached Keycloak lookup), entitlements/check takes an
  optional user_id and answers member_blocked.
- Remove clears the org membership and the tenant attributes; revoke of a
  pending invitation deletes the account only when this tenant's invite
  created it and it is in no other org.
- Remove and block end the Keycloak sessions (realm-wide logout) and list
  the member's API keys for revocation.
- Guards: 409 self, 409 last_admin. Audit: user.role_changed,
  user.blocked, user.unblocked, user.removed, user.invite_revoked,
  user.invite_resent, with the actor (X-Portal-User-* / X-Staff-*).
- Members carry pending + status (invited/active/blocked).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB
sharang merged commit 1f6236a754 into main 2026-10-05 18:43:31 +00:00
Sign in to join this conversation.