ska-referee-test/ref-test/reset.py

51 lines
1.7 KiB
Python

#!/usr/bin/env python
from main import app
from app.models import User
from sqlalchemy.exc import SQLAlchemyError
import sys
from getpass import getpass
with app.app_context():
try: users = User.query.all()
except (SQLAlchemyError, ConnectionError) as exception: sys.exit('Database error:', exception)
print('')
print('This interface will allow you to override the password for an administrator account.')
print('To exit this interface, press Ctrl + C.')
print('')
while True:
username = input('Username: ')
user = None
for _user in users:
if _user.get_username() == username:
user = _user
break
if not user:
print(f'Error: User \'{username}\' does not exist.')
continue
else: break
while True:
email = input('Email address: ')
if not email == user.get_email():
print(f'Error: Incorrect email address for user \'{username}\'.')
continue
else: break
print('')
print('Authenticated using username and email address.')
print('Update the password for the account below.')
print('')
while True:
password = getpass('Enter password: ')
if len(password) < 6 or len(password) > 20:
print(f'Error: Password must be between 6 and 20 characters long.')
reenter_password = getpass('Reenter password: ')
if not password == reenter_password:
print(f'Error: Entered passwords do not match.')
continue
else: break
success, message = user.update(password=password)
if not success:
sys.exit(message)
print('')
print(f'Success: Password for user \'{username}\' has been updated.')