about summary refs log tree commit diff
path: root/gn_auth/auth/authorisation/roles
diff options
context:
space:
mode:
Diffstat (limited to 'gn_auth/auth/authorisation/roles')
-rw-r--r--gn_auth/auth/authorisation/roles/models.py20
-rw-r--r--gn_auth/auth/authorisation/roles/views.py6
2 files changed, 17 insertions, 9 deletions
diff --git a/gn_auth/auth/authorisation/roles/models.py b/gn_auth/auth/authorisation/roles/models.py
index dc1dfdc..6faeaca 100644
--- a/gn_auth/auth/authorisation/roles/models.py
+++ b/gn_auth/auth/authorisation/roles/models.py
@@ -133,10 +133,10 @@ def user_roles(conn: db.DbConnection, user: User) -> Sequence[dict]:
     return tuple()
 
 
-def user_resource_roles(
+def user_roles_on_resource(
         conn: db.DbConnection,
-        user: User,
-        resource: Resource
+        user_id: UUID,
+        resource_id: UUID
 ) -> tuple[Role, ...]:
     """Retrieve all roles assigned to a user for a particular resource."""
     with db.cursor(conn) as cursor:
@@ -147,12 +147,22 @@ def user_resource_roles(
             "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 "
             "WHERE ur.user_id=? AND ur.resource_id=?",
-            (str(user.user_id), str(resource.resource_id)))
+            (str(user_id), str(resource_id)))
 
         return db_rows_to_roles(cursor.fetchall())
     return tuple()
 
 
+def user_resource_roles(
+        conn: db.DbConnection,
+        user: User,
+        resource: Resource
+) -> tuple[Role, ...]:
+    "Retrieve roles a user has on a particular resource."
+    # TODO: Temporary placeholder to prevent system from breaking.
+    return user_roles_on_resource(conn, user.user_id, resource.resource_id)
+
+
 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:
@@ -261,7 +271,7 @@ def role_by_id(conn: db.DbConnection, role_id: UUID) -> Optional[Role]:
 
     _roles = db_rows_to_roles(results)
     if len(_roles) > 1:
-        raise Exception("Data corruption: Expected a single role.")
+        raise Exception("Data corruption: Expected a single role.")# pylint: disable=[broad-exception-raised]
 
     return _roles[0]
 
diff --git a/gn_auth/auth/authorisation/roles/views.py b/gn_auth/auth/authorisation/roles/views.py
index 00def89..dda6f87 100644
--- a/gn_auth/auth/authorisation/roles/views.py
+++ b/gn_auth/auth/authorisation/roles/views.py
@@ -7,7 +7,7 @@ from flask import jsonify, Response, Blueprint, current_app
 
 from ...db import sqlite3 as db
 
-from .models import user_role
+from .models import role_by_id
 
 from ...authentication.oauth2.resource_server import require_oauth
 
@@ -22,6 +22,4 @@ def view_role(role_id: uuid.UUID) -> Response:
     with require_oauth.acquire("profile role") as the_token:
         db_uri = current_app.config["AUTH_DB"]
         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((asdict(a_role[0]), str(a_role[1]))))
+            return jsonify(asdict(role_by_id(conn, role_id)))