chore(p4): 收拢库操作到唯一出口,为统一 sspf Db.lock 做准备

- 新增 platform::db::exec_params,收拢业务层 12 处裸 state.db.query
- 新增 check-db-access.sh 护栏:白名单 3 文件,7 处命中 0 越界
- 平台侧需求写入 ssw-platform/docs/SSPF-BUSINESS-DB-ACCESS.md(目标 v0.5.0)
- Cargo.toml 仍锁 v0.3.0,132 端点契约与响应形状未变
This commit is contained in:
admin committed 2026-11-04 22:20:55 +08:00
1 parent 6a66698607
commit 43dcd3f9eb
7 files changed
+245 -51

No files matched your search

+17
View File
@@ -365,6 +365,23 @@ pub async fn query_json_params(
Ok(out)
}
/// 执行带参数的**写**语句(INSERT / UPDATE / DELETE),丢弃结果集。
///
/// 🔴 **业务层写路径的唯一出口**:此前 `tdc/service/{crud,account,identity}.rs` 有 12 处
/// 直接 `state.db.query(...)`,绕过了本文件 ⇒ 将来把库操作统一切到 sspf 的
/// `Db::exec_params` 时要改 12 处,还容易漏。全部收拢到这里后,
/// **切换面只剩 `platform/db.rs`**(见平台仓 `docs/SSPF-BUSINESS-DB-ACCESS.md`)。
///
/// ⚠️ `pglite::query` 走 extended protocol ⇒ **只能单语句**;多语句请用 `db.exec`
/// (simple protocol,整批一个隐式事务)。
pub async fn exec_params(
db: &PGlite,
sql: &str,
params: &[&(dyn ToSql + Sync)],
) -> Result<(), PgError> {
db.query(sql, params).await.map(|_| ())
}
/// 取第一行第一列的文本值(无行 ⇒ `None`)。
pub async fn query_scalar_text(
db: &PGlite,
+8 -32
View File
@@ -452,10 +452,7 @@ pub async fn create(
let refs = bind_refs(&params);
{
let _g = state.db_lock.lock().await;
state
.db
.query(&sql, &refs)
.await
crate::platform::db::exec_params(&state.db, &sql, &refs).await
.map_err(|e| AccountError::Server(format!("新建账号失败: {}", e)))?;
}
find(state, &id)
@@ -497,10 +494,7 @@ pub async fn update(
let refs = bind_refs(&params);
{
let _g = state.db_lock.lock().await;
state
.db
.query(&sql, &refs)
.await
crate::platform::db::exec_params(&state.db, &sql, &refs).await
.map_err(|e| AccountError::Server(format!("更新账号失败: {}", e)))?;
}
let _ = current;
@@ -525,10 +519,7 @@ pub async fn remove(state: &AppState, operator: &str, id: &str) -> Result<(), Ac
let params: Vec<String> = vec![operator.to_string(), id.to_string()];
let refs = bind_refs(&params);
let _g = state.db_lock.lock().await;
state
.db
.query(&sql, &refs)
.await
crate::platform::db::exec_params(&state.db, &sql, &refs).await
.map(|_| ())
.map_err(|e| AccountError::Server(format!("删除账号失败: {}", e)))
}
@@ -558,10 +549,7 @@ pub async fn set_enabled(
let refs = bind_refs(&params);
{
let _g = state.db_lock.lock().await;
state
.db
.query(&sql, &refs)
.await
crate::platform::db::exec_params(&state.db, &sql, &refs).await
.map_err(|e| AccountError::Server(format!("设置账号状态失败: {}", e)))?;
}
let _ = current;
@@ -598,10 +586,7 @@ pub async fn reset_password(
let refs = bind_refs(&params);
{
let _g = state.db_lock.lock().await;
state
.db
.query(&sql, &refs)
.await
crate::platform::db::exec_params(&state.db, &sql, &refs).await
.map_err(|e| AccountError::Server(format!("重设口令失败: {}", e)))?;
}
find(state, id)
@@ -658,10 +643,7 @@ pub async fn bootstrap_account_layer(
.iter()
.map(|s| s as &(dyn pglite::ToSql + Sync))
.collect();
state
.db
.query(&sql, &refs)
.await
crate::platform::db::exec_params(&state.db, &sql, &refs).await
.map_err(|e| AccountError::Server(format!("种角色 {} 失败: {}", en_code, e)))?;
}
@@ -699,10 +681,7 @@ pub async fn bootstrap_account_layer(
);
let params: Vec<String> = vec![id.clone(), account.clone(), password_hash, secretkey];
let refs = bind_refs(&params);
state
.db
.query(&sql, &refs)
.await
crate::platform::db::exec_params(&state.db, &sql, &refs).await
.map_err(|e| AccountError::Server(format!("引导管理员账号失败: {}", e)))?;
// ④ 绑定角色(`f_object_type='user'` + `f_object_id=<admin f_id>`)⇒ 管理员具备 `user` 角色码。
@@ -731,10 +710,7 @@ pub async fn bootstrap_account_layer(
.iter()
.map(|s| s as &(dyn pglite::ToSql + Sync))
.collect();
state
.db
.query(&rel_sql, &refs)
.await
crate::platform::db::exec_params(&state.db, &rel_sql, &refs).await
.map_err(|e| AccountError::Server(format!("绑定管理员角色失败: {}", e)))?;
}
+3 -3
View File
@@ -650,7 +650,7 @@ pub async fn create(
let refs = bind_refs(&binds);
let res = {
let _g = state.db_lock.lock().await;
state.db.query(&sql, &refs).await
crate::platform::db::exec_params(&state.db, &sql, &refs).await
};
match res {
Ok(_) => ActionResult::ok_msg("新增成功", id).into_response(),
@@ -751,7 +751,7 @@ pub async fn update(
let refs = bind_refs(&binds);
let res = {
let _g = state.db_lock.lock().await;
state.db.query(&sql, &refs).await
crate::platform::db::exec_params(&state.db, &sql, &refs).await
};
match res {
Ok(_) => ActionResult::ok_msg("修改成功", id.to_string()).into_response(),
@@ -807,7 +807,7 @@ pub async fn delete(
let refs = bind_refs(&binds);
let res = {
let _g = state.db_lock.lock().await;
state.db.query(&sql, &refs).await
crate::platform::db::exec_params(&state.db, &sql, &refs).await
};
match res {
Ok(_) => ActionResult::ok_msg("删除成功", id.to_string()).into_response(),
+16 -15
View File
@@ -144,21 +144,22 @@ pub async fn bootstrap_identity(state: &AppState, tdc_config: &TdcConfig) -> Boo
CAST('REGISTERED' AS character varying), 1, now(), CAST($6 AS character varying), \
now(), CAST($7 AS character varying))";
let creator = "SYSTEM".to_string();
if let Err(e) = state
.db
.query(
insert,
&[
&identity_id,
&identity_code,
&identity_name,
&identity_name,
&secret,
&creator,
&creator,
],
)
.await
// 🔴 写路径统一走 `platform::db::exec_params`(与其它 11 处同一出口),
// 将来切到 sspf `Db::exec_params` 时只改那一个函数。
if let Err(e) = crate::platform::db::exec_params(
&state.db,
insert,
&[
&identity_id,
&identity_code,
&identity_name,
&identity_name,
&secret,
&creator,
&creator,
],
)
.await
{
eprintln!("[tdc-connector] 引导本地身份失败: {}", e);
// 🔴 插入失败 ⇒ **库里没有身份行** ⇒ 不得回传密钥(否则会打印一个永不生效的凭证),