21 lines
773 B
Python
21 lines
773 B
Python
from ..models import users
|
|
from ..dependencies import database
|
|
from passlib.context import CryptContext
|
|
|
|
|
|
def add(username="", password="", roles="User", disabled=False):
|
|
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)
|
|
if result is not None:
|
|
result.password=pwd_context.hash(password)
|
|
result.roles=roles
|
|
result.disabled=disabled
|
|
user = result
|
|
change = "updated"
|
|
user_repository.save(user)
|
|
print("{0} {1}".format(username, change))
|