Skip to content

Commit

Permalink
async/await is_server_admin (matrix-org#7363)
Browse files Browse the repository at this point in the history
  • Loading branch information
anoadragon453 authored and phil-flex committed Jun 16, 2020
1 parent 7b2ec46 commit a3e46e6
Show file tree
Hide file tree
Showing 22 changed files with 410 additions and 438 deletions.
1 change: 1 addition & 0 deletions changelog.d/7363.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Convert RegistrationWorkerStore.is_server_admin and dependent code to async/await.
9 changes: 4 additions & 5 deletions synapse/api/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,8 +537,7 @@ def compute_auth_events(

return defer.succeed(auth_ids)

@defer.inlineCallbacks
def check_can_change_room_list(self, room_id: str, user: UserID):
async def check_can_change_room_list(self, room_id: str, user: UserID):
"""Determine whether the user is allowed to edit the room's entry in the
published room list.
Expand All @@ -547,17 +546,17 @@ def check_can_change_room_list(self, room_id: str, user: UserID):
user
"""

is_admin = yield self.is_server_admin(user)
is_admin = await self.is_server_admin(user)
if is_admin:
return True

user_id = user.to_string()
yield self.check_user_in_room(room_id, user_id)
await self.check_user_in_room(room_id, user_id)

# We currently require the user is a "moderator" in the room. We do this
# by checking if they would (theoretically) be able to change the
# m.room.canonical_alias events
power_level_event = yield self.state.get_current_state(
power_level_event = await self.state.get_current_state(
room_id, EventTypes.PowerLevels, ""
)

Expand Down
5 changes: 2 additions & 3 deletions synapse/federation/federation_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -976,14 +976,13 @@ async def get_missing_events(

return signed_events

@defer.inlineCallbacks
def forward_third_party_invite(self, destinations, room_id, event_dict):
async def forward_third_party_invite(self, destinations, room_id, event_dict):
for destination in destinations:
if destination == self.server_name:
continue

try:
yield self.transport_layer.exchange_third_party_invite(
await self.transport_layer.exchange_third_party_invite(
destination=destination, room_id=room_id, event_dict=event_dict
)
return None
Expand Down
64 changes: 31 additions & 33 deletions synapse/groups/groups_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -748,48 +748,48 @@ def accept_knock(self, group_id, requester_user_id, content):

raise NotImplementedError()

@defer.inlineCallbacks
def remove_user_from_group(self, group_id, user_id, requester_user_id, content):
async def remove_user_from_group(
self, group_id, user_id, requester_user_id, content
):
"""Remove a user from the group; either a user is leaving or an admin
kicked them.
"""

yield self.check_group_is_ours(group_id, requester_user_id, and_exists=True)
await self.check_group_is_ours(group_id, requester_user_id, and_exists=True)

is_kick = False
if requester_user_id != user_id:
is_admin = yield self.store.is_user_admin_in_group(
is_admin = await self.store.is_user_admin_in_group(
group_id, requester_user_id
)
if not is_admin:
raise SynapseError(403, "User is not admin in group")

is_kick = True

yield self.store.remove_user_from_group(group_id, user_id)
await self.store.remove_user_from_group(group_id, user_id)

if is_kick:
if self.hs.is_mine_id(user_id):
groups_local = self.hs.get_groups_local_handler()
yield groups_local.user_removed_from_group(group_id, user_id, {})
await groups_local.user_removed_from_group(group_id, user_id, {})
else:
yield self.transport_client.remove_user_from_group_notification(
await self.transport_client.remove_user_from_group_notification(
get_domain_from_id(user_id), group_id, user_id, {}
)

if not self.hs.is_mine_id(user_id):
yield self.store.maybe_delete_remote_profile_cache(user_id)
await self.store.maybe_delete_remote_profile_cache(user_id)

# Delete group if the last user has left
users = yield self.store.get_users_in_group(group_id, include_private=True)
users = await self.store.get_users_in_group(group_id, include_private=True)
if not users:
yield self.store.delete_group(group_id)
await self.store.delete_group(group_id)

return {}

@defer.inlineCallbacks
def create_group(self, group_id, requester_user_id, content):
group = yield self.check_group_is_ours(group_id, requester_user_id)
async def create_group(self, group_id, requester_user_id, content):
group = await self.check_group_is_ours(group_id, requester_user_id)

logger.info("Attempting to create group with ID: %r", group_id)

Expand All @@ -799,7 +799,7 @@ def create_group(self, group_id, requester_user_id, content):
if group:
raise SynapseError(400, "Group already exists")

is_admin = yield self.auth.is_server_admin(
is_admin = await self.auth.is_server_admin(
UserID.from_string(requester_user_id)
)
if not is_admin:
Expand All @@ -822,7 +822,7 @@ def create_group(self, group_id, requester_user_id, content):
long_description = profile.get("long_description")
user_profile = content.get("user_profile", {})

yield self.store.create_group(
await self.store.create_group(
group_id,
requester_user_id,
name=name,
Expand All @@ -834,7 +834,7 @@ def create_group(self, group_id, requester_user_id, content):
if not self.hs.is_mine_id(requester_user_id):
remote_attestation = content["attestation"]

yield self.attestations.verify_attestation(
await self.attestations.verify_attestation(
remote_attestation, user_id=requester_user_id, group_id=group_id
)

Expand All @@ -845,7 +845,7 @@ def create_group(self, group_id, requester_user_id, content):
local_attestation = None
remote_attestation = None

yield self.store.add_user_to_group(
await self.store.add_user_to_group(
group_id,
requester_user_id,
is_admin=True,
Expand All @@ -855,16 +855,15 @@ def create_group(self, group_id, requester_user_id, content):
)

if not self.hs.is_mine_id(requester_user_id):
yield self.store.add_remote_profile_cache(
await self.store.add_remote_profile_cache(
requester_user_id,
displayname=user_profile.get("displayname"),
avatar_url=user_profile.get("avatar_url"),
)

return {"group_id": group_id}

@defer.inlineCallbacks
def delete_group(self, group_id, requester_user_id):
async def delete_group(self, group_id, requester_user_id):
"""Deletes a group, kicking out all current members.
Only group admins or server admins can call this request
Expand All @@ -877,33 +876,32 @@ def delete_group(self, group_id, requester_user_id):
Deferred
"""

yield self.check_group_is_ours(group_id, requester_user_id, and_exists=True)
await self.check_group_is_ours(group_id, requester_user_id, and_exists=True)

# Only server admins or group admins can delete groups.

is_admin = yield self.store.is_user_admin_in_group(group_id, requester_user_id)
is_admin = await self.store.is_user_admin_in_group(group_id, requester_user_id)

if not is_admin:
is_admin = yield self.auth.is_server_admin(
is_admin = await self.auth.is_server_admin(
UserID.from_string(requester_user_id)
)

if not is_admin:
raise SynapseError(403, "User is not an admin")

# Before deleting the group lets kick everyone out of it
users = yield self.store.get_users_in_group(group_id, include_private=True)
users = await self.store.get_users_in_group(group_id, include_private=True)

@defer.inlineCallbacks
def _kick_user_from_group(user_id):
async def _kick_user_from_group(user_id):
if self.hs.is_mine_id(user_id):
groups_local = self.hs.get_groups_local_handler()
yield groups_local.user_removed_from_group(group_id, user_id, {})
await groups_local.user_removed_from_group(group_id, user_id, {})
else:
yield self.transport_client.remove_user_from_group_notification(
await self.transport_client.remove_user_from_group_notification(
get_domain_from_id(user_id), group_id, user_id, {}
)
yield self.store.maybe_delete_remote_profile_cache(user_id)
await self.store.maybe_delete_remote_profile_cache(user_id)

# We kick users out in the order of:
# 1. Non-admins
Expand All @@ -922,11 +920,11 @@ def _kick_user_from_group(user_id):
else:
non_admins.append(u["user_id"])

yield concurrently_execute(_kick_user_from_group, non_admins, 10)
yield concurrently_execute(_kick_user_from_group, admins, 10)
yield _kick_user_from_group(requester_user_id)
await concurrently_execute(_kick_user_from_group, non_admins, 10)
await concurrently_execute(_kick_user_from_group, admins, 10)
await _kick_user_from_group(requester_user_id)

yield self.store.delete_group(group_id)
await self.store.delete_group(group_id)


def _parse_join_policy_from_contents(content):
Expand Down
16 changes: 7 additions & 9 deletions synapse/handlers/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,30 +126,28 @@ def ratelimit(self, requester, update=True, is_admin_redaction=False):
retry_after_ms=int(1000 * (time_allowed - time_now))
)

@defer.inlineCallbacks
def maybe_kick_guest_users(self, event, context=None):
async def maybe_kick_guest_users(self, event, context=None):
# Technically this function invalidates current_state by changing it.
# Hopefully this isn't that important to the caller.
if event.type == EventTypes.GuestAccess:
guest_access = event.content.get("guest_access", "forbidden")
if guest_access != "can_join":
if context:
current_state_ids = yield context.get_current_state_ids()
current_state = yield self.store.get_events(
current_state_ids = await context.get_current_state_ids()
current_state = await self.store.get_events(
list(current_state_ids.values())
)
else:
current_state = yield self.state_handler.get_current_state(
current_state = await self.state_handler.get_current_state(
event.room_id
)

current_state = list(current_state.values())

logger.info("maybe_kick_guest_users %r", current_state)
yield self.kick_guest_users(current_state)
await self.kick_guest_users(current_state)

@defer.inlineCallbacks
def kick_guest_users(self, current_state):
async def kick_guest_users(self, current_state):
for member_event in current_state:
try:
if member_event.type != EventTypes.Member:
Expand Down Expand Up @@ -180,7 +178,7 @@ def kick_guest_users(self, current_state):
# homeserver.
requester = synapse.types.create_requester(target_user, is_guest=True)
handler = self.hs.get_room_member_handler()
yield handler.update_membership(
await handler.update_membership(
requester,
target_user,
member_event.room_id,
Expand Down
Loading

0 comments on commit a3e46e6

Please sign in to comment.