feat(admin): 拆分经营站点与历史分销后台
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"hfb_sys/backend/internal/sitecode"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
"hfb_sys/backend/pkg/response"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// BlockTenantLegacyDistributionAdmin 阻止经营租户后台调用历史分销管理接口。
|
||||
// 独立分销后台使用 /api/distribution/*,不经过该守卫。
|
||||
func BlockTenantLegacyDistributionAdmin() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
tenantScoped := sitecode.HasTenantScope(c.Request.Context())
|
||||
if _, ok := tenancy.FromContext(c.Request.Context()); ok {
|
||||
tenantScoped = true
|
||||
}
|
||||
if tenantScoped && strings.HasPrefix(c.Request.URL.Path, "/api/admin/distribution/") {
|
||||
response.Forbidden(c, "经营站点后台不能管理历史分销渠道")
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"hfb_sys/backend/internal/sitecode"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func TestBlockTenantLegacyDistributionAdmin(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
engine := gin.New()
|
||||
engine.Use(BlockTenantLegacyDistributionAdmin())
|
||||
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())
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -364,6 +364,7 @@ func New(cfg config.Config, deps Dependencies, logger *zap.Logger) *gin.Engine {
|
||||
if distributionRepo != nil {
|
||||
engine.Use(middleware.SiteDomainFallback(distributionRepo, logger))
|
||||
}
|
||||
engine.Use(middleware.BlockTenantLegacyDistributionAdmin())
|
||||
distributionHandler := distribution.NewHandler(distributionService)
|
||||
if invitationHandler != nil && distributionService != nil {
|
||||
invitationHandler.SetSiteScoper(distributionService)
|
||||
|
||||
Reference in New Issue
Block a user