fix: prevent reused account ownership for orphan works
This commit is contained in:
@@ -2,6 +2,10 @@ use module_auth::AuthUser;
|
||||
|
||||
use crate::state::{AppState, PuzzleApiState};
|
||||
|
||||
pub const ORPHAN_WORK_OWNER_USER_ID: &str = "wx-openid-placeholder";
|
||||
pub const ORPHAN_WORK_AUTHOR_DISPLAY_NAME: &str = "失效作者";
|
||||
pub const ORPHAN_WORK_AUTHOR_PUBLIC_USER_CODE: &str = "SY-00000000";
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct WorkAuthorSummary {
|
||||
pub display_name: String,
|
||||
@@ -45,21 +49,15 @@ fn resolve_work_author_by_user_id_with_service(
|
||||
) -> WorkAuthorSummary {
|
||||
let fallback_display_name =
|
||||
normalize_optional_text(fallback_display_name).unwrap_or_else(|| "玩家".to_string());
|
||||
let fallback_public_user_code = normalize_optional_text(fallback_public_user_code);
|
||||
let _fallback_public_user_code = normalize_optional_text(fallback_public_user_code);
|
||||
|
||||
let Some(owner_user_id) = normalize_optional_text(Some(owner_user_id)) else {
|
||||
return WorkAuthorSummary {
|
||||
display_name: fallback_display_name,
|
||||
public_user_code: fallback_public_user_code,
|
||||
};
|
||||
return orphan_work_author_summary();
|
||||
};
|
||||
|
||||
match auth_user_service.get_user_by_id(&owner_user_id) {
|
||||
Ok(Some(user)) => map_auth_user_to_work_author_summary(user, fallback_display_name),
|
||||
Ok(None) | Err(_) => WorkAuthorSummary {
|
||||
display_name: fallback_display_name,
|
||||
public_user_code: fallback_public_user_code,
|
||||
},
|
||||
Ok(None) | Err(_) => orphan_work_author_summary(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,3 +78,65 @@ fn normalize_optional_text(value: Option<&str>) -> Option<String> {
|
||||
.filter(|value| !value.is_empty())
|
||||
.map(ToOwned::to_owned)
|
||||
}
|
||||
|
||||
fn orphan_work_author_summary() -> WorkAuthorSummary {
|
||||
WorkAuthorSummary {
|
||||
display_name: ORPHAN_WORK_AUTHOR_DISPLAY_NAME.to_string(),
|
||||
public_user_code: Some(ORPHAN_WORK_AUTHOR_PUBLIC_USER_CODE.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
/// 中文注释:运维回填只处理空作者或认证仓储不可再解析的历史 owner_user_id,避免把有效作品误转给占位账号。
|
||||
pub fn should_rebind_orphan_work_owner(
|
||||
auth_user_service: &module_auth::AuthUserService,
|
||||
owner_user_id: &str,
|
||||
) -> bool {
|
||||
let Some(owner_user_id) = normalize_optional_text(Some(owner_user_id)) else {
|
||||
return true;
|
||||
};
|
||||
if owner_user_id == ORPHAN_WORK_OWNER_USER_ID {
|
||||
return false;
|
||||
}
|
||||
|
||||
!matches!(auth_user_service.get_user_by_id(&owner_user_id), Ok(Some(_)))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use module_auth::{AuthUserService, InMemoryAuthStore};
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn orphan_work_author_summary_uses_placeholder_account() {
|
||||
assert_eq!(
|
||||
orphan_work_author_summary(),
|
||||
WorkAuthorSummary {
|
||||
display_name: "失效作者".to_string(),
|
||||
public_user_code: Some("SY-00000000".to_string()),
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn missing_author_resolves_to_placeholder_account() {
|
||||
let service = AuthUserService::new(InMemoryAuthStore::default());
|
||||
|
||||
let author = resolve_work_author_by_user_id_with_service(
|
||||
&service,
|
||||
"user_missing",
|
||||
Some("历史昵称"),
|
||||
Some("SY-00000001"),
|
||||
);
|
||||
|
||||
assert_eq!(author, orphan_work_author_summary());
|
||||
}
|
||||
#[test]
|
||||
fn should_rebind_orphan_work_owner_detects_missing_and_empty_author() {
|
||||
let service = AuthUserService::new(InMemoryAuthStore::default());
|
||||
|
||||
assert!(should_rebind_orphan_work_owner(&service, ""));
|
||||
assert!(should_rebind_orphan_work_owner(&service, "user_missing"));
|
||||
assert!(!should_rebind_orphan_work_owner(&service, ORPHAN_WORK_OWNER_USER_ID));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user