aboutsummaryrefslogtreecommitdiff
path: root/gn_auth/auth/authorisation/privileges.py
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2024-06-10 15:22:20 -0500
committerFrederick Muriuki Muriithi2024-06-10 16:45:12 -0500
commitfa7452e4f08352526257d1f0dc5181302a55d3e2 (patch)
tree2a62f5f94d0da4808b469becff71e9ebb62784a9 /gn_auth/auth/authorisation/privileges.py
parent99023088e70c6db684785ab6a1a8b11331058efd (diff)
downloadgn-auth-fa7452e4f08352526257d1f0dc5181302a55d3e2.tar.gz
Provide some endpoints for privileges.
Diffstat (limited to 'gn_auth/auth/authorisation/privileges.py')
-rw-r--r--gn_auth/auth/authorisation/privileges.py52
1 files changed, 0 insertions, 52 deletions
diff --git a/gn_auth/auth/authorisation/privileges.py b/gn_auth/auth/authorisation/privileges.py
deleted file mode 100644
index a977db5..0000000
--- a/gn_auth/auth/authorisation/privileges.py
+++ /dev/null
@@ -1,52 +0,0 @@
-"""Handle privileges"""
-from dataclasses import dataclass
-from typing import Iterable
-
-import sqlite3
-
-from ..db import sqlite3 as db
-from ..authentication.users import User
-
-
-@dataclass(frozen=True)
-class Privilege:
- """Class representing a privilege: creates immutable objects."""
- privilege_id: str
- privilege_description: str
-
-
-def db_row_to_privilege(row: sqlite3.Row) -> Privilege:
- "Convert single db row into a privilege object."
- return Privilege(privilege_id=row["privilege_id"],
- privilege_description=row["privilege_description"])
-
-
-def user_privileges(conn: db.DbConnection, user: User) -> Iterable[Privilege]:
- """Fetch the user's privileges from the database."""
- with db.cursor(conn) as cursor:
- cursor.execute(
- ("SELECT p.privilege_id, p.privilege_description "
- "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.user_id),))
- results = cursor.fetchall()
-
- return (Privilege(row[0], row[1]) for row in results)
-
-def privileges_by_ids(
- conn: db.DbConnection, privileges_ids: tuple[str, ...]) -> tuple[
- Privilege, ...]:
- """Fetch privileges by their ids."""
- if len(privileges_ids) == 0:
- return tuple()
-
- with db.cursor(conn) as cursor:
- clause = ", ".join(["?"] * len(privileges_ids))
- cursor.execute(
- f"SELECT * FROM privileges WHERE privilege_id IN ({clause})",
- privileges_ids)
- return tuple(
- Privilege(row["privilege_id"], row["privilege_description"])
- for row in cursor.fetchall())