22 lines
845 B
Python
Raw Normal View History

2023-10-13 21:44:30 +02:00
from ..models import users
from ..dependencies import database
from passlib.context import CryptContext
2023-10-14 18:20:08 +02:00
def add(username="", password="", roles="User", disabled=False, confirmed=True):
2023-10-13 22:18:41 +02:00
user_repository = users.UserRepository(database=database.database)
2023-10-13 21:44:30 +02:00
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
2023-10-13 22:18:41 +02:00
result = user_repository.find_one_by({'username': username})
change = "added"
2023-10-14 18:20:08 +02:00
user = users.User(username=username, password=pwd_context.hash(password), roles=roles, disabled=disabled, confirmed=confirmed)
2023-10-13 22:18:41 +02:00
if result is not None:
result.password=pwd_context.hash(password)
result.roles=roles
result.disabled=disabled
2023-10-14 18:20:08 +02:00
result.confirmed=confirmed
2023-10-13 22:18:41 +02:00
user = result
change = "updated"
2023-10-13 21:44:30 +02:00
user_repository.save(user)
2023-10-13 22:18:41 +02:00
print("{0} {1}".format(username, change))