Fix error with scheduled tasks by system

Create a system user
This commit is contained in:
Andrew 2021-12-20 20:17:28 -05:00
parent e53da60b4f
commit 58d1ffea54
4 changed files with 31 additions and 7 deletions

View File

@ -70,7 +70,7 @@ class helper_users:
@staticmethod @staticmethod
def get_all_users(): def get_all_users():
query = Users.select() query = Users.select().where(Users.username != "system")
return query return query
@staticmethod @staticmethod
@ -79,8 +79,6 @@ class helper_users:
@staticmethod @staticmethod
def get_user_id_by_name(username): def get_user_id_by_name(username):
if username == "SYSTEM":
return 0
try: try:
return (Users.get(Users.username == username)).user_id return (Users.get(Users.username == username)).user_id
except DoesNotExist: except DoesNotExist:
@ -108,14 +106,14 @@ class helper_users:
if user_id == 0: if user_id == 0:
return { return {
'user_id': 0, 'user_id': 0,
'created': None, 'created': '10/24/2019, 11:34:00',
'last_login': None, 'last_login': '10/24/2019, 11:34:00',
'last_update': None, 'last_update': '10/24/2019, 11:34:00',
'last_ip': "127.27.23.89", 'last_ip': "127.27.23.89",
'username': "SYSTEM", 'username': "SYSTEM",
'password': None, 'password': None,
'enabled': True, 'enabled': True,
'superuser': False, 'superuser': True,
'api_token': None, 'api_token': None,
'roles': [], 'roles': [],
'servers': [], 'servers': [],
@ -130,6 +128,13 @@ class helper_users:
#logger.debug("user: ({}) {}".format(user_id, {})) #logger.debug("user: ({}) {}".format(user_id, {}))
return {} 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 @staticmethod
def add_user(username, password=None, api_token=None, enabled=True, superuser=False): def add_user(username, password=None, api_token=None, enabled=True, superuser=False):
if password is not None: if password is not None:

View File

@ -3,6 +3,7 @@ import pathlib
import time import time
import logging import logging
import sys import sys
from app.classes.models.users import helper_users
from peewee import DoesNotExist from peewee import DoesNotExist
import schedule import schedule
import yaml import yaml
@ -120,6 +121,17 @@ class Controller:
server_obj = self.get_server_obj(server_id) server_obj = self.get_server_obj(server_id)
server_obj.reload_server_settings() 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): def get_server_settings(self, server_id):
for s in self.servers_list: for s in self.servers_list:
if int(s['server_id']) == int(server_id): if int(s['server_id']) == int(server_id):

View File

@ -74,8 +74,11 @@ class db_shortcuts:
try: try:
if query.count() > 0: if query.count() > 0:
for s in query: for s in query:
if s.user_id == 0:
print(s.user_id)
rows.append(model_to_dict(s)) rows.append(model_to_dict(s))
except Exception as e: except Exception as e:
print('QUERY: ', query)
logger.warning("Database Error: {}".format(e)) logger.warning("Database Error: {}".format(e))
pass pass

View File

@ -6,6 +6,7 @@ import time
import argparse import argparse
import logging.config import logging.config
import signal import signal
from app.classes.controllers.management_controller import Management_Controller
""" Our custom classes / pip packages """ """ Our custom classes / pip packages """
from app.classes.shared.console import console 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.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.") 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) Crafty = MainPrompt(tasks_manager, migration_manager)
def sigterm_handler(signum, current_stack_frame): def sigterm_handler(signum, current_stack_frame):