Switch to MariaDB connector and Create UserManager Class
This commit is contained in:
@@ -2,15 +2,26 @@
|
||||
import toml
|
||||
from platformdirs import user_config_dir
|
||||
from pathlib import Path
|
||||
from PySide6.QtCore import QObject, Slot
|
||||
from PySide6.QtCore import QObject, Slot, Signal
|
||||
from .Vermasseln import Vermasseln
|
||||
import shutil
|
||||
from urllib.parse import urlparse
|
||||
from .DB.DbManager import DbManager
|
||||
import os
|
||||
from Crypto.Random import get_random_bytes
|
||||
from base64 import b64encode, b64decode
|
||||
from .DB.UserManager import UserManager
|
||||
from .PyqcrmFlags import PyqcrmFlags
|
||||
|
||||
|
||||
|
||||
class ConfigLoader(QObject):
|
||||
__config = None
|
||||
__version = "0.1-alpha"
|
||||
|
||||
|
||||
dbConnectionError = Signal(str, bool)
|
||||
adminUserError = Signal(str, bool)
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
@@ -33,56 +44,69 @@ class ConfigLoader(QObject):
|
||||
shutil.copyfile(confile, self.config_dir+ '/pyqcrm.toml')
|
||||
|
||||
@Slot(dict, result= bool)
|
||||
def addAdminUser(self, admin_config):
|
||||
print(admin_config)
|
||||
def addAdminUser(self, user_config):
|
||||
admin = UserManager(user_config["user"], PyqcrmFlags.ADMIN).createUser()
|
||||
return True
|
||||
|
||||
@Slot(dict, result= bool)
|
||||
def setConfig(self, app_config):
|
||||
|
||||
base_conf = self.__initializeConfig()
|
||||
conf = self.__checkDbConnection(app_config)
|
||||
if conf:
|
||||
try:
|
||||
with open (self.config_dir + '/pyqcrm.toml', 'w') as f:
|
||||
config = Vermasseln().oscarVermasseln(toml.dumps(app_config))
|
||||
f.write(config)
|
||||
except FileNotFoundError:
|
||||
conf = False
|
||||
print("Konnte die Konfiguration nicht speichern.")
|
||||
conf = self.__checkAdminUser()
|
||||
app_config = toml.dumps(app_config)
|
||||
|
||||
return conf
|
||||
if conf:
|
||||
app_config = base_conf + app_config
|
||||
self.__config = toml.loads(app_config)
|
||||
self.__saveConfig()
|
||||
conf = self.__checkAdminUser()
|
||||
|
||||
|
||||
def __configLoad(self):
|
||||
try:
|
||||
with open (self.config_dir + '/pyqcrm.toml', 'r') as f:
|
||||
config = f.read()
|
||||
|
||||
self.__config = toml.loads(Vermasseln().entschluesseln(config))
|
||||
except FileNotFoundError:
|
||||
print("Konnte die Konfiguration nicht laden.")
|
||||
except TypeError:
|
||||
print("Invalid Configuration")
|
||||
# except Exception as e:
|
||||
# print(str(e))
|
||||
print(f"Invalid Configuration: {__file__}")
|
||||
except Exception as e:
|
||||
print(str(e))
|
||||
|
||||
|
||||
def getConfig(self):
|
||||
|
||||
return self.__config
|
||||
|
||||
def __createConfig(self):
|
||||
def __initializeConfig(self):
|
||||
encrypt_key = b64encode(get_random_bytes(32)).decode("utf-8")
|
||||
conf = f"[pyqcrm]\nVERSION = \"{self.__version}\"\nENCRYPTION_KEY = \"{encrypt_key}\"\n\n"
|
||||
return conf
|
||||
|
||||
with open(self.config_dir + '/pyqcrm.toml', "w") as datei:
|
||||
datei.write("[pyqcrm]")
|
||||
|
||||
def __checkDbConnection(self, db_config):
|
||||
con = DbManager(db_config['database']).getConnection()
|
||||
if con:
|
||||
self.dbConnectionError.emit("Connection OK", True)
|
||||
return True
|
||||
else:
|
||||
self.dbConnectionError.emit("Connection fehlgeschlagen", False)
|
||||
return False
|
||||
|
||||
def __checkAdminUser(self):
|
||||
pass
|
||||
|
||||
self.adminUserError.emit("Kein Admin vorhanden", False)
|
||||
|
||||
@Slot(str)
|
||||
def setEncyrptKey(self, key):
|
||||
self.__config['pyqcrm']['ENCRYPTION_KEY'] = key
|
||||
self.__saveConfig()
|
||||
|
||||
def __saveConfig(self):
|
||||
try:
|
||||
with open (self.config_dir + '/pyqcrm.toml', 'w') as f:
|
||||
config = Vermasseln().oscarVermasseln(toml.dumps(self.__config))
|
||||
f.write(config)
|
||||
except FileNotFoundError:
|
||||
print("Konnte die Konfiguration nicht speichern.")
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
# This Python file uses the following encoding: utf-8
|
||||
import mysql.connector
|
||||
import mariadb
|
||||
|
||||
class DbManager(object):
|
||||
__connection = None
|
||||
@@ -14,6 +14,12 @@ class DbManager(object):
|
||||
return cls.__dbmanager
|
||||
|
||||
def getConnection(cls):
|
||||
if not cls.__connection:
|
||||
try:
|
||||
cls.__connection = mariadb.connect(**cls.__con_param)
|
||||
|
||||
except mariadb.Error as e:
|
||||
print("Connection parameters are wrong: {e}")
|
||||
return cls.__connection
|
||||
|
||||
def __initializeConfig(cls, dbconf):
|
||||
@@ -21,6 +27,6 @@ class DbManager(object):
|
||||
'port': int (dbconf['DB_PORT']), 'host': dbconf['DB_HOST'],
|
||||
'database': dbconf['DB_NAME']
|
||||
}
|
||||
cls.__connection = mysql.connector.connect(**cls.__con_param)
|
||||
|
||||
|
||||
|
||||
|
||||
40
lib/DB/UserManager.py
Normal file
40
lib/DB/UserManager.py
Normal file
@@ -0,0 +1,40 @@
|
||||
from .DbManager import DbManager
|
||||
from ..PyqcrmFlags import PyqcrmFlags
|
||||
|
||||
|
||||
|
||||
class UserManager():
|
||||
|
||||
def __init__(self, user_config, role):
|
||||
self.__con = DbManager().getConnection()
|
||||
self.__cur = self.__con.cursor()
|
||||
self.__username = user_config["PYQCRM_USER"]
|
||||
self.__password = user_config["PYQCRM_USER_PASS"]
|
||||
self.__info = user_config["PYQCRM_USER_INFO"]
|
||||
self.__role = role if role == PyqcrmFlags.ADMIN else 0
|
||||
|
||||
|
||||
def createUser(self):
|
||||
self.__cur.callproc("createUser", (self.__username, self.__password, self.__info, self.__role))
|
||||
self.__con.commit()
|
||||
self.__closeConnection()
|
||||
|
||||
def getUser(self):
|
||||
self.__closeConnection()
|
||||
|
||||
def getUsers(self):
|
||||
self.__closeConnection()
|
||||
|
||||
def delUser(self):
|
||||
self.__closeConnection()
|
||||
|
||||
def updateUser(self):
|
||||
self.__closeConnection()
|
||||
|
||||
def disableUser(self):
|
||||
self.__closeConnection()
|
||||
|
||||
def __closeConnection(self):
|
||||
self.__cur.close()
|
||||
self.__con.close()
|
||||
|
||||
6
lib/PyqcrmFlags.py
Normal file
6
lib/PyqcrmFlags.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from enum import IntFlag
|
||||
|
||||
|
||||
class PyqcrmFlags(IntFlag):
|
||||
ADMIN = 1
|
||||
USER = 2
|
||||
@@ -23,8 +23,8 @@ class Vermasseln:
|
||||
cipher = self.__vermasslungsKobold()
|
||||
decrypted_data = cipher.decrypt_and_verify(encoded_data[0], encoded_data[1])
|
||||
decrypted_data = decrypted_data.decode("utf-8")
|
||||
except (ValueError, IndexError):
|
||||
print("Configuration corrupted")
|
||||
except (ValueError, IndexError) as e:
|
||||
print(f"Configuration corrupted: {str(e)}")
|
||||
decrypted_data = None
|
||||
|
||||
except Exception as e:
|
||||
|
||||
Reference in New Issue
Block a user