61 lines
1.6 KiB
Go
61 lines
1.6 KiB
Go
package middleware
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"hfb_sys/backend/internal/sitecode"
|
|
"hfb_sys/backend/internal/tenancy"
|
|
"hfb_sys/backend/pkg/response"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// BlockTenantLegacyDistributionAdmin 阻止经营租户后台调用历史分销管理接口。
|
|
// 独立分销后台使用 /api/distribution/*,不经过该守卫。
|
|
func BlockTenantLegacyDistributionAdmin(db *gorm.DB) 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/") {
|
|
allowed, err := requestAdminIsPlatformSuperAdmin(c, db)
|
|
if err != nil {
|
|
response.ServiceUnavailable(c, "权限校验服务暂时不可用")
|
|
c.Abort()
|
|
return
|
|
}
|
|
if allowed {
|
|
c.Next()
|
|
return
|
|
}
|
|
response.Forbidden(c, "经营站点后台不能管理历史分销渠道")
|
|
c.Abort()
|
|
return
|
|
}
|
|
c.Next()
|
|
}
|
|
}
|
|
|
|
func requestAdminIsPlatformSuperAdmin(c *gin.Context, db *gorm.DB) (bool, error) {
|
|
if db == nil {
|
|
return false, nil
|
|
}
|
|
value, ok := c.Get(ContextAdminID)
|
|
if !ok {
|
|
return false, nil
|
|
}
|
|
adminID, ok := value.(uint64)
|
|
if !ok || adminID == 0 {
|
|
return false, nil
|
|
}
|
|
var count int64
|
|
err := db.WithContext(c.Request.Context()).
|
|
Table("admin_user_roles AS aur").
|
|
Joins("JOIN roles AS r ON r.id = aur.role_id").
|
|
Where("aur.admin_user_id = ? AND r.code = ?", adminID, "super_admin").
|
|
Count(&count).Error
|
|
return count > 0, err
|
|
}
|