fix(tenant): 隔离经营租户与旧分销站点上下文
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
package sitecode
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"strings"
|
||||
|
||||
@@ -24,10 +25,28 @@ const HeaderName = "X-Distribution-Site-Code"
|
||||
// LegacyHeaderName 是历史版本使用的站点编码请求头,仅作兜底兼容。
|
||||
const LegacyHeaderName = "X-Site-Code"
|
||||
|
||||
type tenantScopeContextKey struct{}
|
||||
|
||||
// WithTenantScope 标记当前请求已识别为经营租户。
|
||||
// 经营租户与历史分销站点使用不同归属模型,识别成功后必须忽略旧分销站点编码。
|
||||
func WithTenantScope(ctx context.Context) context.Context {
|
||||
return context.WithValue(ctx, tenantScopeContextKey{}, true)
|
||||
}
|
||||
|
||||
// HasTenantScope 判断当前请求是否已识别为经营租户。
|
||||
func HasTenantScope(ctx context.Context) bool {
|
||||
enabled, _ := ctx.Value(tenantScopeContextKey{}).(bool)
|
||||
return enabled
|
||||
}
|
||||
|
||||
// FromRequest 按权威优先级解析站点编码:
|
||||
// X-Distribution-Site-Code 头 → X-Site-Code 旧头 → distribution_site_code / site_code query。
|
||||
// 所有来源均做 TrimSpace,返回空串表示主站。
|
||||
// 所有来源均做 TrimSpace,返回空串表示主站。经营租户请求始终返回空串,
|
||||
// 防止历史分销参数把经营租户重新带回旧站点查询。
|
||||
func FromRequest(c *gin.Context) string {
|
||||
if HasTenantScope(c.Request.Context()) {
|
||||
return ""
|
||||
}
|
||||
return firstNonEmpty(
|
||||
c.GetHeader(HeaderName),
|
||||
c.GetHeader(LegacyHeaderName),
|
||||
@@ -40,6 +59,9 @@ func FromRequest(c *gin.Context) string {
|
||||
// 显式 query 参数优先于会话头。SPA 探测新站点时会话头里还是上一个站点的编码,
|
||||
// 必须让 query 压过它,否则从主站跳转到分站时会探测到错误站点。
|
||||
func FromQueryThenHeader(c *gin.Context) string {
|
||||
if HasTenantScope(c.Request.Context()) {
|
||||
return ""
|
||||
}
|
||||
return firstNonEmpty(
|
||||
c.Query("code"),
|
||||
c.Query("site_code"),
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package sitecode
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
@@ -45,6 +46,22 @@ func TestFromRequest(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestFromRequestIgnoresLegacySiteCodeInTenantScope(t *testing.T) {
|
||||
c := newTestContext(
|
||||
"GET",
|
||||
"/api/listings?distribution_site_code=query_site",
|
||||
map[string]string{
|
||||
HeaderName: "new_site",
|
||||
LegacyHeaderName: "legacy_site",
|
||||
},
|
||||
)
|
||||
c.Request = c.Request.WithContext(WithTenantScope(context.Background()))
|
||||
|
||||
if got := FromRequest(c); got != "" {
|
||||
t.Fatalf("经营租户请求不应解析旧分销站点编码, got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFromQueryThenHeader(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
@@ -67,6 +84,19 @@ func TestFromQueryThenHeader(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestFromQueryThenHeaderIgnoresLegacySiteCodeInTenantScope(t *testing.T) {
|
||||
c := newTestContext(
|
||||
"GET",
|
||||
"/distribution/site-info?code=probe_site&distribution_site_code=legacy_site",
|
||||
map[string]string{HeaderName: "header_site"},
|
||||
)
|
||||
c.Request = c.Request.WithContext(WithTenantScope(context.Background()))
|
||||
|
||||
if got := FromQueryThenHeader(c); got != "" {
|
||||
t.Fatalf("经营租户请求不应探测旧分销站点, got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractHost(t *testing.T) {
|
||||
cases := []struct {
|
||||
raw string
|
||||
|
||||
Reference in New Issue
Block a user