aboutsummaryrefslogtreecommitdiff
path: root/gn2/wqflask/oauth2/roles.py
blob: b0f990c7345c92aa235c213109c26f0826b500a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""Handle role endpoints"""
import uuid

from flask import flash, request, url_for, redirect, Blueprint

from .ui import render_ui
from .checks import require_oauth2
from .client import oauth2_get, oauth2_post
from .request_utils import request_error, process_error

roles = Blueprint("role", __name__)

@roles.route("/role/<uuid:role_id>", methods=["GET"])
@require_oauth2
def role(role_id: uuid.UUID):
    def __success__(the_role):
        return render_ui("oauth2/role.html",
                         role=the_role[0],
                         resource_id=uuid.UUID(the_role[1]))

    return oauth2_get(f"auth/role/view/{role_id}").either(
        request_error, __success__)

@roles.route("/create", methods=["GET", "POST"])
@require_oauth2
def create_role():
    """Create a new role."""
    def __roles_error__(error):
        return render_ui(
            "oauth2/create-role.html", roles_error=process_error(error))

    def __gprivs_error__(roles, error):
        return render_ui(
            "oauth2/create-role.html", roles=roles,
            group_privileges_error=process_error(error))

    def __success__(roles, gprivs):
        uprivs = tuple(
            privilege["privilege_id"] for role in roles
            for privilege in role["privileges"])
        return render_ui(
            "oauth2/create-role.html", roles=roles, user_privileges=uprivs,
            group_privileges=gprivs,
            prev_role_name=request.args.get("role_name"))

    def __fetch_gprivs__(roles):
        return oauth2_get("auth/group/privileges").either(
            lambda err: __gprivs_error__(roles, err),
            lambda gprivs: __success__(roles, gprivs))

    if request.method == "GET":
        return oauth2_get("auth/user/roles").either(
            __roles_error__, __fetch_gprivs__)

    form = request.form
    role_name = form.get("role_name")
    privileges = form.getlist("privileges[]")
    if len(privileges) == 0:
        flash("You must assign at least one privilege to the role",
              "alert-danger")
        return redirect(url_for(
            "oauth2.role.create_role", role_name=role_name))
    def __create_error__(error):
        err = process_error(error)
        flash(f"{err['error']}: {err['error_description']}",
              "alert-danger")
        return redirect(url_for("oauth2.role.create_role"))
    def __create_success__(*args):
        flash("Role created successfully.", "alert-success")
        return redirect(url_for("oauth2.role.user_roles"))

    raise DeprecationWarning(
        f"The `{__name__}.create_role(…)` function, as is currently, can "
        "lead to unbounded privilege escalation. See "
        "https://issues.genenetwork.org/issues/gn-auth/problems-with-roles")
    # return oauth2_post(
    #     "auth/group/role/create",data={
    #         "role_name": role_name, "privileges[]": privileges}).either(
    #     __create_error__,__create_success__)