async def find(self, user_id: UserId) -> User: await self._ensure_indexes() user: Optional[User] = await self._find_one(self._collection, {'id': user_id.value()}) if not user: raise UserNotFoundError.create(detail={'id': user_id.value()}) return user
async def get_current_user( di: Container = Depends(get_current_container), token: str = Depends( OAuth2PasswordBearer(tokenUrl='/api/public/users/auth')), ) -> UserAuth: user_id = (await di.get(AuthenticatorService)(token)).user_id return await di.get(UserFinderService)(UserId(user_id))
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()}', )
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', )
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']), ))
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()}', )
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 generate(self, user_id: UserId, expiration_in_days: int) -> str: now = self._now() return self._jwt.encode( header={'alg': 'HS256'}, payload={ 'iss': 'Authlib', 'sub': dumps({'id': user_id.value()}).decode('utf-8'), 'iat': now, 'exp': now + timedelta(days=expiration_in_days), }, key=self._secret, ).decode('utf-8')
async def handle(self, command: RegisterUserCommand) -> None: await self._service(UserId(command.user_id), UserEmail(command.email), UserPassword(command.password))
def decode(self, data: Dict[str, Any]) -> Event: return UserRegistered( UserRegistered.Attributes(UserId(data['id']), UserEmail(data['email'])))
async def search(self, user_id: UserId) -> Optional[User]: await self._ensure_indexes() return await self._find_one(self._collection, {'id': user_id.value()})
def test_id_fails(value: str) -> None: pytest.raises(IdInvalidError, lambda: UserId(value))
async def __call__(self, user_id: UserId, email: UserEmail, password: UserPassword) -> None: if await self._repository.search(user_id): raise UserAlreadyExistError.create(detail={'id': user_id.value()}) user = User.register(user_id, email, password) await self._repository.save_and_publish(user)
async def handle(self, command: DeleteUserCommand) -> None: await self._service(UserId(command.user_id))
async def handle(self, query: FindFullUserQuery) -> Optional[Response]: return await self._service(UserId(query.user_id))
def decode(self, data: Dict[str, Any]) -> Event: return UserPasswordChanged( UserPasswordChanged.Attributes(UserId(data['id']), UserEmail(data['email'])))
async def read(self, token: str) -> UserId: payload = await self.decode(token) data = loads(payload['sub']) return UserId(data['id'])
async def handle(self, command: NotifyUserRegisteredCommand) -> None: await self._service(UserId(command.id), UserEmail(command.email))
async def handle(self, command: ChangeUserPasswordCommand) -> None: await self._service(UserId(command.user_id), command.old_password, UserPassword(command.new_password))