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.
This commit is contained in:
mlackner 2025-11-19 23:06:49 +01:00
parent 0c096f4e40
commit a5e221cbad
2 changed files with 30 additions and 2 deletions

View File

@ -394,7 +394,7 @@ fn EventPage(id: i32) -> Element {
// Participants List // Participants List
h2 { class: "text-xl font-bold mb-2", "Participants" } h2 { class: "text-xl font-bold mb-2", "Participants" }
match &*participants.read_unchecked() { match &*participants.read() {
Some(Ok(list)) => rsx! { Some(Ok(list)) => rsx! {
ul { ul {
class: "w-96 mb-4", 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 // Draw Button

View File

@ -223,10 +223,37 @@ pub async fn add_participant(
#[server] #[server]
pub async fn get_participants(event_id: i32) -> Result<Vec<ParticipantSafe>, ServerFnError> { pub async fn get_participants(event_id: i32) -> Result<Vec<ParticipantSafe>, 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() let db = db::establish_connection()
.await .await
.map_err(|e| ServerFnError::new(e.to_string()))?; .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() let participants = participants::Entity::find()
.filter(participants::Column::EventId.eq(event_id)) .filter(participants::Column::EventId.eq(event_id))
.all(&db) .all(&db)