From 79bc3d28df18c0d36de1337becfdb28284989431 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Mon, 23 Jan 2023 14:38:55 +0300 Subject: oauth2: create group Enable creation of a new group. --- wqflask/wqflask/oauth2/routes.py | 74 +++++++++++++++++-- wqflask/wqflask/static/new/css/mytooltip.css | 16 +++++ .../templates/oauth2/group_join_or_create.html | 83 ++++++++++++++++++++++ wqflask/wqflask/templates/oauth2/view-user.html | 42 +++-------- 4 files changed, 175 insertions(+), 40 deletions(-) create mode 100644 wqflask/wqflask/static/new/css/mytooltip.css create mode 100644 wqflask/wqflask/templates/oauth2/group_join_or_create.html diff --git a/wqflask/wqflask/oauth2/routes.py b/wqflask/wqflask/oauth2/routes.py index 2f4d4492..33282dc1 100644 --- a/wqflask/wqflask/oauth2/routes.py +++ b/wqflask/wqflask/oauth2/routes.py @@ -1,5 +1,6 @@ """Routes for the OAuth2 auth system in GN3""" import requests +from typing import Optional from urllib.parse import urljoin from pymonad.maybe import Just, Maybe, Nothing @@ -15,6 +16,9 @@ from .checks import require_oauth2, user_logged_in oauth2 = Blueprint("oauth2", __name__) SCOPE = "profile group role resource register-client" +def __raise_unimplemented__(): + raise Exception("NOT IMPLEMENTED") + def get_endpoint(uri_path: str) -> Maybe: token = session.get("oauth2_token", False) if token and not bool(session.get("user_details", False)): @@ -36,6 +40,10 @@ def get_endpoint(uri_path: str) -> Maybe: return Nothing +def __user_details__(): + return session.get("user_details", False) or get_endpoint( + "oauth2/user").maybe(False, __id__) + def oauth2_get(uri_path: str) -> Either: token = session.get("oauth2_token") config = app.config @@ -49,6 +57,18 @@ def oauth2_get(uri_path: str) -> Either: return Left(resp.json()) +def oauth2_post(uri_path: str, data: dict) -> Either: + token = session.get("oauth2_token") + config = app.config + client = OAuth2Session( + config["OAUTH2_CLIENT_ID"], config["OAUTH2_CLIENT_SECRET"], + token=token, scope=SCOPE) + resp = client.post(urljoin(config["GN_SERVER_URL"], uri_path), data=data) + if resp.status_code == 200: + return Right(resp.json()) + + return Left(resp.json()) + @oauth2.route("/login", methods=["GET", "POST"]) def login(): """Route to allow users to sign up.""" @@ -139,8 +159,7 @@ def register_client(): @require_oauth2 def user_profile(): __id__ = lambda the_val: the_val - user_details = session.get("user_details", False) or get_endpoint( - "oauth2/user").maybe(False, __id__) + user_details = __user_details__() config = app.config client = OAuth2Session( config["OAUTH2_CLIENT_ID"], config["OAUTH2_CLIENT_SECRET"], @@ -148,10 +167,8 @@ def user_profile(): roles = oauth2_get("oauth2/user-roles").either(lambda x: "Error", lambda x: x) resources = [] - groups = [] if user_details.get("group") else oauth2_get("oauth2/groups").either( - lambda x: "Error", lambda x: x) return render_template( - "oauth2/view-user.html", user_details=user_details, groups=groups, + "oauth2/view-user.html", user_details=user_details, roles=roles, resources=resources) @oauth2.route("/request-add-to-group", methods=["POST"]) @@ -159,7 +176,52 @@ def user_profile(): def request_add_to_group(): return "WOULD SEND MESSAGE TO HAVE YOU ADDED TO GROUP..." +def __handle_error__(redirect_uri: Optional[str] = None, **kwargs): + def __handler__(error): + print(f"ERROR: {error}") + msg = error.get( + "error_message", error.get("error_description", "undefined error")) + flash(f"{error['error']}: {msg}.", + "alert-danger") + if "response_handlers" in kwargs: + for handler in kwargs["response_handlers"]: + handler(response) + if redirect: + return redirect(url_for(redirect_uri, **kwargs)) + + return __handler__ + +def __handle_success__( + success_msg: str, redirect_uri: Optional[str] = None, **kwargs): + def __handler__(response): + flash(f"Success: {success_msg}.", "alert-success") + if "response_handlers" in kwargs: + for handler in kwargs["response_handlers"]: + handler(response) + if redirect: + return redirect(url_for(redirect_uri, **kwargs)) + + return __handler__ + @oauth2.route("/create-group", methods=["POST"]) @require_oauth2 def create_group(): - return "WOULD CREATE A NEW GROUP..." + def __setup_group__(response): + session["user_details"]["group"] = response + + resp = oauth2_post("oauth2/create-group", data=dict(request.form)) + return resp.either( + __handle_error__("oauth2.group_join_or_create"), + __handle_success__( + "Created group", "oauth2.user_profile", + response_handlers=__setup_group__)) + +@oauth2.route("/group-join-or-create", methods=["GET"]) +def group_join_or_create(): + user_details = __user_details__() + if bool(user_details["group"]): + flash("You are already a member of a group.", "alert info.") + return redirect(url_for("oauth2.user_profile")) + groups = oauth2_get("oauth2/groups").either( + lambda x: __raise_unimplemented__(), lambda x: x) + return render_template("oauth2/group_join_or_create.html", groups=groups) diff --git a/wqflask/wqflask/static/new/css/mytooltip.css b/wqflask/wqflask/static/new/css/mytooltip.css new file mode 100644 index 00000000..bc44c488 --- /dev/null +++ b/wqflask/wqflask/static/new/css/mytooltip.css @@ -0,0 +1,16 @@ +.mytooltiptext { + visibility: hidden; + width: inherit; + background-color: #E5E5AF; + color: #595959; + text-align: left; + padding: 5px 0; + border-radius: 6px; + + position: absolute; + z-index: 1; +} + +.mytooltip:hover .mytooltiptext { + visibility: visible; +} diff --git a/wqflask/wqflask/templates/oauth2/group_join_or_create.html b/wqflask/wqflask/templates/oauth2/group_join_or_create.html new file mode 100644 index 00000000..d0a10e1b --- /dev/null +++ b/wqflask/wqflask/templates/oauth2/group_join_or_create.html @@ -0,0 +1,83 @@ +{%extends "base.html"%} +{%block title%}Join or Create Group{%endblock%} + +{%block css%} + +{%endblock%} +{%block content%} +
+

Join or Create Group

+ + {{flash_me()}} + +

You can

+ + {%if groups | length > 0 %} +
+

+ For most users, this is the preffered choice. You request access to an + existing group, and the group leader will chose whether or not to add you to + their group.

+ +

You can only be a member of a single group.

+
+ +
+ Request to be added to group +
+ + +
+
+ +
+
+ +

or

+ {%else%} +

+ +   + There an currently no groups to join. +

+ {%endif%} + +
+

+ Creating a new group automatically makes you that group's administrator. +

+ +

You can only be a member of a single group.

+
+ +
+ Create a new group +
+ + + + Name of the group. + +
+
+ + + + A description to help identify the purpose/goal of the group + +
+
+ +
+
+
+{%endblock%} diff --git a/wqflask/wqflask/templates/oauth2/view-user.html b/wqflask/wqflask/templates/oauth2/view-user.html index 2d5270d9..a617c66e 100644 --- a/wqflask/wqflask/templates/oauth2/view-user.html +++ b/wqflask/wqflask/templates/oauth2/view-user.html @@ -4,6 +4,8 @@

View User

+ {{flash_me()}} +
@@ -13,7 +15,7 @@

Name: {{user_details.name}}

E-Mail: {{user_details.email}}

{%if user_details.group%} -

Group:{{user_details.group}}

+

Group:{{user_details.group.group_name}}

{%else%}

@@ -21,39 +23,11 @@ User is not a member of a group.

-

You can

- -
- Request to be added to group -
- - -
-
- -
-
- -

or

- -
- Create a new group -
- - -
-
- -
-
+

+ Join or Create group +

{%endif%} {%else%}

No details found.

-- cgit v1.2.3