about summary refs log tree commit diff
path: root/gn_auth/auth/authorisation/resources/groups/models.py
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2025-07-16 12:21:54 -0500
committerFrederick Muriuki Muriithi2025-07-16 12:21:54 -0500
commit0e6a67bca38e0339af042554ed9b85a93ca39eee (patch)
treec1c99ebf3dbdc4a0afb2f73da835daa72efac373 /gn_auth/auth/authorisation/resources/groups/models.py
parentc28105e4977f299bbab6f5808ba68269bfab36c1 (diff)
downloadgn-auth-0e6a67bca38e0339af042554ed9b85a93ca39eee.tar.gz
Implement initial views into the groups endpoints.
Diffstat (limited to 'gn_auth/auth/authorisation/resources/groups/models.py')
-rw-r--r--gn_auth/auth/authorisation/resources/groups/models.py34
1 files changed, 34 insertions, 0 deletions
diff --git a/gn_auth/auth/authorisation/resources/groups/models.py b/gn_auth/auth/authorisation/resources/groups/models.py
index 1d44ca4..a4aacc7 100644
--- a/gn_auth/auth/authorisation/resources/groups/models.py
+++ b/gn_auth/auth/authorisation/resources/groups/models.py
@@ -583,3 +583,37 @@ def group_resource(conn: db.DbConnection, group_id: UUID) -> Resource:
 
     raise NotFoundError("Could not find a resource for group with ID "
                         f"{group_id}")
+
+
+def data_resources(
+        conn: db.DbConnection, group_id: UUID) -> Iterable[Resource]:
+    """Fetch a group's data resources."""
+    with db.cursor(conn) as cursor:
+        cursor.execute(
+            "SELECT resource_ownership.group_id, resources.resource_id, "
+            "resources.resource_name, resources.public, resource_categories.* "
+            "FROM resource_ownership INNER JOIN resources "
+            "ON resource_ownership.resource_id=resources.resource_id "
+            "INNER JOIN resource_categories "
+            "ON resources.resource_category_id=resource_categories.resource_category_id "
+            "WHERE group_id=?",
+            (str(group_id),))
+        yield from (resource_from_dbrow(row) for row in cursor.fetchall())
+
+
+def group_leaders(conn: db.DbConnection, group_id: UUID) -> Iterable[User]:
+    """Fetch all of a group's group leaders."""
+    with db.cursor(conn) as cursor:
+        cursor.execute(
+            "SELECT users.* FROM group_users INNER JOIN group_resources "
+            "ON group_users.group_id=group_resources.group_id "
+            "INNER JOIN user_roles "
+            "ON group_resources.resource_id=user_roles.resource_id "
+            "INNER JOIN roles "
+            "ON user_roles.role_id=roles.role_id "
+            "INNER JOIN users "
+            "ON user_roles.user_id=users.user_id "
+            "WHERE group_users.group_id=? "
+            "AND roles.role_name='group-leader'",
+            (str(group_id),))
+        yield from (User.from_sqlite3_row(row) for row in cursor.fetchall())