Skip to content

Commit 0f43658

Browse files
committed
fix: Add is_requester_admin parameter to upgrade room callback (#21)
This is for consistency with the room create API.
1 parent f705619 commit 0f43658

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

synapse/handlers/room.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,18 @@ async def upgrade_room(
220220
if old_room is None:
221221
raise NotFoundError("Unknown room id %s" % (old_room_id,))
222222

223-
await self._third_party_event_rules.on_upgrade_room(requester, new_version)
223+
if (
224+
self._server_notices_mxid is not None
225+
and user_id == self._server_notices_mxid
226+
):
227+
# allow the server notices mxid to create rooms
228+
is_requester_admin = True
229+
else:
230+
is_requester_admin = await self.auth.is_server_admin(requester)
231+
232+
await self._third_party_event_rules.on_upgrade_room(
233+
requester, new_version, is_requester_admin=is_requester_admin
234+
)
224235

225236
new_room_id = self._generate_room_id()
226237

synapse/module_api/callbacks/third_party_event_rules_callbacks.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
ON_THREEPID_BIND_CALLBACK = Callable[[str, str, str], Awaitable]
5656
ON_ADD_USER_THIRD_PARTY_IDENTIFIER_CALLBACK = Callable[[str, str, str], Awaitable]
5757
ON_REMOVE_USER_THIRD_PARTY_IDENTIFIER_CALLBACK = Callable[[str, str, str], Awaitable]
58-
ON_UPGRADE_ROOM_CALLBACK = Callable[[Requester, RoomVersion], Awaitable]
58+
ON_UPGRADE_ROOM_CALLBACK = Callable[[Requester, RoomVersion, bool], Awaitable]
5959

6060

6161
def load_legacy_third_party_event_rules(hs: "HomeServer") -> None:
@@ -605,17 +605,18 @@ async def on_remove_user_third_party_identifier(
605605
)
606606

607607
async def on_upgrade_room(
608-
self, requester: Requester, room_version: RoomVersion
608+
self, requester: Requester, room_version: RoomVersion, is_requester_admin: bool
609609
) -> None:
610610
"""Intercept requests to upgrade a room to maybe deny it (via an exception).
611611
612612
Args:
613613
requester
614614
room_version: The RoomVersion requested for the upgrade.
615+
is_requester_admin: If the requester is an admin
615616
"""
616617
for callback in self._on_upgrade_room_callbacks:
617618
try:
618-
await callback(requester, room_version)
619+
await callback(requester, room_version, is_requester_admin)
619620
except Exception as e:
620621
logger.exception(
621622
"Failed to run module API callback %s: %s", callback, e

tests/rest/client/test_third_party_rules.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,12 @@ def __init__(self, config: Dict, module_api: "ModuleApi") -> None:
106106
)
107107

108108
async def on_upgrade_room(
109-
self, requester: Requester, room_version: RoomVersion
109+
self, requester: Requester, room_version: RoomVersion, is_requester_admin: bool
110110
) -> None:
111-
if room_version.identifier not in self.allowed_room_versions:
111+
if (
112+
not is_requester_admin
113+
and room_version.identifier not in self.allowed_room_versions
114+
):
112115
raise SynapseError(
113116
400,
114117
"You can not upgrade room to that version",
@@ -510,6 +513,15 @@ def upgrade_room_to_version(
510513
)
511514
upgrade_room_to_version(room_id_2, "11", tok=self.tok, expect_code=400)
512515

516+
# Room not in configured list as admin
517+
admin_user_id = self.register_user("admin_kermit", "monkey", admin=True)
518+
519+
admin_tok = self.login("admin_kermit", "monkey")
520+
room_id_3 = self.helper.create_room_as(
521+
admin_user_id, room_version="9", tok=admin_tok
522+
)
523+
upgrade_room_to_version(room_id_3, "11", tok=admin_tok, expect_code=200)
524+
513525
def test_sent_event_end_up_in_room_state(self) -> None:
514526
"""Tests that a state event sent by a module while processing another state event
515527
doesn't get dropped from the state of the room. This is to guard against a bug

0 commit comments

Comments
 (0)