async/await is_server_admin call from _user_can_delete_alias

pull/6839/head
Andrew Morgan 2020-02-03 18:02:11 +00:00
parent 433d5bcf61
commit dd0359951b
2 changed files with 46 additions and 41 deletions

View File

@ -28,7 +28,7 @@ from synapse.api.errors import (
StoreError, StoreError,
SynapseError, SynapseError,
) )
from synapse.types import RoomAlias, UserID, get_domain_from_id from synapse.types import Requester, RoomAlias, UserID, get_domain_from_id
from ._base import BaseHandler from ._base import BaseHandler
@ -158,22 +158,23 @@ class DirectoryHandler(BaseHandler):
# permission in the room; this is permitted. # permission in the room; this is permitted.
logger.info("Skipping updating aliases event due to auth error %s", e) logger.info("Skipping updating aliases event due to auth error %s", e)
@defer.inlineCallbacks async def delete_association(
def delete_association(self, requester, room_alias, send_event=True): self, requester: Requester, room_alias: RoomAlias, send_event: bool = True,
) -> str:
"""Remove an alias from the directory """Remove an alias from the directory
(this is only meant for human users; AS users should call (this is only meant for human users; AS users should call
delete_appservice_association) delete_appservice_association)
Args: Args:
requester (Requester): requester:
room_alias (RoomAlias): room_alias:
send_event (bool): Whether to send an updated m.room.aliases event. send_event: Whether to send an updated m.room.aliases event. Note
Note that, if we delete the canonical alias, we will always attempt that, if we delete the canonical alias, we will always attempt to
to send an m.room.canonical_alias event send an m.room.canonical_alias event
Returns: Returns:
Deferred[unicode]: room id that the alias used to point to Room id that the alias used to point to
Raises: Raises:
NotFoundError: if the alias doesn't exist NotFoundError: if the alias doesn't exist
@ -186,7 +187,7 @@ class DirectoryHandler(BaseHandler):
user_id = requester.user.to_string() user_id = requester.user.to_string()
try: 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: except StoreError as e:
if e.code == 404: if e.code == 404:
raise NotFoundError("Unknown room alias") raise NotFoundError("Unknown room alias")
@ -195,7 +196,7 @@ class DirectoryHandler(BaseHandler):
if not can_delete: if not can_delete:
raise AuthError(403, "You don't have permission to delete the alias.") 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: if not can_delete:
raise SynapseError( raise SynapseError(
400, 400,
@ -203,13 +204,13 @@ class DirectoryHandler(BaseHandler):
errcode=Codes.EXCLUSIVE, errcode=Codes.EXCLUSIVE,
) )
room_id = yield self._delete_association(room_alias) room_id = await self._delete_association(room_alias)
try: try:
if send_event: 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 requester, requester.user.to_string(), room_id, room_alias
) )
except AuthError as e: except AuthError as e:
@ -368,14 +369,13 @@ class DirectoryHandler(BaseHandler):
# either no interested services, or no service with an exclusive lock # either no interested services, or no service with an exclusive lock
return defer.succeed(True) return defer.succeed(True)
@defer.inlineCallbacks async def _user_can_delete_alias(self, alias: RoomAlias, user_id: str) -> bool:
def _user_can_delete_alias(self, alias, user_id): creator = await self.store.get_room_alias_creator(alias.to_string())
creator = yield self.store.get_room_alias_creator(alias.to_string())
if creator is not None and creator == user_id: if creator is not None and creator == user_id:
return True 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 return is_admin
@defer.inlineCallbacks @defer.inlineCallbacks

View File

@ -22,6 +22,7 @@ import logging
import math import math
import string import string
from collections import OrderedDict from collections import OrderedDict
from types import Dict
from six import iteritems, string_types from six import iteritems, string_types
@ -145,15 +146,16 @@ class RoomCreationHandler(BaseHandler):
return ret return ret
@defer.inlineCallbacks async def _upgrade_room(
def _upgrade_room(self, requester, old_room_id, new_version): self, requester: Requester, old_room_id: str, new_version: RoomVersion,
) -> RoomID:
user_id = requester.user.to_string() user_id = requester.user.to_string()
# start by allocating a new room id # 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: if r is None:
raise NotFoundError("Unknown room id %s" % (old_room_id,)) 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, creator_id=user_id, is_public=r["is_public"], room_version=new_version,
) )
@ -164,7 +166,7 @@ class RoomCreationHandler(BaseHandler):
( (
tombstone_event, tombstone_event,
tombstone_context, tombstone_context,
) = yield self.event_creation_handler.create_event( ) = await self.event_creation_handler.create_event(
requester, requester,
{ {
"type": EventTypes.Tombstone, "type": EventTypes.Tombstone,
@ -178,12 +180,12 @@ class RoomCreationHandler(BaseHandler):
}, },
token_id=requester.access_token_id, token_id=requester.access_token_id,
) )
old_room_version = yield self.store.get_room_version_id(old_room_id) old_room_version = await self.store.get_room_version_id(old_room_id)
yield self.auth.check_from_context( await self.auth.check_from_context(
old_room_version, tombstone_event, tombstone_context old_room_version, tombstone_event, tombstone_context
) )
yield self.clone_existing_room( await self.clone_existing_room(
requester, requester,
old_room_id=old_room_id, old_room_id=old_room_id,
new_room_id=new_room_id, new_room_id=new_room_id,
@ -192,25 +194,25 @@ class RoomCreationHandler(BaseHandler):
) )
# now send the tombstone # 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 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 # 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 requester, old_room_id, new_room_id, old_room_state
) )
# Copy over user push rules, tags and migrate room directory 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 old_room_id, new_room_id
) )
# finally, shut down the PLs in the old room, and update them in the new # finally, shut down the PLs in the old room, and update them in the new
# room. # room.
yield self._update_upgraded_room_pls( await self._update_upgraded_room_pls(
requester, old_room_id, new_room_id, old_room_state, requester, old_room_id, new_room_id, old_room_state,
) )
@ -443,19 +445,22 @@ class RoomCreationHandler(BaseHandler):
# XXX invites/joins # XXX invites/joins
# XXX 3pid invites # XXX 3pid invites
@defer.inlineCallbacks async def _move_aliases_to_new_room(
def _move_aliases_to_new_room( self,
self, requester, old_room_id, new_room_id, old_room_state requester: Requester,
old_room_id: str,
new_room_id: str,
old_room_state: Dict[(str, str), str],
): ):
directory_handler = self.hs.get_handlers().directory_handler 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. # check to see if we have a canonical alias.
canonical_alias = None canonical_alias = None
canonical_alias_event_id = old_room_state.get((EventTypes.CanonicalAlias, "")) canonical_alias_event_id = old_room_state.get((EventTypes.CanonicalAlias, ""))
if canonical_alias_event_id: 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: if canonical_alias_event:
canonical_alias = canonical_alias_event.content.get("alias", "") canonical_alias = canonical_alias_event.content.get("alias", "")
@ -475,7 +480,7 @@ class RoomCreationHandler(BaseHandler):
for alias_str in aliases: for alias_str in aliases:
alias = RoomAlias.from_string(alias_str) alias = RoomAlias.from_string(alias_str)
try: try:
yield directory_handler.delete_association( await directory_handler.delete_association(
requester, alias, send_event=False requester, alias, send_event=False
) )
removed_aliases.append(alias_str) removed_aliases.append(alias_str)
@ -496,14 +501,14 @@ class RoomCreationHandler(BaseHandler):
# as when you remove an alias from the directory normally - it just means that # as when you remove an alias from the directory normally - it just means that
# the aliases event gets out of sync with the directory # the aliases event gets out of sync with the directory
# (cf https://github.com/vector-im/riot-web/issues/2369) # (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: except AuthError as e:
logger.warning("Failed to send updated alias event on old room: %s", 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. # we can now add any aliases we successfully removed to the new room.
for alias in removed_aliases: for alias in removed_aliases:
try: try:
yield directory_handler.create_association( await directory_handler.create_association(
requester, requester,
RoomAlias.from_string(alias), RoomAlias.from_string(alias),
new_room_id, new_room_id,
@ -519,7 +524,7 @@ class RoomCreationHandler(BaseHandler):
try: try:
if canonical_alias and (canonical_alias in removed_aliases): 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, requester,
{ {
"type": EventTypes.CanonicalAlias, "type": EventTypes.CanonicalAlias,
@ -531,7 +536,7 @@ class RoomCreationHandler(BaseHandler):
ratelimit=False, 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: except SynapseError as e:
# again I'm not really expecting this to fail, but if it does, I'd rather # 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. # we returned the new room to the client at this point.