feat(tenant): 按租户隔离用户身份与后台权限
This commit is contained in:
@@ -9,12 +9,17 @@ import (
|
||||
"hfb_sys/backend/internal/database"
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/modules/wallet"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
"hfb_sys/backend/pkg/crypto"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
func withdrawalTenantContext(tenantID uint64) context.Context {
|
||||
return tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: tenantID})
|
||||
}
|
||||
|
||||
type withdrawalFixture struct {
|
||||
db *gorm.DB
|
||||
repo *Repository
|
||||
@@ -405,3 +410,72 @@ func TestWithdrawalDetailIncludesDistributionSiteInfo(t *testing.T) {
|
||||
t.Fatalf("unexpected detail site info: %+v", detail)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithdrawalRequestsAreScopedByTenant(t *testing.T) {
|
||||
db := database.NewTestDB()
|
||||
dachuiUser := model.User{TenantID: 101, Phone: "13300000001", Status: "active"}
|
||||
jieyouUser := model.User{TenantID: 202, Phone: "13300000001", 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", Status: "active",
|
||||
}
|
||||
jieyouAccount := model.UserPaymentAccount{
|
||||
TenantID: 202, UserID: jieyouUser.ID, AccountType: "bank",
|
||||
AccountName: "张三", AccountNo: "encrypted", Status: "active",
|
||||
}
|
||||
if err := db.Create(&dachuiAccount).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Create(&jieyouAccount).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, account := range []model.WalletAccount{
|
||||
{TenantID: 101, UserID: dachuiUser.ID, AvailableBalanceCent: 10_000, Status: "active"},
|
||||
{TenantID: 202, UserID: jieyouUser.ID, AvailableBalanceCent: 10_000, Status: "active"},
|
||||
} {
|
||||
if err := db.Create(&account).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
repo := NewRepository(db, wallet.NewRepository(db), &crypto.MockEncryptor{})
|
||||
dachuiCtx := withdrawalTenantContext(101)
|
||||
jieyouCtx := withdrawalTenantContext(202)
|
||||
dachuiWithdrawal, err := repo.Create(dachuiCtx, dachuiUser.ID, CreateWithdrawalRequest{
|
||||
PaymentAccountID: dachuiAccount.ID,
|
||||
AmountCent: 1000,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("create dachui withdrawal: %v", err)
|
||||
}
|
||||
if _, err := repo.Create(jieyouCtx, jieyouUser.ID, CreateWithdrawalRequest{
|
||||
PaymentAccountID: jieyouAccount.ID,
|
||||
AmountCent: 2000,
|
||||
}); err != nil {
|
||||
t.Fatalf("create jieyou withdrawal: %v", err)
|
||||
}
|
||||
|
||||
var stored model.WithdrawalRequest
|
||||
if err := db.First(&stored, dachuiWithdrawal.ID).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if stored.TenantID != 101 {
|
||||
t.Fatalf("withdrawal tenant_id = %d, want 101", stored.TenantID)
|
||||
}
|
||||
if _, err := repo.FindByID(jieyouCtx, dachuiUser.ID, dachuiWithdrawal.ID); !errors.Is(err, ErrWithdrawalNotFound) {
|
||||
t.Fatalf("cross-tenant withdrawal lookup error = %v", err)
|
||||
}
|
||||
list, err := repo.AdminList(jieyouCtx, AdminListQuery{Page: 1, Size: 20})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if list.Total != 1 || len(list.Items) != 1 || list.Items[0].UserID != jieyouUser.ID {
|
||||
t.Fatalf("jieyou withdrawal list = %+v", list)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user