feat(platform-ops): 增加平台只读统计接口与超管入口
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"hfb_sys/backend/internal/platformscope"
|
||||
"hfb_sys/backend/internal/sitecode"
|
||||
"hfb_sys/backend/pkg/response"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func PlatformOpsContext(domain string) gin.HandlerFunc {
|
||||
expected := strings.ToLower(sitecode.ExtractHost(domain))
|
||||
return func(c *gin.Context) {
|
||||
if expected != "" && sitecode.ExtractHost(c.Request.Host) == expected {
|
||||
c.Request = c.Request.WithContext(platformscope.WithScope(c.Request.Context()))
|
||||
}
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
func RequirePlatformOps() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
if !platformscope.IsScope(c.Request.Context()) {
|
||||
response.Forbidden(c, "当前入口不是平台运营后台")
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
// PlatformOpsReadOnlyGuard 保证平台运营域名只能访问登录、身份和只读统计接口。
|
||||
func PlatformOpsReadOnlyGuard() gin.HandlerFunc {
|
||||
allowedPrefixes := []string{
|
||||
"/api/admin/auth/",
|
||||
"/api/admin/platform/",
|
||||
}
|
||||
allowedPaths := map[string]bool{
|
||||
"/api/admin/me": true,
|
||||
"/api/admin/admin-users/me/password": true,
|
||||
}
|
||||
return func(c *gin.Context) {
|
||||
if !platformscope.IsScope(c.Request.Context()) {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
path := c.Request.URL.Path
|
||||
if allowedPaths[path] {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
for _, prefix := range allowedPrefixes {
|
||||
if strings.HasPrefix(path, prefix) {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
}
|
||||
if strings.HasPrefix(path, "/api/admin/") {
|
||||
response.Forbidden(c, "平台运营后台仅提供统计查看")
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"hfb_sys/backend/internal/platformscope"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func TestPlatformOpsContextAndReadOnlyGuard(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
engine := gin.New()
|
||||
engine.Use(PlatformOpsContext("ops.localhost"), PlatformOpsReadOnlyGuard())
|
||||
engine.GET("/probe", func(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"platform": platformscope.IsScope(c.Request.Context())})
|
||||
})
|
||||
engine.GET("/api/admin/platform/overview", func(c *gin.Context) {
|
||||
c.Status(http.StatusNoContent)
|
||||
})
|
||||
engine.GET("/api/admin/users", func(c *gin.Context) {
|
||||
c.Status(http.StatusNoContent)
|
||||
})
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/probe", nil)
|
||||
req.Host = "ops.localhost:5189"
|
||||
rec := httptest.NewRecorder()
|
||||
engine.ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusOK || rec.Body.String() != `{"platform":true}` {
|
||||
t.Fatalf("platform context response = %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
|
||||
req = httptest.NewRequest(http.MethodGet, "/api/admin/platform/overview", nil)
|
||||
req.Host = "ops.localhost:5189"
|
||||
rec = httptest.NewRecorder()
|
||||
engine.ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusNoContent {
|
||||
t.Fatalf("platform overview status = %d", rec.Code)
|
||||
}
|
||||
|
||||
req = httptest.NewRequest(http.MethodGet, "/api/admin/users", nil)
|
||||
req.Host = "ops.localhost:5189"
|
||||
rec = httptest.NewRecorder()
|
||||
engine.ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusForbidden {
|
||||
t.Fatalf("platform non-readonly route status = %d, want 403", rec.Code)
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"hfb_sys/backend/internal/platformscope"
|
||||
"hfb_sys/backend/internal/sitecode"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
|
||||
@@ -66,6 +67,10 @@ func SiteDomainFallback(resolver SiteDomainResolver, logger *zap.Logger) gin.Han
|
||||
}
|
||||
cache := newSiteDomainCache()
|
||||
return func(c *gin.Context) {
|
||||
if platformscope.IsScope(c.Request.Context()) {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
if _, ok := tenancy.FromContext(c.Request.Context()); ok || sitecode.HasTenantScope(c.Request.Context()) {
|
||||
c.Next()
|
||||
return
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"hfb_sys/backend/internal/config"
|
||||
"hfb_sys/backend/internal/platformscope"
|
||||
"hfb_sys/backend/internal/sitecode"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
"hfb_sys/backend/pkg/response"
|
||||
@@ -34,6 +35,10 @@ func TenantContext(resolver TenantResolver, appEnv string, logger *zap.Logger, d
|
||||
defaultTenantCode = strings.TrimSpace(defaultTenantCodes[0])
|
||||
}
|
||||
return func(c *gin.Context) {
|
||||
if platformscope.IsScope(c.Request.Context()) {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
var (
|
||||
scope *tenancy.Scope
|
||||
err error
|
||||
|
||||
Reference in New Issue
Block a user