add users work
This commit is contained in:
parent
680634aeee
commit
9184d13391
@ -1,5 +1,5 @@
|
|||||||
from pymongo import MongoClient
|
from pymongo import MongoClient
|
||||||
import os
|
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"]]
|
database = client[os.environ["MONGO_DATABASE"]]
|
@ -4,8 +4,17 @@ from passlib.context import CryptContext
|
|||||||
|
|
||||||
|
|
||||||
def add(username="", password="", roles="User", disabled=False):
|
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 = 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)
|
user_repository.save(user)
|
||||||
print("{O} added".format(username))
|
print("{0} {1}".format(username, change))
|
||||||
|
@ -12,6 +12,7 @@ app.include_router(token.router)
|
|||||||
@app.on_event("startup")
|
@app.on_event("startup")
|
||||||
async def startup_event():
|
async def startup_event():
|
||||||
user_add.add(username="Peter93", password="toto", roles="Admin")
|
user_add.add(username="Peter93", password="toto", roles="Admin")
|
||||||
|
user_add.add(username="Robert80", password="titi", roles="User")
|
||||||
|
|
||||||
@app.get("/")
|
@app.get("/")
|
||||||
async def root():
|
async def root():
|
||||||
|
Loading…
x
Reference in New Issue
Block a user