From 8a0c18c575c90a8a203567b3dcb6042e06d2ef14 Mon Sep 17 00:00:00 2001 From: terrtia Date: Wed, 8 May 2024 14:09:53 +0200 Subject: [PATCH] chg: [settings] refactor UI settings --- bin/lib/ail_users.py | 63 +++++++- var/www/blueprints/settings_b.py | 43 +++++- var/www/modules/settings/Flask_settings.py | 44 ++---- .../settings/templates/users_list.html | 139 ------------------ var/www/templates/settings/menu_sidebar.html | 4 +- var/www/templates/settings/users_list.html | 137 +++++++++++++++++ 6 files changed, 249 insertions(+), 181 deletions(-) delete mode 100644 var/www/modules/settings/templates/users_list.html create mode 100644 var/www/templates/settings/users_list.html diff --git a/bin/lib/ail_users.py b/bin/lib/ail_users.py index 81fd377e..1cec0f9c 100755 --- a/bin/lib/ail_users.py +++ b/bin/lib/ail_users.py @@ -174,6 +174,14 @@ def exists_token(token): # TODO USER LAST LOGIN TIME # TODO Check if logged + +# TODO USER: - Creation Date +# - Last Login +# - Last Request +# - Last API Usage +# - Organisation ??? +# - Disabled / Lock + class AILUser(UserMixin): def __init__(self, user_id): self.user_id = user_id @@ -200,8 +208,13 @@ class AILUser(UserMixin): def exists(self): # TODO CHECK USAGE return r_serv_db.exists(f'ail:user:metadata:{self.user_id}') - def get_meta(self): - return {'email': self.user_id,} + def get_meta(self, options=set()): # TODO user creation date + meta = {'id': self.user_id} + if 'api_key' in options: # TODO add option to censor key + meta['api_key'] = self.get_api_key() + if 'role' in options: + meta['role'] = get_user_role(self.user_id) + return meta ## SESSION ## @@ -253,6 +266,17 @@ class AILUser(UserMixin): # create new token generate_new_token(self.user_id) + ## TOKEN ## + + def get_api_key(self): + return get_user_token(self.user_id) + + def new_api_key(self): + _delete_user_token(self.user_id) + new_api_key = gen_token() + _set_user_token(self.user_id, new_api_key) + return new_api_key + ## ROLE ## def is_in_role(self, role): # TODO Get role via user alternative ID @@ -266,14 +290,45 @@ class AILUser(UserMixin): def get_role(self): return r_serv_db.hget(f'ail:user:metadata:{self.user_id}', 'role') - # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # + ## ## - def delete(self): # TODO DESTROY SESSION + def delete(self): kill_session_user(self.user_id) + for role_id in get_all_roles(): + r_serv_db.srem(f'ail:users:role:{role_id}', self.user_id) + user_token = self.get_api_key() + if user_token: + r_serv_db.hdel('ail:users:tokens', user_token) + r_serv_db.delete(f'ail:user:metadata:{self.user_id}') + r_serv_db.hdel('ail:users:all', self.user_id) # def create_user(user_id): +#### API #### + +def api_get_users_meta(): + meta = {'users': []} + options = {'api_key', 'role'} + for user_id in get_users(): + user = AILUser(user_id) + meta['users'].append(user.get_meta(options=options)) + return meta + +def api_create_user_api_key(user_id, admin_id): # TODO LOG ADMIN ID + user = AILUser(user_id) + if not user.exists(): + return {'status': 'error', 'reason': 'User not found'}, 404 + print(admin_id) + return user.new_api_key(), 200 + +def api_delete_user(user_id, admin_id): # TODO LOG ADMIN ID + user = AILUser(user_id) + if not user.exists(): + return {'status': 'error', 'reason': 'User not found'}, 404 + print(admin_id) + return user.delete(), 200 + ######################################################################################################################## ######################################################################################################################## diff --git a/var/www/blueprints/settings_b.py b/var/www/blueprints/settings_b.py index b154e196..31d8b9e9 100644 --- a/var/www/blueprints/settings_b.py +++ b/var/www/blueprints/settings_b.py @@ -20,6 +20,7 @@ sys.path.append(os.environ['AIL_BIN']) # Import Project packages ################################## from lib import ail_updates +from lib import ail_users from packages import git_status # ============ BLUEPRINT ============ @@ -40,9 +41,9 @@ def create_json_response(data, status_code): def settings_page(): git_metadata = git_status.get_git_metadata() ail_version = ail_updates.get_ail_version() - admin_level = current_user.is_in_role('admin') + acl_admin = current_user.is_in_role('admin') return render_template("settings_index.html", git_metadata=git_metadata, - ail_version=ail_version, admin_level=admin_level) + ail_version=ail_version, acl_admin=acl_admin) @settings_b.route("/settings/background_update/json", methods=['GET']) @login_required @@ -54,12 +55,46 @@ def get_background_update_metadata_json(): @login_required @login_read_only def settings_modules(): - admin_level = current_user.is_in_role('admin') - return render_template("settings/modules.html", admin_level=admin_level) + acl_admin = current_user.is_in_role('admin') + return render_template("settings/modules.html", acl_admin=acl_admin) +@settings_b.route("/settings/user/profile", methods=['GET']) +@login_required +@login_read_only +def user_profile(): + acl_admin = current_user.is_in_role('admin') +@settings_b.route("/settings/new_user_api_key", methods=['GET']) +@login_required +@login_admin +def new_token_user(): + user_id = request.args.get('user_id') + admin_id = current_user.get_user_id() + r = ail_users.api_create_user_api_key(user_id, admin_id) + if r[1] != 200: + return create_json_response(r[0], r[1]) + else: + return redirect(url_for('settings_b.users_list')) +@settings_b.route("/settings/delete_user", methods=['GET']) +@login_required +@login_admin +def delete_user(): + user_id = request.args.get('user_id') + admin_id = current_user.get_user_id() + r = ail_users.api_delete_user(user_id, admin_id) + if r[1] != 200: + return create_json_response(r[0], r[1]) + else: + return redirect(url_for('settings_b.users_list')) +@settings_b.route("/settings/users", methods=['GET']) +@login_required +@login_admin +def users_list(): + meta = ail_users.api_get_users_meta() + new_user = {} + return render_template("users_list.html", meta=meta, new_user=new_user, acl_admin=True) diff --git a/var/www/modules/settings/Flask_settings.py b/var/www/modules/settings/Flask_settings.py index 2b1b8826..35075ce9 100644 --- a/var/www/modules/settings/Flask_settings.py +++ b/var/www/modules/settings/Flask_settings.py @@ -61,17 +61,6 @@ def new_token(): Users.generate_new_token(current_user.get_id()) return redirect(url_for('settings.edit_profile')) - -@settings.route("/settings/new_token_user", methods=['POST']) -@login_required -@login_admin -def new_token_user(): - user_id = request.form.get('user_id') - if Users.exists_user(user_id): - Users.generate_new_token(user_id) - return redirect(url_for('settings.users_list')) - - @settings.route("/settings/create_user", methods=['GET']) @login_required @login_admin @@ -133,18 +122,18 @@ def create_user_post(): return render_template("create_user.html", all_roles=all_roles, error_mail=True, admin_level=True) -@settings.route("/settings/users_list", methods=['GET']) -@login_required -@login_admin -def users_list(): - all_users = Users.get_users_metadata(Users.get_all_users()) - new_user = request.args.get('new_user') - new_user_dict = {} - if new_user: - new_user_dict['email'] = new_user - new_user_dict['edited'] = request.args.get('new_user_edited') - new_user_dict['password'] = request.args.get('new_user_password') - return render_template("users_list.html", all_users=all_users, new_user=new_user_dict, admin_level=True) +# @settings.route("/settings/users_list", methods=['GET']) +# @login_required +# @login_admin +# def users_list(): +# all_users = Users.get_users_metadata(Users.get_all_users()) +# new_user = request.args.get('new_user') +# new_user_dict = {} +# if new_user: +# new_user_dict['email'] = new_user +# new_user_dict['edited'] = request.args.get('new_user_edited') +# new_user_dict['password'] = request.args.get('new_user_password') +# return render_template("users_list.html", all_users=all_users, new_user=new_user_dict, admin_level=True) @settings.route("/settings/edit_user", methods=['POST']) @@ -155,15 +144,6 @@ def edit_user(): return redirect(url_for('settings.create_user', user_id=user_id)) -@settings.route("/settings/delete_user", methods=['POST']) -@login_required -@login_admin -def delete_user(): - user_id = request.form.get('user_id') - Users.delete_user(user_id) - return redirect(url_for('settings.users_list')) - - @settings.route("/settings/passivedns", methods=['GET']) @login_required @login_read_only diff --git a/var/www/modules/settings/templates/users_list.html b/var/www/modules/settings/templates/users_list.html deleted file mode 100644 index 00601977..00000000 --- a/var/www/modules/settings/templates/users_list.html +++ /dev/null @@ -1,139 +0,0 @@ - - - - - Server Management - AIL - - - - - - - - - - - - - - - - - - {% include 'nav_bar.html' %} - -
-
- - {% include 'settings/menu_sidebar.html' %} - -
- - {% if new_user %} -
-
-
- {% if new_user['edited']=='True' %} -
User Edited
- {% else %} -
User Created
- {% endif %} -
-
-

User: {{new_user['email']}}

-

Password: {{new_user['password']}}

- Hide -
-
-
- {% endif %} - -
- - - - - - - - - - - {% for user in all_users %} - - - - - - - {% endfor %} - -
EmailRoleApi KeyActions
{{user['email']}}{{user['role']}} -
- - {{user['api_key'][:4]}}*********************************{{user['api_key'][-4:]}} - - - - - - - -
-
-
-
- - -
-
- - -
-
-
-
- -
-
-
- - - - - - diff --git a/var/www/templates/settings/menu_sidebar.html b/var/www/templates/settings/menu_sidebar.html index 10eca7a4..1ec58516 100644 --- a/var/www/templates/settings/menu_sidebar.html +++ b/var/www/templates/settings/menu_sidebar.html @@ -83,7 +83,7 @@ - {% if admin_level %} + {% if acl_admin %} @@ -95,7 +95,7 @@