forked from baron/baron-sso
custom claim 타입보정 UI. 대표테넌트 노출 보정
This commit is contained in:
@@ -14,7 +14,9 @@ import (
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -51,6 +53,10 @@ func SeedTenants(db *gorm.DB) error {
|
||||
return errors.New("seed tenant csv has no tenant rows")
|
||||
}
|
||||
|
||||
if err := syncExistingSeedTenantConfigs(db, configs); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
existingSlugs, existingIDs, err := loadExistingTenantIdentitySet(db)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -71,6 +77,69 @@ func SeedTenants(db *gorm.DB) error {
|
||||
return seedTenantConfigs(db, missingConfigs)
|
||||
}
|
||||
|
||||
func syncExistingSeedTenantConfigs(db *gorm.DB, configs []InitialTenantConfig) error {
|
||||
for _, config := range configs {
|
||||
id := strings.TrimSpace(config.TenantID)
|
||||
if id == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
targetSlug := strings.TrimSpace(strings.ToLower(config.Slug))
|
||||
if targetSlug == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
if err := db.Transaction(func(tx *gorm.DB) error {
|
||||
var tenant domain.Tenant
|
||||
if err := tx.First(&tenant, "id = ?", id).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("load existing seed tenant %q: %w", id, err)
|
||||
}
|
||||
|
||||
if strings.TrimSpace(strings.ToLower(tenant.Slug)) == targetSlug {
|
||||
return nil
|
||||
}
|
||||
|
||||
var conflict domain.Tenant
|
||||
err := tx.Select("id").
|
||||
Where("LOWER(TRIM(slug)) = ? AND id <> ?", targetSlug, tenant.ID).
|
||||
First(&conflict).Error
|
||||
if err == nil {
|
||||
return fmt.Errorf("seed tenant slug %q for id %q conflicts with tenant id %q", targetSlug, id, conflict.ID)
|
||||
}
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return fmt.Errorf("check seed tenant slug conflict %q: %w", targetSlug, err)
|
||||
}
|
||||
|
||||
suffix := "-deleted-" + strconv.FormatInt(time.Now().UTC().UnixNano(), 10)
|
||||
if err := tx.Unscoped().
|
||||
Model(&domain.Tenant{}).
|
||||
Where("slug = ? AND id <> ? AND deleted_at IS NOT NULL", targetSlug, tenant.ID).
|
||||
Update("slug", gorm.Expr("slug || ?", suffix)).Error; err != nil {
|
||||
return fmt.Errorf("rename deleted tenant slug %q before seed repair: %w", targetSlug, err)
|
||||
}
|
||||
|
||||
slog.Info(
|
||||
"[Bootstrap] Repairing existing seed tenant slug",
|
||||
"id", tenant.ID,
|
||||
"oldSlug", tenant.Slug,
|
||||
"newSlug", targetSlug,
|
||||
)
|
||||
if err := tx.Model(&domain.Tenant{}).
|
||||
Where("id = ?", tenant.ID).
|
||||
Update("slug", targetSlug).Error; err != nil {
|
||||
return fmt.Errorf("repair seed tenant slug %q for id %q: %w", targetSlug, id, err)
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func loadExistingTenantIdentitySet(db *gorm.DB) (map[string]bool, map[string]bool, error) {
|
||||
var tenants []domain.Tenant
|
||||
if err := db.Select("id", "slug").Find(&tenants).Error; err != nil {
|
||||
|
||||
Reference in New Issue
Block a user