Vaihe 1/1: users-moduuli, kirjautuminen ja käyttäjähallinta
- 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]>
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
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)
|
||||
@@ -0,0 +1,20 @@
|
||||
from enum import StrEnum
|
||||
|
||||
|
||||
class Role(StrEnum):
|
||||
"""User roles (specification 2.2). Public: other modules may use it for authorization."""
|
||||
|
||||
ADMIN = "admin"
|
||||
MANAGEMENT = "management"
|
||||
EMPLOYEE = "employee"
|
||||
INTERN = "intern"
|
||||
SUBCONTRACTOR = "subcontractor"
|
||||
|
||||
@property
|
||||
def can_supervise_interns(self) -> bool:
|
||||
return self in (Role.MANAGEMENT, Role.EMPLOYEE)
|
||||
|
||||
|
||||
class Permission(StrEnum):
|
||||
USERS_MANAGE = "users.manage"
|
||||
CUSTOMERS_USE = "customers.use"
|
||||
@@ -0,0 +1,44 @@
|
||||
from apps.shared.domain.exceptions import DomainError
|
||||
|
||||
|
||||
class InvalidPersonName(DomainError):
|
||||
code = "invalid_name"
|
||||
field = "name"
|
||||
|
||||
|
||||
class InternRequiresEndDate(DomainError):
|
||||
code = "intern_requires_end_date"
|
||||
field = "employment_ends_on"
|
||||
|
||||
|
||||
class InternRequiresSupervisor(DomainError):
|
||||
code = "intern_requires_supervisor"
|
||||
field = "supervisor_id"
|
||||
|
||||
|
||||
class InvalidSupervisor(DomainError):
|
||||
"""The supervisor must be an active management or employee user, not the user itself."""
|
||||
|
||||
code = "invalid_supervisor"
|
||||
field = "supervisor_id"
|
||||
|
||||
|
||||
class EmailAlreadyTaken(DomainError):
|
||||
code = "email_taken"
|
||||
field = "email"
|
||||
|
||||
|
||||
class LastActiveAdmin(DomainError):
|
||||
"""The last active admin cannot be deactivated or moved to another role."""
|
||||
|
||||
code = "last_active_admin"
|
||||
|
||||
|
||||
class SupervisorHasInterns(DomainError):
|
||||
"""A supervisor of active interns cannot be deactivated or moved to a non-supervising role."""
|
||||
|
||||
code = "supervisor_has_interns"
|
||||
|
||||
|
||||
class UserNotFound(DomainError):
|
||||
code = "user_not_found"
|
||||
@@ -0,0 +1,14 @@
|
||||
"""Role based permissions (specification 2.4). Extended module by module."""
|
||||
|
||||
from apps.users.domain.enums import Permission, Role
|
||||
|
||||
_ALL_EXCEPT_SUBCONTRACTOR = frozenset({Role.ADMIN, Role.MANAGEMENT, Role.EMPLOYEE, Role.INTERN})
|
||||
|
||||
_PERMISSIONS: dict[Permission, frozenset[Role]] = {
|
||||
Permission.USERS_MANAGE: frozenset({Role.ADMIN}),
|
||||
Permission.CUSTOMERS_USE: _ALL_EXCEPT_SUBCONTRACTOR,
|
||||
}
|
||||
|
||||
|
||||
def role_has(role: Role, permission: Permission) -> bool:
|
||||
return role in _PERMISSIONS.get(permission, frozenset())
|
||||
@@ -0,0 +1,22 @@
|
||||
from typing import Protocol
|
||||
|
||||
from apps.shared.domain.value_objects import EmailAddress
|
||||
from apps.users.domain.entities import UserAccount
|
||||
|
||||
|
||||
class UserRepository(Protocol):
|
||||
def get_by_id(self, user_id: int) -> UserAccount | None: ...
|
||||
|
||||
def get_by_email(self, email: EmailAddress) -> UserAccount | None: ...
|
||||
|
||||
def save(self, user: UserAccount) -> UserAccount:
|
||||
"""Insert or update; a new user gets its id here."""
|
||||
...
|
||||
|
||||
def is_email_taken(self, email: EmailAddress, *, exclude_id: int | None = None) -> bool: ...
|
||||
|
||||
def lock_active_admin_count(self) -> int:
|
||||
"""Lock the active admin rows for the transaction and return their number."""
|
||||
...
|
||||
|
||||
def has_active_interns(self, supervisor_id: int) -> bool: ...
|
||||
@@ -0,0 +1,17 @@
|
||||
from dataclasses import dataclass
|
||||
|
||||
from apps.users.domain.exceptions import InvalidPersonName
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class PersonName:
|
||||
value: str
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
normalized = " ".join(self.value.split())
|
||||
if not normalized or len(normalized) > 150:
|
||||
raise InvalidPersonName()
|
||||
object.__setattr__(self, "value", normalized)
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.value
|
||||
Reference in New Issue
Block a user