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

Move maybe_kick_guest_users out of BaseHandler #10744

Merged
merged 5 commits into from
Sep 6, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 2 additions & 48 deletions synapse/handlers/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@
import logging
from typing import TYPE_CHECKING, Optional

import synapse.types
from synapse.api.constants import EventTypes, Membership
from synapse.api.constants import EventTypes
from synapse.api.ratelimiting import Ratelimiter
from synapse.types import UserID

if TYPE_CHECKING:
from synapse.server import HomeServer
Expand Down Expand Up @@ -135,48 +133,4 @@ async def maybe_kick_guest_users(self, event, context=None):
current_state = list(current_state_map.values())

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

async def kick_guest_users(self, current_state):
for member_event in current_state:
try:
if member_event.type != EventTypes.Member:
continue

target_user = UserID.from_string(member_event.state_key)
if not self.hs.is_mine(target_user):
continue

if member_event.content["membership"] not in {
Membership.JOIN,
Membership.INVITE,
}:
continue

if (
"kind" not in member_event.content
or member_event.content["kind"] != "guest"
):
continue

# We make the user choose to leave, rather than have the
# event-sender kick them. This is partially because we don't
# need to worry about power levels, and partially because guest
# users are a concept which doesn't hugely work over federation,
# and having homeservers have their own users leave keeps more
# of that decision-making and control local to the guest-having
# homeserver.
requester = synapse.types.create_requester(
target_user, is_guest=True, authenticated_entity=self.server_name
)
handler = self.hs.get_room_member_handler()
await handler.update_membership(
requester,
target_user,
member_event.room_id,
"leave",
ratelimit=False,
require_consent=False,
)
except Exception as e:
logger.exception("Error kicking guest user: %s" % (e,))
await self.hs.get_room_member_handler().kick_guest_users(current_state)
60 changes: 56 additions & 4 deletions synapse/handlers/room_member.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
RoomID,
StateMap,
UserID,
create_requester,
get_domain_from_id,
)
from synapse.util.async_helpers import Linearizer
Expand All @@ -70,6 +71,7 @@ def __init__(self, hs: "HomeServer"):
self.auth = hs.get_auth()
self.state_handler = hs.get_state_handler()
self.config = hs.config
self._server_name = hs.hostname

self.federation_handler = hs.get_federation_handler()
self.directory_handler = hs.get_directory_handler()
Expand Down Expand Up @@ -115,9 +117,8 @@ def __init__(self, hs: "HomeServer"):
burst_count=hs.config.ratelimiting.rc_invites_per_user.burst_count,
)

# This is only used to get at ratelimit function, and
# maybe_kick_guest_users. It's fine there are multiple of these as
# it doesn't store state.
# This is only used to get at the ratelimit function. It's fine there are
# multiple of these as it doesn't store state.
self.base_handler = BaseHandler(hs)

@abc.abstractmethod
Expand Down Expand Up @@ -1099,6 +1100,58 @@ async def _can_guest_join(self, current_state_ids: StateMap[str]) -> bool:
and guest_access.content["guest_access"] == "can_join"
)

async def kick_guest_users(self, current_state: Iterable[EventBase]) -> None:
"""Kick any local guest users from the room.

This is called when the room state changes from guests allowed to not-allowed.

Params:
current_state: the current state of the room. We will iterate this to look
for guest users to kick.
"""
for member_event in current_state:
try:
if member_event.type != EventTypes.Member:
continue

if not self.hs.is_mine_id(member_event.state_key):
continue

if member_event.content["membership"] not in {
Membership.JOIN,
Membership.INVITE,
}:
continue

if (
"kind" not in member_event.content
or member_event.content["kind"] != "guest"
):
continue

# We make the user choose to leave, rather than have the
# event-sender kick them. This is partially because we don't
# need to worry about power levels, and partially because guest
# users are a concept which doesn't hugely work over federation,
# and having homeservers have their own users leave keeps more
# of that decision-making and control local to the guest-having
# homeserver.
target_user = UserID.from_string(member_event.state_key)
requester = create_requester(
target_user, is_guest=True, authenticated_entity=self._server_name
)
handler = self.hs.get_room_member_handler()
await handler.update_membership(
requester,
target_user,
member_event.room_id,
"leave",
ratelimit=False,
require_consent=False,
)
except Exception as e:
logger.exception("Error kicking guest user: %s" % (e,))

async def lookup_room_alias(
self, room_alias: RoomAlias
) -> Tuple[RoomID, List[str]]:
Expand Down Expand Up @@ -1352,7 +1405,6 @@ def __init__(self, hs: "HomeServer"):

self.distributor = hs.get_distributor()
self.distributor.declare("user_left_room")
self._server_name = hs.hostname
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is now redundant, since RoomMemberHandler has its own _server_name.


async def _is_remote_room_too_complex(
self, room_id: str, remote_room_hosts: List[str]
Expand Down