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
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

View File

@ -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

View File

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