From 79df53abade6c2d2cbc91da9f5078cc527fb8c0f Mon Sep 17 00:00:00 2001 From: Munyoki Kilyungi Date: Mon, 25 Mar 2024 20:45:51 +0300 Subject: Flatten roles list in "get_user_roles_on_resource." * gn_auth/auth/authorisation/resources/views.py: Import operator. (get_user_roles_on_resource): Flatten roles list. Signed-off-by: Munyoki Kilyungi --- gn_auth/auth/authorisation/resources/views.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'gn_auth') diff --git a/gn_auth/auth/authorisation/resources/views.py b/gn_auth/auth/authorisation/resources/views.py index 8034110..e256b3e 100644 --- a/gn_auth/auth/authorisation/resources/views.py +++ b/gn_auth/auth/authorisation/resources/views.py @@ -1,6 +1,7 @@ """The views/routes for the resources package""" import uuid import json +import operator import sqlite3 from dataclasses import asdict @@ -381,16 +382,25 @@ def get_user_roles_on_resource(name) -> Response: resid = with_db_connection( lambda conn: get_resource_id(conn, name) ) + + def _extract_privilege_id(privileges): + return tuple( + p_.privilege_id for p_ in privileges + ) + with require_oauth.acquire("profile resource") as _token: _resources = with_db_connection( lambda conn: user_roles_on_resources( conn, _token.user, (resid,) ) ) + _roles = tuple( + _extract_privilege_id(role.privileges) + for role in + _resources.get( + uuid.UUID(resid), {} + ).get("roles", tuple())) return jsonify({ - name: { - "roles": tuple( - asdict(rol) for rol in - _resources.get(resid, {}).get("roles", tuple())) - } - }) + # Flatten this list + "roles": reduce(operator.iconcat, _roles, []) + }) -- cgit v1.2.3