Einloggen funktioniert

This commit is contained in:
2024-12-03 16:30:56 +01:00
parent b028638d56
commit 7157176b5b
13 changed files with 804 additions and 50 deletions

View File

@@ -7,20 +7,25 @@ class DbManager(object):
__dbmanager = None
def __new__(cls, dbconf = None):
if cls.__dbmanager is None:
cls.__dbmanager = super(DbManager, cls).__new__(cls)
cls.__dbmanager.__initializeConfig(dbconf)
return cls.__dbmanager
def getConnection(cls):
if not cls.__connection:
try:
cls.__connection = mariadb.connect(**cls.__con_param)
def getConnection(cls):
try:
if not cls.__connection or not cls.__connection.ping():
cls.__connection = mariadb.connect(**cls.__con_param)
except mariadb.InterfaceError as e:
cls.__connection = mariadb.connect(**cls.__con_param)
print(f"INTERFACE ERROR: {e}")
except mariadb.Error as e:
print("Connection parameters are wrong: {e}")
return cls.__connection
print(f"Connection parameters are wrong: {e}")
return cls.__connection
def __initializeConfig(cls, dbconf):
cls.__con_param = { 'user': dbconf['DB_USER'], 'password': dbconf['DB_PASS'],
@@ -30,3 +35,5 @@ class DbManager(object):

32
lib/DB/UserDAO.py Normal file
View File

@@ -0,0 +1,32 @@
# This Python file uses the following encoding: utf-8
from .DbManager import DbManager
from ..PyqcrmFlags import PyqcrmFlags
import mariadb
class UserDAO:
def __init__(self):
self.__con = DbManager().getConnection()
self.__cur = self.__con.cursor()
def createUser(self, username, password, info, role= PyqcrmFlags.USER):
user_created = True
try:
self.__cur.callproc("createUser", (username, password, info, role))
self.__con.commit()
except mariadb.Error as e:
print(f"Error: {e}")
print(e.errno)
user_created = False
finally:
self.__closeConnection()
return user_created
def getUser(self, username):
self.__cur.callproc("getUser", (username,))
return self.__cur.fetchone()
def __closeConnection(self):
self.__cur.close()
self.__con.close()

View File

@@ -2,38 +2,35 @@ from .DbManager import DbManager
from ..PyqcrmFlags import PyqcrmFlags
from ..Vermasseln import Vermasseln
import mariadb
from .UserDAO import UserDAO
from PySide6.QtCore import Slot, QObject, Signal
class UserManager():
class UserManager(QObject):
def __init__(self, user_config, role):
loginOkay = Signal()
def __init__(self, user_config = None, role = None):
super().__init__()
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
if user_config and role:
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.__hashPassword()
user_created = True
try:
self.__cur.callproc("createUser", (self.__username, self.__password, self.__info, self.__role))
self.__con.commit()
except mariadb.Error as e:
print(f"Error: {e}")
print(e.errno)
user_created = False
finally:
self.__cur.close()
#self.__closeConnection()
return user_created
user_created = UserDAO().createUser(self.__username, self.__password, self.__info, self.__role)
return user_created
def __hashPassword(self):
self.__password = Vermasseln.userPasswordHash(self.__password)
print (self.__password)
def getUser(self):
@@ -51,7 +48,16 @@ class UserManager():
def disableUser(self):
self.__closeConnection()
def __closeConnection(self):
self.__cur.close()
self.__con.close()
@Slot(str, str)
def login(self, username, password):
user = UserDAO().getUser(username)
if user:
self.__checkPassword(password, user[2])
def __checkPassword(self, password, hash_password):
pwList = hash_password.split("$")
hash_Pw = Vermasseln.userPasswordHash(password, pwList[0])
if hash_password == hash_Pw:
self.loginOkay.emit()