about summary refs log tree commit diff
path: root/gn_libs
diff options
context:
space:
mode:
Diffstat (limited to 'gn_libs')
-rw-r--r--gn_libs/privileges/resources.py6
-rw-r--r--gn_libs/requests.py16
2 files changed, 22 insertions, 0 deletions
diff --git a/gn_libs/privileges/resources.py b/gn_libs/privileges/resources.py
index d2131e9..a605ea4 100644
--- a/gn_libs/privileges/resources.py
+++ b/gn_libs/privileges/resources.py
@@ -78,3 +78,9 @@ can_apply_or_reject_edit = partial(
         "     system:system-wide:inbredset:edit-case-attribute "
         "     system:system-wide:inbredset:apply-case-attribute-edit "
         "     system:system-wide:inbredset:reject-case-attribute-edit)"))
+
+
+def can_assign_role(resource_privileges: tuple[str, ...]) -> bool:
+    """Check whether the current user is allowed to assign roles to other users
+    on given resource."""
+    return check("(AND resource:user:assign-role)", resource_privileges)
diff --git a/gn_libs/requests.py b/gn_libs/requests.py
new file mode 100644
index 0000000..18404aa
--- /dev/null
+++ b/gn_libs/requests.py
@@ -0,0 +1,16 @@
+"""Utilities to deal with requests."""
+from json.decoder import JSONDecodeError
+from flask import request
+
+def request_json() -> dict:
+    """Retrieve the JSON sent in a request."""
+    if "application/json" in request.headers.get("Content-Type", ""):
+        try:
+            return request.json or {}
+        except JSONDecodeError:
+            return {}
+
+    if "multipart/form-data" in request.headers.get("Content-Type", ""):
+        return dict(request.form) or {}
+
+    return dict(request.args) or {}