feat(sms): 支持双站点独立配置登录短信
This commit is contained in:
@@ -0,0 +1,172 @@
|
||||
package tenantsms
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"hfb_sys/backend/internal/auditlog"
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
"hfb_sys/backend/pkg/crypto"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
type Repository struct {
|
||||
db *gorm.DB
|
||||
encryptor crypto.Encryptor
|
||||
}
|
||||
|
||||
func NewRepository(db *gorm.DB, encryptor crypto.Encryptor) *Repository {
|
||||
return &Repository{db: db, encryptor: encryptor}
|
||||
}
|
||||
|
||||
func (r *Repository) Get(ctx context.Context) (Config, error) {
|
||||
if r == nil || r.db == nil || r.encryptor == nil {
|
||||
return Config{}, errors.New("tenant sms database unavailable")
|
||||
}
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
if tenantID == 0 {
|
||||
return Config{}, ErrInvalidConfig
|
||||
}
|
||||
var row model.TenantSMSConfig
|
||||
err := r.db.WithContext(ctx).Where("tenant_id = ?", tenantID).First(&row).Error
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return DefaultConfig(), nil
|
||||
}
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
return r.decryptRow(row)
|
||||
}
|
||||
|
||||
func (r *Repository) GetDTO(ctx context.Context) (DTO, error) {
|
||||
cfg, err := r.Get(ctx)
|
||||
if err != nil {
|
||||
return DTO{}, err
|
||||
}
|
||||
return cfg.DTO(), nil
|
||||
}
|
||||
|
||||
func (r *Repository) Update(ctx context.Context, actorID uint64, req UpdateRequest, meta auditlog.Meta) (DTO, error) {
|
||||
if r == nil || r.db == nil || r.encryptor == nil {
|
||||
return DTO{}, errors.New("tenant sms dependencies unavailable")
|
||||
}
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
if tenantID == 0 || actorID == 0 {
|
||||
return DTO{}, ErrInvalidConfig
|
||||
}
|
||||
|
||||
var result Config
|
||||
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
var row model.TenantSMSConfig
|
||||
err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).
|
||||
Where("tenant_id = ?", tenantID).
|
||||
First(&row).Error
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return err
|
||||
}
|
||||
|
||||
cfg := DefaultConfig()
|
||||
if err == nil {
|
||||
decrypted, decryptErr := r.decryptRow(row)
|
||||
if decryptErr != nil {
|
||||
return decryptErr
|
||||
}
|
||||
cfg = decrypted
|
||||
}
|
||||
cfg = applyUpdate(cfg, req)
|
||||
normalized, normalizeErr := normalizeConfig(cfg)
|
||||
if normalizeErr != nil {
|
||||
return normalizeErr
|
||||
}
|
||||
encrypted, encryptErr := r.encryptor.Encrypt(normalized.AccessKeySecret)
|
||||
if encryptErr != nil {
|
||||
return fmt.Errorf("encrypt tenant sms secret: %w", encryptErr)
|
||||
}
|
||||
|
||||
row.TenantID = tenantID
|
||||
row.Enabled = normalized.Enabled
|
||||
row.Provider = normalized.Provider
|
||||
row.AccessKeyID = normalized.AccessKeyID
|
||||
row.AccessKeySecret = encrypted
|
||||
row.Endpoint = normalized.Endpoint
|
||||
row.SignName = normalized.SignName
|
||||
row.LoginTemplateCode = normalized.LoginTemplateCode
|
||||
row.UpdatedBy = &actorID
|
||||
if row.ID == 0 {
|
||||
if createErr := tx.Create(&row).Error; createErr != nil {
|
||||
return createErr
|
||||
}
|
||||
} else if saveErr := tx.Save(&row).Error; saveErr != nil {
|
||||
return saveErr
|
||||
}
|
||||
|
||||
result = normalized
|
||||
return auditlog.Append(tx, auditlog.Entry{
|
||||
ActorType: "admin",
|
||||
ActorID: actorID,
|
||||
Action: "tenant_sms_config.update",
|
||||
BizType: "tenant_sms_config",
|
||||
BizID: &row.ID,
|
||||
Meta: meta,
|
||||
Detail: map[string]any{
|
||||
"tenant_id": tenantID,
|
||||
"enabled": normalized.Enabled,
|
||||
"provider": normalized.Provider,
|
||||
"access_key_id": normalized.AccessKeyID,
|
||||
"has_secret": normalized.AccessKeySecret != "",
|
||||
"endpoint": normalized.Endpoint,
|
||||
"sign_name": normalized.SignName,
|
||||
"login_template_code": normalized.LoginTemplateCode,
|
||||
},
|
||||
})
|
||||
})
|
||||
if err != nil {
|
||||
return DTO{}, err
|
||||
}
|
||||
return result.DTO(), nil
|
||||
}
|
||||
|
||||
func (r *Repository) decryptRow(row model.TenantSMSConfig) (Config, error) {
|
||||
secret, err := r.encryptor.Decrypt(row.AccessKeySecret)
|
||||
if err != nil {
|
||||
return Config{}, fmt.Errorf("decrypt tenant sms secret: %w", err)
|
||||
}
|
||||
return Config{
|
||||
Enabled: row.Enabled,
|
||||
Provider: row.Provider,
|
||||
AccessKeyID: row.AccessKeyID,
|
||||
AccessKeySecret: secret,
|
||||
Endpoint: row.Endpoint,
|
||||
SignName: row.SignName,
|
||||
LoginTemplateCode: row.LoginTemplateCode,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func applyUpdate(cfg Config, req UpdateRequest) Config {
|
||||
if req.Enabled != nil {
|
||||
cfg.Enabled = *req.Enabled
|
||||
}
|
||||
if req.Provider != nil {
|
||||
cfg.Provider = *req.Provider
|
||||
}
|
||||
if req.AccessKeyID != nil {
|
||||
cfg.AccessKeyID = *req.AccessKeyID
|
||||
}
|
||||
if req.AccessKeySecret != nil && *req.AccessKeySecret != "" {
|
||||
cfg.AccessKeySecret = *req.AccessKeySecret
|
||||
}
|
||||
if req.Endpoint != nil {
|
||||
cfg.Endpoint = *req.Endpoint
|
||||
}
|
||||
if req.SignName != nil {
|
||||
cfg.SignName = *req.SignName
|
||||
}
|
||||
if req.LoginTemplateCode != nil {
|
||||
cfg.LoginTemplateCode = *req.LoginTemplateCode
|
||||
}
|
||||
return cfg
|
||||
}
|
||||
Reference in New Issue
Block a user