From 43983679c8590e0d3d2d80706cbcbc578ba048e0 Mon Sep 17 00:00:00 2001 From: Mo8it Date: Mon, 9 May 2022 02:12:04 +0200 Subject: [PATCH] Add reset_admin_password script --- .../scripts/maintain/reset_admin_password.py | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 advlabdb/scripts/maintain/reset_admin_password.py diff --git a/advlabdb/scripts/maintain/reset_admin_password.py b/advlabdb/scripts/maintain/reset_admin_password.py new file mode 100644 index 0000000..6f0cf11 --- /dev/null +++ b/advlabdb/scripts/maintain/reset_admin_password.py @@ -0,0 +1,53 @@ +from flask_security import admin_change_password + +from ... import app, db +from ...independent_funs import randomPassword +from ...models import Admin, User +from ..terminal_utils import box, spaced_hl, validating_input + + +def main(): + print("This script will generate a new random password for a choosen admin.") + print() + + with app.app_context(): + with db.session.begin(): + admin_query = Admin.query + admins = admin_query.filter(Admin.user.has(User.active == True)) + activate_user = False + if admins.count() == 0: + print("There is no admin with an active user. The user of the choosen admin will be activated") + admins = admin_query.all() + activate_user = True + else: + admins = admins.all() + + num_admins = len(admins) + + prompt = "Admins:\n" + for ind, admin in enumerate(admins): + user = admin.user + prompt += f"[{ind}] {user.first_name} {user.last_name}: {user.email}\n" + prompt += "Enter number" + + admin_index = validating_input( + prompt, + options=[str(ind) for ind in range(num_admins)], + format_function=lambda ans: int(ans), + ) + + choosen_admin_user = admins[admin_index].user + + new_password = randomPassword() + + admin_change_password( + choosen_admin_user, new_password, notify=False + ) # Password is automatically hashed with this function + + box(new_password, "New password") + + print("Done!") + + +if __name__ == "__main__": + main()