- shared-moduuli: DomainError, EmailAddress, UnitOfWork, tapahtumat, TimestampedModel, daisyUI-lomakerenderöinti ja yhteiset pohjat - users-moduuli Clean Architecture -kerroksin: oma käyttäjämalli (AUTH_USER_MODEL), roolit ja oikeudet, harjoittelija-, vastuuhenkilö- ja viimeisen pääkäyttäjän säännöt - kirjautuminen sähköpostilla, yritysten rajoitus, "muista minut", istunnon katkaisu kun tunnus poistetaan käytöstä tai harjoittelu päättyy - kertakäyttöiset salasanalinkit (72 h / 60 min, uusi mitätöi vanhan), salasanasääntö ja create_admin-komento - käyttäjähallinta: lista (HTMX-haku), lomakkeet ja kopioitava linkki - Tailwind 4 + daisyUI 5 + HTMX + Alpine npm-buildilla, suomennokset - testit: domain, application (fakeilla), integraatio ja selaintestit (axe, vaalea/tumma, 1920/320 px); ruff, mypy ja import-linter - dokumentit päivitetty toteutuksen mukaisiksi Co-Authored-By: Claude Opus 5.5 <[email protected]>
126 lines
3.4 KiB
Python
126 lines
3.4 KiB
Python
from dataclasses import dataclass, replace
|
|
from datetime import date
|
|
from typing import Self
|
|
|
|
from apps.shared.domain.value_objects import EmailAddress
|
|
from apps.users.domain.enums import Role
|
|
from apps.users.domain.exceptions import (
|
|
InternRequiresEndDate,
|
|
InternRequiresSupervisor,
|
|
InvalidSupervisor,
|
|
)
|
|
from apps.users.domain.value_objects import PersonName
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
class UserAccount:
|
|
"""Business view of a user (specification 2.1, 2.2).
|
|
|
|
Invariant: an intern always has an end date and a supervisor; other roles never have them.
|
|
"""
|
|
|
|
id: int | None
|
|
name: PersonName
|
|
email: EmailAddress
|
|
role: Role
|
|
is_active: bool
|
|
employment_ends_on: date | None
|
|
supervisor_id: int | None
|
|
|
|
@classmethod
|
|
def register(
|
|
cls,
|
|
*,
|
|
name: PersonName,
|
|
email: EmailAddress,
|
|
role: Role,
|
|
employment_ends_on: date | None = None,
|
|
supervisor_id: int | None = None,
|
|
) -> Self:
|
|
return cls._validated(
|
|
id=None,
|
|
name=name,
|
|
email=email,
|
|
role=role,
|
|
is_active=True,
|
|
employment_ends_on=employment_ends_on,
|
|
supervisor_id=supervisor_id,
|
|
)
|
|
|
|
@classmethod
|
|
def reconstitute(
|
|
cls,
|
|
*,
|
|
id: int,
|
|
name: PersonName,
|
|
email: EmailAddress,
|
|
role: Role,
|
|
is_active: bool,
|
|
employment_ends_on: date | None,
|
|
supervisor_id: int | None,
|
|
) -> Self:
|
|
"""Rebuild a stored user without checking rules, so a bad row cannot break login."""
|
|
return cls(id, name, email, role, is_active, employment_ends_on, supervisor_id)
|
|
|
|
def update_details(
|
|
self,
|
|
*,
|
|
name: PersonName,
|
|
email: EmailAddress,
|
|
role: Role,
|
|
is_active: bool,
|
|
employment_ends_on: date | None,
|
|
supervisor_id: int | None,
|
|
) -> Self:
|
|
return self._validated(
|
|
id=self.id,
|
|
name=name,
|
|
email=email,
|
|
role=role,
|
|
is_active=is_active,
|
|
employment_ends_on=employment_ends_on,
|
|
supervisor_id=supervisor_id,
|
|
)
|
|
|
|
def with_id(self, id: int) -> Self:
|
|
return replace(self, id=id)
|
|
|
|
def can_log_in(self, today: date) -> bool:
|
|
if not self.is_active:
|
|
return False
|
|
if self.role is Role.INTERN:
|
|
return self.employment_ends_on is not None and self.employment_ends_on >= today
|
|
return True
|
|
|
|
@property
|
|
def is_active_admin(self) -> bool:
|
|
return self.is_active and self.role is Role.ADMIN
|
|
|
|
@property
|
|
def can_supervise(self) -> bool:
|
|
return self.is_active and self.role.can_supervise_interns
|
|
|
|
@classmethod
|
|
def _validated(
|
|
cls,
|
|
*,
|
|
id: int | None,
|
|
name: PersonName,
|
|
email: EmailAddress,
|
|
role: Role,
|
|
is_active: bool,
|
|
employment_ends_on: date | None,
|
|
supervisor_id: int | None,
|
|
) -> Self:
|
|
if role is Role.INTERN:
|
|
if employment_ends_on is None:
|
|
raise InternRequiresEndDate()
|
|
if supervisor_id is None:
|
|
raise InternRequiresSupervisor()
|
|
if id is not None and supervisor_id == id:
|
|
raise InvalidSupervisor()
|
|
else:
|
|
employment_ends_on = None
|
|
supervisor_id = None
|
|
return cls(id, name, email, role, is_active, employment_ends_on, supervisor_id)
|