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:
@@ -31,6 +31,14 @@ func NewUserRepository(db *gorm.DB) interfaces.UserRepository {
|
||||
|
||||
// CreateUser creates a user
|
||||
func (r *userRepository) CreateUser(ctx context.Context, user *types.User) error {
|
||||
// users.tenant_id is nullable in both PostgreSQL and SQLite. GORM would
|
||||
// otherwise serialise the uint64 zero value as 0, which violates the
|
||||
// PostgreSQL FK and loses the distinction between "not provisioned yet"
|
||||
// and a real tenant. Omitting the column stores SQL NULL; reads hydrate it
|
||||
// back as zero, the domain sentinel used by tenantless auth flows.
|
||||
if user != nil && user.TenantID == 0 {
|
||||
return r.db.WithContext(ctx).Omit("tenant_id").Create(user).Error
|
||||
}
|
||||
return r.db.WithContext(ctx).Create(user).Error
|
||||
}
|
||||
|
||||
@@ -104,6 +112,19 @@ func (r *userRepository) GetUserByTenantID(ctx context.Context, tenantID uint64)
|
||||
|
||||
// UpdateUser updates a user
|
||||
func (r *userRepository) UpdateUser(ctx context.Context, user *types.User) error {
|
||||
if user != nil && user.TenantID == 0 {
|
||||
return r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
// Preserve Save's all-fields behaviour while keeping the nullable
|
||||
// tenant column out of the struct write, then explicitly store NULL.
|
||||
// Writing uint64(0) would violate the PostgreSQL tenant FK.
|
||||
if err := tx.Omit("tenant_id").Save(user).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Model(&types.User{}).
|
||||
Where("id = ?", user.ID).
|
||||
UpdateColumn("tenant_id", nil).Error
|
||||
})
|
||||
}
|
||||
return r.db.WithContext(ctx).Save(user).Error
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func TestUserRepositoryTenantlessCreateAndUpdateKeepNullTenantID(t *testing.T) {
|
||||
db, err := gorm.Open(sqlite.Open("file:user_tenantless?mode=memory&cache=shared"), &gorm.Config{})
|
||||
if err != nil {
|
||||
t.Fatalf("open sqlite: %v", err)
|
||||
}
|
||||
if err := db.AutoMigrate(&types.Tenant{}, &types.User{}); err != nil {
|
||||
t.Fatalf("migrate: %v", err)
|
||||
}
|
||||
|
||||
repo := NewUserRepository(db)
|
||||
user := &types.User{
|
||||
ID: "tenantless-user",
|
||||
Username: "before-update",
|
||||
Email: "tenantless@example.com",
|
||||
PasswordHash: "hashed",
|
||||
TenantID: 0,
|
||||
IsActive: true,
|
||||
}
|
||||
if err := repo.CreateUser(context.Background(), user); err != nil {
|
||||
t.Fatalf("CreateUser: %v", err)
|
||||
}
|
||||
assertNullTenantID(t, db, user.ID)
|
||||
|
||||
user.Username = "after-update"
|
||||
if err := repo.UpdateUser(context.Background(), user); err != nil {
|
||||
t.Fatalf("UpdateUser: %v", err)
|
||||
}
|
||||
assertNullTenantID(t, db, user.ID)
|
||||
|
||||
var stored types.User
|
||||
if err := db.First(&stored, "id = ?", user.ID).Error; err != nil {
|
||||
t.Fatalf("reload user: %v", err)
|
||||
}
|
||||
if stored.Username != "after-update" || stored.TenantID != 0 {
|
||||
t.Fatalf("stored user = %#v", stored)
|
||||
}
|
||||
}
|
||||
|
||||
func assertNullTenantID(t *testing.T, db *gorm.DB, userID string) {
|
||||
t.Helper()
|
||||
var count int64
|
||||
if err := db.Table("users").Where("id = ? AND tenant_id IS NULL", userID).Count(&count).Error; err != nil {
|
||||
t.Fatalf("check tenant_id: %v", err)
|
||||
}
|
||||
if count != 1 {
|
||||
t.Fatalf("tenant_id for user %s is not NULL", userID)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user