From 31e987cd2b5059cc0c45429103c45619a69caa27 Mon Sep 17 00:00:00 2001 From: lucasbutzke Date: Mon, 28 Jul 2025 21:55:59 -0400 Subject: [PATCH] [RTOP-40] User check returnign if there were already created users if invalid JWT is used --- webserver/restapi.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/webserver/restapi.py b/webserver/restapi.py index a6b0f85..fdb1573 100644 --- a/webserver/restapi.py +++ b/webserver/restapi.py @@ -136,16 +136,28 @@ def update_user(user_id): # TODO List all users (Admin only) @restapi_bp.route("/", methods=["GET"]) -@jwt_required() def list_users(): # TODO implement role-based access control # For now, we will just check if the user is an admin # if not is_admin(): # return jsonify({"msg": "Admin privileges required"}), 403 + print("Listing users...") + + # If there are no users, we don't need to verify JWT + try: + verify_jwt_in_request() + except Exception as e: + print("No JWT token provided, checking for users without authentication") + try: + users_exist = User.query.first() is not None + except Exception as e: + print(f"Error checking for users: {e}") + return jsonify({"msg": "User retrieval error"}), 500 + + if not users_exist: + return jsonify({"msg": "No users found"}), 404 + return jsonify({"msg": "Users found"}), 200 - # TODO just returns if there is already users in the database if - # not logged in as admin - # if verify_jwt_in_request(optional=True): try: users = User.query.all() except Exception as e: @@ -154,6 +166,7 @@ def list_users(): return jsonify([user.to_dict() for user in users]), 200 + # password change for specific user by any authenticated user @restapi_bp.route("//password_change", methods=["PUT"]) @jwt_required()