formatting

This commit is contained in:
WorldTeacher
2024-05-16 15:31:06 +02:00
parent 4f7b564352
commit ef5f862a2b
4 changed files with 39 additions and 26 deletions

View File

@@ -7,14 +7,13 @@ from src.backend.database import Database
# change passwords for apparats, change passwords for users, list users, create and delete users etc
# create a class that has all commands. for each command, create a function that does the thing
class AdminCommands:
"""Basic Admin commands for the admin console. This class is used to create, delete, and list users. It also has the ability to change passwords for users.
"""
"""Basic Admin commands for the admin console. This class is used to create, delete, and list users. It also has the ability to change passwords for users."""
def __init__(self):
"""Defaulf Constructor for the AdminCommands class.
"""
"""Defaulf Constructor for the AdminCommands class."""
self.db = Database()
def create_password(self, password:str)->tuple[str,str]:
def create_password(self, password: str) -> tuple[str, str]:
"""Create a hashed password and a salt for the password.
Args:
@@ -25,8 +24,9 @@ class AdminCommands:
"""
salt = self.create_salt()
hashed_password = self.hash_password(password)
return (hashed_password,salt)
def create_salt(self)->str:
return (hashed_password, salt)
def create_salt(self) -> str:
"""Generate a random 16 digit long salt for the password.
Returns:
@@ -39,13 +39,12 @@ class AdminCommands:
)
def create_admin(self):
"""Create the admin in the database. This is only used once, when the database is created.
"""
"""Create the admin in the database. This is only used once, when the database is created."""
salt = self.create_salt()
hashed_password = self.hash_password("admin")
self.db.createUser("admin", salt+hashed_password, "admin", salt)
def hash_password(self, password:str)->str:
self.db.createUser("admin", salt + hashed_password, "admin", salt)
def hash_password(self, password: str) -> str:
"""Hash a password using SHA256.
Args:
@@ -57,7 +56,7 @@ class AdminCommands:
hashed = hashlib.sha256((password).encode("utf-8")).hexdigest()
return hashed
def list_users(self)->list[tuple]:
def list_users(self) -> list[tuple]:
"""List all available users in the database.
Returns:
@@ -65,7 +64,7 @@ class AdminCommands:
"""
return self.db.getUsers()
def delete_user(self, username:str):
def delete_user(self, username: str):
"""Delete a selected user from the database.
Args:
@@ -82,4 +81,3 @@ class AdminCommands:
"""
hashed_password = self.hash_password(password)
self.db.changePassword(username, hashed_password)