fix(tenant): 收紧双站后台边界并补齐货架管理

This commit is contained in:
yml
2026-11-15 22:28:40 +08:00
parent a68eb815a9
commit 0cb385d84f
42 changed files with 831 additions and 97 deletions
@@ -44,6 +44,9 @@ func (r *Repository) Create(ctx context.Context, renterID uint64, req CreateRequ
if requestTenantID > 0 && renter.TenantID != requestTenantID {
return ErrPermissionDenied
}
if requestTenantID > 0 && listing.DistributionSiteID != nil && *listing.DistributionSiteID > 0 {
return ErrListingUnavailable
}
buyerTenantID := renter.TenantID
if requestTenantID > 0 {
buyerTenantID = requestTenantID
+6 -2
View File
@@ -546,7 +546,9 @@ func applyTenantOrderScope(query *gorm.DB, ctx context.Context) *gorm.DB {
if tenantID == 0 {
return query
}
return query.Where("(o.buyer_tenant_id = ? OR o.source_tenant_id = ?)", tenantID, tenantID)
return query.
Where("(o.buyer_tenant_id = ? OR o.source_tenant_id = ?)", tenantID, tenantID).
Where("(o.distribution_site_id IS NULL OR o.distribution_site_id = 0)")
}
func applyBuyerTenantOrderScope(query *gorm.DB, ctx context.Context) *gorm.DB {
@@ -554,7 +556,9 @@ func applyBuyerTenantOrderScope(query *gorm.DB, ctx context.Context) *gorm.DB {
if tenantID == 0 {
return query
}
return query.Where("o.buyer_tenant_id = ?", tenantID)
return query.
Where("o.buyer_tenant_id = ?", tenantID).
Where("(o.distribution_site_id IS NULL OR o.distribution_site_id = 0)")
}
func tenantOrderRole(ctx context.Context, order model.RentalOrder) string {
+2 -2
View File
@@ -229,7 +229,7 @@ func (s *Service) AdminClose(ctx context.Context, adminID uint64, orderID uint64
if orderID == 0 || req.Reason == "" {
return ErrOrderCannotComplete
}
if err := s.ensureAdminOrderAccess(ctx, orderID); err != nil {
if err := s.ensureAdminOrderCapability(ctx, orderID, tenantOrderCapabilityRefund); err != nil {
return err
}
return s.repo.AdminClose(ctx, adminID, orderID, req, meta)
@@ -255,7 +255,7 @@ func (s *Service) AdminSeal(ctx context.Context, adminID uint64, orderID uint64,
if orderID == 0 || req.Reason == "" {
return ErrOrderCannotComplete
}
if err := s.ensureAdminOrderAccess(ctx, orderID); err != nil {
if err := s.ensureAdminOrderCapability(ctx, orderID, tenantOrderCapabilityRefund); err != nil {
return err
}
return s.repo.AdminSeal(ctx, adminID, orderID, req, meta)
@@ -180,6 +180,13 @@ func TestAdminOrdersAreScopedByCurrentTenant(t *testing.T) {
if err := db.Create(&tenants).Error; err != nil {
t.Fatal(err)
}
legacySite := model.DistributionSite{
ID: 999, SiteCode: "legacy_ops_test", SiteName: "历史分销测试站",
Domain: "legacy-ops-test.example.com", Status: "active",
}
if err := db.Create(&legacySite).Error; err != nil {
t.Fatal(err)
}
owner := model.User{TenantID: 101, Phone: "13000000101", Status: "active"}
renterB := model.User{TenantID: 202, Phone: "13000000202", Status: "active"}
renterA := model.User{TenantID: 101, Phone: "13000000102", Status: "active"}
@@ -234,6 +241,13 @@ func TestAdminOrdersAreScopedByCurrentTenant(t *testing.T) {
SourceTenantID: 303, PaymentTenantID: 101, Status: orderStatusRenting,
},
}
legacySiteID := legacySite.ID
orders = append(orders, model.RentalOrder{
OrderNo: "RO-LEGACY-DISTRIBUTION", ListingID: listingA.ID, AccountID: accountA.ID,
OwnerID: owner.ID, RenterID: renterA.ID, BuyerTenantID: 101,
SourceTenantID: 101, PaymentTenantID: 101, DistributionSiteID: &legacySiteID,
Status: orderStatusRenting,
})
if err := db.Create(&orders).Error; err != nil {
t.Fatal(err)
}
@@ -268,6 +282,12 @@ func TestAdminOrdersAreScopedByCurrentTenant(t *testing.T) {
if err := repo.EnsureAdminOrderCapability(orderTenantContext(202), orders[0].ID, tenantOrderCapabilityHandoff); !errors.Is(err, gorm.ErrRecordNotFound) {
t.Fatalf("buyer tenant handoff capability error = %v", err)
}
if err := service.AdminClose(orderTenantContext(101), 9, orders[0].ID, AdminActionRequest{Reason: "source close"}, AuditMeta{}); !errors.Is(err, gorm.ErrRecordNotFound) {
t.Fatalf("source tenant close capability error = %v", err)
}
if err := service.AdminSeal(orderTenantContext(101), 9, orders[0].ID, AdminActionRequest{Reason: "source seal"}, AuditMeta{}); !errors.Is(err, gorm.ErrRecordNotFound) {
t.Fatalf("source tenant seal capability error = %v", err)
}
sourceResult, err := repo.ListAdmin(orderTenantContext(101), AdminOrderQuery{Page: 1, PageSize: 20})
if err != nil {
@@ -21,12 +21,13 @@ func (r *Repository) EnsureAdminOrderCapability(ctx context.Context, orderID uin
return nil
}
var order struct {
BuyerTenantID uint64
SourceTenantID uint64
BuyerTenantID uint64
SourceTenantID uint64
DistributionSiteID *uint64
}
if err := r.db.WithContext(ctx).
Table("rental_orders").
Select("buyer_tenant_id, source_tenant_id").
Select("buyer_tenant_id, source_tenant_id, distribution_site_id").
Where("id = ?", orderID).
Scan(&order).Error; err != nil {
return err
@@ -34,6 +35,9 @@ func (r *Repository) EnsureAdminOrderCapability(ctx context.Context, orderID uin
if order.BuyerTenantID == 0 && order.SourceTenantID == 0 {
return gorm.ErrRecordNotFound
}
if order.DistributionSiteID != nil && *order.DistributionSiteID > 0 {
return gorm.ErrRecordNotFound
}
isBuyer := order.BuyerTenantID == tenantID
isSource := order.SourceTenantID == tenantID
if !isBuyer && !isSource {