"""Composition root of the users module: wires use cases to their implementations.""" from apps.shared.infrastructure.clock import DjangoClock from apps.shared.infrastructure.unit_of_work import DjangoUnitOfWork from apps.users.application.dto import PasswordLinkPurpose from apps.users.application.queries import UserQueries from apps.users.application.use_cases import ( CheckLoginAllowed, CreateUser, IssuePasswordLink, RequestPasswordReset, UpdateUser, ) from apps.users.infrastructure.orm import User from apps.users.infrastructure.password_links import DjangoPasswordLinkService from apps.users.infrastructure.queries import DjangoUserQueries from apps.users.infrastructure.repositories import DjangoUserRepository from apps.users.infrastructure.throttle import LoginThrottle from apps.users.infrastructure.tokens import token_generator def create_user() -> CreateUser: return CreateUser(DjangoUserRepository(), DjangoUnitOfWork(), DjangoPasswordLinkService()) def update_user() -> UpdateUser: return UpdateUser(DjangoUserRepository(), DjangoUnitOfWork()) def issue_password_link() -> IssuePasswordLink: return IssuePasswordLink(DjangoUserRepository(), DjangoPasswordLinkService()) def request_password_reset() -> RequestPasswordReset: return RequestPasswordReset(DjangoUserRepository(), DjangoPasswordLinkService(), DjangoClock()) def check_login_allowed() -> CheckLoginAllowed: return CheckLoginAllowed(DjangoUserRepository(), DjangoClock()) def user_queries() -> UserQueries: return DjangoUserQueries() def login_throttle() -> LoginThrottle: return LoginThrottle() def user_for_password_link(user_id: int, purpose: PasswordLinkPurpose, token: str) -> User | None: """The auth user a valid password link belongs to (Django's SetPasswordForm needs it).""" user = User.objects.filter(pk=user_id).first() if user is None or not token_generator(purpose).check_token(user, token): return None return user