Skip to content

Commit

Permalink
Add all the necessary checks to make banning work.
Browse files Browse the repository at this point in the history
  • Loading branch information
erikjohnston committed Sep 1, 2014
1 parent 3faa2ae commit b8ab9f1
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 6 deletions.
40 changes: 38 additions & 2 deletions synapse/api/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ def check(self, event, snapshot, raises=False):
"""
try:
if hasattr(event, "room_id"):
is_state = hasattr(event, "state_key")

if event.type == RoomMemberEvent.TYPE:
yield self._can_replace_state(event)
allowed = yield self.is_membership_change_allowed(event)
defer.returnValue(allowed)
return
Expand All @@ -56,10 +59,11 @@ def check(self, event, snapshot, raises=False):
room_id=snapshot.room_id,
)

if hasattr(event, "state_key"):
if is_state:
# TODO (erikj): This really only should be called for *new*
# state
yield self._can_add_state(event)
yield self._can_replace_state(event)
else:
yield self._can_send_event(event)

Expand Down Expand Up @@ -175,7 +179,7 @@ def is_membership_change_allowed(self, event):
else:
ban_level = 5 # FIXME (erikj): What should we do here?

if ban_level < user_level:
if user_level < ban_level:
raise AuthError(403, "You don't have permission to ban")
else:
raise AuthError(500, "Unknown membership %s" % membership)
Expand Down Expand Up @@ -267,3 +271,35 @@ def _can_add_state(self, event):
)

defer.returnValue(True)

@defer.inlineCallbacks
def _can_replace_state(self, event):
current_state = yield self.store.get_current_state(
event.room_id,
event.type,
event.state_key,
)

if current_state:
current_state = current_state[0]

user_level = yield self.store.get_power_level(
event.room_id,
event.user_id,
)

if user_level:
user_level = int(user_level)
else:
user_level = 0

logger.debug("Checking power level for %s, %s", event.user_id, user_level)
if current_state and hasattr(current_state, "required_power_level"):
req = current_state.required_power_level

logger.debug("Checked power level for %s, %s", event.user_id, req)
if user_level < req:
raise AuthError(
403,
"You don't have permission to change that state"
)
2 changes: 2 additions & 0 deletions synapse/api/events/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class SynapseEvent(JsonEncodedObject):
"user_id", # sender/initiator
"content", # HTTP body, JSON
"state_key",
"required_power_level",
]

internal_keys = [
Expand All @@ -52,6 +53,7 @@ class SynapseEvent(JsonEncodedObject):
"destinations",
"origin",
"outlier",
"power_level",
]

required_keys = [
Expand Down
1 change: 1 addition & 0 deletions synapse/federation/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class Pdu(JsonEncodedObject):
"power_level",
"prev_state_id",
"prev_state_origin",
"required_power_level",
]

internal_keys = [
Expand Down
12 changes: 11 additions & 1 deletion synapse/handlers/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def create(etype, **content):

power_levels_event = create(
etype=RoomPowerLevelsEvent.TYPE,
**{creator.to_string(): 10}
**{creator.to_string(): 10, "default": 0}
)

join_rule = JoinRules.PUBLIC if is_public else JoinRules.INVITE
Expand Down Expand Up @@ -343,6 +343,16 @@ def change_membership(self, event=None, do_auth=True):
if do_auth:
yield self.auth.check(event, snapshot, raises=True)

# If we're banning someone, set a req power level
if event.membership == Membership.BAN:
if not hasattr(event, "required_power_level") or event.required_power_level is None:
# Add some default required_power_level
user_level = yield self.store.get_power_level(
event.room_id,
event.user_id,
)
event.required_power_level = user_level

if prev_state and prev_state.membership == event.membership:
# double same action, treat this event as a NOOP.
defer.returnValue({})
Expand Down
3 changes: 1 addition & 2 deletions synapse/storage/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,7 @@ def _get_power_level(self, txn, room_id, user_id):
rows = txn.execute(sql, (room_id, user_id,)).fetchall()

if len(rows) == 1:
defer.returnValue(rows[0][0])
return
return rows[0][0]

sql = (
"SELECT level FROM room_default_levels as r "
Expand Down
2 changes: 1 addition & 1 deletion synapse/storage/schema/im.sql
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ CREATE TABLE IF NOT EXISTS room_ops_levels(
event_id TEXT NOT NULL,
room_id TEXT NOT NULL,
ban_level INTEGER,
kick_level INTEGER,
kick_level INTEGER
);

CREATE INDEX IF NOT EXISTS room_ops_levels_event_id ON room_ops_levels(event_id);
Expand Down

0 comments on commit b8ab9f1

Please sign in to comment.