68 lines
1.5 KiB
Go
68 lines
1.5 KiB
Go
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()
|
|
}
|
|
}
|