diff --git a/areas/users/user_service.py b/areas/users/user_service.py index 882a7ad..a0abfda 100644 --- a/areas/users/user_service.py +++ b/areas/users/user_service.py @@ -1,5 +1,6 @@ from database import db from areas.apps import App, AppRole, AppsService +from areas.roles.role_service import RoleService from helpers import KratosApi from flask import current_app @@ -57,14 +58,16 @@ class UserService: return UserService.get_user(res["id"]) @staticmethod - def put_user(id, data): + def put_user(id, user_editing_id, data): kratos_data = { "schema_id": "default", "traits": {"email": data["email"], "name": data["name"]}, } KratosApi.put("/admin/identities/{}".format(id), kratos_data) - if data["app_roles"]: + is_admin = RoleService.is_user_admin(user_editing_id) + + if is_admin and data["app_roles"]: app_roles = data["app_roles"] for ar in app_roles: app = App.query.filter_by(slug=ar["name"]).first() diff --git a/areas/users/users.py b/areas/users/users.py index 03b7c30..1ef36cd 100644 --- a/areas/users/users.py +++ b/areas/users/users.py @@ -1,5 +1,5 @@ from flask import jsonify, request -from flask_jwt_extended import jwt_required +from flask_jwt_extended import get_jwt, jwt_required from flask_cors import cross_origin from flask_expects_json import expects_json @@ -23,6 +23,7 @@ def get_users(): @api_v1.route("/users/", methods=["GET"]) @jwt_required() @cross_origin() +@admin_required() def get_user(id): res = UserService.get_user(id) return jsonify(res) @@ -43,9 +44,11 @@ def post_user(): @jwt_required() @cross_origin() @expects_json(schema) +@admin_required() def put_user(id): data = request.get_json() - res = UserService.put_user(id, data) + user_id = __get_user_id_from_jwt() + res = UserService.put_user(id, user_id, data) return jsonify(res) @@ -70,3 +73,28 @@ def post_multiple_users(): data = request.get_json() res = UserService.post_multiple_users(data) return jsonify(res) + + +@api_v1.route("/me", methods=["GET"]) +@jwt_required() +@cross_origin() +def get_personal_info(): + user_id = __get_user_id_from_jwt() + res = UserService.get_user(user_id) + return jsonify(res) + + +@api_v1.route("/me", methods=["PUT"]) +@jwt_required() +@cross_origin() +@expects_json(schema) +def update_personal_info(): + data = request.get_json() + user_id = __get_user_id_from_jwt() + res = UserService.put_user(user_id, user_id, data) + return jsonify(res) + + +def __get_user_id_from_jwt(): + claims = get_jwt() + return claims["user_id"] diff --git a/docker-compose.yml b/docker-compose.yml index 8e0bae6..43e2e2f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -40,7 +40,7 @@ services: - kube_port_mysql entrypoint: ["bash", "-c", "flask run --host $$(hostname -i)"] kube_port_kratos_admin: - image: bitnami/kubectl:1.24.2 + image: bitnami/kubectl:1.24.3 user: "${KUBECTL_UID}:${KUBECTL_GID}" expose: - 8000 @@ -48,7 +48,7 @@ services: - "$KUBECONFIG:/.kube/config" entrypoint: ["bash", "-c", "kubectl -n stackspin port-forward --address $$(hostname -i) service/kratos-admin 8000:80"] kube_port_hydra_admin: - image: bitnami/kubectl:1.24.2 + image: bitnami/kubectl:1.24.3 user: "${KUBECTL_UID}:${KUBECTL_GID}" expose: - 4445 @@ -56,7 +56,7 @@ services: - "$KUBECONFIG:/.kube/config" entrypoint: ["bash", "-c", "kubectl -n stackspin port-forward --address $$(hostname -i) service/hydra-admin 4445:4445"] kube_port_kratos_public: - image: bitnami/kubectl:1.24.2 + image: bitnami/kubectl:1.24.3 user: "${KUBECTL_UID}:${KUBECTL_GID}" ports: - "8080:8080" @@ -66,7 +66,7 @@ services: - "$KUBECONFIG:/.kube/config" entrypoint: ["bash", "-c", "kubectl -n stackspin port-forward --address $$(hostname -i) service/kratos-public 8080:80"] kube_port_mysql: - image: bitnami/kubectl:1.24.2 + image: bitnami/kubectl:1.24.3 user: "${KUBECTL_UID}:${KUBECTL_GID}" expose: - 3306 diff --git a/helpers/auth_guard.py b/helpers/auth_guard.py index 900e35e..36bbeeb 100644 --- a/helpers/auth_guard.py +++ b/helpers/auth_guard.py @@ -2,10 +2,10 @@ from functools import wraps from areas.roles.role_service import RoleService -from flask_jwt_extended import verify_jwt_in_request -from flask_jwt_extended import get_jwt +from flask_jwt_extended import get_jwt, verify_jwt_in_request from helpers import Unauthorized + def admin_required(): def wrapper(fn): @wraps(fn) @@ -21,4 +21,4 @@ def admin_required(): return decorator - return wrapper \ No newline at end of file + return wrapper