|
@@ -359,6 +359,230 @@ pub async fn assert_region_in_scope(
|
|
|
Ok(())
|
|
Ok(())
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+/// ops 区域写口允许的业务层级(openapi/第1册 OR-01:仅市/区县;⛔ 省/根/未定级)。
|
|
|
|
|
+pub fn is_ops_region_writable_level(level: Option<&str>) -> bool {
|
|
|
|
|
+ matches!(
|
|
|
|
|
+ level.map(str::trim).filter(|s| !s.is_empty()),
|
|
|
|
|
+ Some("市" | "区县" | "city" | "district")
|
|
|
|
|
+ )
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/// 把各负责范围种子映射出的最近省级节点收成允许省集合;根/未定级种子(`None`)**不**贡献任何省。
|
|
|
|
|
+pub fn collect_allowed_provinces<'a, I>(seed_provinces: I) -> std::collections::HashSet<String>
|
|
|
|
|
+where
|
|
|
|
|
+ I: IntoIterator<Item = Option<&'a str>>,
|
|
|
|
|
+{
|
|
|
|
|
+ seed_provinces
|
|
|
|
|
+ .into_iter()
|
|
|
|
|
+ .filter_map(|p| p.map(str::trim).filter(|s| !s.is_empty()).map(str::to_string))
|
|
|
|
|
+ .collect()
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/// 目标节点的最近省是否落在允许省集合内;不可判定(无省级祖先)⇒ 失败关闭。
|
|
|
|
|
+pub fn ops_province_write_allowed(
|
|
|
|
|
+ allowed: &std::collections::HashSet<String>,
|
|
|
|
|
+ target_province: Option<&str>,
|
|
|
|
|
+) -> bool {
|
|
|
|
|
+ match target_province.map(str::trim).filter(|s| !s.is_empty()) {
|
|
|
|
|
+ Some(p) => allowed.contains(p),
|
|
|
|
|
+ None => false,
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/// 合并本地负责省与网关注入省上界(BUG-3D-40 反噬修复)。
|
|
|
|
|
+///
|
|
|
|
|
+/// * 无注入(`grant_seeds_present=false`)⇒ 用本地;
|
|
|
|
|
+/// * 有注入但解析后为空(根/未定级/非区域 id 等)⇒ **回落本地**——注入只做收窄,
|
|
|
|
|
+/// ⛔ 把本地已算对的授权抹成 ∅(单调性反转);与 [`bos_api::grant::usable_instance_ids`]
|
|
|
|
|
+/// 「失败关闭/回落本地 `bos_data_scope`」同口径;
|
|
|
|
|
+/// * 两边都有有效省 ⇒ 取交;
|
|
|
|
|
+/// * 本地空、注入有有效省 ⇒ 用注入。
|
|
|
|
|
+pub fn merge_ops_province_allowed(
|
|
|
|
|
+ local_allowed: &std::collections::HashSet<String>,
|
|
|
|
|
+ grant_seeds_present: bool,
|
|
|
|
|
+ grant_allowed: &std::collections::HashSet<String>,
|
|
|
|
|
+) -> std::collections::HashSet<String> {
|
|
|
|
|
+ if !grant_seeds_present {
|
|
|
|
|
+ return local_allowed.clone();
|
|
|
|
|
+ }
|
|
|
|
|
+ if grant_allowed.is_empty() {
|
|
|
|
|
+ return local_allowed.clone();
|
|
|
|
|
+ }
|
|
|
|
|
+ if local_allowed.is_empty() {
|
|
|
|
|
+ return grant_allowed.clone();
|
|
|
|
|
+ }
|
|
|
|
|
+ local_allowed
|
|
|
|
|
+ .intersection(grant_allowed)
|
|
|
|
|
+ .cloned()
|
|
|
|
|
+ .collect()
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/// 节点 → 最近省级祖先 id(闭包含自身 ⇒ 省节点命中自身);祖先链无「省」⇒ `None`。
|
|
|
|
|
+async fn nearest_province_node(
|
|
|
|
|
+ state: &AppState,
|
|
|
|
|
+ node: &str,
|
|
|
|
|
+) -> Result<Option<String>, BosError> {
|
|
|
|
|
+ let node = node.trim();
|
|
|
|
|
+ if node.is_empty() {
|
|
|
|
|
+ return Ok(None);
|
|
|
|
|
+ }
|
|
|
|
|
+ let mut p = Params::default();
|
|
|
|
|
+ let ph = p.push(Value::String(node.to_string()));
|
|
|
|
|
+ let sql = format!(
|
|
|
|
|
+ "SELECT a.ancestor_id AS province_node_id \
|
|
|
|
|
+ FROM dim_node_ancestor a \
|
|
|
|
|
+ JOIN bos_region_meta m ON m.node_id = a.ancestor_id \
|
|
|
|
|
+ WHERE a.node_id = {ph}::text AND m.level IN ('省', 'province') \
|
|
|
|
|
+ ORDER BY CAST(a.depth AS BIGINT) ASC LIMIT 1"
|
|
|
|
|
+ );
|
|
|
|
|
+ Ok(state
|
|
|
|
|
+ .db
|
|
|
|
|
+ .query_one::<Value>(&sql, p.values())
|
|
|
|
|
+ .await?
|
|
|
|
|
+ .and_then(|r| {
|
|
|
|
|
+ r.get("province_node_id")
|
|
|
|
|
+ .and_then(|v| match v {
|
|
|
|
|
+ Value::String(s) => Some(s.trim().to_string()),
|
|
|
|
|
+ Value::Number(n) => Some(n.to_string()),
|
|
|
|
|
+ _ => None,
|
|
|
|
|
+ })
|
|
|
|
|
+ .filter(|s| !s.is_empty())
|
|
|
|
|
+ }))
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/// holder 的 `bos_data_scope` **未展开**区域种子(`target_type=region`)。
|
|
|
|
|
+async fn bos_data_scope_region_seeds(
|
|
|
|
|
+ state: &AppState,
|
|
|
|
|
+ auth: &AuthContext,
|
|
|
|
|
+) -> Result<Vec<String>, BosError> {
|
|
|
|
|
+ #[derive(Deserialize)]
|
|
|
|
|
+ struct SeedRow {
|
|
|
|
|
+ #[serde(deserialize_with = "de::as_string")]
|
|
|
|
|
+ id: String,
|
|
|
|
|
+ }
|
|
|
|
|
+ let sql = format!(
|
|
|
|
|
+ "SELECT DISTINCT s.target_id AS id FROM bos_data_scope s \
|
|
|
|
|
+ WHERE s.holder_type IN ({}) \
|
|
|
|
|
+ AND s.holder_id IN ($1::text, $2::text) AND s.target_type = 'region'",
|
|
|
|
|
+ bos_scope::REGION_HOLDER_TYPES
|
|
|
|
|
+ );
|
|
|
|
|
+ let rows = state
|
|
|
|
|
+ .db
|
|
|
|
|
+ .query_all::<SeedRow>(
|
|
|
|
|
+ &sql,
|
|
|
|
|
+ &[
|
|
|
|
|
+ Value::String(auth.user_id.clone()),
|
|
|
|
|
+ Value::String(auth.subject.subject_id.clone()),
|
|
|
|
|
+ ],
|
|
|
|
|
+ )
|
|
|
|
|
+ .await?;
|
|
|
|
|
+ Ok(rows
|
|
|
|
|
+ .into_iter()
|
|
|
|
|
+ .map(|r| r.id)
|
|
|
|
|
+ .filter(|s| !s.trim().is_empty())
|
|
|
|
|
+ .collect())
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/// BUG-3D-40:运营主/子账号区域树写闸(Admin 放行;其它角色由调用方 `require_roles` 先挡)。
|
|
|
|
|
+///
|
|
|
|
|
+/// * `create_level`=`Some` ⇒ 新建:该层级须为市/区县,`anchor`=父节点;
|
|
|
|
|
+/// * `create_level`=`None` ⇒ 改名:`anchor` 自身层级须为市/区县;
|
|
|
|
|
+/// * 省份上界=负责范围**未展开**种子与网关注入种子各自映射的最近省,经
|
|
|
|
|
+/// [`merge_ops_province_allowed`] 合并(注入只收窄;解析不出省的注入**不**抹平本地);
|
|
|
|
|
+/// 目标(父或本节点)最近省须落在集合内;
|
|
|
|
|
+/// * 仍须叠加 [`assert_region_in_scope`](含下级展开)。
|
|
|
|
|
+pub async fn assert_ops_region_tree_write(
|
|
|
|
|
+ state: &AppState,
|
|
|
|
|
+ auth: &AuthContext,
|
|
|
|
|
+ anchor: &str,
|
|
|
|
|
+ create_level: Option<&str>,
|
|
|
|
|
+) -> Result<(), BosError> {
|
|
|
|
|
+ if auth.role == RoleCode::Admin {
|
|
|
|
|
+ return Ok(());
|
|
|
|
|
+ }
|
|
|
|
|
+ if !matches!(auth.role, RoleCode::OpsMain | RoleCode::OpsSub) {
|
|
|
|
|
+ return Ok(());
|
|
|
|
|
+ }
|
|
|
|
|
+ let anchor = anchor.trim();
|
|
|
|
|
+ if anchor.is_empty() {
|
|
|
|
|
+ return Err(BosError::forbidden_role(
|
|
|
|
|
+ "区域运营主/子账号仅可在负责省份下维护市/区县(KLT-P301):缺少目标节点",
|
|
|
|
|
+ ));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if let Some(level) = create_level {
|
|
|
|
|
+ if !is_ops_region_writable_level(Some(level)) {
|
|
|
|
|
+ return Err(BosError::forbidden_role(format!(
|
|
|
|
|
+ "区域运营主/子账号仅可在负责省份下建市/区县,不能建「{level}」(KLT-P301)"
|
|
|
|
|
+ )));
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ let meta = region_meta_by(&state.db, &[anchor.to_string()]).await?;
|
|
|
|
|
+ let level = meta.get(anchor).and_then(|m| m.level.as_deref());
|
|
|
|
|
+ if !is_ops_region_writable_level(level) {
|
|
|
|
|
+ let label = level.unwrap_or("未定级/根");
|
|
|
|
|
+ return Err(BosError::forbidden_role(format!(
|
|
|
|
|
+ "区域运营主/子账号仅可维护负责省份下的市/区县,不能改「{label}」节点(KLT-P301)"
|
|
|
|
|
+ )));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ let local_seeds = bos_data_scope_region_seeds(state, auth).await?;
|
|
|
|
|
+ let mut local_provs = Vec::with_capacity(local_seeds.len());
|
|
|
|
|
+ for sid in &local_seeds {
|
|
|
|
|
+ local_provs.push(nearest_province_node(state, sid).await?);
|
|
|
|
|
+ }
|
|
|
|
|
+ let local_allowed = collect_allowed_provinces(local_provs.iter().map(|p| p.as_deref()));
|
|
|
|
|
+
|
|
|
|
|
+ let grant_seeds: Vec<String> = auth.granted_resource_instances.iter().cloned().collect();
|
|
|
|
|
+ let grant_seeds_present = !grant_seeds.is_empty();
|
|
|
|
|
+ let mut grant_allowed = std::collections::HashSet::new();
|
|
|
|
|
+ let mut discarded_grant_seeds: Vec<String> = Vec::new();
|
|
|
|
|
+ if grant_seeds_present {
|
|
|
|
|
+ for sid in &grant_seeds {
|
|
|
|
|
+ match nearest_province_node(state, sid).await? {
|
|
|
|
|
+ Some(p) => {
|
|
|
|
|
+ grant_allowed.insert(p);
|
|
|
|
|
+ }
|
|
|
|
|
+ None => discarded_grant_seeds.push(sid.clone()),
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if grant_allowed.is_empty() && !local_allowed.is_empty() {
|
|
|
|
|
+ tracing::warn!(
|
|
|
|
|
+ local_seeds = ?local_seeds,
|
|
|
|
|
+ local_allowed = ?local_allowed,
|
|
|
|
|
+ grant_seeds = ?grant_seeds,
|
|
|
|
|
+ discarded_grant_seeds = ?discarded_grant_seeds,
|
|
|
|
|
+ "区域写口:网关注入种子均未能解析为省级节点,回落本地 bos_data_scope 负责省(注入只收窄、不抹平)"
|
|
|
|
|
+ );
|
|
|
|
|
+ } else if !discarded_grant_seeds.is_empty() {
|
|
|
|
|
+ tracing::debug!(
|
|
|
|
|
+ discarded_grant_seeds = ?discarded_grant_seeds,
|
|
|
|
|
+ grant_allowed = ?grant_allowed,
|
|
|
|
|
+ "区域写口:部分网关注入种子未能解析为省级节点,已丢弃后与本地取交"
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ let allowed = merge_ops_province_allowed(&local_allowed, grant_seeds_present, &grant_allowed);
|
|
|
|
|
+ let target_province = nearest_province_node(state, anchor).await?;
|
|
|
|
|
+ if !ops_province_write_allowed(&allowed, target_province.as_deref()) {
|
|
|
|
|
+ return Err(BosError::forbidden_role(format!(
|
|
|
|
|
+ "区域 {anchor} 不在本团队负责省份下,区域运营主/子账号不能建/改(KLT-P301);\
|
|
|
|
|
+ allowed_provinces={} local_seeds={} grant_seeds={} discarded_grant_seeds={}",
|
|
|
|
|
+ {
|
|
|
|
|
+ let mut v: Vec<_> = allowed.iter().cloned().collect();
|
|
|
|
|
+ v.sort();
|
|
|
|
|
+ v.join(",")
|
|
|
|
|
+ },
|
|
|
|
|
+ local_seeds.join(","),
|
|
|
|
|
+ grant_seeds.join(","),
|
|
|
|
|
+ discarded_grant_seeds.join(","),
|
|
|
|
|
+ )));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ assert_region_in_scope(state, auth, anchor).await
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
#[derive(Deserialize)]
|
|
#[derive(Deserialize)]
|
|
|
struct KeyCount { #[serde(deserialize_with = "de::as_string")]
|
|
struct KeyCount { #[serde(deserialize_with = "de::as_string")]
|
|
|
k: String,
|
|
k: String,
|
|
@@ -737,3 +961,83 @@ pub async fn pairs_by_where(
|
|
|
}
|
|
}
|
|
|
Ok(out)
|
|
Ok(out)
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+#[cfg(test)]
|
|
|
|
|
+mod tests {
|
|
|
|
|
+ use super::{
|
|
|
|
|
+ collect_allowed_provinces, is_ops_region_writable_level, merge_ops_province_allowed,
|
|
|
|
|
+ ops_province_write_allowed,
|
|
|
|
|
+ };
|
|
|
|
|
+ use std::collections::HashSet;
|
|
|
|
|
+
|
|
|
|
|
+ #[test]
|
|
|
|
|
+ fn ops_writable_level_only_city_and_district() {
|
|
|
|
|
+ assert!(is_ops_region_writable_level(Some("市")));
|
|
|
|
|
+ assert!(is_ops_region_writable_level(Some("区县")));
|
|
|
|
|
+ assert!(is_ops_region_writable_level(Some(" city ")));
|
|
|
|
|
+ assert!(!is_ops_region_writable_level(Some("省")));
|
|
|
|
|
+ assert!(!is_ops_region_writable_level(Some("province")));
|
|
|
|
|
+ assert!(!is_ops_region_writable_level(None));
|
|
|
|
|
+ assert!(!is_ops_region_writable_level(Some("")));
|
|
|
|
|
+ assert!(!is_ops_region_writable_level(Some(" ")));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ #[test]
|
|
|
|
|
+ fn root_or_untyped_seeds_do_not_contribute_provinces() {
|
|
|
|
|
+ let allowed = collect_allowed_provinces([None, Some(""), Some(" "), Some("江苏")]);
|
|
|
|
|
+ assert_eq!(allowed, HashSet::from(["江苏".to_string()]));
|
|
|
|
|
+ // 根/未定级种子经 nearest_province 得 None ⇒ 不贡献任何省(堵住「全网种子展开=全树」)。
|
|
|
|
|
+ assert!(collect_allowed_provinces([None, None]).is_empty());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ #[test]
|
|
|
|
|
+ fn province_write_fail_closed_when_target_unknown_or_out_of_scope() {
|
|
|
|
|
+ let allowed = HashSet::from(["江苏".to_string(), "上海".to_string()]);
|
|
|
|
|
+ assert!(ops_province_write_allowed(&allowed, Some("江苏")));
|
|
|
|
|
+ assert!(!ops_province_write_allowed(&allowed, Some("广东")));
|
|
|
|
|
+ assert!(!ops_province_write_allowed(&allowed, None));
|
|
|
|
|
+ assert!(!ops_province_write_allowed(&allowed, Some("")));
|
|
|
|
|
+ assert!(!ops_province_write_allowed(&HashSet::new(), Some("江苏")));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /// 真值表:注入只收窄、解析不出省的注入不抹平本地(反噬第 4 行)。
|
|
|
|
|
+ #[test]
|
|
|
|
|
+ fn merge_province_injection_narrows_never_clears_local() {
|
|
|
|
|
+ let jiangsu = HashSet::from(["江苏".to_string()]);
|
|
|
|
|
+ let empty = HashSet::new();
|
|
|
|
|
+ let suzhou_as_jiangsu = HashSet::from(["江苏".to_string()]); // 苏州市 → nearest 省
|
|
|
|
|
+ let guangdong = HashSet::from(["广东".to_string()]);
|
|
|
|
|
+
|
|
|
|
|
+ // 1) 本地有省、无注入 ⇒ 本地
|
|
|
|
|
+ assert_eq!(
|
|
|
|
|
+ merge_ops_province_allowed(&jiangsu, false, &empty),
|
|
|
|
|
+ jiangsu
|
|
|
|
|
+ );
|
|
|
|
|
+ // 2) 本地空、注入可解析 ⇒ 注入
|
|
|
|
|
+ assert_eq!(
|
|
|
|
|
+ merge_ops_province_allowed(&empty, true, &suzhou_as_jiangsu),
|
|
|
|
|
+ jiangsu
|
|
|
|
|
+ );
|
|
|
|
|
+ // 3) 两边都有有效省 ⇒ 取交
|
|
|
|
|
+ assert_eq!(
|
|
|
|
|
+ merge_ops_province_allowed(&jiangsu, true, &suzhou_as_jiangsu),
|
|
|
|
|
+ jiangsu
|
|
|
|
|
+ );
|
|
|
|
|
+ assert_eq!(
|
|
|
|
|
+ merge_ops_province_allowed(&jiangsu, true, &guangdong),
|
|
|
|
|
+ empty
|
|
|
|
|
+ );
|
|
|
|
|
+ // 4) 本地有省、注入=根/无法解析 id(grant_allowed=∅)⇒ 回落本地,⛔ 抹成 ∅
|
|
|
|
|
+ assert_eq!(
|
|
|
|
|
+ merge_ops_province_allowed(&jiangsu, true, &empty),
|
|
|
|
|
+ jiangsu
|
|
|
|
|
+ );
|
|
|
|
|
+ // 5) 本地空、注入也无法解析 ⇒ ∅(合理拒)
|
|
|
|
|
+ assert_eq!(merge_ops_province_allowed(&empty, true, &empty), empty);
|
|
|
|
|
+ // 混入:有效市 id + 客户 id(客户解析不出)⇒ grant_allowed 仍有江苏,与本地取交
|
|
|
|
|
+ assert_eq!(
|
|
|
|
|
+ merge_ops_province_allowed(&jiangsu, true, &suzhou_as_jiangsu),
|
|
|
|
|
+ jiangsu
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
|
|
+}
|