state-res: Move more fetch_state calls into auth_check (from sub-functions)

This commit is contained in:
Jonas Platte 2021-09-04 00:44:06 +02:00
parent a6377067cd
commit 4bba58d781
No known key found for this signature in database
GPG Key ID: CC154DE0E30B7C67

View File

@ -284,7 +284,7 @@ where
// If the event type's required power level is greater than the sender's power level, reject // If the event type's required power level is greater than the sender's power level, reject
// If the event has a state_key that starts with an @ and does not match the sender, reject. // If the event has a state_key that starts with an @ and does not match the sender, reject.
if !can_send_event(incoming_event, &fetch_state) { if !can_send_event(incoming_event, fetch_state(&EventType::RoomPowerLevels, ""), &fetch_state) {
warn!("user cannot send event"); warn!("user cannot send event");
return Ok(false); return Ok(false);
} }
@ -292,9 +292,12 @@ where
if incoming_event.event_type() == EventType::RoomPowerLevels { if incoming_event.event_type() == EventType::RoomPowerLevels {
info!("starting m.room.power_levels check"); info!("starting m.room.power_levels check");
if let Some(required_pwr_lvl) = if let Some(required_pwr_lvl) = check_power_levels(
check_power_levels(room_version, incoming_event, &fetch_state) room_version,
{ incoming_event,
fetch_state(&EventType::RoomPowerLevels, ""),
&fetch_state,
) {
if !required_pwr_lvl { if !required_pwr_lvl {
warn!("power level was not allowed"); warn!("power level was not allowed");
return Ok(false); return Ok(false);
@ -531,13 +534,11 @@ fn valid_membership_change<E: Event>(
/// Is the user allowed to send a specific event based on the rooms power levels. /// Is the user allowed to send a specific event based on the rooms power levels.
/// ///
/// Does the event have the correct userId as its state_key if it's not the "" state_key. /// Does the event have the correct userId as its state_key if it's not the "" state_key.
pub fn can_send_event<E, F>(event: &Arc<E>, fetch_state: F) -> bool fn can_send_event<E, F>(event: &Arc<E>, ple: Option<Arc<E>>, fetch_state: F) -> bool
where where
E: Event, E: Event,
F: Fn(&EventType, &str) -> Option<Arc<E>>, F: Fn(&EventType, &str) -> Option<Arc<E>>,
{ {
let ple = fetch_state(&EventType::RoomPowerLevels, "");
let event_type_power_level = let event_type_power_level =
get_send_level(&event.event_type(), event.state_key(), ple.as_ref()); get_send_level(&event.event_type(), event.state_key(), ple.as_ref());
let user_level = get_user_power_level(event.sender(), fetch_state); let user_level = get_user_power_level(event.sender(), fetch_state);
@ -558,9 +559,10 @@ where
} }
/// Confirm that the event sender has the required power levels. /// Confirm that the event sender has the required power levels.
pub fn check_power_levels<E, F>( fn check_power_levels<E, F>(
room_version: &RoomVersion, room_version: &RoomVersion,
power_event: &Arc<E>, power_event: &Arc<E>,
previous_power_event: Option<Arc<E>>,
fetch_state: F, fetch_state: F,
) -> Option<bool> ) -> Option<bool>
where where
@ -579,7 +581,7 @@ where
} }
} }
let current_state = if let Some(current_state) = fetch_state(&power_event.event_type(), "") { let current_state = if let Some(current_state) = previous_power_event {
current_state current_state
} else { } else {
// If there is no previous m.room.power_levels event in the room, allow // If there is no previous m.room.power_levels event in the room, allow