diff options
| author | Frederick Muriuki Muriithi <fredmanglis@gmail.com> | 2026-10-05 10:11:45 -0500 |
|---|---|---|
| committer | Frederick Muriuki Muriithi <fredmanglis@gmail.com> | 2026-10-05 11:36:26 -0500 |
| commit | 2ebc99cf340547a7276209cf39a54caad62a1bb6 (patch) | |
| tree | 9b0ad5b9f230eb7f7037bbdc65dbafd61162741c /gn_auth | |
| parent | f5af9014450136fbc154a5d1ffe4311e1338367b (diff) | |
| download | gn-auth-2ebc99cf340547a7276209cf39a54caad62a1bb6.tar.gz | |
fix (resources/base): Make `created_by` mandatory.
The `created_by` field is mandatory, and every resource should have a creator, including the system itself. Any chicken/egg situation that arises from this situation should be resolved by manually editing the data or building code to initialise the application at startup, not by ignoring this invariant.
Diffstat (limited to 'gn_auth')
| -rw-r--r-- | gn_auth/auth/authorisation/resources/base.py | 10 | ||||
| -rw-r--r-- | gn_auth/auth/authorisation/resources/data.py | 1 | ||||
| -rw-r--r-- | gn_auth/auth/authorisation/resources/models.py | 24 | ||||
| -rw-r--r-- | gn_auth/auth/authorisation/resources/views.py | 1 | ||||
| -rw-r--r-- | gn_auth/scripts/assign_data_to_default_admin.py | 13 | ||||
| -rw-r--r-- | gn_auth/scripts/batch_assign_data_to_default_admin.py | 9 |
6 files changed, 37 insertions, 21 deletions
diff --git a/gn_auth/auth/authorisation/resources/base.py b/gn_auth/auth/authorisation/resources/base.py index e4a1239..7ab5d0b 100644 --- a/gn_auth/auth/authorisation/resources/base.py +++ b/gn_auth/auth/authorisation/resources/base.py @@ -28,9 +28,9 @@ class Resource: resource_name: str resource_category: ResourceCategory public: bool - resource_data: Sequence[dict[str, Any]] = tuple() - created_by: Optional[User] = None + created_by: User created_at: datetime.datetime = datetime.datetime(1970, 1, 1, 0, 0, 0) + resource_data: Sequence[dict[str, Any]] = tuple() @staticmethod def from_resource(# pylint: disable=[too-many-arguments, too-many-positional-arguments] @@ -38,17 +38,17 @@ class Resource: resource_id: Optional[UUID] = None, resource_name: Optional[str] = None, resource_category: Optional[ResourceCategory] = None, - public: Optional[bool] = None, + public: bool = False, resource_data: Optional[Sequence[dict[str, Any]]] = None, created_by: Optional[User] = None, - created_at: Optional[datetime.datetime] = None + created_at: Optional[datetime.datetime] = None, ): """Takes a Resource object `resource` and updates the attributes specified in `kwargs`.""" return Resource( resource_id=resource_id or resource.resource_id, resource_name=resource_name or resource.resource_name, resource_category=resource_category or resource.resource_category, - public=bool(public) or resource.public, + public=public or resource.public, resource_data=resource_data or resource.resource_data, created_by=created_by or resource.created_by, created_at=created_at or resource.created_at) diff --git a/gn_auth/auth/authorisation/resources/data.py b/gn_auth/auth/authorisation/resources/data.py index 54c500a..2724cda 100644 --- a/gn_auth/auth/authorisation/resources/data.py +++ b/gn_auth/auth/authorisation/resources/data.py @@ -24,5 +24,6 @@ def __attach_data__( return tuple( Resource(resource.resource_id, resource.resource_name, resource.resource_category, resource.public, + resource.created_by, resource.created_at, organised.get(resource.resource_id, tuple())) for resource in resources) diff --git a/gn_auth/auth/authorisation/resources/models.py b/gn_auth/auth/authorisation/resources/models.py index 5762551..4e88c0f 100644 --- a/gn_auth/auth/authorisation/resources/models.py +++ b/gn_auth/auth/authorisation/resources/models.py @@ -57,7 +57,8 @@ def create_resource(# pylint: disable=[too-many-arguments, too-many-positional-a ) -> Resource: """Create a resource item.""" def __create_resource__(cursor: db.DbCursor) -> Resource: - resource = Resource(uuid4(), resource_name, resource_category, public) + resource = Resource( + uuid4(), resource_name, resource_category, public, user, created_at) cursor.execute( "INSERT INTO resources VALUES (?, ?, ?, ?, ?, ?)", (str(resource.resource_id), @@ -193,14 +194,18 @@ def group_leader_resources( if is_group_leader(conn, user, group): with db.cursor(conn) as cursor: cursor.execute( - "SELECT r.* FROM resource_ownership AS ro " + "SELECT r.*, creat.* FROM resource_ownership AS ro " "INNER JOIN resources AS r " "ON ro.resource_id=r.resource_id " + "INNER JOIN users AS creat " + "ON resource.created_by=creat.user_id " "WHERE ro.group_id=?", (str(group.group_id),)) return tuple( Resource(UUID(row[0]), row[1], - res_categories[UUID(row[2])], bool(row[3])) + res_categories[UUID(row[2])], bool(row[3]), + User.from_sqlite3_row(row), + datetime.fromtimestamp(row[6])) for row in cursor.fetchall()) return tuple() @@ -299,7 +304,8 @@ def attach_resource_data(cursor: db.DbCursor, resource: Resource) -> Resource: cursor, resource.resource_id)) return Resource( resource.resource_id, resource.resource_name, - resource.resource_category, resource.public, data_rows) + resource.resource_category, resource.public, + resource.created_by, resource.created_at, data_rows) def resource_by_id( conn: db.DbConnection, user: User, resource_id: UUID) -> Resource: @@ -314,14 +320,18 @@ def resource_by_id( f"'{resource_id}'.") with db.cursor(conn) as cursor: - cursor.execute("SELECT * FROM resources WHERE resource_id=:id", - {"id": str(resource_id)}) + cursor.execute( + "SELECT r.* FROM resources AS r INNER JOIN users AS creat " + "ON r.created_by=creat.user_id WHERE resource_id=:id", + {"id": str(resource_id)}) row = cursor.fetchone() if row: return Resource( UUID(row["resource_id"]), row["resource_name"], resource_category_by_id(conn, row["resource_category_id"]), - bool(int(row["public"]))) + bool(int(row["public"])), + User.from_sqlite3_row(row), + datetime.fromtimestamp(row["created_at"])) raise NotFoundError(f"Could not find a resource with id '{resource_id}'") diff --git a/gn_auth/auth/authorisation/resources/views.py b/gn_auth/auth/authorisation/resources/views.py index b359a83..2737916 100644 --- a/gn_auth/auth/authorisation/resources/views.py +++ b/gn_auth/auth/authorisation/resources/views.py @@ -454,6 +454,7 @@ def toggle_public(resource_id: UUID) -> Response: new_resource = Resource( old_rsc.resource_id, old_rsc.resource_name, old_rsc.resource_category, public, + old_rsc.created_by, old_rsc.created_at, old_rsc.resource_data) with db.cursor(conn) as cursor: cursor.execute( diff --git a/gn_auth/scripts/assign_data_to_default_admin.py b/gn_auth/scripts/assign_data_to_default_admin.py index 69fc50c..5c8dae7 100644 --- a/gn_auth/scripts/assign_data_to_default_admin.py +++ b/gn_auth/scripts/assign_data_to_default_admin.py @@ -7,6 +7,7 @@ import json import time import random import logging +import datetime from pathlib import Path from uuid import UUID, uuid4 @@ -135,11 +136,11 @@ def __resource_category_by_key__( row["resource_category_description"]) -def __create_resources__(cursor: authdb.DbCursor) -> tuple[Resource, ...]: +def __create_resources__(cursor: authdb.DbCursor, admin: User) -> tuple[Resource, ...]: """Create default resources.""" resources = tuple(Resource( uuid4(), name, __resource_category_by_key__(cursor, catkey), - True, tuple() + True, admin, datetime.datetime.now(), tuple() ) for name, catkey in ( ("mRNA-euhrin", "mrna"), ("pheno-xboecp", "phenotype"), @@ -155,7 +156,7 @@ def __create_resources__(cursor: authdb.DbCursor) -> tuple[Resource, ...]: return resources -def default_resources(conn: authdb.DbConnection, group: Group) -> tuple[ +def default_resources(conn: authdb.DbConnection, group: Group, admin: User) -> tuple[ Resource, ...]: """Create default resources, or return them if they exist.""" with authdb.cursor(conn) as cursor: @@ -170,7 +171,7 @@ def default_resources(conn: authdb.DbConnection, group: Group) -> tuple[ (str(group.group_id),)) rows = cursor.fetchall() if len(rows) == 0: - return __create_resources__(cursor) + return __create_resources__(cursor, admin) return tuple(Resource( UUID(row["resource_id"]), @@ -180,6 +181,8 @@ def default_resources(conn: authdb.DbConnection, group: Group) -> tuple[ row["resource_category_key"], row["resource_category_description"]), bool(row["public"]), + admin, + datetime.datetime.now(), tuple() ) for row in rows) @@ -405,7 +408,7 @@ def entry(authdbpath, mysqldburi): biodb.database_connection(mysqldburi) as bioconn): admin = select_sys_admin(sys_admins(authconn)) the_admin_group = admin_group(authconn, admin) - resources = default_resources(authconn, the_admin_group) + resources = default_resources(authconn, the_admin_group, admin) add_resources_to_group(authconn, resources, the_admin_group) for resource in resources: assign_data_to_resource( diff --git a/gn_auth/scripts/batch_assign_data_to_default_admin.py b/gn_auth/scripts/batch_assign_data_to_default_admin.py index 95d9794..862c4bd 100644 --- a/gn_auth/scripts/batch_assign_data_to_default_admin.py +++ b/gn_auth/scripts/batch_assign_data_to_default_admin.py @@ -16,7 +16,7 @@ from gn_auth.auth.authentication.users import User from gn_auth.auth.authorisation.resources.groups.models import ( Group, db_row_to_group) from gn_auth.scripts.assign_data_to_default_admin import ( - default_resources, assign_data_to_resource) + sys_admins, default_resources, assign_data_to_resource) def resources_group(conn: authdb.DbConnection) -> Maybe: @@ -48,10 +48,10 @@ def resource_owner(conn: authdb.DbConnection) -> Maybe: User.from_sqlite3_row) -def assign_data(authconn: authdb.DbConnection, bioconn, group: Group): +def assign_data(authconn: authdb.DbConnection, bioconn, group: Group, admin: User): """Do actual data assignments.""" try: - for resource in default_resources(authconn, group): + for resource in default_resources(authconn, group, admin): assign_data_to_resource(authconn, bioconn, resource, group) return 1 @@ -76,9 +76,10 @@ if __name__ == "__main__": if Path(authdbpath).exists(): with (authdb.connection(authdbpath) as authconn, biodb.database_connection(mysqldburi) as bioconn): + admin = sys_admins(authconn)[0] return resources_group(authconn).maybe( 1, - lambda group: assign_data(authconn, bioconn, group)) + lambda group: assign_data(authconn, bioconn, group, admin)) logging.error("There is no such SQLite3 database file.") return 1 |
