diff --git a/internal/server/admin_lifecycle.go b/internal/server/admin_lifecycle.go index 049c9c9..1003f12 100644 --- a/internal/server/admin_lifecycle.go +++ b/internal/server/admin_lifecycle.go @@ -56,7 +56,7 @@ func (s *Server) adminReprovision(w http.ResponseWriter, r *http.Request) { _, lerr := s.Keycloak.ListOrgMembers(ctx, t.Slug) switch { case errors.Is(lerr, keycloak.ErrOrgNotFound): - res, err := s.Keycloak.CreateOrgAndInvite(ctx, keycloak.InviteInput{TenantID: t.ID, Slug: t.Slug, Name: t.Name, AdminEmail: email, TenantStatus: t.Status}) + res, err := s.Keycloak.CreateOrgAndInvite(ctx, keycloak.InviteInput{TenantID: t.ID, Slug: t.Slug, Name: t.Name, AdminEmail: email, TenantStatus: s.onPremStatus(t)}) if err != nil { if errors.Is(err, keycloak.ErrUserConflict) { writeError(w, http.StatusConflict, "email_taken", "an account with this e-mail exists outside this tenant") diff --git a/internal/server/keycloak.go b/internal/server/keycloak.go index 08fb5f6..09bb758 100644 --- a/internal/server/keycloak.go +++ b/internal/server/keycloak.go @@ -24,6 +24,16 @@ type kcInvite struct { MailErr error } +// onPremStatus is the tenant_status a new workspace admin gets in Keycloak: +// the workspace's own on-prem; empty in the cloud, where the adapter keeps +// writing "trial" as before. +func (s *Server) onPremStatus(t *store.Tenant) string { + if s.onPrem() { + return t.Status + } + return "" +} + func (s *Server) provisionKeycloak(ctx context.Context, t *store.Tenant, in keycloak.InviteInput) (kcInvite, error) { if in.AdminEmail == "" { // Skip silently — caller chose not to invite anyone yet (sales-led diff --git a/internal/server/onprem_test.go b/internal/server/onprem_test.go index 207fa99..aad3e9d 100644 --- a/internal/server/onprem_test.go +++ b/internal/server/onprem_test.go @@ -364,3 +364,23 @@ type createResp struct { Tenant *store.Tenant `json:"tenant"` InviteURL string `json:"invite_url"` } + +// The cloud admin keeps the adapter's default tenant_status ("trial"); only +// on-prem passes the workspace's own. +func TestTenantStatusForTheAdminOnlyOnPrem(t *testing.T) { + eachStore(t, func(t *testing.T, h *testHarness) { + if resp, body := h.do("POST", "/v1/tenants", map[string]any{"slug": "cloud-demo", "name": "Demo", "kind": "demo", "admin_email": "demo@cloud.test"}); resp.StatusCode != http.StatusCreated { + t.Fatalf("cloud create = %d %s", resp.StatusCode, body) + } + if c := h.kcMock.Created; len(c) != 1 || c[0].TenantStatus != "" { + t.Fatalf("cloud: %+v", c) + } + onPremHarness(h, nil) + if resp, body := h.do("POST", "/v1/tenants", map[string]any{"slug": "werk", "name": "Werk", "admin_email": "anna.admin@onprem.test"}); resp.StatusCode != http.StatusCreated { + t.Fatalf("on-prem create = %d %s", resp.StatusCode, body) + } + if c := h.kcMock.Created; len(c) != 2 || c[1].TenantStatus != store.StatusActive { + t.Errorf("on-prem: %+v", c) + } + }) +} diff --git a/internal/server/tenants.go b/internal/server/tenants.go index 463808d..28c1f5c 100644 --- a/internal/server/tenants.go +++ b/internal/server/tenants.go @@ -165,7 +165,7 @@ func (s *Server) createTenantCore(ctx context.Context, r *http.Request, in creat // tenant back — the operator can resend the invite via the KC admin UI. // We emit an audit event regardless so the failure is traceable. invite, kcErr := s.provisionKeycloak(ctx, t, keycloak.InviteInput{ - TenantStatus: t.Status, + TenantStatus: s.onPremStatus(t), AdminEmail: in.AdminEmail, AdminName: in.AdminName, AdminFirst: in.adminFirst, AdminLast: in.adminLast, EmailVerified: in.emailVerified, })