mirror of
https://github.com/element-hq/synapse.git
synced 2024-11-22 09:35:45 +03:00
Make default state_default 50
Make it so that, before there is a power-levels event in the room, you need a power level of at least 50 to send state. Partially addresses https://github.com/matrix-org/matrix-doc/issues/1192
This commit is contained in:
parent
52423607bd
commit
5c9afd6f80
2 changed files with 167 additions and 20 deletions
|
@ -370,28 +370,22 @@ def get_send_level(etype, state_key, power_levels_event):
|
|||
int: power level required to send this event.
|
||||
"""
|
||||
|
||||
send_level_event = power_levels_event # todo: rename refs below
|
||||
send_level = None
|
||||
if send_level_event:
|
||||
send_level = send_level_event.content.get("events", {}).get(
|
||||
etype
|
||||
)
|
||||
if power_levels_event:
|
||||
power_levels_content = power_levels_event.content
|
||||
else:
|
||||
power_levels_content = {}
|
||||
|
||||
# see if we have a custom level for this event type
|
||||
send_level = power_levels_content.get("events", {}).get(etype)
|
||||
|
||||
# otherwise, fall back to the state_default/events_default.
|
||||
if send_level is None:
|
||||
if state_key is not None:
|
||||
send_level = send_level_event.content.get(
|
||||
"state_default", 50
|
||||
)
|
||||
send_level = power_levels_content.get("state_default", 50)
|
||||
else:
|
||||
send_level = send_level_event.content.get(
|
||||
"events_default", 0
|
||||
)
|
||||
send_level = power_levels_content.get("events_default", 0)
|
||||
|
||||
if send_level:
|
||||
send_level = int(send_level)
|
||||
else:
|
||||
send_level = 0
|
||||
|
||||
return send_level
|
||||
return int(send_level)
|
||||
|
||||
|
||||
def _can_send_event(event, auth_events):
|
||||
|
|
153
tests/test_event_auth.py
Normal file
153
tests/test_event_auth.py
Normal file
|
@ -0,0 +1,153 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Copyright 2018 New Vector Ltd
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from synapse import event_auth
|
||||
from synapse.api.errors import AuthError
|
||||
from synapse.events import FrozenEvent
|
||||
import unittest
|
||||
|
||||
|
||||
class EventAuthTestCase(unittest.TestCase):
|
||||
@unittest.DEBUG
|
||||
def test_random_users_cannot_send_state_before_first_pl(self):
|
||||
"""
|
||||
Check that, before the first PL lands, the creator is the only user
|
||||
that can send a state event.
|
||||
"""
|
||||
creator = "@creator:example.com"
|
||||
joiner = "@joiner:example.com"
|
||||
auth_events = {
|
||||
("m.room.create", ""): _create_event(creator),
|
||||
("m.room.member", creator): _join_event(creator),
|
||||
("m.room.member", joiner): _join_event(joiner),
|
||||
}
|
||||
|
||||
# creator should be able to send state
|
||||
event_auth.check(
|
||||
_random_state_event(creator), auth_events,
|
||||
do_sig_check=False,
|
||||
)
|
||||
|
||||
# joiner should not be able to send state
|
||||
self.assertRaises(
|
||||
AuthError,
|
||||
event_auth.check,
|
||||
_random_state_event(joiner),
|
||||
auth_events,
|
||||
do_sig_check=False,
|
||||
),
|
||||
|
||||
@unittest.DEBUG
|
||||
def test_state_default_level(self):
|
||||
"""
|
||||
Check that users above the state_default level can send state and
|
||||
those below cannot
|
||||
"""
|
||||
creator = "@creator:example.com"
|
||||
pleb = "@joiner:example.com"
|
||||
king = "@joiner2:example.com"
|
||||
|
||||
auth_events = {
|
||||
("m.room.create", ""): _create_event(creator),
|
||||
("m.room.member", creator): _join_event(creator),
|
||||
("m.room.power_levels", ""): _power_levels_event(creator, {
|
||||
"state_default": "30",
|
||||
"users": {
|
||||
pleb: "29",
|
||||
king: "30",
|
||||
},
|
||||
}),
|
||||
("m.room.member", pleb): _join_event(pleb),
|
||||
("m.room.member", king): _join_event(king),
|
||||
}
|
||||
|
||||
# pleb should not be able to send state
|
||||
self.assertRaises(
|
||||
AuthError,
|
||||
event_auth.check,
|
||||
_random_state_event(pleb),
|
||||
auth_events,
|
||||
do_sig_check=False,
|
||||
),
|
||||
|
||||
# king should be able to send state
|
||||
event_auth.check(
|
||||
_random_state_event(king), auth_events,
|
||||
do_sig_check=False,
|
||||
)
|
||||
|
||||
|
||||
# helpers for making events
|
||||
|
||||
TEST_ROOM_ID = "!test:room"
|
||||
|
||||
|
||||
def _create_event(user_id):
|
||||
return FrozenEvent({
|
||||
"room_id": TEST_ROOM_ID,
|
||||
"event_id": _get_event_id(),
|
||||
"type": "m.room.create",
|
||||
"sender": user_id,
|
||||
"content": {
|
||||
"creator": user_id,
|
||||
},
|
||||
})
|
||||
|
||||
|
||||
def _join_event(user_id):
|
||||
return FrozenEvent({
|
||||
"room_id": TEST_ROOM_ID,
|
||||
"event_id": _get_event_id(),
|
||||
"type": "m.room.member",
|
||||
"sender": user_id,
|
||||
"state_key": user_id,
|
||||
"content": {
|
||||
"membership": "join",
|
||||
},
|
||||
})
|
||||
|
||||
|
||||
def _power_levels_event(sender, content):
|
||||
return FrozenEvent({
|
||||
"room_id": TEST_ROOM_ID,
|
||||
"event_id": _get_event_id(),
|
||||
"type": "m.room.power_levels",
|
||||
"sender": sender,
|
||||
"state_key": "",
|
||||
"content": content,
|
||||
})
|
||||
|
||||
|
||||
def _random_state_event(sender):
|
||||
return FrozenEvent({
|
||||
"room_id": TEST_ROOM_ID,
|
||||
"event_id": _get_event_id(),
|
||||
"type": "test.state",
|
||||
"sender": sender,
|
||||
"state_key": "",
|
||||
"content": {
|
||||
"membership": "join",
|
||||
},
|
||||
})
|
||||
|
||||
|
||||
event_count = 0
|
||||
|
||||
|
||||
def _get_event_id():
|
||||
global event_count
|
||||
c = event_count
|
||||
event_count += 1
|
||||
return "!%i:example.com" % (c, )
|
Loading…
Reference in a new issue