85 lines
2.7 KiB
Go

package host
import (
"strings"
"hyapp/pkg/appcode"
"hyapp/pkg/xerr"
)
type RoleScope string
const (
RoleScopeCountry RoleScope = "country"
RoleScopeRegion RoleScope = "region"
RoleScopeGlobal RoleScope = "global"
RoleScopeSceneOrganizationExpansion = "organization_role_expansion"
RoleScopeSceneManagerOperations = "manager_operations"
// RoleScopeSceneManagerAddBDLeader 兼容上一版 gateway/admin 的策略查询,解析后统一返回 manager_operations。
RoleScopeSceneManagerAddBDLeader = "manager_add_bd_leader"
)
// RoleScopePolicy 把 App 固有边界和可运营扩展能力分开;业务入口只根据解析结果判断目标范围。
type RoleScopePolicy struct {
Scene string
BaseScope RoleScope
ExpandedScope RoleScope
RegionExpansionConfigurable bool
}
var defaultRoleScopePolicies = map[string]RoleScopePolicy{
RoleScopeSceneOrganizationExpansion: {
Scene: RoleScopeSceneOrganizationExpansion,
BaseScope: RoleScopeRegion,
ExpandedScope: RoleScopeRegion,
},
RoleScopeSceneManagerOperations: {
Scene: RoleScopeSceneManagerOperations,
BaseScope: RoleScopeCountry,
ExpandedScope: RoleScopeRegion,
RegionExpansionConfigurable: true,
},
}
var appRoleScopePolicyOverrides = map[string]map[string]RoleScopePolicy{
"huwaa": {
RoleScopeSceneOrganizationExpansion: {
Scene: RoleScopeSceneOrganizationExpansion,
BaseScope: RoleScopeGlobal,
ExpandedScope: RoleScopeGlobal,
},
RoleScopeSceneManagerOperations: {
Scene: RoleScopeSceneManagerOperations,
BaseScope: RoleScopeGlobal,
ExpandedScope: RoleScopeGlobal,
},
},
}
// ResolveRoleScopePolicy 是 App 差异的唯一解析入口;新增 App 只扩展注册表,不修改搜索或写入 handler。
func ResolveRoleScopePolicy(rawAppCode string, rawScene string) (RoleScopePolicy, error) {
scene := strings.ToLower(strings.TrimSpace(rawScene))
if scene == RoleScopeSceneManagerAddBDLeader {
scene = RoleScopeSceneManagerOperations
}
policy, ok := defaultRoleScopePolicies[scene]
if !ok {
return RoleScopePolicy{}, xerr.New(xerr.InvalidArgument, "role scope scene is invalid")
}
if overrides := appRoleScopePolicyOverrides[appcode.Normalize(rawAppCode)]; overrides != nil {
if override, exists := overrides[scene]; exists {
policy = override
}
}
return policy, nil
}
// EffectiveScope 只允许声明为 configurable 的策略接受经理个人扩区开关。
func (p RoleScopePolicy) EffectiveScope(regionExpansionEnabled bool) RoleScope {
if p.RegionExpansionConfigurable && regionExpansionEnabled {
return p.ExpandedScope
}
return p.BaseScope
}