From fa7452e4f08352526257d1f0dc5181302a55d3e2 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Mon, 10 Jun 2024 15:22:20 -0500 Subject: Provide some endpoints for privileges. --- gn_auth/auth/authorisation/privileges/views.py | 32 ++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 gn_auth/auth/authorisation/privileges/views.py (limited to 'gn_auth/auth/authorisation/privileges/views.py') diff --git a/gn_auth/auth/authorisation/privileges/views.py b/gn_auth/auth/authorisation/privileges/views.py new file mode 100644 index 0000000..d50e5cb --- /dev/null +++ b/gn_auth/auth/authorisation/privileges/views.py @@ -0,0 +1,32 @@ +"""Routes for privileges.""" +from dataclasses import asdict + +from werkzeug.exceptions import NotFound +from flask import jsonify, Blueprint, current_app as app + +from gn_auth.auth.db import sqlite3 as db + +from .models import all_privileges, privilege_by_id + +privileges = Blueprint("privileges", __name__) + +@privileges.route("/", methods=["GET"]) +@privileges.route("/list", methods=["GET"]) +def list_privileges(): + """List all the available privileges.""" + with db.connection(app.config["AUTH_DB"]) as conn: + _privileges = all_privileges(conn) + + return jsonify(_privileges if bool(_privileges) else []), 200 + +@privileges.route("//", methods=["GET"]) +@privileges.route("//view", methods=["GET"]) +def view_privilege(privilege_id: str): + """View details of a single privilege""" + with db.connection(app.config["AUTH_DB"]) as conn: + _privilege = privilege_by_id(conn, privilege_id) + + if bool(_privilege): + return jsonify(_privilege) + + raise NotFound(f"No privilege exists with ID '{privilege_id}'") -- cgit v1.2.3