2022-05-09 00:12:04 +00:00
|
|
|
from flask_security import admin_change_password
|
2022-05-16 16:12:53 +00:00
|
|
|
from sqlalchemy import select
|
2022-05-09 00:12:04 +00:00
|
|
|
|
|
|
|
from ... import app, db
|
2022-05-16 20:20:36 +00:00
|
|
|
from ...advlabdb_independent_funs import randomPassword
|
2022-05-09 00:12:04 +00:00
|
|
|
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():
|
2022-05-16 16:12:53 +00:00
|
|
|
admins = db.session.execute(select(Admin).join(User).where(User.active == True)).scalars().all()
|
2022-05-09 00:12:04 +00:00
|
|
|
activate_user = False
|
2022-05-16 16:12:53 +00:00
|
|
|
|
|
|
|
if len(admins) == 0:
|
2022-05-09 00:12:04 +00:00
|
|
|
print("There is no admin with an active user. The user of the choosen admin will be activated")
|
2022-05-16 16:12:53 +00:00
|
|
|
admins = db.session.execute(select(Admin)).scalars().all()
|
2022-05-09 00:12:04 +00:00
|
|
|
activate_user = True
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2022-05-16 16:12:53 +00:00
|
|
|
if activate_user:
|
|
|
|
choosen_admin_user.active = True
|
|
|
|
|
2022-05-09 00:12:04 +00:00
|
|
|
box(new_password, "New password")
|
|
|
|
|
|
|
|
print("Done!")
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|