From 369ac7ad15c6513b4567184c50328aab645ae517 Mon Sep 17 00:00:00 2001 From: Andrew Date: Sun, 9 Jan 2022 18:04:54 -0500 Subject: [PATCH 1/8] initial changes for zip imports selection --- app/classes/shared/helpers.py | 8 + app/classes/shared/main_controller.py | 67 ++----- app/classes/web/ajax_handler.py | 8 + app/classes/web/panel_handler.py | 48 ++++- .../templates/panel/panel_edit_user.html | 30 +++- app/frontend/templates/server/wizard.html | 169 +++++++++++++++++- app/translations/en_EN.json | 5 +- 7 files changed, 266 insertions(+), 69 deletions(-) diff --git a/app/classes/shared/helpers.py b/app/classes/shared/helpers.py index 93b1fb0d..f4ee7611 100644 --- a/app/classes/shared/helpers.py +++ b/app/classes/shared/helpers.py @@ -730,6 +730,14 @@ class Helpers: output += '\n' return output + @staticmethod + def unzipServer(zip_path): + tempDir = tempfile.mkdtemp() + with zipfile.ZipFile(zip_path, 'r') as zip_ref: + #extracts archive to temp directory + zip_ref.extractall(tempDir) + return tempDir + @staticmethod def in_path(parent_path, child_path): # Smooth out relative path names, note: if you are concerned about symbolic links, you should use os.path.realpath too diff --git a/app/classes/shared/main_controller.py b/app/classes/shared/main_controller.py index 84b2b0d9..8f273c54 100644 --- a/app/classes/shared/main_controller.py +++ b/app/classes/shared/main_controller.py @@ -295,64 +295,25 @@ class Controller: server_id = helper.create_uuid() new_server_dir = os.path.join(helper.servers_dir, server_id) backup_path = os.path.join(helper.backup_path, server_id) - zip_path = helper.get_os_understandable_path(zip_path) + tempDir = helper.get_os_understandable_path(zip_path) if helper.check_file_perms(zip_path): helper.ensure_dir_exists(new_server_dir) helper.ensure_dir_exists(backup_path) - tempDir = tempfile.mkdtemp() has_properties = False - with zipfile.ZipFile(zip_path, 'r') as zip_ref: - #extracts archive to temp directory - zip_ref.extractall(tempDir) - if len(zip_ref.filelist) > 1: - for item in os.listdir(tempDir): - if str(item) == 'server.properties': - has_properties = True - try: - shutil.move(os.path.join(tempDir, item), os.path.join(new_server_dir, item)) - except Exception as ex: - logger.error('ERROR IN ZIP IMPORT: {}'.format(ex)) - if not has_properties: - logger.info("No server.properties found on zip file import. Creating one with port selection of {}".format(str(port))) - with open(os.path.join(new_server_dir, "server.properties"), "w") as f: - f.write("server-port={}".format(port)) - f.close() - zip_ref.close() - else: - - #iterates list of files - for i in range(len(zip_ref.filelist)): - #checks if the list of files inside of a dir is greater than 1 or if it's not a directory. - if len(zip_ref.filelist) > 1 or not zip_ref.filelist[i].is_dir(): - #sets local variable to be that filename and we break out of the loop since we found our root dir. - test = zip_ref.filelist[i-1].filename - break - path_list = test.split('/') - root_path = path_list[0] - if len(path_list) > 1: - for i in range(len(path_list)-1): - try: - root_path = os.path.join(root_path, path_list[i+1]) - except: - root_path = root_path - - full_root_path = os.path.join(tempDir, root_path) - - - for item in os.listdir(full_root_path): - if str(item) == 'server.properties': - has_properties = True - try: - shutil.move(os.path.join(full_root_path, item), os.path.join(new_server_dir, item)) - except Exception as ex: - logger.error('ERROR IN ZIP IMPORT: {}'.format(ex)) - if not has_properties: - logger.info("No server.properties found on zip file import. Creating one with port selection of {}".format(str(port))) - with open(os.path.join(new_server_dir, "server.properties"), "w") as f: - f.write("server-port={}".format(port)) - f.close() - zip_ref.close() + #extracts archive to temp directory + for item in os.listdir(tempDir): + if str(item) == 'server.properties': + has_properties = True + try: + shutil.move(os.path.join(tempDir, item), os.path.join(new_server_dir, item)) + except Exception as ex: + logger.error('ERROR IN ZIP IMPORT: {}'.format(ex)) + if not has_properties: + logger.info("No server.properties found on zip file import. Creating one with port selection of {}".format(str(port))) + with open(os.path.join(new_server_dir, "server.properties"), "w") as f: + f.write("server-port={}".format(port)) + f.close() else: return "false" diff --git a/app/classes/web/ajax_handler.py b/app/classes/web/ajax_handler.py index d2c6de83..ebca1582 100644 --- a/app/classes/web/ajax_handler.py +++ b/app/classes/web/ajax_handler.py @@ -275,6 +275,14 @@ class AjaxHandler(BaseHandler): self.controller.rename_backup_dir(server_id, new_server_id, new_server['server_uuid']) self.controller.remove_server(server_id, True) self.redirect('/panel/dashboard') + + elif page == "unzip_server": + print("in unzip server") + path = self.get_argument('path', None) + logger.info( + "Removing server from panel for server: {}".format(self.controller.servers.get_server_friendly_name(server_id))) + self.controller.remove_server(server_id, False) + return "test" @tornado.web.authenticated diff --git a/app/classes/web/panel_handler.py b/app/classes/web/panel_handler.py index 4d037281..090f39c7 100644 --- a/app/classes/web/panel_handler.py +++ b/app/classes/web/panel_handler.py @@ -402,19 +402,22 @@ class PanelHandler(BaseHandler): page_data['role-servers'] = auth_role_servers page_data['user-roles'] = user_roles - if exec_user['superuser'] == 1: - super_auth_servers = [] - super_auth_servers.append("Access To All Servers") - page_data['users'] = self.controller.users.get_all_users() - page_data['roles'] = self.controller.roles.get_all_roles() - page_data['auth-servers'][exec_user['user_id']] = super_auth_servers - else: - page_data['users'] = self.controller.users.user_query(exec_user['user_id']) - page_data['roles'] = self.controller.users.user_role_query(exec_user['user_id']) + page_data['users'] = self.controller.users.user_query(exec_user['user_id']) + page_data['roles'] = self.controller.users.user_role_query(exec_user['user_id']) + for user in page_data['users']: if user.user_id != exec_user['user_id']: user.api_token = "********" + if exec_user['superuser']: + for user in self.controller.users.get_all_users(): + if user.superuser == 1: + super_auth_servers = [] + super_auth_servers.append("Super User Access To All Servers") + page_data['users'] = self.controller.users.get_all_users() + page_data['roles'] = self.controller.roles.get_all_roles() + page_data['auth-servers'][user.user_id] = super_auth_servers + template = "panel/panel_config.html" elif page == "add_user": @@ -470,6 +473,12 @@ class PanelHandler(BaseHandler): page_data['quantity_server'] = self.controller.crafty_perms.list_crafty_permissions_quantity_limits(user_id) page_data['languages'] = [] page_data['languages'].append(self.controller.users.get_user_lang_by_id(user_id)) + #checks if super user. If not we disable the button. + if exec_user['superuser'] and str(exec_user['user_id']) != str(user_id): + page_data['super-disabled'] = '' + else: + page_data['super-disabled'] = 'disabled' + for file in sorted(os.listdir(os.path.join(helper.root_dir, 'app', 'translations'))): if file.endswith('.json'): if file != str(page_data['languages'][0] + '.json'): @@ -832,6 +841,18 @@ class PanelHandler(BaseHandler): enabled = int(float(self.get_argument('enabled', '0'))) regen_api = int(float(self.get_argument('regen_api', '0'))) lang = bleach.clean(self.get_argument('language'), 'en_EN') + if exec_user['superuser']: + #Checks if user is trying to change super user status of self. We don't want that. Automatically make them stay super user since we know they are. + if str(exec_user['user_id']) != str(user_id): + superuser = bleach.clean(self.get_argument('superuser', '0')) + else: + superuser = '1' + else: + superuser = '0' + if superuser == '1': + superuser = True + else: + superuser = False if Enum_Permissions_Crafty.User_Config not in exec_user_crafty_permissions: if str(user_id) != str(exec_user_id): @@ -910,6 +931,7 @@ class PanelHandler(BaseHandler): "regen_api": regen_api, "roles": roles, "lang": lang, + "superuser": superuser, } user_crafty_data = { "permissions_mask": permissions_mask, @@ -934,6 +956,14 @@ class PanelHandler(BaseHandler): email = bleach.clean(self.get_argument('email', "default@example.com")) enabled = int(float(self.get_argument('enabled', '0'))), lang = bleach.clean(self.get_argument('lang', 'en_EN')) + if exec_user['superuser']: + superuser = bleach.clean(self.get_argument('superuser', '0')) + else: + superuser = '0' + if superuser == '1': + superuser = True + else: + superuser = False if Enum_Permissions_Crafty.User_Config not in exec_user_crafty_permissions: self.redirect("/panel/error?error=Unauthorized access: not a user editor") diff --git a/app/frontend/templates/panel/panel_edit_user.html b/app/frontend/templates/panel/panel_edit_user.html index 3bab1825..8e884f9f 100644 --- a/app/frontend/templates/panel/panel_edit_user.html +++ b/app/frontend/templates/panel/panel_edit_user.html @@ -187,9 +187,9 @@ @@ -251,6 +251,32 @@ {% block js %}