From a5e221cbad9377f156983c72a04cff25cda9d3e3 Mon Sep 17 00:00:00 2001 From: mlackner Date: Wed, 19 Nov 2025 23:06:49 +0100 Subject: [PATCH] feat(events): restrict participant list access Verify that the requesting user is either the event creator or a participant before returning the participant list. Update the UI to safely handle errors and loading states. --- src/main.rs | 5 +++-- src/server_fns.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 2f1f011..0db090e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -394,7 +394,7 @@ fn EventPage(id: i32) -> Element { // Participants List h2 { class: "text-xl font-bold mb-2", "Participants" } - match &*participants.read_unchecked() { + match &*participants.read() { Some(Ok(list)) => rsx! { ul { class: "w-96 mb-4", @@ -403,7 +403,8 @@ fn EventPage(id: i32) -> Element { } } }, - _ => rsx! { "Loading..." } + Some(Err(e)) => rsx! { p { class: "text-red-500", "Error: {e}" } }, + None => rsx! { "Loading..." } } // Draw Button diff --git a/src/server_fns.rs b/src/server_fns.rs index 37f69c7..f931189 100644 --- a/src/server_fns.rs +++ b/src/server_fns.rs @@ -223,10 +223,37 @@ pub async fn add_participant( #[server] pub async fn get_participants(event_id: i32) -> Result, ServerFnError> { + let session: tower_sessions::Session = FullstackContext::extract().await?; + let user_id: i32 = session + .get("user_id") + .await + .map_err(|e| ServerFnError::new(e.to_string()))? + .ok_or(ServerFnError::new("Not logged in"))?; + let db = db::establish_connection() .await .map_err(|e| ServerFnError::new(e.to_string()))?; + // Check if user is creator or participant + let event = events::Entity::find_by_id(event_id) + .one(&db) + .await + .map_err(|e| ServerFnError::new(e.to_string()))? + .ok_or(ServerFnError::new("Event not found"))?; + + let is_creator = event.creator_id == user_id; + let is_participant = participants::Entity::find() + .filter(participants::Column::EventId.eq(event_id)) + .filter(participants::Column::UserId.eq(user_id)) + .one(&db) + .await + .map_err(|e| ServerFnError::new(e.to_string()))? + .is_some(); + + if !is_creator && !is_participant { + return Err(ServerFnError::new("Not authorized")); + } + let participants = participants::Entity::find() .filter(participants::Column::EventId.eq(event_id)) .all(&db)