From b81119f53eb76f3609c53d0ef0ad58215c60a93a Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sat, 14 Oct 2023 23:04:19 +0200 Subject: [PATCH] add email key --- app/dependencies/user_add.py | 6 ++++-- app/models/users.py | 9 ++++++++- app/routers/users.py | 8 +++++--- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/app/dependencies/user_add.py b/app/dependencies/user_add.py index 74cc7f4..fae97c5 100644 --- a/app/dependencies/user_add.py +++ b/app/dependencies/user_add.py @@ -1,20 +1,22 @@ from ..models import users from ..dependencies import database from passlib.context import CryptContext +from pydantic import EmailStr -def add(username="", password="", roles="User", disabled=False, confirmed=True): +def add(username="", password="", roles="User", disabled=False, confirmed=True, email="test@toto.com"): 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, confirmed=confirmed) + user = users.User(username=username, password=pwd_context.hash(password), roles=roles, disabled=disabled, confirmed=confirmed, email=email) if result is not None: result.password=pwd_context.hash(password) result.roles=roles result.disabled=disabled result.confirmed=confirmed + result.email=email user = result change = "updated" user_repository.save(user) diff --git a/app/models/users.py b/app/models/users.py index f56841d..4912304 100644 --- a/app/models/users.py +++ b/app/models/users.py @@ -1,4 +1,4 @@ -from pydantic import BaseModel +from pydantic import BaseModel, EmailStr from pydantic_mongo import AbstractRepository, ObjectIdField class User(BaseModel): @@ -9,6 +9,7 @@ class User(BaseModel): disabled: bool = False removed: bool = False confirmed: bool = False + email: EmailStr class UserOut(BaseModel): id: ObjectIdField = None @@ -17,15 +18,21 @@ class UserOut(BaseModel): disabled: bool removed: bool confirmed: bool + email: EmailStr + class UserIn(BaseModel): username: str roles: str password: str + email: EmailStr + class UserCreate(BaseModel): username: str password: str + email: EmailStr + class UserInDB(User): password: str diff --git a/app/routers/users.py b/app/routers/users.py index 22c3a06..8d6580b 100644 --- a/app/routers/users.py +++ b/app/routers/users.py @@ -20,7 +20,7 @@ async def read_users(authorize: Annotated[bool, Depends(permissions_checker.Perm listUsers = [] user_repository = users.UserRepository(database=database.database) for user_index in user_repository.find_by({}, limit=limit, skip=skip): - user = users.UserOut(id=user_index.id, username=user_index.username, disabled=user_index.disabled, roles=user_index.roles, removed=user_index.removed, confirmed=user_index.confirmed) + user = users.UserOut(id=user_index.id, username=user_index.username, email=user_index.email, disabled=user_index.disabled, roles=user_index.roles, removed=user_index.removed, confirmed=user_index.confirmed) listUsers.append(user) return listUsers @@ -40,7 +40,7 @@ async def read_users_id(authorize: Annotated[bool, Depends(permissions_checker.P listUsers = [] user_repository = users.UserRepository(database=database.database) for user_index in user_repository.find_by({key: {'$regex': value}}, limit=limit, skip=skip): - user = users.UserOut(id=user_index.id, username=user_index.username, disabled=user_index.disabled, roles=user_index.roles, removed=user_index.removed, confirmed=user_index.confirmed) + user = users.UserOut(id=user_index.id, username=user_index.username, disabled=user_index.disabled, roles=user_index.roles, email=user_index.email, removed=user_index.removed, confirmed=user_index.confirmed) listUsers.append(user) return listUsers @@ -82,6 +82,7 @@ async def read_users_me(current_user: Annotated[users.User, Depends(users_token. current_user.username = userSingle.username current_user.password = user_token.get_password_hash(userSingle.password) current_user.roles = userSingle.roles + current_user.email = userSingle.email user_repository.save(current_user) return current_user @@ -101,6 +102,7 @@ async def read_users_id(authorize: Annotated[bool, Depends(permissions_checker.P user.username = userSingle.username user.password = user_token.get_password_hash(userSingle.password) user.roles = userSingle.roles + user.email = userSingle.email user_repository.save(user) return user @@ -114,6 +116,6 @@ async def read_users_me(userSingle: users.UserCreate | None = None): ) fm = FastMail(mail.conf) user_repository = users.UserRepository(database=database.database) - current_user = users.User(username=userSingle.username, password=user_token.get_password_hash(userSingle.password)) + current_user = users.User(username=userSingle.username, password=user_token.get_password_hash(userSingle.password), email=userSingle.email) user_repository.save(current_user) return JSONResponse(status_code=200, content={"message": "email has been sent"}) \ No newline at end of file