From a09926f48a1e3012cf96524ea26c2a31f9b0d94f Mon Sep 17 00:00:00 2001 From: "Torsten Schulz (local)" Date: Wed, 4 Mar 2026 23:47:02 +0100 Subject: [PATCH] Enhance room access validation with detailed debug logging Added comprehensive debug logging for room access checks in the `room_access_allowed` function, providing insights into access denial reasons such as missing owner IDs, gender restrictions, and password requirements. This enhancement improves troubleshooting capabilities when debugging is enabled, ensuring clearer feedback on access control decisions. --- src/commands.rs | 114 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) diff --git a/src/commands.rs b/src/commands.rs index 9825e5f..49deeda 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -1177,6 +1177,9 @@ async fn room_access_allowed( config: &ServerConfig, ) -> Result<(), &'static str> { let Some(room) = room_meta else { + if room_debug_enabled() { + eprintln!("[yourchat2][room-debug][access] denied=room_not_found"); + } return Err("room_not_found"); }; let is_admin = user_rights.contains("admin"); @@ -1188,19 +1191,48 @@ async fn room_access_allowed( if let Some(required_gender) = room.gender_restriction_id { if required_gender > 0 && user_gender_id != Some(required_gender) { + if room_debug_enabled() { + eprintln!( + "[yourchat2][room-debug][access] denied=room_gender_restricted room='{}' required_gender_id={} required_gender='{}' user_gender_id={:?} user_gender='{}' user_falukant_id={:?}", + room.name, + required_gender, + gender_label_from_id(Some(required_gender)), + user_gender_id, + gender_label_from_id(user_gender_id), + falukant_user_id + ); + } return Err("room_gender_restricted"); } } if !room.is_public { let Some(owner_id) = room.owner_id else { + if room_debug_enabled() { + eprintln!( + "[yourchat2][room-debug][access] denied=room_private room='{}' reason='missing_owner_id'", + room.name + ); + } return Err("room_private"); }; let Some(fid) = falukant_user_id else { + if room_debug_enabled() { + eprintln!( + "[yourchat2][room-debug][access] denied=room_private room='{}' owner_id={} reason='missing_user_id'", + room.name, owner_id + ); + } return Err("room_private"); }; let is_owner = db::user_is_room_owner(config, owner_id, fid).await; if !is_owner && !is_admin && !has_required_right { + if room_debug_enabled() { + eprintln!( + "[yourchat2][room-debug][access] denied=room_private room='{}' owner_id={} user_falukant_id={} is_owner={} is_admin={} has_required_right={}", + room.name, owner_id, fid, is_owner, is_admin, has_required_right + ); + } return Err("room_private"); } } @@ -1208,9 +1240,22 @@ async fn room_access_allowed( if let Some(room_password) = &room.password { if !room_password.is_empty() { if provided_password.trim().is_empty() { + if room_debug_enabled() { + eprintln!( + "[yourchat2][room-debug][access] denied=room_password_required room='{}' provided_password_len=0", + room.name + ); + } return Err("room_password_required"); } if !password_matches(room_password, provided_password) { + if room_debug_enabled() { + eprintln!( + "[yourchat2][room-debug][access] denied=room_password_invalid room='{}' provided_password_len={}", + room.name, + provided_password.len() + ); + } return Err("room_password_invalid"); } } @@ -1219,15 +1264,39 @@ async fn room_access_allowed( if let Some(min_age) = room.min_age { if min_age > 0 { let Some(fid) = falukant_user_id else { + if room_debug_enabled() { + eprintln!( + "[yourchat2][room-debug][access] denied=room_age_data_missing room='{}' min_age={} reason='missing_user_id'", + room.name, min_age + ); + } return Err("room_age_data_missing"); }; if fid <= 0 { + if room_debug_enabled() { + eprintln!( + "[yourchat2][room-debug][access] denied=room_age_data_missing room='{}' min_age={} user_falukant_id={}", + room.name, min_age, fid + ); + } return Err("room_age_data_missing"); } let Some(user_age) = age else { + if room_debug_enabled() { + eprintln!( + "[yourchat2][room-debug][access] denied=room_age_data_missing room='{}' min_age={} user_falukant_id={} reason='missing_user_age'", + room.name, min_age, fid + ); + } return Err("room_age_data_missing"); }; if user_age < min_age { + if room_debug_enabled() { + eprintln!( + "[yourchat2][room-debug][access] denied=room_min_age_not_met room='{}' min_age={} user_age={}", + room.name, min_age, user_age + ); + } return Err("room_min_age_not_met"); } } @@ -1235,26 +1304,62 @@ async fn room_access_allowed( if let Some(max_age) = room.max_age { if max_age > 0 { let Some(user_age) = age else { + if room_debug_enabled() { + eprintln!( + "[yourchat2][room-debug][access] denied=room_age_data_missing room='{}' max_age={} reason='missing_user_age'", + room.name, max_age + ); + } return Err("room_age_data_missing"); }; if user_age > max_age { + if room_debug_enabled() { + eprintln!( + "[yourchat2][room-debug][access] denied=room_max_age_exceeded room='{}' max_age={} user_age={}", + room.name, max_age, user_age + ); + } return Err("room_max_age_exceeded"); } } } if let Some(required_right) = room.required_user_right_id { if required_right > 0 && !user_right_type_ids.contains(&required_right) { + if room_debug_enabled() { + eprintln!( + "[yourchat2][room-debug][access] denied=room_required_right_missing room='{}' required_right_id={} user_right_ids={:?}", + room.name, required_right, user_right_type_ids + ); + } return Err("room_required_right_missing"); } } if room.friends_of_owner_only { let Some(owner_id) = room.owner_id else { + if room_debug_enabled() { + eprintln!( + "[yourchat2][room-debug][access] denied=room_friends_only room='{}' reason='missing_owner_id'", + room.name + ); + } return Err("room_friends_only"); }; let Some(fid) = falukant_user_id else { + if room_debug_enabled() { + eprintln!( + "[yourchat2][room-debug][access] denied=room_friends_only room='{}' owner_id={} reason='missing_user_id'", + room.name, owner_id + ); + } return Err("room_friends_only"); }; if !db::is_friend_of_room_owner(config, owner_id, fid).await && !is_admin { + if room_debug_enabled() { + eprintln!( + "[yourchat2][room-debug][access] denied=room_friends_only room='{}' owner_id={} user_falukant_id={} is_admin={}", + room.name, owner_id, fid, is_admin + ); + } return Err("room_friends_only"); } } @@ -1556,6 +1661,15 @@ fn room_debug_enabled() -> bool { ) } +fn gender_label_from_id(gender_id: Option) -> &'static str { + match gender_id { + Some(1) => "m", + Some(2) => "f", + Some(_) => "unknown", + None => "none", + } +} + fn command_from_chat_line(message: &str, token: Option) -> Option { let trimmed = message.trim(); if !trimmed.starts_with('/') {