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:
parent
0c096f4e40
commit
a5e221cbad
@ -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
|
||||
|
||||
@ -223,10 +223,37 @@ pub async fn add_participant(
|
||||
|
||||
#[server]
|
||||
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()
|
||||
.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)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user