Blorg-Backend/userHandler.py

43 lines
2.1 KiB
Python

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 checkUserExistence(dbConnection: psycopg2.extensions.connection, username: str) -> bool:
return dbHandler.checkFieldValueExistence(dbConnection, "users", "username", username)
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 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])