From 03ec42508ce9d0c068c6120ed9c3e8cbacaf0fe2 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sat, 14 Oct 2023 22:14:29 +0200 Subject: [PATCH] add fastapi-mail --- app/models/users.py | 6 +++++- app/routers/users.py | 28 +++++++++++++++++++--------- requierements.txt | 3 ++- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/app/models/users.py b/app/models/users.py index d0b45cf..f56841d 100644 --- a/app/models/users.py +++ b/app/models/users.py @@ -5,7 +5,7 @@ class User(BaseModel): id: ObjectIdField = None username: str password: str - roles: str + roles: str = "User" disabled: bool = False removed: bool = False confirmed: bool = False @@ -23,6 +23,10 @@ class UserIn(BaseModel): roles: str password: str +class UserCreate(BaseModel): + username: str + password: str + class UserInDB(User): password: str diff --git a/app/routers/users.py b/app/routers/users.py index d392cce..2f38da9 100644 --- a/app/routers/users.py +++ b/app/routers/users.py @@ -3,8 +3,7 @@ from ..dependencies import users_token, permissions_checker, database from ..models import users from typing import Annotated from bson import ObjectId -from passlib.context import CryptContext - +from fastapi.responses import JSONResponse router = APIRouter() @@ -80,29 +79,40 @@ async def read_users_id(item_id : str, authorize: Annotated[bool, Depends(permis @router.put("/users/me",tags=["users"], response_model=users.User, response_model_exclude=["id", "password", "roles", "disabled"]) async def read_users_me(current_user: Annotated[users.User, Depends(users_token.get_current_active_user)], authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))], userSingle: users.UserIn | None = None): user_repository = users.UserRepository(database=database.database) - pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto") current_user.username = userSingle.username - current_user.password = pwd_context.hash(userSingle.password) + current_user.password = user_token.get_password_hash(userSingle.password) current_user.roles = userSingle.roles user_repository.save(current_user) return current_user @router.put("/users", tags=["users"], response_model=users.User, status_code=status.HTTP_200_OK) async def read_users_id(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], userSingle: users.UserIn | None = None): - user_repository = users.UserRepository(database=database.database) if userSingle is None: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail="Body request is empty" - ) + ) + user_repository = users.UserRepository(database=database.database) + user = user_repository.find_one_by({"username": {'$eq': userSingle.username}}) if user is None: response.status_code = status.HTTP_201_CREATED user = users.User() user.username = userSingle.username - pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto") - - user.password = pwd_context.hash(userSingle.password) + user.password = user_token.get_password_hash(userSingle.password) user.roles = userSingle.roles user_repository.save(user) return user + + +@router.post("/users",tags=["users"]) +async def read_users_me(userSingle: users.UserCreate | None = None): + if userSingle is None: + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail="Body request is empty" + ) + user_repository = users.UserRepository(database=database.database) + current_user = users.Users(username=userSingle.username, password=user_token.get_password_hash(userSingle.password)) + user_repository.save(current_user) + return current_user diff --git a/requierements.txt b/requierements.txt index 5bd6d38..ad8071b 100644 --- a/requierements.txt +++ b/requierements.txt @@ -3,4 +3,5 @@ uvicorn[standard] pydantic-mongo python-jose[cryptography] passlib[bcrypt] -python-multipart \ No newline at end of file +python-multipart +fastapi-mail \ No newline at end of file