From 7c822b715adba612655d70d5063767fb3798c68f Mon Sep 17 00:00:00 2001 From: xithical <86810816+xithical@users.noreply.github.com> Date: Fri, 18 Mar 2022 20:46:56 -0500 Subject: [PATCH 1/7] Add functions for importing 3.x users, require passwords when adding users --- app/classes/controllers/users_controller.py | 6 +++++- app/classes/models/users.py | 13 ++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/app/classes/controllers/users_controller.py b/app/classes/controllers/users_controller.py index 09adaca7..e073dc12 100644 --- a/app/classes/controllers/users_controller.py +++ b/app/classes/controllers/users_controller.py @@ -96,8 +96,12 @@ class Users_Controller: users_helper.update_user(user_id, up_data) @staticmethod - def add_user(username, password=None, email="default@example.com", enabled: bool = True, superuser: bool = False): + def add_user(username, password, email="default@example.com", enabled: bool = True, superuser: bool = False): return users_helper.add_user(username, password=password, email=email, enabled=enabled, superuser=superuser) + + @staticmethod + def add_rawpass_user(username, password, email="default@example.com", enabled: bool = True, superuser: bool = False): + return users_helper.add_rawpass_user(username, password=password, email=email, enabled=enabled, superuser=superuser) @staticmethod def remove_user(user_id): diff --git a/app/classes/models/users.py b/app/classes/models/users.py index 4c32c538..b99a53fc 100644 --- a/app/classes/models/users.py +++ b/app/classes/models/users.py @@ -147,7 +147,7 @@ class helper_users: return user @staticmethod - def add_user(username: str, password: Optional[str] = None, email: Optional[str] = None, enabled: bool = True, superuser: bool = False) -> str: + def add_user(username: str, password: str = None, email: Optional[str] = None, enabled: bool = True, superuser: bool = False) -> str: if password is not None: pw_enc = helper.encode_pass(password) else: @@ -161,6 +161,17 @@ class helper_users: Users.created: helper.get_time_as_string() }).execute() return user_id + + @staticmethod + def add_rawpass_user(username: str, password: str = None, email: Optional[str] = None, enabled: bool = True, superuser: bool = False) -> str: + user_id = Users.insert({ + Users.username: username.lower(), + Users.password: password, + Users.email: email, + Users.enabled: enabled, + Users.superuser: superuser, + Users.created: helper.get_time_as_string() + }).execute() @staticmethod def update_user(user_id, up_data=None): From 4c14b58a2a9dc55aad20e735fcb3c0073565567e Mon Sep 17 00:00:00 2001 From: xithical <86810816+xithical@users.noreply.github.com> Date: Fri, 18 Mar 2022 21:55:39 -0500 Subject: [PATCH 2/7] Add user importing from 3 - servers to be added --- app/classes/shared/command.py | 9 +++++++++ app/classes/shared/import3.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 app/classes/shared/import3.py diff --git a/app/classes/shared/command.py b/app/classes/shared/command.py index 18b4f868..9acba552 100644 --- a/app/classes/shared/command.py +++ b/app/classes/shared/command.py @@ -6,6 +6,7 @@ import logging from app.classes.shared.console import console from app.classes.shared.helpers import helper +from app.classes.shared.import3 import import3 from app.classes.web.websocket_helper import websocket_helper logger = logging.getLogger(__name__) @@ -58,6 +59,10 @@ class MainPrompt(cmd.Cmd): else: print(f'Name: {thread.name} Identifier: {thread.ident}') + @staticmethod + def do_import3(): + import3.start_import() + def universal_exit(self): logger.info("Stopping all server daemons / threads") console.info("Stopping all server daemons / threads - This may take a few seconds") @@ -75,3 +80,7 @@ class MainPrompt(cmd.Cmd): @staticmethod def help_migrations(): console.help("Only for advanced users. Use with caution") + + @staticmethod + def help_import3(): + console.help("Import users and servers from Crafty 3") \ No newline at end of file diff --git a/app/classes/shared/import3.py b/app/classes/shared/import3.py new file mode 100644 index 00000000..8c451492 --- /dev/null +++ b/app/classes/shared/import3.py @@ -0,0 +1,33 @@ +import json +import os +import console +import logging +logger = logging.getLogger(__name__) + +from app.classes.controllers.users_controller import users_helper +from app.classes.shared.main_controller import Controller + +class import3: + def start_import(self): + folder = os.path.normpath(input("Please input the path to the migrations folder in your installation of Crafty 3: ")) + if not os.path.exists(folder): + console.log("Crafty cannot find the path you entered. Does Crafty's user have permission to access it?") + console.log("Please run the import3 command again and enter a valid path.") + else: + with open (os.path.join(folder, "users.json")) as f: + user_json = json.load(f.read()) + with open (os.path.join(folder, "mc_settings.json")) as f: + servers_json = json.load(f.read()) + self.import_users(user_json) + self.import_servers(servers_json) + + @staticmethod + def import_users(json_data): + for user in json_data: + users_helper.add_rawpass_user(user.username, user.password) + console.log(f"Imported user {user.username} from Crafty 3") + logger.info(f"Imported user {user.username} from Crafty 3") + + @staticmethod + def import_servers(json_data): + return \ No newline at end of file From 4af62cbacaae5975990972268b166558123cf310 Mon Sep 17 00:00:00 2001 From: _iEpic Date: Fri, 18 Mar 2022 23:59:10 -0400 Subject: [PATCH 3/7] Added basic Import functions --- app/classes/models/users.py | 1 + app/classes/shared/command.py | 5 ++-- app/classes/shared/import3.py | 45 +++++++++++++++++++++++++---------- 3 files changed, 36 insertions(+), 15 deletions(-) diff --git a/app/classes/models/users.py b/app/classes/models/users.py index b99a53fc..6027c2b9 100644 --- a/app/classes/models/users.py +++ b/app/classes/models/users.py @@ -172,6 +172,7 @@ class helper_users: Users.superuser: superuser, Users.created: helper.get_time_as_string() }).execute() + return user_id @staticmethod def update_user(user_id, up_data=None): diff --git a/app/classes/shared/command.py b/app/classes/shared/command.py index 9acba552..5306b8e5 100644 --- a/app/classes/shared/command.py +++ b/app/classes/shared/command.py @@ -59,8 +59,7 @@ class MainPrompt(cmd.Cmd): else: print(f'Name: {thread.name} Identifier: {thread.ident}') - @staticmethod - def do_import3(): + def do_import3(self, _line): import3.start_import() def universal_exit(self): @@ -83,4 +82,4 @@ class MainPrompt(cmd.Cmd): @staticmethod def help_import3(): - console.help("Import users and servers from Crafty 3") \ No newline at end of file + console.help("Import users and servers from Crafty 3") diff --git a/app/classes/shared/import3.py b/app/classes/shared/import3.py index 8c451492..593a6e25 100644 --- a/app/classes/shared/import3.py +++ b/app/classes/shared/import3.py @@ -1,33 +1,54 @@ import json import os -import console import logging logger = logging.getLogger(__name__) from app.classes.controllers.users_controller import users_helper from app.classes.shared.main_controller import Controller +from app.classes.shared.console import console class import3: + + def __init__(self): + self.controller = Controller() + def start_import(self): folder = os.path.normpath(input("Please input the path to the migrations folder in your installation of Crafty 3: ")) if not os.path.exists(folder): - console.log("Crafty cannot find the path you entered. Does Crafty's user have permission to access it?") - console.log("Please run the import3 command again and enter a valid path.") + console.info("Crafty cannot find the path you entered. Does Crafty's user have permission to access it?") + console.info("Please run the import3 command again and enter a valid path.") else: with open (os.path.join(folder, "users.json")) as f: - user_json = json.load(f.read()) + user_json = json.loads(f.read()) with open (os.path.join(folder, "mc_settings.json")) as f: - servers_json = json.load(f.read()) + servers_json = json.loads(f.read()) self.import_users(user_json) - self.import_servers(servers_json) + self.import_servers(servers_json, self.controller) @staticmethod def import_users(json_data): - for user in json_data: - users_helper.add_rawpass_user(user.username, user.password) - console.log(f"Imported user {user.username} from Crafty 3") - logger.info(f"Imported user {user.username} from Crafty 3") + # If there is only one user to import json needs to call the data differently + if isinstance(json_data, list): + for user in json_data: + users_helper.add_rawpass_user(user['username'], user['password']) + console.info(f"Imported user {user['username']} from Crafty 3") + logger.info(f"Imported user {user['username']} from Crafty 3") + else: + console.info("There is only one user detected. Cannot create duplicate Admin account.") + logger.info("There is only one user detected. Cannot create duplicate Admin account.") @staticmethod - def import_servers(json_data): - return \ No newline at end of file + def import_servers(json_data, controller): + # If there is only one server to import json needs to call the data differently + if isinstance(json_data, list): + for server in json_data: + new_server_id = controller.import_jar_server(server_name=server['server_name'], server_path=server['server_path'], server_jar=server['server_jar'], min_mem=int(server['memory_min']), max_mem=int(server['memory_max']), port=server['server_port']) + console.info(f"Imported server {server['server_name']}[{server['id']}] from Crafty 3 to new server id {new_server_id}") + logger.info(f"Imported server {server['server_name']}[{server['id']}] from Crafty 3 to new server id {new_server_id}") + else: + new_server_id = controller.import_jar_server(server_name=json_data['server_name'], server_path=json_data['server_path'], server_jar=json_data['server_jar'], min_mem=int(json_data['memory_min']), max_mem=int(json_data['memory_max']), port=json_data['server_port']) + console.info(f"Imported server {json_data['server_name']}[{json_data['id']}] from Crafty 3 to new server id {new_server_id}") + logger.info(f"Imported server {json_data['server_name']}[{json_data['id']}] from Crafty 3 to new server id {new_server_id}") + + +import3 = import3() From cda7b59a12bd9153f0430d16cfef7b0c7143a0bb Mon Sep 17 00:00:00 2001 From: xithical <86810816+xithical@users.noreply.github.com> Date: Fri, 18 Mar 2022 23:06:04 -0500 Subject: [PATCH 4/7] Fix how memory args are passed in importing from 3 --- app/classes/shared/import3.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/classes/shared/import3.py b/app/classes/shared/import3.py index 593a6e25..0cb7ace5 100644 --- a/app/classes/shared/import3.py +++ b/app/classes/shared/import3.py @@ -42,11 +42,11 @@ class import3: # If there is only one server to import json needs to call the data differently if isinstance(json_data, list): for server in json_data: - new_server_id = controller.import_jar_server(server_name=server['server_name'], server_path=server['server_path'], server_jar=server['server_jar'], min_mem=int(server['memory_min']), max_mem=int(server['memory_max']), port=server['server_port']) + new_server_id = controller.import_jar_server(server_name=server['server_name'], server_path=server['server_path'], server_jar=server['server_jar'], min_mem=(int(server['memory_min'])/1000), max_mem=(int(server['memory_max'])/1000), port=server['server_port']) console.info(f"Imported server {server['server_name']}[{server['id']}] from Crafty 3 to new server id {new_server_id}") logger.info(f"Imported server {server['server_name']}[{server['id']}] from Crafty 3 to new server id {new_server_id}") else: - new_server_id = controller.import_jar_server(server_name=json_data['server_name'], server_path=json_data['server_path'], server_jar=json_data['server_jar'], min_mem=int(json_data['memory_min']), max_mem=int(json_data['memory_max']), port=json_data['server_port']) + new_server_id = controller.import_jar_server(server_name=json_data['server_name'], server_path=json_data['server_path'], server_jar=json_data['server_jar'], min_mem=(int(json_data['memory_min'])/1000), max_mem=(int(json_data['memory_max'])/1000), port=json_data['server_port']) console.info(f"Imported server {json_data['server_name']}[{json_data['id']}] from Crafty 3 to new server id {new_server_id}") logger.info(f"Imported server {json_data['server_name']}[{json_data['id']}] from Crafty 3 to new server id {new_server_id}") From bf6d0d0368768e9b22f9a0dc4fab20a703e9f86a Mon Sep 17 00:00:00 2001 From: xithical <86810816+xithical@users.noreply.github.com> Date: Sun, 20 Mar 2022 12:01:14 -0500 Subject: [PATCH 5/7] Appease the linter --- app/classes/controllers/users_controller.py | 2 +- app/classes/models/users.py | 2 +- app/classes/shared/import3.py | 11 +++++++---- app/classes/web/public_handler.py | 10 +++++----- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/app/classes/controllers/users_controller.py b/app/classes/controllers/users_controller.py index e073dc12..bd51fd8f 100644 --- a/app/classes/controllers/users_controller.py +++ b/app/classes/controllers/users_controller.py @@ -98,7 +98,7 @@ class Users_Controller: @staticmethod def add_user(username, password, email="default@example.com", enabled: bool = True, superuser: bool = False): return users_helper.add_user(username, password=password, email=email, enabled=enabled, superuser=superuser) - + @staticmethod def add_rawpass_user(username, password, email="default@example.com", enabled: bool = True, superuser: bool = False): return users_helper.add_rawpass_user(username, password=password, email=email, enabled=enabled, superuser=superuser) diff --git a/app/classes/models/users.py b/app/classes/models/users.py index 6027c2b9..751fdb1c 100644 --- a/app/classes/models/users.py +++ b/app/classes/models/users.py @@ -161,7 +161,7 @@ class helper_users: Users.created: helper.get_time_as_string() }).execute() return user_id - + @staticmethod def add_rawpass_user(username: str, password: str = None, email: Optional[str] = None, enabled: bool = True, superuser: bool = False) -> str: user_id = Users.insert({ diff --git a/app/classes/shared/import3.py b/app/classes/shared/import3.py index 0cb7ace5..db9670b7 100644 --- a/app/classes/shared/import3.py +++ b/app/classes/shared/import3.py @@ -1,12 +1,13 @@ import json import os import logging -logger = logging.getLogger(__name__) from app.classes.controllers.users_controller import users_helper from app.classes.shared.main_controller import Controller from app.classes.shared.console import console +logger = logging.getLogger(__name__) + class import3: def __init__(self): @@ -42,13 +43,15 @@ class import3: # If there is only one server to import json needs to call the data differently if isinstance(json_data, list): for server in json_data: - new_server_id = controller.import_jar_server(server_name=server['server_name'], server_path=server['server_path'], server_jar=server['server_jar'], min_mem=(int(server['memory_min'])/1000), max_mem=(int(server['memory_max'])/1000), port=server['server_port']) + new_server_id = controller.import_jar_server(server_name=server['server_name'], server_path=server['server_path'], server_jar=server['server_jar'], + min_mem=(int(server['memory_min'])/1000), max_mem=(int(server['memory_max'])/1000), port=server['server_port']) console.info(f"Imported server {server['server_name']}[{server['id']}] from Crafty 3 to new server id {new_server_id}") logger.info(f"Imported server {server['server_name']}[{server['id']}] from Crafty 3 to new server id {new_server_id}") else: - new_server_id = controller.import_jar_server(server_name=json_data['server_name'], server_path=json_data['server_path'], server_jar=json_data['server_jar'], min_mem=(int(json_data['memory_min'])/1000), max_mem=(int(json_data['memory_max'])/1000), port=json_data['server_port']) + new_server_id = controller.import_jar_server(server_name=json_data['server_name'], server_path=json_data['server_path'], server_jar=json_data['server_jar'], + min_mem=(int(json_data['memory_min'])/1000), max_mem=(int(json_data['memory_max'])/1000), port=json_data['server_port']) console.info(f"Imported server {json_data['server_name']}[{json_data['id']}] from Crafty 3 to new server id {new_server_id}") logger.info(f"Imported server {json_data['server_name']}[{json_data['id']}] from Crafty 3 to new server id {new_server_id}") -import3 = import3() +import3 = import3() \ No newline at end of file diff --git a/app/classes/web/public_handler.py b/app/classes/web/public_handler.py index 8cc2d7c2..72d2d4aa 100644 --- a/app/classes/web/public_handler.py +++ b/app/classes/web/public_handler.py @@ -42,7 +42,7 @@ class PublicHandler(BaseHandler): 'lang_page': helper.getLangPage(helper.get_setting('language')), 'query': "" } - if (self.request.query): + if self.request.query: page_data['query'] = self.request.query # sensible defaults @@ -66,7 +66,7 @@ class PublicHandler(BaseHandler): # if we have no page, let's go to login else: - if (self.request.query): + if self.request.query: self.redirect('/public/login?'+self.request.query) else: self.redirect('/public/login') @@ -90,7 +90,7 @@ class PublicHandler(BaseHandler): 'lang_page': helper.getLangPage(helper.get_setting('language')), 'query': "" } - if (self.request.query): + if self.request.query: page_data['query'] = self.request.query if page == 'login': @@ -147,7 +147,7 @@ class PublicHandler(BaseHandler): self.controller.management.add_to_audit_log(user_data.user_id, "Logged in", 0, self.get_remote_ip()) - if (self.request.query_arguments.get('next')): + if self.request.query_arguments.get('next'): next_page = self.request.query_arguments.get('next')[0].decode() else: next_page = "/panel/dashboard" @@ -165,7 +165,7 @@ class PublicHandler(BaseHandler): else: self.redirect(f'/public/login?error_msg={error_msg}') else: - if (self.request.query): + if self.request.query: self.redirect('/public/login?'+self.request.query) else: self.redirect('/public/login') From 1279784cc74b5ec7821d17fcf714bb990e6a13f5 Mon Sep 17 00:00:00 2001 From: xithical <86810816+xithical@users.noreply.github.com> Date: Sun, 20 Mar 2022 12:14:51 -0500 Subject: [PATCH 6/7] Appease the linter again --- app/classes/shared/command.py | 2 +- app/classes/shared/import3.py | 17 +++++++++-------- app/classes/web/public_handler.py | 8 ++++---- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/app/classes/shared/command.py b/app/classes/shared/command.py index 5306b8e5..112effbb 100644 --- a/app/classes/shared/command.py +++ b/app/classes/shared/command.py @@ -79,7 +79,7 @@ class MainPrompt(cmd.Cmd): @staticmethod def help_migrations(): console.help("Only for advanced users. Use with caution") - + @staticmethod def help_import3(): console.help("Import users and servers from Crafty 3") diff --git a/app/classes/shared/import3.py b/app/classes/shared/import3.py index db9670b7..0d244460 100644 --- a/app/classes/shared/import3.py +++ b/app/classes/shared/import3.py @@ -19,9 +19,9 @@ class import3: console.info("Crafty cannot find the path you entered. Does Crafty's user have permission to access it?") console.info("Please run the import3 command again and enter a valid path.") else: - with open (os.path.join(folder, "users.json")) as f: + with open (os.path.join(folder, "users.json"), encoding="utf-8") as f: user_json = json.loads(f.read()) - with open (os.path.join(folder, "mc_settings.json")) as f: + with open (os.path.join(folder, "mc_settings.json", encoding="utf-8")) as f: servers_json = json.loads(f.read()) self.import_users(user_json) self.import_servers(servers_json, self.controller) @@ -43,15 +43,16 @@ class import3: # If there is only one server to import json needs to call the data differently if isinstance(json_data, list): for server in json_data: - new_server_id = controller.import_jar_server(server_name=server['server_name'], server_path=server['server_path'], server_jar=server['server_jar'], - min_mem=(int(server['memory_min'])/1000), max_mem=(int(server['memory_max'])/1000), port=server['server_port']) + new_server_id = controller.import_jar_server(server_name=server['server_name'], server_path=server['server_path'], + server_jar=server['server_jar'], min_mem=(int(server['memory_min'])/1000), + max_mem=(int(server['memory_max'])/1000), port=server['server_port']) console.info(f"Imported server {server['server_name']}[{server['id']}] from Crafty 3 to new server id {new_server_id}") logger.info(f"Imported server {server['server_name']}[{server['id']}] from Crafty 3 to new server id {new_server_id}") else: - new_server_id = controller.import_jar_server(server_name=json_data['server_name'], server_path=json_data['server_path'], server_jar=json_data['server_jar'], - min_mem=(int(json_data['memory_min'])/1000), max_mem=(int(json_data['memory_max'])/1000), port=json_data['server_port']) + new_server_id = controller.import_jar_server(server_name=json_data['server_name'], server_path=json_data['server_path'], + server_jar=json_data['server_jar'], min_mem=(int(json_data['memory_min'])/1000), + max_mem=(int(json_data['memory_max'])/1000), port=json_data['server_port']) console.info(f"Imported server {json_data['server_name']}[{json_data['id']}] from Crafty 3 to new server id {new_server_id}") logger.info(f"Imported server {json_data['server_name']}[{json_data['id']}] from Crafty 3 to new server id {new_server_id}") - -import3 = import3() \ No newline at end of file +import3 = import3() diff --git a/app/classes/web/public_handler.py b/app/classes/web/public_handler.py index 72d2d4aa..80363afb 100644 --- a/app/classes/web/public_handler.py +++ b/app/classes/web/public_handler.py @@ -96,7 +96,7 @@ class PublicHandler(BaseHandler): if page == 'login': next_page = "/public/login" - if (self.request.query): + if self.request.query: next_page = '/public/login?'+self.request.query entered_username = bleach.clean(self.get_argument('username')) @@ -112,7 +112,7 @@ class PublicHandler(BaseHandler): #self.clear_cookie("user") #self.clear_cookie("user_data") self.clear_cookie("token") - if (self.request.query): + if self.request.query: self.redirect(f'/public/login?error_msg={error_msg}&{self.request.query}') else: self.redirect(f'/public/login?error_msg={error_msg}') @@ -124,7 +124,7 @@ class PublicHandler(BaseHandler): #self.clear_cookie("user") #self.clear_cookie("user_data") self.clear_cookie("token") - if (self.request.query): + if self.request.query: self.redirect(f'/public/login?error_msg={error_msg}&{self.request.query}') else: self.redirect(f'/public/login?error_msg={error_msg}') @@ -160,7 +160,7 @@ class PublicHandler(BaseHandler): error_msg = "Inncorrect username or password. Please try again." # log this failed login attempt self.controller.management.add_to_audit_log(user_data.user_id, "Tried to log in", 0, self.get_remote_ip()) - if (self.request.query): + if self.request.query: self.redirect(f'/public/login?error_msg={error_msg}&{self.request.query}') else: self.redirect(f'/public/login?error_msg={error_msg}') From 4177ca882c42b4023b64b40644c3d63918b4a401 Mon Sep 17 00:00:00 2001 From: xithical <86810816+xithical@users.noreply.github.com> Date: Sun, 20 Mar 2022 12:19:11 -0500 Subject: [PATCH 7/7] Put encoding arg in its proper location for maximum linter appeasal --- app/classes/shared/import3.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/classes/shared/import3.py b/app/classes/shared/import3.py index 0d244460..dfcd5bcf 100644 --- a/app/classes/shared/import3.py +++ b/app/classes/shared/import3.py @@ -21,7 +21,7 @@ class import3: else: with open (os.path.join(folder, "users.json"), encoding="utf-8") as f: user_json = json.loads(f.read()) - with open (os.path.join(folder, "mc_settings.json", encoding="utf-8")) as f: + with open (os.path.join(folder, "mc_settings.json"), encoding="utf-8") as f: servers_json = json.loads(f.read()) self.import_users(user_json) self.import_servers(servers_json, self.controller)