Add beginnings of ban support.

This commit is contained in:
Erik Johnston 2014-09-01 16:15:34 +01:00
parent 865469f233
commit db7109c43b
8 changed files with 122 additions and 29 deletions

View file

@ -57,6 +57,8 @@ class Auth(object):
) )
if hasattr(event, "state_key"): if hasattr(event, "state_key"):
# TODO (erikj): This really only should be called for *new*
# state
yield self._can_add_state(event) yield self._can_add_state(event)
else: else:
yield self._can_send_event(event) yield self._can_send_event(event)
@ -152,12 +154,29 @@ class Auth(object):
# TODO (erikj): private rooms # TODO (erikj): private rooms
raise AuthError(403, "You are not allowed to join this room") raise AuthError(403, "You are not allowed to join this room")
elif Membership.LEAVE == membership: elif Membership.LEAVE == membership:
# TODO (erikj): Implement kicks.
if not caller_in_room: # trying to leave a room you aren't joined if not caller_in_room: # trying to leave a room you aren't joined
raise AuthError(403, "You are not in room %s." % event.room_id) raise AuthError(403, "You are not in room %s." % event.room_id)
elif target_user_id != event.user_id: elif target_user_id != event.user_id:
# trying to force another user to leave # trying to force another user to leave
raise AuthError(403, "Cannot force %s to leave." % raise AuthError(403, "Cannot force %s to leave." %
target_user_id) target_user_id)
elif Membership.BAN == membership:
user_level = yield self.store.get_power_level(
event.room_id,
event.user_id,
)
ban_level, _ = yield self.store.get_ops_levels(event.room_id)
if ban_level:
ban_level = int(ban_level)
else:
ban_level = 5 # FIXME (erikj): What should we do here?
if ban_level < user_level:
raise AuthError(403, "You don't have permission to ban")
else: else:
raise AuthError(500, "Unknown membership %s" % membership) raise AuthError(500, "Unknown membership %s" % membership)

View file

@ -23,7 +23,8 @@ class Membership(object):
JOIN = u"join" JOIN = u"join"
KNOCK = u"knock" KNOCK = u"knock"
LEAVE = u"leave" LEAVE = u"leave"
LIST = (INVITE, JOIN, KNOCK, LEAVE) BAN = u"ban"
LIST = (INVITE, JOIN, KNOCK, LEAVE, BAN)
class Feedback(object): class Feedback(object):

View file

@ -16,7 +16,7 @@
from synapse.api.events.room import ( from synapse.api.events.room import (
RoomTopicEvent, MessageEvent, RoomMemberEvent, FeedbackEvent, RoomTopicEvent, MessageEvent, RoomMemberEvent, FeedbackEvent,
InviteJoinEvent, RoomConfigEvent, RoomNameEvent, GenericEvent, InviteJoinEvent, RoomConfigEvent, RoomNameEvent, GenericEvent,
RoomPowerLevelsEvent, RoomJoinRulesEvent, RoomPowerLevelsEvent, RoomJoinRulesEvent, RoomOpsPowerLevelsEvent,
RoomCreateEvent, RoomAddStateLevelEvent, RoomSendEventLevelEvent RoomCreateEvent, RoomAddStateLevelEvent, RoomSendEventLevelEvent
) )
@ -38,6 +38,7 @@ class EventFactory(object):
RoomCreateEvent, RoomCreateEvent,
RoomAddStateLevelEvent, RoomAddStateLevelEvent,
RoomSendEventLevelEvent, RoomSendEventLevelEvent,
RoomOpsPowerLevelsEvent,
] ]
def __init__(self, hs): def __init__(self, hs):

View file

@ -167,3 +167,10 @@ class RoomSendEventLevelEvent(SynapseStateEvent):
def get_content_template(self): def get_content_template(self):
return {} return {}
class RoomOpsPowerLevelsEvent(SynapseStateEvent):
TYPE = "m.room.ops_levels"
def get_content_template(self):
return {}

View file

@ -22,7 +22,7 @@ from synapse.api.errors import StoreError, SynapseError
from synapse.api.events.room import ( from synapse.api.events.room import (
RoomMemberEvent, RoomCreateEvent, RoomPowerLevelsEvent, RoomMemberEvent, RoomCreateEvent, RoomPowerLevelsEvent,
RoomJoinRulesEvent, RoomAddStateLevelEvent, RoomJoinRulesEvent, RoomAddStateLevelEvent,
RoomSendEventLevelEvent, RoomSendEventLevelEvent, RoomOpsPowerLevelsEvent,
) )
from synapse.util import stringutils from synapse.util import stringutils
from ._base import BaseRoomHandler from ._base import BaseRoomHandler
@ -151,35 +151,44 @@ class RoomCreationHandler(BaseRoomHandler):
"user_id": creator.to_string(), "user_id": creator.to_string(),
} }
creation_event = self.event_factory.create_event( def create(etype, **content):
return self.event_factory.create_event(
etype=etype,
content=content,
**event_keys
)
creation_event = create(
etype=RoomCreateEvent.TYPE, etype=RoomCreateEvent.TYPE,
content={"creator": creator.to_string(), "default": 0}, creator=creator.to_string(),
**event_keys default=0,
) )
power_levels_event = self.event_factory.create_event( power_levels_event = create(
etype=RoomPowerLevelsEvent.TYPE, etype=RoomPowerLevelsEvent.TYPE,
content={creator.to_string(): 10}, **{creator.to_string(): 10}
**event_keys
) )
join_rule = JoinRules.PUBLIC if is_public else JoinRules.INVITE join_rule = JoinRules.PUBLIC if is_public else JoinRules.INVITE
join_rules_event = self.event_factory.create_event( join_rules_event = create(
etype=RoomJoinRulesEvent.TYPE, etype=RoomJoinRulesEvent.TYPE,
content={"join_rule": join_rule}, join_rule=join_rule,
**event_keys
) )
add_state_event = self.event_factory.create_event( add_state_event = create(
etype=RoomAddStateLevelEvent.TYPE, etype=RoomAddStateLevelEvent.TYPE,
content={"level": 10}, level=10,
**event_keys
) )
send_event = self.event_factory.create_event( send_event = create(
etype=RoomSendEventLevelEvent.TYPE, etype=RoomSendEventLevelEvent.TYPE,
content={"level": 0}, level=0,
**event_keys )
ops = create(
etype=RoomOpsPowerLevelsEvent.TYPE,
ban_level=5,
kick_level=5,
) )
return [ return [
@ -188,6 +197,7 @@ class RoomCreationHandler(BaseRoomHandler):
join_rules_event, join_rules_event,
add_state_event, add_state_event,
send_event, send_event,
ops,
] ]
@ -493,10 +503,9 @@ class RoomMemberHandler(BaseRoomHandler):
host = target_user.domain host = target_user.domain
destinations.append(host) destinations.append(host)
# If we are joining a remote HS, include that. # Always include target domain
if membership == Membership.JOIN: host = target_user.domain
host = target_user.domain destinations.append(host)
destinations.append(host)
return self._on_new_room_event( return self._on_new_room_event(
event, snapshot, extra_destinations=destinations, event, snapshot, extra_destinations=destinations,

View file

@ -127,8 +127,6 @@ class DataStore(RoomMemberStore, RoomStore,
self._store_room_member_txn(txn, event) self._store_room_member_txn(txn, event)
elif event.type == FeedbackEvent.TYPE: elif event.type == FeedbackEvent.TYPE:
self._store_feedback_txn(txn, event) self._store_feedback_txn(txn, event)
# elif event.type == RoomConfigEvent.TYPE:
# self._store_room_config_txn(txn, event)
elif event.type == RoomNameEvent.TYPE: elif event.type == RoomNameEvent.TYPE:
self._store_room_name_txn(txn, event) self._store_room_name_txn(txn, event)
elif event.type == RoomTopicEvent.TYPE: elif event.type == RoomTopicEvent.TYPE:

View file

@ -27,6 +27,9 @@ import logging
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
OpsLevel = collections.namedtuple("OpsLevel", ("ban_level", "kick_level"))
class RoomStore(SQLBaseStore): class RoomStore(SQLBaseStore):
@defer.inlineCallbacks @defer.inlineCallbacks
@ -145,8 +148,13 @@ class RoomStore(SQLBaseStore):
else: else:
defer.returnValue(None) defer.returnValue(None)
@defer.inlineCallbacks
def get_power_level(self, room_id, user_id): def get_power_level(self, room_id, user_id):
return self._db_pool.runInteraction(
self._get_power_level,
room_id, user_id,
)
def _get_power_level(self, txn, room_id, user_id):
sql = ( sql = (
"SELECT level FROM room_power_levels as r " "SELECT level FROM room_power_levels as r "
"INNER JOIN current_state_events as c " "INNER JOIN current_state_events as c "
@ -154,7 +162,7 @@ class RoomStore(SQLBaseStore):
"WHERE c.room_id = ? AND r.user_id = ? " "WHERE c.room_id = ? AND r.user_id = ? "
) )
rows = yield self._execute(None, sql, room_id, user_id) rows = txn.execute(sql, (room_id, user_id,)).fetchall()
if len(rows) == 1: if len(rows) == 1:
defer.returnValue(rows[0][0]) defer.returnValue(rows[0][0])
@ -167,12 +175,33 @@ class RoomStore(SQLBaseStore):
"WHERE c.room_id = ? " "WHERE c.room_id = ? "
) )
rows = yield self._execute(None, sql, room_id) rows = txn.execute(sql, (room_id,)).fetchall()
if len(rows) == 1: if len(rows) == 1:
defer.returnValue(rows[0][0]) return rows[0][0]
else: else:
defer.returnValue(None) return None
def get_ops_levels(self, room_id):
return self._db_pool.runInteraction(
self._get_ops_levels,
room_id,
)
def _get_ops_levels(self, txn, room_id):
sql = (
"SELECT ban_level, kick_level FROM room_ops_levels as r "
"INNER JOIN current_state_events as c "
"ON r.event_id = c.event_id "
"WHERE c.room_id = ? "
)
rows = txn.execute(sql, (room_id,)).fetchall()
if len(rows) == 1:
return OpsLevel(rows[0][0], rows[0][1])
else:
return OpsLevel(None, None)
def get_add_state_level(self, room_id): def get_add_state_level(self, room_id):
return self._get_level_from_table("room_add_state_levels", room_id) return self._get_level_from_table("room_add_state_levels", room_id)
@ -286,6 +315,24 @@ class RoomStore(SQLBaseStore):
}, },
) )
def _store_ops_level(self, txn, event):
content = {
"event_id": event.event_id,
"room_id": event.room_id,
}
if "kick_level" in event.content:
content["kick_level"] = event.content["kick_level"]
if "ban_level" in event.content:
content["ban_level"] = event.content["ban_level"]
self._simple_insert_txn(
txn,
"room_send_event_levels",
content,
)
class RoomsTable(Table): class RoomsTable(Table):
table_name = "rooms" table_name = "rooms"

View file

@ -146,6 +146,17 @@ CREATE INDEX IF NOT EXISTS room_send_event_levels_event_id ON room_send_event_le
CREATE INDEX IF NOT EXISTS room_send_event_levels_room_id ON room_send_event_levels(room_id); CREATE INDEX IF NOT EXISTS room_send_event_levels_room_id ON room_send_event_levels(room_id);
CREATE TABLE IF NOT EXISTS room_ops_levels(
event_id TEXT NOT NULL,
room_id TEXT NOT NULL,
ban_level INTEGER,
kick_level INTEGER,
);
CREATE INDEX IF NOT EXISTS room_ops_levels_event_id ON room_ops_levels(event_id);
CREATE INDEX IF NOT EXISTS room_ops_levels_room_id ON room_ops_levels(room_id);
CREATE TABLE IF NOT EXISTS room_hosts( CREATE TABLE IF NOT EXISTS room_hosts(
room_id TEXT NOT NULL, room_id TEXT NOT NULL,
host TEXT NOT NULL, host TEXT NOT NULL,