- 分销管理分组去掉 legacyOnly 冻结,改为按子项权限显示;经营站点后台 (tenant 上下文)不再只对超管可见,持有 distribution:*/qrcode:* 的角色都能进入 - 租户后台历史分销守卫由「仅超管放行」改为「超管或持有任一 distribution:*/qrcode:* 权限放行」,逐接口仍由 requirePerm 校验,无权限账号继续 403 - 新增迁移 000116:cs 角色补 distribution:site_view / rule_view / order_view / settlement_view(二维码池 qrcode:view/manage 原本已有) - 补充守卫用例:客服持分销权限放行、仅二维码池权限放行、无分销权限 403
220 lines
7.3 KiB
Go
220 lines
7.3 KiB
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"hfb_sys/backend/internal/database"
|
|
"hfb_sys/backend/internal/model"
|
|
"hfb_sys/backend/internal/sitecode"
|
|
"hfb_sys/backend/internal/tenancy"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func TestBlockTenantLegacyDistributionAdmin(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
engine := gin.New()
|
|
engine.Use(BlockTenantLegacyDistributionAdmin(nil))
|
|
engine.GET("/api/admin/distribution/sites", func(c *gin.Context) {
|
|
c.Status(http.StatusNoContent)
|
|
})
|
|
engine.GET("/api/admin/invitations", func(c *gin.Context) {
|
|
c.Status(http.StatusNoContent)
|
|
})
|
|
engine.GET("/api/distribution/sites/1", func(c *gin.Context) {
|
|
c.Status(http.StatusNoContent)
|
|
})
|
|
|
|
tenantMiddleware := func(c *gin.Context) {
|
|
ctx := tenancy.WithScope(c.Request.Context(), tenancy.Scope{TenantID: 2, TenantCode: "jieyou"})
|
|
ctx = sitecode.WithTenantScope(ctx)
|
|
c.Request = c.Request.WithContext(ctx)
|
|
c.Next()
|
|
}
|
|
tenantEngine := gin.New()
|
|
tenantEngine.Use(tenantMiddleware, BlockTenantLegacyDistributionAdmin(nil))
|
|
tenantEngine.GET("/api/admin/distribution/sites", func(c *gin.Context) {
|
|
c.Status(http.StatusNoContent)
|
|
})
|
|
|
|
rec := httptest.NewRecorder()
|
|
engine.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/api/admin/distribution/sites", nil))
|
|
if rec.Code != http.StatusNoContent {
|
|
t.Fatalf("legacy request status = %d, want 204", rec.Code)
|
|
}
|
|
|
|
rec = httptest.NewRecorder()
|
|
tenantEngine.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/api/admin/distribution/sites", nil))
|
|
if rec.Code != http.StatusForbidden {
|
|
t.Fatalf("tenant legacy admin status = %d, want 403", rec.Code)
|
|
}
|
|
|
|
for _, path := range []string{"/api/admin/invitations", "/api/distribution/sites/1"} {
|
|
rec = httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodGet, path, nil)
|
|
engine.ServeHTTP(rec, req)
|
|
if rec.Code != http.StatusNoContent {
|
|
t.Fatalf("%s status = %d, want 204", path, rec.Code)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestBlockTenantLegacyDistributionAdminAllowsPlatformSuperAdmin(t *testing.T) {
|
|
db := database.NewTestDB()
|
|
role := model.Role{Code: "super_admin", Name: "超级管理员"}
|
|
if err := db.Create(&role).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
admin := model.AdminUser{Username: "legacy-dist-root", PasswordHash: "x", Status: "active"}
|
|
if err := db.Create(&admin).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.Create(&model.AdminUserRole{
|
|
AdminID: admin.ID,
|
|
RoleID: role.ID,
|
|
TenantID: 0,
|
|
}).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
gin.SetMode(gin.TestMode)
|
|
engine := gin.New()
|
|
engine.Use(func(c *gin.Context) {
|
|
ctx := tenancy.WithScope(c.Request.Context(), tenancy.Scope{TenantID: 1, TenantCode: "dachui"})
|
|
c.Request = c.Request.WithContext(sitecode.WithTenantScope(ctx))
|
|
c.Set(ContextAdminID, admin.ID)
|
|
c.Next()
|
|
}, BlockTenantLegacyDistributionAdmin(db))
|
|
engine.GET("/api/admin/distribution/sites", func(c *gin.Context) {
|
|
c.Status(http.StatusNoContent)
|
|
})
|
|
|
|
rec := httptest.NewRecorder()
|
|
engine.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/api/admin/distribution/sites", nil))
|
|
if rec.Code != http.StatusNoContent {
|
|
t.Fatalf("platform super admin status = %d, want 204, body=%s", rec.Code, rec.Body.String())
|
|
}
|
|
}
|
|
|
|
// 客服持有分销/二维码池权限时,经营站点后台可以进入分销管理。
|
|
func TestBlockTenantLegacyDistributionAdminAllowsRoleWithDistributionPermission(t *testing.T) {
|
|
db := database.NewTestDB()
|
|
role := model.Role{Code: "cs", Name: "客服"}
|
|
if err := db.Create(&role).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
permission := model.Permission{
|
|
Code: "distribution:site_view",
|
|
Name: "查看分销网站",
|
|
Resource: "distribution",
|
|
Action: "site_view",
|
|
}
|
|
if err := db.Create(&permission).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.Create(&model.RolePermission{RoleID: role.ID, PermissionID: permission.ID}).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
admin := model.AdminUser{Username: "legacy-dist-cs", PasswordHash: "x", Status: "active"}
|
|
if err := db.Create(&admin).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.Create(&model.AdminUserRole{
|
|
AdminID: admin.ID,
|
|
RoleID: role.ID,
|
|
TenantID: 1,
|
|
}).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
rec := serveTenantLegacyDistributionAdmin(t, db, admin.ID)
|
|
if rec.Code != http.StatusNoContent {
|
|
t.Fatalf("cs with distribution permission status = %d, want 204, body=%s", rec.Code, rec.Body.String())
|
|
}
|
|
}
|
|
|
|
// 二维码池权限同样放行:客服维护分站二维码池依赖该权限。
|
|
func TestBlockTenantLegacyDistributionAdminAllowsRoleWithQrCodePermission(t *testing.T) {
|
|
db := database.NewTestDB()
|
|
role := model.Role{Code: "qrcode_operator", Name: "二维码池维护"}
|
|
if err := db.Create(&role).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
permission := model.Permission{Code: "qrcode:view", Name: "查看二维码池", Resource: "qrcode", Action: "view"}
|
|
if err := db.Create(&permission).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.Create(&model.RolePermission{RoleID: role.ID, PermissionID: permission.ID}).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
admin := model.AdminUser{Username: "legacy-dist-qr", PasswordHash: "x", Status: "active"}
|
|
if err := db.Create(&admin).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.Create(&model.AdminUserRole{
|
|
AdminID: admin.ID,
|
|
RoleID: role.ID,
|
|
TenantID: 1,
|
|
}).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
rec := serveTenantLegacyDistributionAdmin(t, db, admin.ID)
|
|
if rec.Code != http.StatusNoContent {
|
|
t.Fatalf("qrcode operator status = %d, want 204, body=%s", rec.Code, rec.Body.String())
|
|
}
|
|
}
|
|
|
|
// 不含任何分销/二维码池权限的角色仍被拦截。
|
|
func TestBlockTenantLegacyDistributionAdminRejectsRoleWithoutDistributionPermission(t *testing.T) {
|
|
db := database.NewTestDB()
|
|
role := model.Role{Code: "content_ops", Name: "内容运营"}
|
|
if err := db.Create(&role).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
permission := model.Permission{Code: "order:view", Name: "查看订单", Resource: "order", Action: "view"}
|
|
if err := db.Create(&permission).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.Create(&model.RolePermission{RoleID: role.ID, PermissionID: permission.ID}).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
admin := model.AdminUser{Username: "legacy-dist-content", PasswordHash: "x", Status: "active"}
|
|
if err := db.Create(&admin).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.Create(&model.AdminUserRole{
|
|
AdminID: admin.ID,
|
|
RoleID: role.ID,
|
|
TenantID: 1,
|
|
}).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
rec := serveTenantLegacyDistributionAdmin(t, db, admin.ID)
|
|
if rec.Code != http.StatusForbidden {
|
|
t.Fatalf("role without distribution permission status = %d, want 403, body=%s", rec.Code, rec.Body.String())
|
|
}
|
|
}
|
|
|
|
func serveTenantLegacyDistributionAdmin(t *testing.T, db *gorm.DB, adminID uint64) *httptest.ResponseRecorder {
|
|
t.Helper()
|
|
gin.SetMode(gin.TestMode)
|
|
engine := gin.New()
|
|
engine.Use(func(c *gin.Context) {
|
|
ctx := tenancy.WithScope(c.Request.Context(), tenancy.Scope{TenantID: 1, TenantCode: "dachui"})
|
|
c.Request = c.Request.WithContext(sitecode.WithTenantScope(ctx))
|
|
c.Set(ContextAdminID, adminID)
|
|
c.Next()
|
|
}, BlockTenantLegacyDistributionAdmin(db))
|
|
engine.GET("/api/admin/distribution/sites", func(c *gin.Context) {
|
|
c.Status(http.StatusNoContent)
|
|
})
|
|
rec := httptest.NewRecorder()
|
|
engine.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/api/admin/distribution/sites", nil))
|
|
return rec
|
|
}
|