feat(auth): support tenantless provisioning and gated self-service workspace creation

Allow deployments to register users without auto-creating a personal tenant, expose creation policy via /auth/me, and guide tenantless accounts through a workspace onboarding flow.
This commit is contained in:
wizardchen
2026-07-13 18:04:14 +08:00
committed by lyingbug
parent 50b8c0a72e
commit ecf2c1fb9b
36 changed files with 1543 additions and 89 deletions
+30 -2
View File
@@ -31,8 +31,8 @@ type TenantHandler struct {
memberService interfaces.TenantMemberService
kbService interfaces.KnowledgeBaseService
config *config.Config
// systemSettingSvc resolves runtime tunables for tenant limits
// (currently `tenant.max_owned_per_user`). Reading goes DB > ENV >
// systemSettingSvc resolves runtime tenant policies and limits.
// Reading goes DB > ENV >
// in-code default, so a SystemAdmin's UI override applies on the
// very next CreateTenant call.
systemSettingSvc interfaces.SystemSettingService
@@ -228,6 +228,17 @@ func (h *TenantHandler) CreateTenant(c *gin.Context) {
return
}
// Deployment-level policy: ordinary users may be restricted to joining
// existing workspaces by invitation. This check is authoritative; the
// frontend capability only improves UX and cannot bypass it. Cross-tenant
// superusers retain the catalog-management create path.
if !caller.CanAccessAllTenants &&
!resolveTenantSelfServiceCreationEnabled(ctx, h.config, h.systemSettingSvc) {
logger.Warnf(ctx, "Self-service tenant creation denied by policy for user %s", caller.ID)
c.Error(errors.NewTenantCreationDisabledError())
return
}
var tenantData types.Tenant
if caller.CanAccessAllTenants {
@@ -402,6 +413,23 @@ func (h *TenantHandler) CreateTenant(c *gin.Context) {
}
}
// When a tenantless user creates their first workspace, make it their
// default login tenant. Roll the just-created resources back if this
// finalisation fails so the user is not left with an unreachable tenant.
if caller.TenantID == 0 {
caller.TenantID = createdTenant.ID
if err := h.userService.UpdateUser(ctx, caller); err != nil {
logger.Errorf(ctx, "Failed to set first tenant %d as default for user %s: %v",
createdTenant.ID, caller.ID, err)
if h.memberService != nil {
_ = h.memberService.RemoveMember(ctx, caller.ID, createdTenant.ID)
}
_ = h.service.DeleteTenant(ctx, createdTenant.ID)
c.Error(errors.NewInternalServerError("Failed to finalise default tenant").WithDetails(err.Error()))
return
}
}
logger.Infof(
ctx,
"Tenant created successfully, ID: %d, name: %s",