about summary refs log tree commit diff
path: root/gn3/auth/authorisation/privileges.py
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2022-11-15 12:55:06 +0300
committerFrederick Muriuki Muriithi2022-11-15 12:55:06 +0300
commit1f37de222e3f93908f2db3dfef33740aea3c828c (patch)
treeaf8db79dc8f84c9e678bc9cbccb65c39588530ac /gn3/auth/authorisation/privileges.py
parenta0e654fe11bc5ccd346019bc5785c3791012a7df (diff)
downloadgenenetwork3-1f37de222e3f93908f2db3dfef33740aea3c828c.tar.gz
auth: Specify types for privileges, roles, groups
Use specified types for privileges, roles and types rather than using strings
to help with limiting bugs.

* gn3/auth/authorisation/groups.py: Specify and use the `Group` type
* gn3/auth/authorisation/privileges.py: Specify and use the `Privilege` type
* gn3/auth/authorisation/roles.py: Specify the `Role` type. Add the
  `create_role` function.
Diffstat (limited to 'gn3/auth/authorisation/privileges.py')
-rw-r--r--gn3/auth/authorisation/privileges.py14
1 files changed, 11 insertions, 3 deletions
diff --git a/gn3/auth/authorisation/privileges.py b/gn3/auth/authorisation/privileges.py
index 99b36ef..c60a58c 100644
--- a/gn3/auth/authorisation/privileges.py
+++ b/gn3/auth/authorisation/privileges.py
@@ -1,16 +1,24 @@
 """Handle privileges"""
 from uuid import UUID
+from typing import Iterable, NamedTuple
 
 from gn3.auth import db
 
-def user_privileges(conn, user_id: UUID):
+class Privilege(NamedTuple):
+    """Class representing a privilege: creates immutable objects."""
+    privilege_id: UUID
+    privilege_name: str
+
+def user_privileges(conn: db.DbConnection, user_id: UUID) -> Iterable[Privilege]:
     """Fetch the user's privileges from the database."""
     with db.cursor(conn) as cursor:
         cursor.execute(
-            ("SELECT p.privilege_name "
+            ("SELECT p.privilege_id, p.privilege_name "
              "FROM user_roles AS ur "
              "INNER JOIN role_privileges AS rp ON ur.role_id=rp.role_id "
              "INNER JOIN privileges AS p ON rp.privilege_id=p.privilege_id "
              "WHERE ur.user_id=?"),
             (str(user_id),))
-        return tuple(row[0] for row in cursor.fetchall())
+        results = cursor.fetchall()
+
+    return (Privilege(row[0], row[1]) for row in results)