summary refs log tree commit diff
path: root/synapse/api
diff options
context:
space:
mode:
authorPaul "LeoNerd" Evans <paul@matrix.org>2015-04-27 17:09:25 +0100
committerPaul "LeoNerd" Evans <paul@matrix.org>2015-04-27 17:09:25 +0100
commit38432d8c253fda453cd645491f46ef37714c4f7c (patch)
tree76d9f0dee077b01038a939f611fbcb4b89aea496 /synapse/api
parentAlso remember to check 'invite' level for changes (diff)
parentMerge pull request #132 from matrix-org/observer_and_locks (diff)
downloadsynapse-38432d8c253fda453cd645491f46ef37714c4f7c.tar.xz
Merge branch 'develop' into invite_power_level
Diffstat (limited to 'synapse/api')
-rw-r--r--synapse/api/auth.py48
-rw-r--r--synapse/api/errors.py4
2 files changed, 14 insertions, 38 deletions
diff --git a/synapse/api/auth.py b/synapse/api/auth.py
index d8b7614791..935dffbabe 100644
--- a/synapse/api/auth.py
+++ b/synapse/api/auth.py
@@ -183,17 +183,7 @@ class Auth(object):
         else:
             join_rule = JoinRules.INVITE
 
-        user_level = self._get_power_level_from_event_state(
-            event,
-            event.user_id,
-            auth_events,
-        )
-
-        # TODO(paul): There's an awful lot of int()-casting in this code;
-        #   surely we should be squashing strings to integers at a higher
-        #   level, maybe when we insert?
-        if user_level is not None:
-            user_level = int(user_level)
+        user_level = self._get_user_power_level(event.user_id, auth_events)
 
         # FIXME (erikj): What should we do here as the default?
         ban_level = self._get_named_level(auth_events, "ban", 50)
@@ -281,23 +271,26 @@ class Auth(object):
         key = (EventTypes.PowerLevels, "", )
         return auth_events.get(key)
 
-    def _get_power_level_from_event_state(self, event, user_id, auth_events):
+    def _get_user_power_level(self, user_id, auth_events):
         power_level_event = self._get_power_level_event(auth_events)
-        level = None
 
         if power_level_event:
             level = power_level_event.content.get("users", {}).get(user_id)
             if not level:
                 level = power_level_event.content.get("users_default", 0)
+
+            if level is None:
+                return 0
+            else:
+                return int(level)
         else:
             key = (EventTypes.Create, "", )
             create_event = auth_events.get(key)
             if (create_event is not None and
                     create_event.content["creator"] == user_id):
                 return 100
-
-        return level
-
+            else:
+                return 0
 
     def _get_named_level(self, auth_events, name, default):
         power_level_event = self._get_power_level_event(auth_events)
@@ -504,16 +497,7 @@ class Auth(object):
         else:
             send_level = 0
 
-        user_level = self._get_power_level_from_event_state(
-            event,
-            event.user_id,
-            auth_events,
-        )
-
-        if user_level:
-            user_level = int(user_level)
-        else:
-            user_level = 0
+        user_level = self._get_user_power_level(event.user_id, auth_events)
 
         if user_level < send_level:
             raise AuthError(
@@ -545,11 +529,7 @@ class Auth(object):
         return True
 
     def _check_redaction(self, event, auth_events):
-        user_level = self._get_power_level_from_event_state(
-            event,
-            event.user_id,
-            auth_events,
-        )
+        user_level = self._get_user_power_level(event.user_id, auth_events)
 
         redact_level = self._get_named_level(auth_events, "redact", 50)
 
@@ -579,11 +559,7 @@ class Auth(object):
         if not current_state:
             return
 
-        user_level = self._get_power_level_from_event_state(
-            event,
-            event.user_id,
-            auth_events,
-        )
+        user_level = self._get_user_power_level(event.user_id, auth_events)
 
         # Check other levels:
         levels_to_check = [
diff --git a/synapse/api/errors.py b/synapse/api/errors.py
index eddd889778..72d2bd5b4c 100644
--- a/synapse/api/errors.py
+++ b/synapse/api/errors.py
@@ -35,8 +35,8 @@ class Codes(object):
     LIMIT_EXCEEDED = "M_LIMIT_EXCEEDED"
     CAPTCHA_NEEDED = "M_CAPTCHA_NEEDED"
     CAPTCHA_INVALID = "M_CAPTCHA_INVALID"
-    MISSING_PARAM = "M_MISSING_PARAM",
-    TOO_LARGE = "M_TOO_LARGE",
+    MISSING_PARAM = "M_MISSING_PARAM"
+    TOO_LARGE = "M_TOO_LARGE"
     EXCLUSIVE = "M_EXCLUSIVE"