aboutsummaryrefslogtreecommitdiff
path: root/gn_auth/auth/authorisation/resources/groups/models.py
diff options
context:
space:
mode:
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())