批量优化

This commit is contained in:
hero920103 committed 2026-10-08 15:30:37 +08:00
1 parent 438145cae7
commit c925b198d1
50 files changed
+5716 -291

No files matched your search

+49 -8
View File
@@ -1,6 +1,7 @@
package auth
import (
"errors"
"strings"
"github.com/google/uuid"
@@ -11,16 +12,22 @@ import (
// SyncTenantUserInput 老表 → 认证中心同步入参
type SyncTenantUserInput struct {
Tid uint64
Account string // 企业内账号
Name string
Phone string
Email string
PasswordHash string // 已哈希的密码;为空表示不修改密码
Tid uint64
Account string // 企业内账号
Name string
Phone string
Email string
// PasswordHash 已哈希的密码;为空表示不修改密码。
// 注意:身份已存在时,只有 UpdatePassword=true 才会生效(改密场景),
// 否则保持不动 —— 避免 B 企业建同名用户时把 A 企业的密码静默覆盖掉。
PasswordHash string
GroupID uint64
OrgID uint64
Status int8
IsDefault int8
// UpdatePassword 身份已存在时是否允许更新密码(仅改密场景传 true)
UpdatePassword bool
}
// SyncTenantUser 把租户用户同步到统一认证中心(幂等,可重复调用)。
@@ -69,7 +76,7 @@ func RemoveTenantUser(tid uint64, account, phone, email string) error {
// findOrCreateIdentity 按归并键查找身份,不存在则创建
func findOrCreateIdentity(in SyncTenantUserInput) (*models.AuthIdentity, error) {
if m := findIdentityByKey(in.Account, in.Phone, in.Email); m != nil {
// 补全手机/邮箱/昵称;密码仅在显式传入时更新
// 补全手机/邮箱/昵称;密码仅在显式授权(改密场景)时更新
update := map[string]interface{}{}
if in.Phone != "" {
update["mobile"] = in.Phone
@@ -83,7 +90,7 @@ func findOrCreateIdentity(in SyncTenantUserInput) (*models.AuthIdentity, error)
if in.Status > 0 {
update["status"] = in.Status
}
if in.PasswordHash != "" {
if in.UpdatePassword && in.PasswordHash != "" {
update["password_hash"] = in.PasswordHash
update["password_algo"] = passwordutil.AlgoOf(in.PasswordHash)
}
@@ -230,3 +237,37 @@ func upsertTenantBinding(identity *models.AuthIdentity, in SyncTenantUserInput)
Update(update)
return err
}
// LocateIdentityByKey 按归并键(手机 > 邮箱 > 账号)查找已有身份。
// 第二个返回值表示是否命中。供「新增租户用户」流程判断:
// 命中则要求先验证该身份的登录密码,验证通过后只建企业绑定。
func LocateIdentityByKey(account, phone, email string) (*models.AuthIdentity, bool) {
m := findIdentityByKey(account, phone, email)
return m, m != nil
}
// GetIdentityByID 按主键读取认证身份
func GetIdentityByID(id uint64) (*models.AuthIdentity, error) {
var m models.AuthIdentity
if err := models.Orm.QueryTable(new(models.AuthIdentity)).Filter("id", id).One(&m); err != nil {
return nil, err
}
return &m, nil
}
// VerifyIdentityPassword 校验身份的登录密码(兼容 legacy 哈希算法)
func VerifyIdentityPassword(identity *models.AuthIdentity, plain string) bool {
if identity == nil || identity.PasswordHash == nil || *identity.PasswordHash == "" {
return false
}
return passwordutil.Verify(*identity.PasswordHash, plain)
}
// BindIdentityToTenant 把已有身份绑定到企业(只写绑定层,不改身份资料与密码)。
// 供「验证密码后绑定到新企业」流程使用,绑定层的企业内账号/姓名/联系方式独立维护。
func BindIdentityToTenant(identity *models.AuthIdentity, in SyncTenantUserInput) error {
if identity == nil {
return errors.New("身份不存在")
}
return upsertTenantBinding(identity, in)
}