feat(tenant): 按租户隔离用户身份与后台权限

This commit is contained in:
yml
2026-11-15 15:37:23 +08:00
parent ca57f604bf
commit abf0fce89b
43 changed files with 1572 additions and 196 deletions
@@ -8,6 +8,7 @@ import (
"strings"
"hfb_sys/backend/internal/model"
"hfb_sys/backend/internal/tenancy"
"hfb_sys/backend/pkg/crypto"
"gorm.io/datatypes"
@@ -28,16 +29,22 @@ func NewRepository(db *gorm.DB, encryptor crypto.Encryptor) *Repository {
func (r *Repository) List(ctx context.Context, userID uint64, page, pageSize int) (*PaginatedResult, error) {
db := r.db.WithContext(ctx)
tenantID := tenancy.IDFromContext(ctx)
baseQuery := func() *gorm.DB {
query := db.Model(&model.UserPaymentAccount{}).Where("user_id = ? AND status = ?", userID, "active")
if tenantID > 0 {
query = query.Where("tenant_id = ?", tenantID)
}
return query
}
var total int64
if err := db.Model(&model.UserPaymentAccount{}).
Where("user_id = ? AND status = ?", userID, "active").
Count(&total).Error; err != nil {
if err := baseQuery().Count(&total).Error; err != nil {
return nil, err
}
offset := (page - 1) * pageSize
var accounts []model.UserPaymentAccount
if err := db.Where("user_id = ? AND status = ?", userID, "active").
if err := baseQuery().
Order("is_default DESC, created_at DESC").
Offset(offset).Limit(pageSize).
Find(&accounts).Error; err != nil {
@@ -63,8 +70,8 @@ func (r *Repository) List(ctx context.Context, userID uint64, page, pageSize int
func (r *Repository) FindByID(ctx context.Context, userID, id uint64) (*PaymentAccountDTO, error) {
var account model.UserPaymentAccount
if err := r.db.WithContext(ctx).Where("id = ? AND user_id = ? AND status = ?", id, userID, "active").
First(&account).Error; err != nil {
query := r.accountQuery(ctx, userID).Where("id = ?", id)
if err := query.First(&account).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, ErrAccountNotFound
}
@@ -75,6 +82,7 @@ func (r *Repository) FindByID(ctx context.Context, userID, id uint64) (*PaymentA
func (r *Repository) Create(ctx context.Context, userID uint64, req CreatePaymentAccountRequest) (*PaymentAccountDTO, error) {
db := r.db.WithContext(ctx)
tenantID := tenancy.IDFromContext(ctx)
if err := r.validateCertificateURLs(ctx, userID, req.CertificateURLs, nil); err != nil {
return nil, err
}
@@ -93,12 +101,15 @@ func (r *Repository) Create(ctx context.Context, userID uint64, req CreatePaymen
// 如果是第一个账号,自动设为默认
isDefault := false
var count int64
db.Model(&model.UserPaymentAccount{}).Where("user_id = ? AND status = ?", userID, "active").Count(&count)
if err := r.accountQuery(ctx, userID).Count(&count).Error; err != nil {
return nil, err
}
if count == 0 {
isDefault = true
}
account := model.UserPaymentAccount{
TenantID: tenantID,
UserID: userID,
AccountType: req.AccountType,
AccountName: req.AccountName,
@@ -119,8 +130,9 @@ func (r *Repository) Create(ctx context.Context, userID uint64, req CreatePaymen
func (r *Repository) Update(ctx context.Context, userID, id uint64, req UpdatePaymentAccountRequest) (*PaymentAccountDTO, error) {
db := r.db.WithContext(ctx)
tenantID := tenancy.IDFromContext(ctx)
var account model.UserPaymentAccount
if err := db.Where("id = ? AND user_id = ? AND status = ?", id, userID, "active").
if err := r.accountQuery(ctx, userID).Where("id = ?", id).
First(&account).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, ErrAccountNotFound
@@ -146,9 +158,13 @@ func (r *Repository) Update(ctx context.Context, userID, id uint64, req UpdatePa
if req.IsDefault != nil && *req.IsDefault {
// 先取消其他默认账号
db.Model(&model.UserPaymentAccount{}).
Where("user_id = ? AND id != ?", userID, id).
Update("is_default", false)
query := db.Model(&model.UserPaymentAccount{}).Where("user_id = ? AND id != ?", userID, id)
if tenantID > 0 {
query = query.Where("tenant_id = ?", tenantID)
}
if err := query.Update("is_default", false).Error; err != nil {
return nil, err
}
updates["is_default"] = true
}
@@ -212,7 +228,7 @@ func paymentCertificateKey(value string) (string, bool) {
func (r *Repository) Delete(ctx context.Context, userID, id uint64) error {
db := r.db.WithContext(ctx)
var account model.UserPaymentAccount
if err := db.Where("id = ? AND user_id = ? AND status = ?", id, userID, "active").
if err := r.accountQuery(ctx, userID).Where("id = ?", id).
First(&account).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return ErrAccountNotFound
@@ -227,7 +243,7 @@ func (r *Repository) Delete(ctx context.Context, userID, id uint64) error {
func (r *Repository) SetDefault(ctx context.Context, userID, id uint64) error {
// 验证账号存在
var account model.UserPaymentAccount
if err := r.db.WithContext(ctx).Where("id = ? AND user_id = ? AND status = ?", id, userID, "active").
if err := r.accountQuery(ctx, userID).Where("id = ?", id).
First(&account).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return ErrAccountNotFound
@@ -236,10 +252,14 @@ func (r *Repository) SetDefault(ctx context.Context, userID, id uint64) error {
}
return r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
tenantID := tenancy.IDFromContext(ctx)
// 取消其他默认账号
if err := tx.Model(&model.UserPaymentAccount{}).
Where("user_id = ? AND id != ?", userID, id).
Update("is_default", false).Error; err != nil {
query := tx.Model(&model.UserPaymentAccount{}).
Where("user_id = ? AND id != ?", userID, id)
if tenantID > 0 {
query = query.Where("tenant_id = ?", tenantID)
}
if err := query.Update("is_default", false).Error; err != nil {
return err
}
// 设置当前为默认
@@ -249,8 +269,7 @@ func (r *Repository) SetDefault(ctx context.Context, userID, id uint64) error {
func (r *Repository) CountByUser(ctx context.Context, userID uint64) (int64, error) {
var count int64
err := r.db.WithContext(ctx).Model(&model.UserPaymentAccount{}).
Where("user_id = ? AND status = ?", userID, "active").
err := r.accountQuery(ctx, userID).
Count(&count).Error
return count, err
}
@@ -258,7 +277,12 @@ func (r *Repository) CountByUser(ctx context.Context, userID uint64) (int64, err
func (r *Repository) ValidateRealname(ctx context.Context, userID uint64, accountName string) error {
var user model.User
db := r.db.WithContext(ctx)
if err := db.First(&user, userID).Error; err != nil {
tenantID := tenancy.IDFromContext(ctx)
userQuery := db.Where("id = ?", userID)
if tenantID > 0 {
userQuery = userQuery.Where("tenant_id = ?", tenantID)
}
if err := userQuery.First(&user).Error; err != nil {
return err
}
@@ -268,7 +292,11 @@ func (r *Repository) ValidateRealname(ctx context.Context, userID uint64, accoun
// 获取实名信息
var realname model.UserRealname
if err := db.Where("user_id = ? AND status = ?", userID, "verified").
realnameQuery := db.Where("user_id = ? AND status = ?", userID, "verified")
if tenantID > 0 {
realnameQuery = realnameQuery.Where("tenant_id = ?", tenantID)
}
if err := realnameQuery.
First(&realname).Error; err != nil {
return ErrRealnameRequired
}
@@ -293,6 +321,15 @@ func (r *Repository) ValidateRealname(ctx context.Context, userID uint64, accoun
return nil
}
func (r *Repository) accountQuery(ctx context.Context, userID uint64) *gorm.DB {
query := r.db.WithContext(ctx).Model(&model.UserPaymentAccount{}).
Where("user_id = ? AND status = ?", userID, "active")
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
query = query.Where("tenant_id = ?", tenantID)
}
return query
}
// 使用脱敏姓名进行前缀匹配(兼容旧数据)
func (r *Repository) validateByMaskedName(maskedName, accountName string) error {
maskedName = strings.ReplaceAll(maskedName, " ", "")