From a060c0d277300d5fda06772bf95e1456159111e2 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Tue, 11 Jun 2024 11:20:34 -0500 Subject: List users assigned a particular role on a specific resource. --- gn_auth/auth/authorisation/resources/views.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/gn_auth/auth/authorisation/resources/views.py b/gn_auth/auth/authorisation/resources/views.py index f0413e8..4c8411f 100644 --- a/gn_auth/auth/authorisation/resources/views.py +++ b/gn_auth/auth/authorisation/resources/views.py @@ -535,3 +535,21 @@ def unassign_resource_role_privilege(resource_id: UUID, role_id: UUID): "status": "Success", "message": "Privilege was unassigned." }), 200 + + +@resources.route("//role//users", + methods=["GET"]) +@require_oauth("profile group resource") +def resource_role_users(resource_id: UUID, role_id: UUID): + """Retrieve users assigned role on resource.""" + with (require_oauth.acquire("profile group resource") as _token, + db.connection(app.config["AUTH_DB"]) as conn, + db.cursor(conn) as cursor): + # MAYBE: check user has something like resource:role:view-users + cursor.execute( + "SELECT u.* FROM user_roles AS ur INNER JOIN users AS u " + "ON ur.user_id=u.user_id WHERE ur.resource_id=? AND ur.role_id=?", + (str(resource_id), str(role_id))) + results = cursor.fetchall() or [] + + return jsonify(tuple(User.from_sqlite3_row(row) for row in results)), 200 -- cgit v1.2.3