diff options
author | Frederick Muriuki Muriithi | 2023-09-22 10:29:18 +0300 |
---|---|---|
committer | Frederick Muriuki Muriithi | 2023-09-26 03:44:33 +0300 |
commit | 196a9399d28e20c55cbb173ce4052845cfad5bf3 (patch) | |
tree | f5925740bf72d933cecc6a1493784a535d25943a /gn_auth/auth/authorisation/roles | |
parent | ac76839f1cadbbc647c9772b3fdd4d496587b400 (diff) | |
download | gn-auth-196a9399d28e20c55cbb173ce4052845cfad5bf3.tar.gz |
Update fetching a single role
Diffstat (limited to 'gn_auth/auth/authorisation/roles')
-rw-r--r-- | gn_auth/auth/authorisation/roles/models.py | 10 | ||||
-rw-r--r-- | gn_auth/auth/authorisation/roles/views.py | 2 |
2 files changed, 8 insertions, 4 deletions
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])))) |