Einloggen funktioniert
This commit is contained in:
@@ -48,7 +48,7 @@ class ConfigLoader(QObject):
|
||||
@Slot(dict, result= bool)
|
||||
def addAdminUser(self, user_config):
|
||||
admin = UserManager(user_config["user"], PyqcrmFlags.ADMIN).createUser()
|
||||
print (admin)
|
||||
|
||||
if not admin:
|
||||
self.usernameNotAvailable.emit()
|
||||
return admin
|
||||
@@ -112,7 +112,6 @@ class ConfigLoader(QObject):
|
||||
@Slot(str)
|
||||
def setEncyrptKey(self, key):
|
||||
self.__config['pyqcrm']['ENCRYPTION_KEY'] = key
|
||||
print(self.__config)
|
||||
self.__saveConfig()
|
||||
|
||||
def __saveConfig(self):
|
||||
|
||||
@@ -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
32
lib/DB/UserDAO.py
Normal 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()
|
||||
|
||||
|
||||
@@ -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()
|
||||
|
||||
|
||||
@@ -47,9 +47,9 @@ class Vermasseln:
|
||||
return cipher
|
||||
|
||||
@classmethod
|
||||
def userPasswordHash(self, password):
|
||||
|
||||
salt = "".join(random.choice(string.ascii_letters + string.digits) for i in range (32))
|
||||
def userPasswordHash(self, password, salt = None):
|
||||
if not salt:
|
||||
salt = "".join(random.choice(string.ascii_letters + string.digits) for i in range (32))
|
||||
hash_pw = (salt + password).encode("utf-8")
|
||||
h_obj = SHA3_512.new(hash_pw)
|
||||
password = salt + "$" + h_obj.hexdigest()
|
||||
|
||||
Reference in New Issue
Block a user