add fastapi-mail

This commit is contained in:
Valentin CZERYBA 2023-10-14 22:14:29 +02:00
parent 39f2031079
commit 03ec42508c
3 changed files with 26 additions and 11 deletions

View File

@ -5,7 +5,7 @@ class User(BaseModel):
id: ObjectIdField = None id: ObjectIdField = None
username: str username: str
password: str password: str
roles: str roles: str = "User"
disabled: bool = False disabled: bool = False
removed: bool = False removed: bool = False
confirmed: bool = False confirmed: bool = False
@ -23,6 +23,10 @@ class UserIn(BaseModel):
roles: str roles: str
password: str password: str
class UserCreate(BaseModel):
username: str
password: str
class UserInDB(User): class UserInDB(User):
password: str password: str

View File

@ -3,8 +3,7 @@ from ..dependencies import users_token, permissions_checker, database
from ..models import users from ..models import users
from typing import Annotated from typing import Annotated
from bson import ObjectId from bson import ObjectId
from passlib.context import CryptContext from fastapi.responses import JSONResponse
router = APIRouter() 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"]) @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): 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) user_repository = users.UserRepository(database=database.database)
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
current_user.username = userSingle.username 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 current_user.roles = userSingle.roles
user_repository.save(current_user) user_repository.save(current_user)
return current_user return current_user
@router.put("/users", tags=["users"], response_model=users.User, status_code=status.HTTP_200_OK) @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): 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: if userSingle is None:
raise HTTPException( raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST, status_code=status.HTTP_400_BAD_REQUEST,
detail="Body request is empty" detail="Body request is empty"
) )
user_repository = users.UserRepository(database=database.database)
user = user_repository.find_one_by({"username": {'$eq': userSingle.username}}) user = user_repository.find_one_by({"username": {'$eq': userSingle.username}})
if user is None: if user is None:
response.status_code = status.HTTP_201_CREATED response.status_code = status.HTTP_201_CREATED
user = users.User() user = users.User()
user.username = userSingle.username user.username = userSingle.username
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto") user.password = user_token.get_password_hash(userSingle.password)
user.password = pwd_context.hash(userSingle.password)
user.roles = userSingle.roles user.roles = userSingle.roles
user_repository.save(user) user_repository.save(user)
return 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

View File

@ -3,4 +3,5 @@ uvicorn[standard]
pydantic-mongo pydantic-mongo
python-jose[cryptography] python-jose[cryptography]
passlib[bcrypt] passlib[bcrypt]
python-multipart python-multipart
fastapi-mail