add users work

This commit is contained in:
Valentin CZERYBA 2023-10-13 22:18:41 +02:00
parent 680634aeee
commit 9184d13391
3 changed files with 14 additions and 4 deletions

View File

@ -1,5 +1,5 @@
from pymongo import MongoClient
import os
client = MongoClient(host=os.environ["MONGO_HOST"], username=os.environ["MONGO_USER"], password=os.environ["MONGO_PASSWORD"])
client = MongoClient("mongodb+srv://{0}:{1}@{2}/?retryWrites=true&w=majority&appName=AtlasApp".format(os.environ["MONGO_USER"], os.environ["MONGO_PASSWORD"], os.environ["MONGO_HOST"]))
database = client[os.environ["MONGO_DATABASE"]]

View File

@ -4,8 +4,17 @@ from passlib.context import CryptContext
def add(username="", password="", roles="User", disabled=False):
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
user = users.User(username=username, password=pwd_context.hash(password), roles=roles, disabled=disabled)
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("{O} added".format(username))
print("{0} {1}".format(username, change))

View File

@ -12,6 +12,7 @@ app.include_router(token.router)
@app.on_event("startup")
async def startup_event():
user_add.add(username="Peter93", password="toto", roles="Admin")
user_add.add(username="Robert80", password="titi", roles="User")
@app.get("/")
async def root():