2021-10-28 16:09:10 +02:00
|
|
|
from flask import jsonify, request
|
2022-06-21 14:41:54 +02:00
|
|
|
from flask_jwt_extended import get_jwt, jwt_required
|
2021-10-28 16:09:10 +02:00
|
|
|
from flask_cors import cross_origin
|
2021-11-02 08:54:07 +01:00
|
|
|
from flask_expects_json import expects_json
|
2021-10-28 16:09:10 +02:00
|
|
|
|
|
|
|
from areas import api_v1
|
2021-11-02 08:54:07 +01:00
|
|
|
from helpers import KratosApi
|
2022-06-08 21:41:59 +02:00
|
|
|
from helpers.auth_guard import admin_required
|
2022-04-14 13:32:35 +02:00
|
|
|
|
2021-11-02 08:54:07 +01:00
|
|
|
from .validation import schema
|
2022-04-14 13:32:35 +02:00
|
|
|
from .user_service import UserService
|
2021-10-28 16:09:10 +02:00
|
|
|
|
|
|
|
|
2021-11-02 08:54:07 +01:00
|
|
|
@api_v1.route("/users", methods=["GET"])
|
2021-10-28 16:09:10 +02:00
|
|
|
@jwt_required()
|
|
|
|
@cross_origin()
|
2022-06-08 21:41:59 +02:00
|
|
|
@admin_required()
|
2021-10-28 16:09:10 +02:00
|
|
|
def get_users():
|
2022-04-14 13:32:35 +02:00
|
|
|
res = UserService.get_users()
|
|
|
|
return jsonify(res)
|
2021-10-28 16:09:10 +02:00
|
|
|
|
2021-11-02 08:54:07 +01:00
|
|
|
|
|
|
|
@api_v1.route("/users/<string:id>", methods=["GET"])
|
2021-10-28 16:09:10 +02:00
|
|
|
@jwt_required()
|
|
|
|
@cross_origin()
|
2022-06-21 14:41:54 +02:00
|
|
|
@admin_required()
|
2021-10-28 16:09:10 +02:00
|
|
|
def get_user(id):
|
2022-04-14 13:32:35 +02:00
|
|
|
res = UserService.get_user(id)
|
|
|
|
return jsonify(res)
|
2021-10-28 16:09:10 +02:00
|
|
|
|
|
|
|
|
2021-11-02 08:54:07 +01:00
|
|
|
@api_v1.route("/users", methods=["POST"])
|
2021-10-28 16:09:10 +02:00
|
|
|
@jwt_required()
|
|
|
|
@cross_origin()
|
2021-11-02 08:54:07 +01:00
|
|
|
@expects_json(schema)
|
2022-06-15 14:30:19 +02:00
|
|
|
@admin_required()
|
2021-10-28 16:09:10 +02:00
|
|
|
def post_user():
|
|
|
|
data = request.get_json()
|
2022-04-14 13:32:35 +02:00
|
|
|
res = UserService.post_user(data)
|
|
|
|
return jsonify(res)
|
2021-10-28 16:09:10 +02:00
|
|
|
|
|
|
|
|
2021-11-02 08:54:07 +01:00
|
|
|
@api_v1.route("/users/<string:id>", methods=["PUT"])
|
2021-10-28 16:09:10 +02:00
|
|
|
@jwt_required()
|
|
|
|
@cross_origin()
|
2021-11-02 08:54:07 +01:00
|
|
|
@expects_json(schema)
|
2022-06-21 14:41:54 +02:00
|
|
|
@admin_required()
|
2021-10-28 16:09:10 +02:00
|
|
|
def put_user(id):
|
|
|
|
data = request.get_json()
|
2022-07-11 21:55:31 +02:00
|
|
|
user_id = __get_user_id_from_jwt()
|
|
|
|
res = UserService.put_user(id, user_id, data)
|
2022-04-14 13:32:35 +02:00
|
|
|
return jsonify(res)
|
2021-10-28 16:09:10 +02:00
|
|
|
|
|
|
|
|
2021-11-02 08:54:07 +01:00
|
|
|
@api_v1.route("/users/<string:id>", methods=["DELETE"])
|
2021-10-28 16:09:10 +02:00
|
|
|
@jwt_required()
|
|
|
|
@cross_origin()
|
2022-06-08 21:41:59 +02:00
|
|
|
@admin_required()
|
2021-10-28 16:09:10 +02:00
|
|
|
def delete_user(id):
|
2022-05-19 19:01:26 +02:00
|
|
|
res = KratosApi.delete("/identities/{}".format(id))
|
|
|
|
if res.status_code == 204:
|
|
|
|
UserService.delete_user(id)
|
|
|
|
return jsonify(), res.status_code
|
|
|
|
return jsonify(res.json()), res.status_code
|
2022-06-21 14:41:54 +02:00
|
|
|
|
|
|
|
|
|
|
|
@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()
|
2022-07-11 21:55:31 +02:00
|
|
|
res = UserService.put_user(user_id, user_id, data)
|
2022-06-21 14:41:54 +02:00
|
|
|
return jsonify(res)
|
|
|
|
|
|
|
|
|
|
|
|
def __get_user_id_from_jwt():
|
|
|
|
claims = get_jwt()
|
|
|
|
return claims["user_id"]
|