import psycopg2 from psycopg2 import sql import dbHandler import securityHandler debug: bool = True def debugPrint(msg: str) -> None: if debug: print("(USER HANDLER) PRINT: " + msg) def createUser(dbConnection: psycopg2.extensions.connection, username: str, firstName: str, lastName: str, description: str, country: str, theme: str, accentColor: str, password: str): debugPrint("Now creating new user with following attributes : username = " + username + ", firstName = " + firstName + ", lastName = " + lastName + ", description = " + description + ", country = " + country + ", theme = " + theme + ", accentColor = " + accentColor + "...") dbHandler.insertRow(dbConnection, 'users', ['username', 'firstname', 'lastname', 'description', 'country', 'theme', 'accentcolor', 'passwordhash'], [username, firstName, lastName, description, country, theme, accentColor, securityHandler.hashPassword(password)]) def checkIDExistence(dbConnection: psycopg2.extensions.connection, userID: int) -> bool: return dbHandler.checkFieldValueExistence(dbConnection, "users", "id", userID) def checkUserExistence(dbConnection: psycopg2.extensions.connection, username: str) -> bool: return dbHandler.checkFieldValueExistence(dbConnection, "users", "username", username) def checkUserSettingExistence(dbConnection: psycopg2.extensions.connection, settingName: str) -> bool: return dbHandler.checkRowExistence(dbConnection, "users", settingName) def getHashValueByUserID(dbConnection: psycopg2.extensions.connection, userID: int) -> str: return dbHandler.getFieldValueByID(dbConnection, "users", userID, "passwordhash") def getIDByUsername(dbConnection: psycopg2.extensions.connection, username: str) -> int: debugPrint("Attempting to get ID of username " + username + "...") sanitisedQuery = sql.SQL(""" SELECT id FROM users WHERE "username" = {username} """).format( username=sql.Literal(username) ) return int(dbHandler._execQuery(dbConnection, sanitisedQuery)[0][0]) def getUserInfoByID(dbConnection: psycopg2.extensions.connection, userID: int, userField: str) -> str: debugPrint("Attempting to get " + userField + " of userID " + str(userID) + "...") return str(dbHandler.getFieldValueByID(dbConnection, "users", userID, userField)) def getIDByAuthToken(dbConnection: psycopg2.extensions.connection, authToken: str) -> int: debugPrint("Attempting to get ID from authToken...") sanitisedQuery = sql.SQL(""" SELECT ownerid FROM authtokens WHERE "token" = {authToken} """).format( authToken=sql.Literal(authToken) ) return int(dbHandler._execQuery(dbConnection, sanitisedQuery)[0][0]) def changeUserSettingValue(dbConnection: psycopg2.extensions.connection, userID: int, userField: str, newValue) -> str: return dbHandler.changeFieldValueByID(dbConnection, "users", userID, userField, newValue)