From 72a85d7e647524a5b1cef506e442e05de3716d6a Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 29 Sep 2021 19:32:21 -0400 Subject: [PATCH] Adds user based languages changes server creation so that it adds it to roles instead of creating one Changes server creation so you cannot create a server without a role. --- app/classes/controllers/servers_controller.py | 1 + app/classes/models/users.py | 13 ++++++------- app/classes/shared/helpers.py | 5 +++-- app/classes/shared/main_models.py | 2 +- app/classes/web/panel_handler.py | 11 +++++++---- app/classes/web/server_handler.py | 14 +++++++++----- app/frontend/templates/panel/dashboard.html | 2 +- 7 files changed, 28 insertions(+), 20 deletions(-) diff --git a/app/classes/controllers/servers_controller.py b/app/classes/controllers/servers_controller.py index ed8ec7a8..c6596cd1 100644 --- a/app/classes/controllers/servers_controller.py +++ b/app/classes/controllers/servers_controller.py @@ -42,6 +42,7 @@ class Servers_Controller: for role in roles_list: role_id = role.role_id role_data = Roles_Controller.get_role_with_servers(role_id) + role_data['servers'] = {server_id} server_permissions.delete_roles_permissions(role_id, role_data['servers']) server_permissions.remove_roles_of_server(server_id) servers_helper.remove_server(server_id) diff --git a/app/classes/models/users.py b/app/classes/models/users.py index ae12973d..3b8f96bd 100644 --- a/app/classes/models/users.py +++ b/app/classes/models/users.py @@ -40,8 +40,8 @@ class Users(Model): password = CharField(default="") enabled = BooleanField(default=True) superuser = BooleanField(default=False) - api_token = CharField(default="", unique=True, index=True), # we may need to revisit this - lang = CharField(default='en_EN') + api_token = CharField(default="", unique=True, index=True) # we may need to revisit this + lang = CharField(default="en_EN") class Meta: table_name = "users" @@ -73,6 +73,10 @@ class helper_users: query = Users.select() return query + @staticmethod + def get_user_lang_by_id(user_id): + return Users.get(Users.user_id == user_id).lang + @staticmethod def get_user_id_by_name(username): if username == "SYSTEM": @@ -82,10 +86,6 @@ class helper_users: except DoesNotExist: return None - @staticmethod - def get_user_lang_by_id(user_id): - return Users.get(Users.user_id == user_id).lang - @staticmethod def get_user_by_api_token(token: str): query = Users.select().where(Users.api_token == token) @@ -119,7 +119,6 @@ class helper_users: 'api_token': None, 'roles': [], 'servers': [], - 'lang': 'en_EN' } user = model_to_dict(Users.get(Users.user_id == user_id)) diff --git a/app/classes/shared/helpers.py b/app/classes/shared/helpers.py index 5af7d6e8..2185c32d 100644 --- a/app/classes/shared/helpers.py +++ b/app/classes/shared/helpers.py @@ -284,11 +284,12 @@ class Helpers: break path_list = test.split('/') root_path = path_list[0] + ''' if len(path_list) > 1: for i in range(len(path_list) - 2): root_path = os.path.join(root_path, path_list[i + 1]) - - full_root_path = os.path.join(tempDir, root_path) +''' + full_root_path = tempDir for item in os.listdir(full_root_path): try: diff --git a/app/classes/shared/main_models.py b/app/classes/shared/main_models.py index 83bcc5c2..596fa372 100644 --- a/app/classes/shared/main_models.py +++ b/app/classes/shared/main_models.py @@ -48,7 +48,7 @@ class db_builder: # Users.enabled: True, # Users.superuser: True #}).execute() - user_id = users_helper.add_user(username, password=password, superuser=True) + user_id = users_helper.add_user(username=username, password=password, superuser=True) #users_helper.update_user(user_id, user_crafty_data={"permissions_mask":"111", "server_quantity":[-1,-1,-1]} ) #console.info("API token is {}".format(api_token)) diff --git a/app/classes/web/panel_handler.py b/app/classes/web/panel_handler.py index c0147b3b..b562576d 100644 --- a/app/classes/web/panel_handler.py +++ b/app/classes/web/panel_handler.py @@ -366,10 +366,11 @@ class PanelHandler(BaseHandler): page_data['permissions_list'] = set() page_data['quantity_server'] = self.controller.crafty_perms.list_all_crafty_permissions_quantity_limits() page_data['languages'] = [] + page_data['languages'].append(self.controller.users.get_user_lang_by_id(exec_user_id)) for file in os.listdir(os.path.join(helper.root_dir, 'app', 'translations')): if file.endswith('.json'): - page_data['languages'].append(file.split('.')[0]) - print(page_data['languages']) + if file != str(page_data['languages'][0] + '.json'): + page_data['languages'].append(file.split('.')[0]) template = "panel/panel_edit_user.html" @@ -390,9 +391,11 @@ class PanelHandler(BaseHandler): page_data['permissions_list'] = self.controller.crafty_perms.get_crafty_permissions_list(user_id) 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(exec_user_id)) for file in os.listdir(os.path.join(helper.root_dir, 'app', 'translations')): if file.endswith('.json'): - page_data['languages'].append(file.split('.')[0]) + if file != str(page_data['languages'][0] + '.json'): + page_data['languages'].append(file.split('.')[0]) if user_id is None: self.redirect("/panel/error?error=Invalid User ID") @@ -701,7 +704,7 @@ class PanelHandler(BaseHandler): password1 = bleach.clean(self.get_argument('password1', None)) enabled = int(float(self.get_argument('enabled', '0'))) regen_api = int(float(self.get_argument('regen_api', '0'))) - lang = bleach.clean(self.get_argument('lang'), 'en_EN') + lang = bleach.clean(self.get_argument('language'), 'en_EN') if Enum_Permissions_Crafty.User_Config not in exec_user_crafty_permissions: if user_id != exec_user_id: diff --git a/app/classes/web/server_handler.py b/app/classes/web/server_handler.py index 40aa9806..5d3997c0 100644 --- a/app/classes/web/server_handler.py +++ b/app/classes/web/server_handler.py @@ -66,10 +66,15 @@ class ServerHandler(BaseHandler): 'menu_servers': defined_servers, 'show_contribute': helper.get_setting("show_contribute_link", True) } + page_data['lang'] = self.controller.users.get_user_lang_by_id(exec_user_id) if page == "step1": - if not exec_user['superuser'] and not self.controller.crafty_perms.can_create_server(exec_user_id): + if len(self.controller.users.get_user_roles_id(exec_user_id)) <= 0: + self.redirect("/panel/error?error=Unauthorized access: you must have a role to create a server.") + return + + elif not exec_user['superuser'] and not self.controller.crafty_perms.can_create_server(exec_user_id): self.redirect("/panel/error?error=Unauthorized access: not a server creator or server limit reached") return @@ -200,11 +205,10 @@ class ServerHandler(BaseHandler): new_server_id, self.get_remote_ip()) - #These lines create a new Role for the Server with full permissions and add the user to it + #These lines add the server to all the user's existing roles with full permissions new_server_uuid = self.controller.servers.get_server_data_by_id(new_server_id).get("server_uuid") - role_id = self.controller.roles.add_role("Creator of Server with uuid={}".format(new_server_uuid)) - self.controller.server_perms.add_role_server(new_server_id, role_id, "11111111") - self.controller.users.add_role_to_user(exec_user_id, role_id) + for role_id in self.controller.users.get_user_roles_id(exec_user_id): + self.controller.server_perms.add_role_server(new_server_id, role_id, "11111111") if not exec_user['superuser']: self.controller.server_perms.add_server_creation(exec_user_id) diff --git a/app/frontend/templates/panel/dashboard.html b/app/frontend/templates/panel/dashboard.html index 0dcbfc53..17b7bffb 100644 --- a/app/frontend/templates/panel/dashboard.html +++ b/app/frontend/templates/panel/dashboard.html @@ -92,7 +92,7 @@

{{ translate('dashboard', 'welcome', data['lang']) }}


- {{ translate('dashboard', 'no-servers') }} {{ translate('dashboard', 'newServer', data['lang']) }}. + {{ translate('dashboard', 'no-servers', data['lang']) }} {{ translate('dashboard', 'newServer', data['lang']) }}.
{% end %}