add email key

This commit is contained in:
Valentin CZERYBA 2023-10-14 23:04:19 +02:00
parent d108c54d81
commit b81119f53e
3 changed files with 17 additions and 6 deletions

View File

@ -1,20 +1,22 @@
from ..models import users from ..models import users
from ..dependencies import database from ..dependencies import database
from passlib.context import CryptContext 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) user_repository = users.UserRepository(database=database.database)
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto") pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
result = user_repository.find_one_by({'username': username}) result = user_repository.find_one_by({'username': username})
change = "added" 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: if result is not None:
result.password=pwd_context.hash(password) result.password=pwd_context.hash(password)
result.roles=roles result.roles=roles
result.disabled=disabled result.disabled=disabled
result.confirmed=confirmed result.confirmed=confirmed
result.email=email
user = result user = result
change = "updated" change = "updated"
user_repository.save(user) user_repository.save(user)

View File

@ -1,4 +1,4 @@
from pydantic import BaseModel from pydantic import BaseModel, EmailStr
from pydantic_mongo import AbstractRepository, ObjectIdField from pydantic_mongo import AbstractRepository, ObjectIdField
class User(BaseModel): class User(BaseModel):
@ -9,6 +9,7 @@ class User(BaseModel):
disabled: bool = False disabled: bool = False
removed: bool = False removed: bool = False
confirmed: bool = False confirmed: bool = False
email: EmailStr
class UserOut(BaseModel): class UserOut(BaseModel):
id: ObjectIdField = None id: ObjectIdField = None
@ -17,15 +18,21 @@ class UserOut(BaseModel):
disabled: bool disabled: bool
removed: bool removed: bool
confirmed: bool confirmed: bool
email: EmailStr
class UserIn(BaseModel): class UserIn(BaseModel):
username: str username: str
roles: str roles: str
password: str password: str
email: EmailStr
class UserCreate(BaseModel): class UserCreate(BaseModel):
username: str username: str
password: str password: str
email: EmailStr
class UserInDB(User): class UserInDB(User):
password: str password: str

View File

@ -20,7 +20,7 @@ async def read_users(authorize: Annotated[bool, Depends(permissions_checker.Perm
listUsers = [] listUsers = []
user_repository = users.UserRepository(database=database.database) user_repository = users.UserRepository(database=database.database)
for user_index in user_repository.find_by({}, limit=limit, skip=skip): 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) listUsers.append(user)
return listUsers return listUsers
@ -40,7 +40,7 @@ async def read_users_id(authorize: Annotated[bool, Depends(permissions_checker.P
listUsers = [] listUsers = []
user_repository = users.UserRepository(database=database.database) user_repository = users.UserRepository(database=database.database)
for user_index in user_repository.find_by({key: {'$regex': value}}, limit=limit, skip=skip): 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) listUsers.append(user)
return listUsers 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.username = userSingle.username
current_user.password = user_token.get_password_hash(userSingle.password) current_user.password = user_token.get_password_hash(userSingle.password)
current_user.roles = userSingle.roles current_user.roles = userSingle.roles
current_user.email = userSingle.email
user_repository.save(current_user) user_repository.save(current_user)
return 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.username = userSingle.username
user.password = user_token.get_password_hash(userSingle.password) user.password = user_token.get_password_hash(userSingle.password)
user.roles = userSingle.roles user.roles = userSingle.roles
user.email = userSingle.email
user_repository.save(user) user_repository.save(user)
return user return user
@ -114,6 +116,6 @@ async def read_users_me(userSingle: users.UserCreate | None = None):
) )
fm = FastMail(mail.conf) fm = FastMail(mail.conf)
user_repository = users.UserRepository(database=database.database) 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) user_repository.save(current_user)
return JSONResponse(status_code=200, content={"message": "email has been sent"}) return JSONResponse(status_code=200, content={"message": "email has been sent"})