Make auth module use EventTypes constants

This commit is contained in:
Erik Johnston 2014-12-12 16:31:50 +00:00
parent 1fc2a0e33e
commit fc409096ac

View file

@ -17,12 +17,8 @@
from twisted.internet import defer from twisted.internet import defer
from synapse.api.constants import Membership, JoinRules from synapse.api.constants import EventTypes, Membership, JoinRules
from synapse.api.errors import AuthError, StoreError, Codes, SynapseError from synapse.api.errors import AuthError, StoreError, Codes, SynapseError
from synapse.api.events.room import (
RoomMemberEvent, RoomPowerLevelsEvent, RoomRedactionEvent,
RoomJoinRulesEvent, RoomCreateEvent, RoomAliasesEvent,
)
from synapse.util.logutils import log_function from synapse.util.logutils import log_function
from synapse.util.async import run_on_reactor from synapse.util.async import run_on_reactor
@ -53,17 +49,17 @@ class Auth(object):
logger.warn("Trusting event: %s", event.event_id) logger.warn("Trusting event: %s", event.event_id)
return True return True
if event.type == RoomCreateEvent.TYPE: if event.type == EventTypes.Create:
# FIXME # FIXME
return True return True
# FIXME: Temp hack # FIXME: Temp hack
if event.type == RoomAliasesEvent.TYPE: if event.type == EventTypes.Aliases:
return True return True
logger.debug("Auth events: %s", auth_events) logger.debug("Auth events: %s", auth_events)
if event.type == RoomMemberEvent.TYPE: if event.type == EventTypes.Member:
allowed = self.is_membership_change_allowed( allowed = self.is_membership_change_allowed(
event, auth_events event, auth_events
) )
@ -76,10 +72,10 @@ class Auth(object):
self.check_event_sender_in_room(event, auth_events) self.check_event_sender_in_room(event, auth_events)
self._can_send_event(event, auth_events) self._can_send_event(event, auth_events)
if event.type == RoomPowerLevelsEvent.TYPE: if event.type == EventTypes.PowerLevels:
self._check_power_levels(event, auth_events) self._check_power_levels(event, auth_events)
if event.type == RoomRedactionEvent.TYPE: if event.type == EventTypes.Redaction:
self._check_redaction(event, auth_events) self._check_redaction(event, auth_events)
logger.debug("Allowing! %s", event) logger.debug("Allowing! %s", event)
@ -95,7 +91,7 @@ class Auth(object):
def check_joined_room(self, room_id, user_id): def check_joined_room(self, room_id, user_id):
member = yield self.state.get_current_state( member = yield self.state.get_current_state(
room_id=room_id, room_id=room_id,
event_type=RoomMemberEvent.TYPE, event_type=EventTypes.Member,
state_key=user_id state_key=user_id
) )
self._check_joined_room(member, user_id, room_id) self._check_joined_room(member, user_id, room_id)
@ -106,7 +102,7 @@ class Auth(object):
curr_state = yield self.state.get_current_state(room_id) curr_state = yield self.state.get_current_state(room_id)
for event in curr_state: for event in curr_state:
if event.type == RoomMemberEvent.TYPE: if event.type == EventTypes.Member:
try: try:
if self.hs.parse_userid(event.state_key).domain != host: if self.hs.parse_userid(event.state_key).domain != host:
continue continue
@ -120,7 +116,7 @@ class Auth(object):
defer.returnValue(False) defer.returnValue(False)
def check_event_sender_in_room(self, event, auth_events): def check_event_sender_in_room(self, event, auth_events):
key = (RoomMemberEvent.TYPE, event.user_id, ) key = (EventTypes.Member, event.user_id, )
member_event = auth_events.get(key) member_event = auth_events.get(key)
return self._check_joined_room( return self._check_joined_room(
@ -142,7 +138,7 @@ class Auth(object):
# Check if this is the room creator joining: # Check if this is the room creator joining:
if len(event.prev_events) == 1 and Membership.JOIN == membership: if len(event.prev_events) == 1 and Membership.JOIN == membership:
# Get room creation event: # Get room creation event:
key = (RoomCreateEvent.TYPE, "", ) key = (EventTypes.Create, "", )
create = auth_events.get(key) create = auth_events.get(key)
if create and event.prev_events[0][0] == create.event_id: if create and event.prev_events[0][0] == create.event_id:
if create.content["creator"] == event.state_key: if create.content["creator"] == event.state_key:
@ -151,19 +147,19 @@ class Auth(object):
target_user_id = event.state_key target_user_id = event.state_key
# get info about the caller # get info about the caller
key = (RoomMemberEvent.TYPE, event.user_id, ) key = (EventTypes.Member, event.user_id, )
caller = auth_events.get(key) caller = auth_events.get(key)
caller_in_room = caller and caller.membership == Membership.JOIN caller_in_room = caller and caller.membership == Membership.JOIN
caller_invited = caller and caller.membership == Membership.INVITE caller_invited = caller and caller.membership == Membership.INVITE
# get info about the target # get info about the target
key = (RoomMemberEvent.TYPE, target_user_id, ) key = (EventTypes.Member, target_user_id, )
target = auth_events.get(key) target = auth_events.get(key)
target_in_room = target and target.membership == Membership.JOIN target_in_room = target and target.membership == Membership.JOIN
key = (RoomJoinRulesEvent.TYPE, "", ) key = (EventTypes.JoinRules, "", )
join_rule_event = auth_events.get(key) join_rule_event = auth_events.get(key)
if join_rule_event: if join_rule_event:
join_rule = join_rule_event.content.get( join_rule = join_rule_event.content.get(
@ -258,7 +254,7 @@ class Auth(object):
return True return True
def _get_power_level_from_event_state(self, event, user_id, auth_events): def _get_power_level_from_event_state(self, event, user_id, auth_events):
key = (RoomPowerLevelsEvent.TYPE, "", ) key = (EventTypes.PowerLevels, "", )
power_level_event = auth_events.get(key) power_level_event = auth_events.get(key)
level = None level = None
if power_level_event: if power_level_event:
@ -266,7 +262,7 @@ class Auth(object):
if not level: if not level:
level = power_level_event.content.get("users_default", 0) level = power_level_event.content.get("users_default", 0)
else: else:
key = (RoomCreateEvent.TYPE, "", ) key = (EventTypes.Create, "", )
create_event = auth_events.get(key) create_event = auth_events.get(key)
if (create_event is not None and if (create_event is not None and
create_event.content["creator"] == user_id): create_event.content["creator"] == user_id):
@ -275,7 +271,7 @@ class Auth(object):
return level return level
def _get_ops_level_from_event_state(self, event, auth_events): def _get_ops_level_from_event_state(self, event, auth_events):
key = (RoomPowerLevelsEvent.TYPE, "", ) key = (EventTypes.PowerLevels, "", )
power_level_event = auth_events.get(key) power_level_event = auth_events.get(key)
if power_level_event: if power_level_event:
@ -356,25 +352,25 @@ class Auth(object):
def add_auth_events(self, builder, context): def add_auth_events(self, builder, context):
yield run_on_reactor() yield run_on_reactor()
if builder.type == RoomCreateEvent.TYPE: if builder.type == EventTypes.Create:
builder.auth_events = [] builder.auth_events = []
return return
auth_ids = [] auth_ids = []
key = (RoomPowerLevelsEvent.TYPE, "", ) key = (EventTypes.PowerLevels, "", )
power_level_event = context.current_state.get(key) power_level_event = context.current_state.get(key)
if power_level_event: if power_level_event:
auth_ids.append(power_level_event.event_id) auth_ids.append(power_level_event.event_id)
key = (RoomJoinRulesEvent.TYPE, "", ) key = (EventTypes.JoinRules, "", )
join_rule_event = context.current_state.get(key) join_rule_event = context.current_state.get(key)
key = (RoomMemberEvent.TYPE, builder.user_id, ) key = (EventTypes.Member, builder.user_id, )
member_event = context.current_state.get(key) member_event = context.current_state.get(key)
key = (RoomCreateEvent.TYPE, "", ) key = (EventTypes.Create, "", )
create_event = context.current_state.get(key) create_event = context.current_state.get(key)
if create_event: if create_event:
auth_ids.append(create_event.event_id) auth_ids.append(create_event.event_id)
@ -385,7 +381,7 @@ class Auth(object):
else: else:
is_public = False is_public = False
if builder.type == RoomMemberEvent.TYPE: if builder.type == EventTypes.Member:
e_type = builder.content["membership"] e_type = builder.content["membership"]
if e_type in [Membership.JOIN, Membership.INVITE]: if e_type in [Membership.JOIN, Membership.INVITE]:
if join_rule_event: if join_rule_event:
@ -415,7 +411,7 @@ class Auth(object):
@log_function @log_function
def _can_send_event(self, event, auth_events): def _can_send_event(self, event, auth_events):
key = (RoomPowerLevelsEvent.TYPE, "", ) key = (EventTypes.PowerLevels, "", )
send_level_event = auth_events.get(key) send_level_event = auth_events.get(key)
send_level = None send_level = None
if send_level_event: if send_level_event: