From 196a9399d28e20c55cbb173ce4052845cfad5bf3 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Fri, 22 Sep 2023 10:29:18 +0300 Subject: Update fetching a single role --- gn_auth/auth/authorisation/roles/models.py | 10 +++++++--- gn_auth/auth/authorisation/roles/views.py | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) (limited to 'gn_auth') diff --git a/gn_auth/auth/authorisation/roles/models.py b/gn_auth/auth/authorisation/roles/models.py index 206b05e..4281446 100644 --- a/gn_auth/auth/authorisation/roles/models.py +++ b/gn_auth/auth/authorisation/roles/models.py @@ -115,7 +115,9 @@ def user_role(conn: db.DbConnection, user: User, role_id: UUID) -> Either: """Retrieve a specific non-resource role assigned to the user.""" with db.cursor(conn) as cursor: cursor.execute( - "SELECT r.*, p.* FROM user_roles AS ur INNER JOIN roles AS r " + "SELECT res.resource_id, ur.user_id, r.*, p.* " + "FROM resources AS res INNER JOIN user_roles AS ur " + "ON res.resource_id=ur.resource_id INNER JOIN roles AS r " "ON ur.role_id=r.role_id INNER JOIN role_privileges AS rp " "ON r.role_id=rp.role_id INNER JOIN privileges AS p " "ON rp.privilege_id=p.privilege_id " @@ -124,8 +126,10 @@ def user_role(conn: db.DbConnection, user: User, role_id: UUID) -> Either: results = cursor.fetchall() if results: - return Right(tuple( - reduce(__organise_privileges__, results, {}).values())[0]) + res_role_obj = tuple(reduce(__organise_privileges__, results, {}).values())[0] + resource_id = res_role_obj["resource_id"] + role = tuple(res_role_obj["roles"].values())[0] + return Right((role, resource_id)) return Left(NotFoundError( f"Could not find role with id '{role_id}'",)) diff --git a/gn_auth/auth/authorisation/roles/views.py b/gn_auth/auth/authorisation/roles/views.py index 29d0991..a9b337f 100644 --- a/gn_auth/auth/authorisation/roles/views.py +++ b/gn_auth/auth/authorisation/roles/views.py @@ -23,4 +23,4 @@ def view_role(role_id: uuid.UUID) -> Response: with db.connection(db_uri) as conn: the_role = user_role(conn, the_token.user, role_id) return the_role.either( - __error__, lambda a_role: jsonify(dictify(a_role))) + __error__, lambda a_role: jsonify((dictify(a_role[0]), str(a_role[1])))) -- cgit v1.2.3