diff --git a/horsygui.py b/horsygui.py index abd3ec7..db6e469 100644 --- a/horsygui.py +++ b/horsygui.py @@ -160,6 +160,11 @@ def upload_gui(): gui.popup('Upload', str(upload(True, ui, login_ui, UiMainWindow))) +def change_password_gui(): + from modules.change_password import change + change(ui.oldpass_box.toPlainText(), ui.newpass_box.toPlainText()) + + # Run functions on startup if __name__ == "__main__": # Checking version @@ -200,6 +205,7 @@ if __name__ == "__main__": ui.upload_button.clicked.connect(upload_gui) ui.regmessage_button.clicked.connect(lambda: webbrowser.open(f"{horsy_vars.protocol}{horsy_vars.server_url}" f"/registration")) + ui.changepass_button.clicked.connect(change_password_gui) # Handle GUI exiting to exit whole program sys.exit(app.exec_()) diff --git a/modules/change_email.py b/modules/change_email.py new file mode 100644 index 0000000..e69de29 diff --git a/modules/change_password.py b/modules/change_password.py new file mode 100644 index 0000000..e074778 --- /dev/null +++ b/modules/change_password.py @@ -0,0 +1,52 @@ +import requests +import modules.gui as gui +import threading +from modules.auth import del_auth +import modules.vars as horsy_vars +import json +from PyQt5 import QtWidgets + + +def change(oldpass, newpass): + ui = gui.Ui_MainWindow() + ui.setupUi(QtWidgets.QMainWindow()) + + if oldpass == "" or newpass == "": + gui.popup('Error', 'Please enter both old and new passwords.') + return + + with open(horsy_vars.horsypath + 'config.cfg') as f: + config = json.load(f) + + try: + if oldpass != config['auth']['password']: + gui.popup('Error', 'Old password does not match with password in config.cfg') + return + except KeyError: + gui.popup('Error', 'You don\'t have a password set in config.cfg') + return + + try: + r = requests.put(horsy_vars.protocol + horsy_vars.server_url + '/users', + json={'auth': config['auth'], 'password': newpass}).text + try: + if r['message'] == 'Unauthorized': + gui.popup('Error', 'Invalid credentials \nDeleting auth from config') + del_auth() + + elif r['message'] == 'Internal server error': + gui.popup('Error', 'Internal server error') + return 'Internal server error' + + elif r == '': + gui.popup('Success', 'Success, your password has been changed') + + else: + print('[red]Unknown error, please try again[/red]') + print('Server response:') + print(r) + return 'Unknown error, please try again, \n Server response: \n' + str(r) + except: + pass + except: + gui.popup('Error', 'Unexpected error.')