From 9184d133910cb9c6ed85cd055140213d0ca6d0ff Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Fri, 13 Oct 2023 22:18:41 +0200 Subject: [PATCH] add users work --- app/dependencies/database.py | 2 +- app/dependencies/user_add.py | 15 ++++++++++++--- app/main.py | 1 + 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/app/dependencies/database.py b/app/dependencies/database.py index 92c5441..7ff0550 100644 --- a/app/dependencies/database.py +++ b/app/dependencies/database.py @@ -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"]] \ No newline at end of file diff --git a/app/dependencies/user_add.py b/app/dependencies/user_add.py index 9495e2e..198c10f 100644 --- a/app/dependencies/user_add.py +++ b/app/dependencies/user_add.py @@ -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)) diff --git a/app/main.py b/app/main.py index 6e098ec..8297dde 100644 --- a/app/main.py +++ b/app/main.py @@ -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():