async def find_email(self, email: UserEmail) -> User: await self._ensure_indexes() user: Optional[User] = await self._find_one(self._collection, {'email': email.value()}) if not user: raise UserNotFoundError.create(detail={'email': email.value()}) return user
async def find_email_and_refresh_token( self, email: UserEmail, refresh_token: UserRefreshToken) -> User: await self._ensure_indexes() user: Optional[User] = await self._find_one( self._collection, { 'email': email.value(), 'refresh_token': refresh_token.value() }) if not user: raise UserNotFoundError.create( detail={ 'email': email.value(), 'refresh_token': refresh_token.value() }) return user
async def __call__(self, email: UserEmail, password: str) -> TokenGeneratorResponse: try: user = await self._repository.find(email) user.validate_password(password) except (UserNotFoundError, UserPasswordNotMatchError) as err: raise UserUnauthorizedError.create(detail={'email': email.value()}).with_exception(err) token = await self._factory.generate(user.id(), self._expiration_days) return TokenGeneratorResponse(user_id=user.id().value(), access_token=token, token_type=self._token_type)
async def notify_user_password_resetted(self, user_id: UserId, email: UserEmail) -> None: self._send_mail( user_id=user_id.value(), to_address=email.value(), subject='Project - The password was reset', mime_text_plain_content='The password was reset', )
async def notify_user_registered(self, user_id: UserId, email: UserEmail) -> None: self._send_mail( user_id=user_id.value(), to_address=email.value(), subject='Welcome to Project', mime_text_plain_content=f'Welcome {email.value()}', )
def decode(self, data: Dict[str, Any]) -> Event: return UserPasswordForgotten( UserPasswordForgotten.Attributes( UserId(data['id']), UserEmail(data['email']), UserRefreshToken(data['refresh_token']), UserRefreshTokenExpirationIn( data['refresh_token_expiration_in']), ))
def to_aggregate(self, data: Dict[str, Any]) -> User: return User( UserId(data['id']), UserEmail(data['email']), UserPassword(data['password'], True), None if data['refresh_token'] is None else UserRefreshToken( data['refresh_token']), None if data['refresh_token_expiration_in'] is None else UserRefreshTokenExpirationIn(data['refresh_token_expiration_in']), )
async def notify_user_password_forgotten( self, user_id: UserId, email: UserEmail, refresh_token: UserRefreshToken) -> None: self._send_mail( user_id=user_id.value(), to_address=email.value(), subject='Project - Here you have your reset password code', mime_text_plain_content= f'Here you have your reset password code: {refresh_token.value()}', )
async def handle(self, command: RegisterUserCommand) -> None: await self._service(UserId(command.user_id), UserEmail(command.email), UserPassword(command.password))
def test_email_fails(value: str) -> None: pytest.raises(UserEmailNotValidError, lambda: UserEmail(value))
def decode(self, data: Dict[str, Any]) -> Event: return UserRegistered( UserRegistered.Attributes(UserId(data['id']), UserEmail(data['email'])))
async def find(self, email: UserEmail) -> User: user: Optional[User] = await self._find_one(self._collection, {'email': email.value()}) if not user: raise UserNotFoundError.create(detail={'email': email.value()}) # pragma: no cover return user
def decode(self, data: Dict[str, Any]) -> Event: return UserPasswordChanged( UserPasswordChanged.Attributes(UserId(data['id']), UserEmail(data['email'])))
async def handle(self, command: NotifyUserRegisteredCommand) -> None: await self._service(UserId(command.id), UserEmail(command.email))
async def handle(self, query: GenerateTokenQuery) -> Optional[Response]: return await self._service(UserEmail(query.email), query.password)
async def handle(self, command: ForgetUserPasswordCommand) -> None: await self._service(UserEmail(command.email))
async def handle(self, command: ResetUserPasswordCommand) -> None: await self._service( UserEmail(command.email), UserRefreshToken(command.refresh_token), UserPassword(command.new_password) )