From 76c464946d01073b8bcb757345d0d42b9a8207e4 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Sat, 28 Jan 2023 03:16:45 +0300 Subject: auth: rework dictify Define a Protocol type to use with the `dictify` function and implement the `dictify` methods for the various classes. --- gn3/auth/authorisation/roles.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'gn3/auth/authorisation/roles.py') 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( -- cgit v1.2.3