2023-10-14 23:04:19 +02:00

24 lines
938 B
Python

from ..models import users
from ..dependencies import database
from passlib.context import CryptContext
from pydantic import EmailStr
def add(username="", password="", roles="User", disabled=False, confirmed=True, email="test@toto.com"):
user_repository = users.UserRepository(database=database.database)
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
result = user_repository.find_one_by({'username': username})
change = "added"
user = users.User(username=username, password=pwd_context.hash(password), roles=roles, disabled=disabled, confirmed=confirmed, email=email)
if result is not None:
result.password=pwd_context.hash(password)
result.roles=roles
result.disabled=disabled
result.confirmed=confirmed
result.email=email
user = result
change = "updated"
user_repository.save(user)
print("{0} {1}".format(username, change))