aboutsummaryrefslogtreecommitdiff
path: root/gn3/auth/authorisation/roles.py
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2023-01-28 03:16:45 +0300
committerFrederick Muriuki Muriithi2023-01-28 03:20:01 +0300
commit76c464946d01073b8bcb757345d0d42b9a8207e4 (patch)
tree7d5cd20018c65207b809842277e7533d7490de7b /gn3/auth/authorisation/roles.py
parente6e173b74d381f590ff5a8e7957489ea4d50c06b (diff)
downloadgenenetwork3-76c464946d01073b8bcb757345d0d42b9a8207e4.tar.gz
auth: rework dictify
Define a Protocol type to use with the `dictify` function and implement the `dictify` methods for the various classes.
Diffstat (limited to 'gn3/auth/authorisation/roles.py')
-rw-r--r--gn3/auth/authorisation/roles.py9
1 files changed, 8 insertions, 1 deletions
diff --git a/gn3/auth/authorisation/roles.py b/gn3/auth/authorisation/roles.py
index cd59a36..86759b1 100644
--- a/gn3/auth/authorisation/roles.py
+++ b/gn3/auth/authorisation/roles.py
@@ -1,11 +1,12 @@
"""Handle management of roles"""
from uuid import UUID, uuid4
from functools import reduce
-from typing import Sequence, Iterable, NamedTuple
+from typing import Any, Sequence, Iterable, NamedTuple
from pymonad.maybe import Just, Maybe, Nothing
from gn3.auth import db
+from gn3.auth.dictify import dictify
from gn3.auth.authentication.users import User
from gn3.auth.authentication.checks import authenticated_p
@@ -18,6 +19,12 @@ class Role(NamedTuple):
role_name: str
privileges: Iterable[Privilege]
+ def dictify(self) -> dict[str, Any]:
+ """Return a dict representation of `Role` objects."""
+ return {
+ "role_id": self.role_id, "role_name": self.role_name,
+ "privileges": tuple(dictify(priv) for priv in self.privileges)
+ }
@authenticated_p
@authorised_p(("group:role:create-role",), error_message="Could not create role")
def create_role(