feat(ai): add tenant-safe hierarchical expense learning

This commit is contained in:
caoxiaozhu
2026-07-16 14:30:41 +08:00
parent 6bdf65bc24
commit ee88a36baf
65 changed files with 6909 additions and 232 deletions

View File

@@ -53,6 +53,7 @@ class AuthenticatedUser:
avatar: str
is_admin: bool = False
employee_id: str | None = None
department_id: str | None = None
tenant_id: str = "default"
@@ -116,7 +117,7 @@ class AuthService:
}
if auth_session.username.strip().casefold() not in allowed_identifiers:
return None
return self._build_admin_user(record)
return self._restore_session_scope(self._build_admin_user(record), auth_session)
if auth_session.principal_type != "employee":
return None
@@ -133,7 +134,17 @@ class AuthService:
employee = self.db.execute(stmt).scalars().first()
if employee is None or employee.employment_status == "停用":
return None
return self._build_employee_user(employee)
return self._restore_session_scope(self._build_employee_user(employee), auth_session)
@staticmethod
def _restore_session_scope(
user: AuthenticatedUser,
auth_session: AuthSession,
) -> AuthenticatedUser:
"""会话恢复时以签发并认证过的会话租户为准,禁止回落到默认租户。"""
user.tenant_id = str(auth_session.tenant_id or "default").strip() or "default"
return user
def get_user_snapshot(self, identifier: str) -> AuthUserRead | None:
normalized = identifier.strip()
@@ -249,6 +260,7 @@ class AuthService:
avatar=(employee.name or "?")[:1].upper(),
is_admin=False,
employee_id=employee.id,
department_id=employee.organization_unit_id,
)
@staticmethod