feat(tenant): 按租户隔离用户身份与后台权限
This commit is contained in:
@@ -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, " ", "")
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
package paymentaccount
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"hfb_sys/backend/internal/database"
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
)
|
||||
|
||||
func paymentTenantContext(tenantID uint64) context.Context {
|
||||
return tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: tenantID})
|
||||
}
|
||||
|
||||
func TestPaymentAccountsAreScopedByTenant(t *testing.T) {
|
||||
db := database.NewTestDB()
|
||||
dachuiUser := model.User{TenantID: 101, Phone: "13500000001", Status: "active"}
|
||||
jieyouUser := model.User{TenantID: 202, Phone: "13500000001", Status: "active"}
|
||||
if err := db.Create(&dachuiUser).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Create(&jieyouUser).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
dachuiAccount := model.UserPaymentAccount{
|
||||
TenantID: 101, UserID: dachuiUser.ID, AccountType: "bank",
|
||||
AccountName: "张三", AccountNo: "encrypted-dachui", Status: "active",
|
||||
}
|
||||
jieyouAccount := model.UserPaymentAccount{
|
||||
TenantID: 202, UserID: jieyouUser.ID, AccountType: "bank",
|
||||
AccountName: "张三", AccountNo: "encrypted-jieyou", Status: "active",
|
||||
}
|
||||
if err := db.Create(&dachuiAccount).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Create(&jieyouAccount).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
repo := NewRepository(db, nil)
|
||||
page, err := repo.List(paymentTenantContext(101), dachuiUser.ID, 1, 10)
|
||||
if err != nil {
|
||||
t.Fatalf("list dachui accounts: %v", err)
|
||||
}
|
||||
if page.Total != 1 || len(page.Items) != 1 || page.Items[0].ID != dachuiAccount.ID {
|
||||
t.Fatalf("dachui payment accounts = %+v", page)
|
||||
}
|
||||
if _, err := repo.FindByID(paymentTenantContext(202), jieyouUser.ID, dachuiAccount.ID); !errors.Is(err, ErrAccountNotFound) {
|
||||
t.Fatalf("cross-tenant account lookup error = %v, want ErrAccountNotFound", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPaymentAccountCreateWritesTenant(t *testing.T) {
|
||||
db := database.NewTestDB()
|
||||
user := model.User{TenantID: 101, Phone: "13500000002", Status: "active"}
|
||||
if err := db.Create(&user).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
repo := NewRepository(db, nil)
|
||||
account, err := repo.Create(paymentTenantContext(101), user.ID, CreatePaymentAccountRequest{
|
||||
AccountType: "bank",
|
||||
AccountName: "李四",
|
||||
AccountNo: "6222020000000000",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("create payment account: %v", err)
|
||||
}
|
||||
var stored model.UserPaymentAccount
|
||||
if err := db.First(&stored, account.ID).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if stored.TenantID != 101 {
|
||||
t.Fatalf("stored tenant_id = %d, want 101", stored.TenantID)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user