feat(tenant): 按租户隔离用户身份与后台权限
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"hfb_sys/backend/internal/logging"
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/modules/invitation"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
"hfb_sys/backend/pkg/crypto"
|
||||
|
||||
"go.uber.org/zap"
|
||||
@@ -32,7 +33,11 @@ func NewRepository(db *gorm.DB, encryptor crypto.Encryptor, idHashKey string) *R
|
||||
|
||||
func (r *Repository) FindByUserID(ctx context.Context, userID uint64) (*model.UserRealname, error) {
|
||||
var record model.UserRealname
|
||||
if err := r.db.WithContext(ctx).Where("user_id = ?", userID).First(&record).Error; err != nil {
|
||||
query := r.db.WithContext(ctx).Where("user_id = ?", userID)
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
query = query.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
if err := query.First(&record).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &record, nil
|
||||
@@ -41,8 +46,11 @@ func (r *Repository) FindByUserID(ctx context.Context, userID uint64) (*model.Us
|
||||
// IsVerifiedUser 判断当前账号是否已经完成实名认证。
|
||||
func (r *Repository) IsVerifiedUser(ctx context.Context, userID uint64) (bool, error) {
|
||||
var userStatus string
|
||||
if err := r.db.WithContext(ctx).Model(&model.User{}).
|
||||
Where("id = ?", userID).Pluck("realname_status", &userStatus).Error; err != nil {
|
||||
userQuery := r.db.WithContext(ctx).Model(&model.User{}).Where("id = ?", userID)
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
userQuery = userQuery.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
if err := userQuery.Pluck("realname_status", &userStatus).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return false, nil
|
||||
}
|
||||
@@ -52,9 +60,12 @@ func (r *Repository) IsVerifiedUser(ctx context.Context, userID uint64) (bool, e
|
||||
return true, nil
|
||||
}
|
||||
var count int64
|
||||
err := r.db.WithContext(ctx).Model(&model.UserRealname{}).
|
||||
Where("user_id = ? AND status = ?", userID, StatusVerified).
|
||||
Count(&count).Error
|
||||
realnameQuery := r.db.WithContext(ctx).Model(&model.UserRealname{}).
|
||||
Where("user_id = ? AND status = ?", userID, StatusVerified)
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
realnameQuery = realnameQuery.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
err := realnameQuery.Count(&count).Error
|
||||
return count > 0, err
|
||||
}
|
||||
|
||||
@@ -67,11 +78,15 @@ func (r *Repository) ExistsVerifiedIdentity(ctx context.Context, userID uint64,
|
||||
return false, nil
|
||||
}
|
||||
idNoHash := FingerprintIDNo(r.idHashKey, idNo)
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
|
||||
var count int64
|
||||
if err := r.db.WithContext(ctx).Model(&model.UserRealname{}).
|
||||
Where("status = ? AND id_no_hash = ? AND user_id <> ?", StatusVerified, idNoHash, userID).
|
||||
Count(&count).Error; err != nil {
|
||||
query := r.db.WithContext(ctx).Model(&model.UserRealname{}).
|
||||
Where("status = ? AND id_no_hash = ? AND user_id <> ?", StatusVerified, idNoHash, userID)
|
||||
if tenantID > 0 {
|
||||
query = query.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
if err := query.Count(&count).Error; err != nil {
|
||||
return false, err
|
||||
}
|
||||
if count > 0 {
|
||||
@@ -80,9 +95,12 @@ func (r *Repository) ExistsVerifiedIdentity(ctx context.Context, userID uint64,
|
||||
|
||||
masked := maskIDNo(idNo)
|
||||
var candidates []model.UserRealname
|
||||
if err := r.db.WithContext(ctx).
|
||||
Where("status = ? AND user_id <> ? AND masked_id_no = ?", StatusVerified, userID, masked).
|
||||
Find(&candidates).Error; err != nil {
|
||||
query = r.db.WithContext(ctx).
|
||||
Where("status = ? AND user_id <> ? AND masked_id_no = ?", StatusVerified, userID, masked)
|
||||
if tenantID > 0 {
|
||||
query = query.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
if err := query.Find(&candidates).Error; err != nil {
|
||||
return false, err
|
||||
}
|
||||
for _, candidate := range candidates {
|
||||
@@ -121,7 +139,9 @@ func (r *Repository) backfillIDNoHash(ctx context.Context, recordID uint64, idNo
|
||||
|
||||
// SaveResult 保存实名结果并更新用户实名状态。idNoHash 仅认证通过时写入。
|
||||
func (r *Repository) SaveResult(ctx context.Context, userID uint64, provider string, result ProviderResult, idNoHash string) (*model.UserRealname, error) {
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
record := model.UserRealname{
|
||||
TenantID: tenantID,
|
||||
UserID: userID,
|
||||
Provider: provider,
|
||||
ProviderOrderNo: result.ProviderOrderNo,
|
||||
@@ -140,8 +160,11 @@ func (r *Repository) SaveResult(ctx context.Context, userID uint64, provider str
|
||||
|
||||
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
var existing model.UserRealname
|
||||
findErr := tx.Clauses(clause.Locking{Strength: "UPDATE"}).
|
||||
Where("user_id = ?", userID).First(&existing).Error
|
||||
findQuery := tx.Clauses(clause.Locking{Strength: "UPDATE"}).Where("user_id = ?", userID)
|
||||
if tenantID > 0 {
|
||||
findQuery = findQuery.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
findErr := findQuery.First(&existing).Error
|
||||
switch {
|
||||
case findErr == nil:
|
||||
existing.Provider = record.Provider
|
||||
@@ -178,7 +201,11 @@ func (r *Repository) SaveResult(ctx context.Context, userID uint64, provider str
|
||||
if result.Status == StatusPending {
|
||||
userStatus = "pending"
|
||||
}
|
||||
if err := tx.Model(&model.User{}).Where("id = ?", userID).Update("realname_status", userStatus).Error; err != nil {
|
||||
userQuery := tx.Model(&model.User{}).Where("id = ?", userID)
|
||||
if tenantID > 0 {
|
||||
userQuery = userQuery.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
if err := userQuery.Update("realname_status", userStatus).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if userStatus == "verified" {
|
||||
@@ -209,7 +236,7 @@ func isDuplicateIDNoError(err error) bool {
|
||||
return true
|
||||
}
|
||||
text := strings.ToLower(err.Error())
|
||||
return strings.Contains(text, "1062") && strings.Contains(text, "uk_user_realname_id_no_hash")
|
||||
return strings.Contains(text, "1062") && strings.Contains(text, "uk_user_realname_tenant_id_no")
|
||||
}
|
||||
|
||||
// IsIDNoDuplicateError 供其他模块复用身份证唯一键冲突判断。
|
||||
|
||||
Reference in New Issue
Block a user