Much neater fetching of defined powerlevels from m.room.power_levels state event

pull/133/head
Paul "LeoNerd" Evans 2015-04-21 20:53:23 +01:00
parent b568c0231c
commit d3309933f5
1 changed files with 21 additions and 31 deletions

View File

@ -195,12 +195,8 @@ class Auth(object):
if user_level is not None: if user_level is not None:
user_level = int(user_level) user_level = int(user_level)
ban_level, kick_level, redact_level = ( # FIXME (erikj): What should we do here as the default?
self._get_ops_level_from_event_state( ban_level = self._get_named_level(auth_events, "ban", 50)
event,
auth_events,
)
)
logger.debug( logger.debug(
"is_membership_change_allowed: %s", "is_membership_change_allowed: %s",
@ -216,11 +212,6 @@ class Auth(object):
} }
) )
if ban_level:
ban_level = int(ban_level)
else:
ban_level = 50 # FIXME (erikj): What should we do here?
if Membership.JOIN != membership: if Membership.JOIN != membership:
# JOIN is the only action you can perform if you're not in the room # JOIN is the only action you can perform if you're not in the room
if not caller_in_room: # caller isn't joined if not caller_in_room: # caller isn't joined
@ -265,10 +256,7 @@ class Auth(object):
403, "You cannot unban user &s." % (target_user_id,) 403, "You cannot unban user &s." % (target_user_id,)
) )
elif target_user_id != event.user_id: elif target_user_id != event.user_id:
if kick_level: kick_level = self._get_named_level(auth_events, "kick", 50)
kick_level = int(kick_level)
else:
kick_level = 50 # FIXME (erikj): What should we do here?
if user_level < kick_level: if user_level < kick_level:
raise AuthError( raise AuthError(
@ -282,10 +270,14 @@ class Auth(object):
return True return True
def _get_power_level_from_event_state(self, event, user_id, auth_events): def _get_power_level_event(self, auth_events):
key = (EventTypes.PowerLevels, "", ) key = (EventTypes.PowerLevels, "", )
power_level_event = auth_events.get(key) return auth_events.get(key)
def _get_power_level_from_event_state(self, event, user_id, auth_events):
power_level_event = self._get_power_level_event(auth_events)
level = None level = None
if power_level_event: if power_level_event:
level = power_level_event.content.get("users", {}).get(user_id) level = power_level_event.content.get("users", {}).get(user_id)
if not level: if not level:
@ -299,17 +291,18 @@ class Auth(object):
return level return level
def _get_ops_level_from_event_state(self, event, auth_events):
key = (EventTypes.PowerLevels, "", )
power_level_event = auth_events.get(key)
if power_level_event: def _get_named_level(self, auth_events, name, default):
return ( power_level_event = self._get_power_level_event(auth_events)
power_level_event.content.get("ban", 50),
power_level_event.content.get("kick", 50), if not power_level_event:
power_level_event.content.get("redact", 50), return default
)
return None, None, None, level = power_level_event.content.get(name, None)
if level is not None:
return int(level)
else:
return default
@defer.inlineCallbacks @defer.inlineCallbacks
def get_user_by_req(self, request): def get_user_by_req(self, request):
@ -551,10 +544,7 @@ class Auth(object):
auth_events, auth_events,
) )
_, _, redact_level = self._get_ops_level_from_event_state( redact_level = self._get_named_level(auth_events, "redact", 50)
event,
auth_events,
)
if user_level < redact_level: if user_level < redact_level:
raise AuthError( raise AuthError(