diff --git a/app/dependencies/database.py b/app/dependencies/database.py new file mode 100644 index 0000000..92c5441 --- /dev/null +++ b/app/dependencies/database.py @@ -0,0 +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"]) +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 new file mode 100644 index 0000000..9495e2e --- /dev/null +++ b/app/dependencies/user_add.py @@ -0,0 +1,11 @@ +from ..models import users +from ..dependencies import database +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) + user_repository.save(user) + print("{O} added".format(username)) diff --git a/app/dependencies/users_active.py b/app/dependencies/users_active.py index 0078b75..a4f04e7 100644 --- a/app/dependencies/users_active.py +++ b/app/dependencies/users_active.py @@ -1,6 +1,7 @@ from datetime import datetime, timedelta from typing import Annotated +from pymongo import MongoClient from fastapi import Depends, HTTPException, status from fastapi.security import OAuth2PasswordBearer from jose import JWTError, jwt diff --git a/app/main.py b/app/main.py index 28a1294..6e098ec 100644 --- a/app/main.py +++ b/app/main.py @@ -1,13 +1,18 @@ from fastapi import FastAPI from .routers import users, token - +from .dependencies import user_add app = FastAPI() app.include_router(users.router) app.include_router(token.router) + +@app.on_event("startup") +async def startup_event(): + user_add.add(username="Peter93", password="toto", roles="Admin") + @app.get("/") async def root(): return {"message": "Hello World !"} \ No newline at end of file diff --git a/app/routers/users.py b/app/routers/users.py index 33952f7..52a57e6 100644 --- a/app/routers/users.py +++ b/app/routers/users.py @@ -1,5 +1,5 @@ from fastapi import APIRouter, Depends -from ..dependencies import users_active, permissions_checker +from ..dependencies import users_active, permissions_checker, database from ..models import users from typing import Annotated