sync update from LAN repo (source master @ ca48c4ec)

This commit is contained in:
gozeros committed 2026-11-05 10:53:54 +08:00
1 parent 8bc8b0e082
commit ea9dcff9c0
873 files changed
+107866 -14011

No files matched your search

@@ -13,10 +13,10 @@ class UserNotFoundError(LookupError):
while specific call sites can pin to this class to distinguish
"concurrent delete during update" from other lookups.
"""
class CredentialConflictError(ValueError):
"""Credentials changed after the caller authenticated the old password."""
class CredentialConflictError(ValueError):
"""Credentials changed after the caller authenticated the old password."""
class UserRepository(ABC):
@@ -65,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 ``@``).
@@ -103,26 +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)
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.
@@ -155,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.