fix: password change bugs

This commit is contained in:
2026-06-10 20:09:40 +03:00
parent de870b78d2
commit 99714ac476
4 changed files with 24 additions and 14 deletions

View File

@@ -35,10 +35,6 @@ class IUserRepository(ABC):
async def count_individuals(self, *, query: str) -> int: async def count_individuals(self, *, query: str) -> int:
raise NotImplementedError raise NotImplementedError
@abstractmethod
async def get_password_hash(self, user_id: str) -> str:
raise NotImplementedError
@abstractmethod @abstractmethod
async def set_password(self, user_id: str, password_hash: str) -> UserEntity: async def set_password(self, user_id: str, password_hash: str) -> UserEntity:
raise NotImplementedError raise NotImplementedError

View File

@@ -18,14 +18,12 @@ class SetPasswordCommand:
@transactional @transactional
async def __call__(self, email: str, password: str) -> bool: async def __call__(self, email: str, password: str) -> bool:
try: try:
user = await self._unit_of_work.user_repository.get_user_by_email(email) password_hash = await self._hash_service.hash(password)
password_hash = self._hash_service.hash_password(password)
await self._unit_of_work.user_repository.set_password( await self._unit_of_work.user_repository.set_password(
user_id=user.id, user_email=email,
password_hash=password_hash, password_hash=password_hash,
) )
self._logger.info(f'Set password for user {user.id}') self._logger.info(f'Set password for user {email}')
return True return True
except ApplicationException: except ApplicationException:
raise raise

View File

@@ -75,9 +75,24 @@ class UserRepository(IUserRepository):
self._logger.exception(str(exc)) self._logger.exception(str(exc))
raise ApplicationException(status_code=500, message='Database error') raise ApplicationException(status_code=500, message='Database error')
async def _get_user(self, user_id: str) -> UserModel:
stmt = (
select(UserModel)
.where(
UserModel.id == user_id,
UserModel.is_deleted.is_(False),
)
)
result = await self._session.execute(stmt)
user: UserModel | None = result.scalar_one_or_none()
if user is None:
self._logger.warning(f'User not found with user_id {user_id}')
raise ApplicationException(status_code=404, message='User not found')
return user
async def _update_field(self, user_id: str, **fields: object) -> UserEntity: async def _update_field(self, user_id: str, **fields: object) -> UserEntity:
try: try:
user = await self._get_active_user(user_id) user = await self._get_user(user_id)
for key, value in fields.items(): for key, value in fields.items():
setattr(user, key, value) setattr(user, key, value)
await self._session.flush() await self._session.flush()
@@ -89,8 +104,9 @@ class UserRepository(IUserRepository):
self._logger.exception(str(exception)) self._logger.exception(str(exception))
raise InternalServerException(message=f'Database error: {str(exception)}') raise InternalServerException(message=f'Database error: {str(exception)}')
async def set_password(self, user_id: str, password_hash: str) -> UserEntity: async def set_password(self, user_email: str, password_hash: str) -> UserEntity:
return await self._update_field(user_id, password_hash=password_hash) user = await self.get_user_by_email(user_email)
return await self._update_field(user.id, password_hash=password_hash)
async def get_user_by_email(self, email: str) -> UserEntity: async def get_user_by_email(self, email: str) -> UserEntity:
try: try:

View File

@@ -3,7 +3,7 @@ from fastapi.responses import ORJSONResponse
from starlette import status from starlette import status
from src.presentation.dependencies.commands import get_set_password_command from src.presentation.dependencies.commands import get_set_password_command
from src.application.commands.set_password import SetPasswordCommand from src.application.commands.set_password import SetPasswordCommand
from presentation.schemas.password import SetPasswordRequest from src.presentation.schemas.password import SetPasswordRequest
users_router = APIRouter(prefix='/users', tags=['users']) users_router = APIRouter(prefix='/users', tags=['users'])