From 2b06cd39407f2b8c7c9602fce99a94c0678bf89f Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Sun, 5 Jan 2020 00:21:46 -0500 Subject: [PATCH 1/5] Modify a copy of an upgraded room's PL before sending to the new room --- synapse/handlers/room.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/synapse/handlers/room.py b/synapse/handlers/room.py index 89c9118b262d..f94f44994771 100644 --- a/synapse/handlers/room.py +++ b/synapse/handlers/room.py @@ -16,6 +16,7 @@ # limitations under the License. """Contains functions for performing events on rooms.""" +import copy import itertools import logging import math @@ -271,7 +272,7 @@ def _update_upgraded_room_pls( except AuthError as e: logger.warning("Unable to update PLs in old room: %s", e) - logger.info("Setting correct PLs in new room") + logger.info("Setting correct PLs in new room to %s", old_room_pl_state.content) yield self.event_creation_handler.create_and_send_nonmember_event( requester, { @@ -367,11 +368,14 @@ def clone_existing_room( # Raise the requester's power level in the new room if necessary current_power_level = power_levels["users"][requester.user.to_string()] if current_power_level < needed_power_level: - # Assign this power level to the requester - power_levels["users"][requester.user.to_string()] = needed_power_level + # Perform a deepcopy of the dict in order to not modify initial_state, as its + # contents are preserved as the state for the old room later on + initial_state = copy.deepcopy(initial_state) - # Set the power levels to the modified state - initial_state[(EventTypes.PowerLevels, "")] = power_levels + # Assign this power level to the requester + initial_state[(EventTypes.PowerLevels, "")]["users"][ + requester.user.to_string() + ] = needed_power_level yield self._send_events_for_new_room( requester, @@ -733,7 +737,7 @@ def _send_events_for_new_room( initial_state, creation_content, room_alias=None, - power_level_content_override=None, + power_level_content_override=None, # Doesn't apply when initial state has power level state event content creator_join_profile=None, ): def create(etype, content, **kwargs): From 6ca7cac4a48133f8bfbac86f070f36966025e3bf Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Sun, 5 Jan 2020 02:08:17 -0500 Subject: [PATCH 2/5] Add changelog --- changelog.d/6633.bugfix | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/6633.bugfix diff --git a/changelog.d/6633.bugfix b/changelog.d/6633.bugfix new file mode 100644 index 000000000000..4bacf2602180 --- /dev/null +++ b/changelog.d/6633.bugfix @@ -0,0 +1 @@ +Fix bug where a moderator upgraded a room and became an admin in the new room. \ No newline at end of file From ef5cec3785d5a8187649ac54965b6b0a3a34b508 Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Sun, 5 Jan 2020 10:30:43 -0500 Subject: [PATCH 3/5] Only deepcopy power levels state --- synapse/handlers/room.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/synapse/handlers/room.py b/synapse/handlers/room.py index f94f44994771..040b0889097b 100644 --- a/synapse/handlers/room.py +++ b/synapse/handlers/room.py @@ -370,12 +370,12 @@ def clone_existing_room( if current_power_level < needed_power_level: # Perform a deepcopy of the dict in order to not modify initial_state, as its # contents are preserved as the state for the old room later on - initial_state = copy.deepcopy(initial_state) + existing_power_levels = initial_state[(EventTypes.PowerLevels, "")] + new_power_levels = copy.deepcopy(existing_power_levels) + initial_state[(EventTypes.PowerLevels, "")] = new_power_levels # Assign this power level to the requester - initial_state[(EventTypes.PowerLevels, "")]["users"][ - requester.user.to_string() - ] = needed_power_level + new_power_levels["users"][requester.user.to_string()] = needed_power_level yield self._send_events_for_new_room( requester, From 55822dff3409296ee7bdb5be81f2cb42f329fa83 Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Sun, 5 Jan 2020 10:55:57 -0500 Subject: [PATCH 4/5] comment update --- synapse/handlers/room.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/synapse/handlers/room.py b/synapse/handlers/room.py index 040b0889097b..17ad1ec8c9e8 100644 --- a/synapse/handlers/room.py +++ b/synapse/handlers/room.py @@ -368,8 +368,8 @@ def clone_existing_room( # Raise the requester's power level in the new room if necessary current_power_level = power_levels["users"][requester.user.to_string()] if current_power_level < needed_power_level: - # Perform a deepcopy of the dict in order to not modify initial_state, as its - # contents are preserved as the state for the old room later on + # Perform a deepcopy in order to not modify the original power levels in a + # room, as its contents are preserved as the state for the old room later on existing_power_levels = initial_state[(EventTypes.PowerLevels, "")] new_power_levels = copy.deepcopy(existing_power_levels) initial_state[(EventTypes.PowerLevels, "")] = new_power_levels From c69cc27454e891c68d8bfdd47ffab193a58fbbf6 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Mon, 6 Jan 2020 09:31:18 +0000 Subject: [PATCH 5/5] remove some redundant variables --- synapse/handlers/room.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/synapse/handlers/room.py b/synapse/handlers/room.py index 17ad1ec8c9e8..4f489762fca7 100644 --- a/synapse/handlers/room.py +++ b/synapse/handlers/room.py @@ -366,16 +366,15 @@ def clone_existing_room( needed_power_level = max(state_default, ban, max(event_power_levels.values())) # Raise the requester's power level in the new room if necessary - current_power_level = power_levels["users"][requester.user.to_string()] + current_power_level = power_levels["users"][user_id] if current_power_level < needed_power_level: # Perform a deepcopy in order to not modify the original power levels in a # room, as its contents are preserved as the state for the old room later on - existing_power_levels = initial_state[(EventTypes.PowerLevels, "")] - new_power_levels = copy.deepcopy(existing_power_levels) + new_power_levels = copy.deepcopy(power_levels) initial_state[(EventTypes.PowerLevels, "")] = new_power_levels # Assign this power level to the requester - new_power_levels["users"][requester.user.to_string()] = needed_power_level + new_power_levels["users"][user_id] = needed_power_level yield self._send_events_for_new_room( requester,