From 58d1ffea54fb2a26d429d5164f41f15c4faeec80 Mon Sep 17 00:00:00 2001 From: Andrew Date: Mon, 20 Dec 2021 20:17:28 -0500 Subject: [PATCH] Fix error with scheduled tasks by system Create a system user --- app/classes/models/users.py | 19 ++++++++++++------- app/classes/shared/main_controller.py | 12 ++++++++++++ app/classes/shared/main_models.py | 3 +++ main.py | 4 ++++ 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/app/classes/models/users.py b/app/classes/models/users.py index f959a5bb..d6bd1607 100644 --- a/app/classes/models/users.py +++ b/app/classes/models/users.py @@ -70,7 +70,7 @@ class helper_users: @staticmethod def get_all_users(): - query = Users.select() + query = Users.select().where(Users.username != "system") return query @staticmethod @@ -79,8 +79,6 @@ class helper_users: @staticmethod def get_user_id_by_name(username): - if username == "SYSTEM": - return 0 try: return (Users.get(Users.username == username)).user_id except DoesNotExist: @@ -108,14 +106,14 @@ class helper_users: if user_id == 0: return { 'user_id': 0, - 'created': None, - 'last_login': None, - 'last_update': None, + 'created': '10/24/2019, 11:34:00', + 'last_login': '10/24/2019, 11:34:00', + 'last_update': '10/24/2019, 11:34:00', 'last_ip': "127.27.23.89", 'username': "SYSTEM", 'password': None, 'enabled': True, - 'superuser': False, + 'superuser': True, 'api_token': None, 'roles': [], 'servers': [], @@ -130,6 +128,13 @@ class helper_users: #logger.debug("user: ({}) {}".format(user_id, {})) return {} + def check_system_user(user_id): + try: + Users.get(Users.user_id == user_id).user_id == user_id + return True + except: + return False + @staticmethod def add_user(username, password=None, api_token=None, enabled=True, superuser=False): if password is not None: diff --git a/app/classes/shared/main_controller.py b/app/classes/shared/main_controller.py index 14f0da70..6d1fb694 100644 --- a/app/classes/shared/main_controller.py +++ b/app/classes/shared/main_controller.py @@ -3,6 +3,7 @@ import pathlib import time import logging import sys +from app.classes.models.users import helper_users from peewee import DoesNotExist import schedule import yaml @@ -120,6 +121,17 @@ class Controller: server_obj = self.get_server_obj(server_id) server_obj.reload_server_settings() + @staticmethod + def check_system_user(): + if helper_users.get_user_id_by_name("system") is not None: + return True + else: + return False + + @staticmethod + def add_system_user(): + helper_users.add_user("system", helper.random_string_generator(64), helper_users.new_api_token(), False, True) + def get_server_settings(self, server_id): for s in self.servers_list: if int(s['server_id']) == int(server_id): diff --git a/app/classes/shared/main_models.py b/app/classes/shared/main_models.py index 596fa372..31298062 100644 --- a/app/classes/shared/main_models.py +++ b/app/classes/shared/main_models.py @@ -74,8 +74,11 @@ class db_shortcuts: try: if query.count() > 0: for s in query: + if s.user_id == 0: + print(s.user_id) rows.append(model_to_dict(s)) except Exception as e: + print('QUERY: ', query) logger.warning("Database Error: {}".format(e)) pass diff --git a/main.py b/main.py index 0762e7e3..4526dc1d 100644 --- a/main.py +++ b/main.py @@ -6,6 +6,7 @@ import time import argparse import logging.config import signal +from app.classes.controllers.management_controller import Management_Controller """ Our custom classes / pip packages """ from app.classes.shared.console import console @@ -146,6 +147,9 @@ if __name__ == '__main__': console.warning("We have detected Crafty's port, {} may not be open on the host network or a firewall is blocking it. Remote client connections to Crafty may be limited.".format(helper.get_setting('https_port'))) console.help("If you are not forwarding ports from your public IP or your router does not support hairpin NAT you can safely disregard the previous message.") + if not controller.check_system_user(): + controller.add_system_user() + Crafty = MainPrompt(tasks_manager, migration_manager) def sigterm_handler(signum, current_stack_frame):