Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
async/await is_server_admin call in _user_can_delete_alias
Browse files Browse the repository at this point in the history
  • Loading branch information
anoadragon453 committed Feb 3, 2020
1 parent 3700805 commit 53488a4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 33 deletions.
20 changes: 9 additions & 11 deletions synapse/handlers/directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,7 @@ def create_association(
# permission in the room; this is permitted.
logger.info("Skipping updating aliases event due to auth error %s", e)

@defer.inlineCallbacks
def delete_association(self, requester, room_alias, send_event=True):
async def delete_association(self, requester, room_alias, send_event=True):
"""Remove an alias from the directory
(this is only meant for human users; AS users should call
Expand All @@ -186,7 +185,7 @@ def delete_association(self, requester, room_alias, send_event=True):
user_id = requester.user.to_string()

try:
can_delete = yield self._user_can_delete_alias(room_alias, user_id)
can_delete = await self._user_can_delete_alias(room_alias, user_id)
except StoreError as e:
if e.code == 404:
raise NotFoundError("Unknown room alias")
Expand All @@ -195,21 +194,21 @@ def delete_association(self, requester, room_alias, send_event=True):
if not can_delete:
raise AuthError(403, "You don't have permission to delete the alias.")

can_delete = yield self.can_modify_alias(room_alias, user_id=user_id)
can_delete = await self.can_modify_alias(room_alias, user_id=user_id)
if not can_delete:
raise SynapseError(
400,
"This alias is reserved by an application service.",
errcode=Codes.EXCLUSIVE,
)

room_id = yield self._delete_association(room_alias)
room_id = await self._delete_association(room_alias)

try:
if send_event:
yield self.send_room_alias_update_event(requester, room_id)
await self.send_room_alias_update_event(requester, room_id)

yield self._update_canonical_alias(
await self._update_canonical_alias(
requester, requester.user.to_string(), room_id, room_alias
)
except AuthError as e:
Expand Down Expand Up @@ -368,14 +367,13 @@ def can_modify_alias(self, alias, user_id=None):
# either no interested services, or no service with an exclusive lock
return defer.succeed(True)

@defer.inlineCallbacks
def _user_can_delete_alias(self, alias, user_id):
creator = yield self.store.get_room_alias_creator(alias.to_string())
async def _user_can_delete_alias(self, alias, user_id):
creator = await self.store.get_room_alias_creator(alias.to_string())

if creator is not None and creator == user_id:
return True

is_admin = yield self.auth.is_server_admin(UserID.from_string(user_id))
is_admin = await self.auth.is_server_admin(UserID.from_string(user_id))
return is_admin

@defer.inlineCallbacks
Expand Down
42 changes: 20 additions & 22 deletions synapse/handlers/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,14 @@ def upgrade_room(

return ret

@defer.inlineCallbacks
def _upgrade_room(self, requester, old_room_id, new_version):
async def _upgrade_room(self, requester, old_room_id, new_version):
user_id = requester.user.to_string()

# start by allocating a new room id
r = yield self.store.get_room(old_room_id)
r = await self.store.get_room(old_room_id)
if r is None:
raise NotFoundError("Unknown room id %s" % (old_room_id,))
new_room_id = yield self._generate_room_id(
new_room_id = await self._generate_room_id(
creator_id=user_id, is_public=r["is_public"], room_version=new_version,
)

Expand All @@ -164,7 +163,7 @@ def _upgrade_room(self, requester, old_room_id, new_version):
(
tombstone_event,
tombstone_context,
) = yield self.event_creation_handler.create_event(
) = await self.event_creation_handler.create_event(
requester,
{
"type": EventTypes.Tombstone,
Expand All @@ -178,12 +177,12 @@ def _upgrade_room(self, requester, old_room_id, new_version):
},
token_id=requester.access_token_id,
)
old_room_version = yield self.store.get_room_version_id(old_room_id)
yield self.auth.check_from_context(
old_room_version = await self.store.get_room_version_id(old_room_id)
await self.auth.check_from_context(
old_room_version, tombstone_event, tombstone_context
)

yield self.clone_existing_room(
await self.clone_existing_room(
requester,
old_room_id=old_room_id,
new_room_id=new_room_id,
Expand All @@ -192,25 +191,25 @@ def _upgrade_room(self, requester, old_room_id, new_version):
)

# now send the tombstone
yield self.event_creation_handler.send_nonmember_event(
await self.event_creation_handler.send_nonmember_event(
requester, tombstone_event, tombstone_context
)

old_room_state = yield tombstone_context.get_current_state_ids()
old_room_state = await tombstone_context.get_current_state_ids()

# update any aliases
yield self._move_aliases_to_new_room(
await self._move_aliases_to_new_room(
requester, old_room_id, new_room_id, old_room_state
)

# Copy over user push rules, tags and migrate room directory state
yield self.room_member_handler.transfer_room_state_on_room_upgrade(
await self.room_member_handler.transfer_room_state_on_room_upgrade(
old_room_id, new_room_id
)

# finally, shut down the PLs in the old room, and update them in the new
# room.
yield self._update_upgraded_room_pls(
await self._update_upgraded_room_pls(
requester, old_room_id, new_room_id, old_room_state,
)

Expand Down Expand Up @@ -443,19 +442,18 @@ def clone_existing_room(
# XXX invites/joins
# XXX 3pid invites

@defer.inlineCallbacks
def _move_aliases_to_new_room(
async def _move_aliases_to_new_room(
self, requester, old_room_id, new_room_id, old_room_state
):
directory_handler = self.hs.get_handlers().directory_handler

aliases = yield self.store.get_aliases_for_room(old_room_id)
aliases = await self.store.get_aliases_for_room(old_room_id)

# check to see if we have a canonical alias.
canonical_alias = None
canonical_alias_event_id = old_room_state.get((EventTypes.CanonicalAlias, ""))
if canonical_alias_event_id:
canonical_alias_event = yield self.store.get_event(canonical_alias_event_id)
canonical_alias_event = await self.store.get_event(canonical_alias_event_id)
if canonical_alias_event:
canonical_alias = canonical_alias_event.content.get("alias", "")

Expand All @@ -475,7 +473,7 @@ def _move_aliases_to_new_room(
for alias_str in aliases:
alias = RoomAlias.from_string(alias_str)
try:
yield directory_handler.delete_association(
await directory_handler.delete_association(
requester, alias, send_event=False
)
removed_aliases.append(alias_str)
Expand All @@ -496,14 +494,14 @@ def _move_aliases_to_new_room(
# as when you remove an alias from the directory normally - it just means that
# the aliases event gets out of sync with the directory
# (cf https://github.com/vector-im/riot-web/issues/2369)
yield directory_handler.send_room_alias_update_event(requester, old_room_id)
await directory_handler.send_room_alias_update_event(requester, old_room_id)
except AuthError as e:
logger.warning("Failed to send updated alias event on old room: %s", e)

# we can now add any aliases we successfully removed to the new room.
for alias in removed_aliases:
try:
yield directory_handler.create_association(
await directory_handler.create_association(
requester,
RoomAlias.from_string(alias),
new_room_id,
Expand All @@ -519,7 +517,7 @@ def _move_aliases_to_new_room(

try:
if canonical_alias and (canonical_alias in removed_aliases):
yield self.event_creation_handler.create_and_send_nonmember_event(
await self.event_creation_handler.create_and_send_nonmember_event(
requester,
{
"type": EventTypes.CanonicalAlias,
Expand All @@ -531,7 +529,7 @@ def _move_aliases_to_new_room(
ratelimit=False,
)

yield directory_handler.send_room_alias_update_event(requester, new_room_id)
await directory_handler.send_room_alias_update_event(requester, new_room_id)
except SynapseError as e:
# again I'm not really expecting this to fail, but if it does, I'd rather
# we returned the new room to the client at this point.
Expand Down

0 comments on commit 53488a4

Please sign in to comment.