sync update from LAN repo (source dhk-1 @ 3b1ec5b4)

This commit is contained in:
gozeros committed 2026-11-05 10:56:34 +08:00
1 parent da6521b6cb
commit 264e2200d6
995 files changed
+148105 -13399

No files matched your search

@@ -15,6 +15,10 @@ class UserNotFoundError(LookupError):
"""
class CredentialConflictError(ValueError):
"""Credentials changed after the caller authenticated the old password."""
class UserRepository(ABC):
"""Abstract interface for user data storage.
@@ -61,6 +65,15 @@ class UserRepository(ABC):
"""
raise NotImplementedError
async def get_users_by_ids(self, user_ids: list[str]) -> list[User]:
"""Batch lookup users; fallback keeps lightweight repositories compatible."""
users = []
for user_id in dict.fromkeys(user_ids):
user = await self.get_user_by_id(user_id)
if user is not None:
users.append(user)
return users
@abstractmethod
async def find_user_by_email_local_part(self, local_part: str) -> User | None:
"""Find a user by the local part of their email (the bit before ``@``).
@@ -99,6 +112,26 @@ class UserRepository(ABC):
"""
return []
async def update_credentials(self, user: User, password_hash: str | None, *, email: str | None = None) -> User:
"""Fallback for non-SQL implementations; SQL overrides with atomic CAS."""
user.password_hash = password_hash
user.login_password = password_hash
user.token_version += 1
user.needs_setup = False
if email is not None:
user.email = email
return await self.update_user(user)
async def create_initial_admin(self, user: User) -> User:
if await self.count_admin_users():
raise ValueError("System already initialized")
return await self.create_user(user)
async def start_login_session(self, user: User) -> User:
"""Invalidate earlier sessions after a successful credential check."""
user.token_version += 1
return await self.update_user(user)
@abstractmethod
async def update_user(self, user: User) -> User:
"""Update an existing user.
@@ -131,6 +164,25 @@ class UserRepository(ABC):
"""Return all users ordered by creation date."""
raise NotImplementedError
@abstractmethod
async def list_users_page(
self, *, q: str | None = None, limit: int = 20, offset: int = 0
) -> list[User]:
"""Return one page of users ordered by creation date.
``q`` optionally matches (case-insensitive substring) email or user id.
"""
raise NotImplementedError
@abstractmethod
async def count_users_filtered(
self, *, q: str | None = None, approved_or_admin: bool = False
) -> int:
"""Count users, optionally filtered by the same ``q`` as
:meth:`list_users_page`; ``approved_or_admin`` restricts to users the
login whitelist would let in (approved or admin role)."""
raise NotImplementedError
@abstractmethod
async def get_user_by_oauth(self, provider: str, oauth_id: str) -> User | None:
"""Get user by OAuth provider and ID.