add files

This commit is contained in:
WorldTeacher
2024-01-26 08:28:01 +01:00
parent dd9ee24a8f
commit 0a9818940c
110 changed files with 21563 additions and 0 deletions

View File

@@ -0,0 +1,52 @@
import hashlib
import random
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:
def __init__(self):
self.db = Database()
def create_password(self, password):
salt = self.create_salt()
hashed_password = self.hash_password(password)
return (hashed_password,salt)
def create_salt(self):
return "".join(
random.choices(
"abcdefghijklmnopqrstuvwxyzQWERTZUIOPLKJHGFDSAYXCVBNM0123456789", k=16
)
)
def create_admin(self):
salt = self.create_salt()
hashed_password = self.hash_password("admin")
self.db.create_user("admin", salt+hashed_password, "admin", salt)
def hash_password(self, password):
hashed = hashlib.sha256((password).encode("utf-8")).hexdigest()
return hashed
def list_users(self):
return self.db.get_users()
def delete_user(self, username):
self.db.delete_user(username)
def change_password(self, username, password):
hashed_password = self.hash_password(password)
self.db.change_password(username, hashed_password)
if __name__ == "__main__":
c = AdminCommands()
c.create_user("test", "test", "user")
c.create_user("admin", "admin", "admin")
print(c.list_users())
c.delete_user("test")
print(c.list_users())
c.change_password("admin", "nopass")
print(c.list_users())