190 build - улучшение работы всего сервиса. часть 1

This commit is contained in:
2026-10-21 13:23:38 +03:00
parent e4bedb1264
commit c6dcfcf533
44 changed files with 5391 additions and 0 deletions

View File

@@ -11,6 +11,10 @@ from pydantic import BaseModel, Field
from .async_work import run_db
from .auth_bridge import HOCKEY_COOKIE_NAME, HOCKEY_SESSION_SECONDS, HockeyAuthDependencies
<<<<<<< HEAD
from .program_log import log_event
=======
>>>>>>> e4bedb1264fca8a6bc8a41f0ca1ff67b176c6651
class SessionActivityPayload(BaseModel):
@@ -29,15 +33,26 @@ def create_hockey_auth_router(auth: HockeyAuthDependencies) -> APIRouter:
router = APIRouter(tags=["Hockey authentication"])
@router.get("/login", response_class=HTMLResponse, include_in_schema=False)
<<<<<<< HEAD
async def login_page(request: Request, reason: str = "", next: str = "/"):
next_path = "/logs" if next == "/logs" else "/"
if await auth.optional_user(request):
return RedirectResponse(next_path, status_code=303)
=======
async def login_page(request: Request, reason: str = ""):
if await auth.optional_user(request):
return RedirectResponse("/", status_code=303)
>>>>>>> e4bedb1264fca8a6bc8a41f0ca1ff67b176c6651
messages = {
"expired": "Сессия завершена. Войдите снова.",
"inactive": "Вы вышли из сессии после двух часов без действий. Войдите снова.",
"logout": "Вы вышли из хоккейной панели.",
}
<<<<<<< HEAD
return _login_response(message=messages.get(reason, ""), next_path=next_path)
=======
return _login_response(message=messages.get(reason, ""))
>>>>>>> e4bedb1264fca8a6bc8a41f0ca1ff67b176c6651
@router.post("/login", response_class=HTMLResponse, include_in_schema=False)
async def login_submit(request: Request):
@@ -50,24 +65,51 @@ def create_hockey_auth_router(auth: HockeyAuthDependencies) -> APIRouter:
values = parse_qs((await request.body()).decode("utf-8", errors="replace"))
username = (values.get("username") or [""])[0]
password = (values.get("password") or [""])[0]
<<<<<<< HEAD
next_path = "/logs" if (values.get("next") or [""])[0] == "/logs" else "/"
=======
>>>>>>> e4bedb1264fca8a6bc8a41f0ca1ff67b176c6651
try:
result = await run_db(auth.adapter.login, username, password)
except HTTPException as error:
<<<<<<< HEAD
log_event("WARNING", "authentication", "login.unavailable", "Ошибка входа",
actor_login=username[:200], details={"status": error.status_code, "reason": str(error.detail)})
=======
>>>>>>> e4bedb1264fca8a6bc8a41f0ca1ff67b176c6651
return _login_response(
error=str(error.detail),
username=username,
status_code=error.status_code,
<<<<<<< HEAD
next_path=next_path,
)
if result is None:
log_event("WARNING", "authentication", "login.rejected", "Отклонена попытка входа", actor_login=username[:200])
=======
)
if result is None:
>>>>>>> e4bedb1264fca8a6bc8a41f0ca1ff67b176c6651
return _login_response(
error="Неверный логин или пароль.",
username=username,
status_code=401,
<<<<<<< HEAD
next_path=next_path,
)
token, _user = result
request.state.wfl_user = _user
log_event("INFO", "authentication", "login.accepted", "Пользователь вошёл в панель",
actor_id=_user.id, actor_login=_user.login)
response = RedirectResponse(next_path, status_code=303)
=======
)
token, _user = result
response = RedirectResponse("/", status_code=303)
>>>>>>> e4bedb1264fca8a6bc8a41f0ca1ff67b176c6651
_set_session_cookie(response, request, token, HOCKEY_SESSION_SECONDS)
return response
@@ -94,7 +136,17 @@ def create_hockey_auth_router(auth: HockeyAuthDependencies) -> APIRouter:
@router.post("/logout", include_in_schema=False)
async def logout(request: Request):
<<<<<<< HEAD
try:
user = await auth.optional_user(request)
except HTTPException:
user = None
await run_db(auth.adapter.logout, request.cookies.get(HOCKEY_COOKIE_NAME, ""))
log_event("INFO", "authentication", "logout", "Пользователь вышел из панели",
actor_id=getattr(user, "id", ""), actor_login=getattr(user, "login", ""))
=======
await run_db(auth.adapter.logout, request.cookies.get(HOCKEY_COOKIE_NAME, ""))
>>>>>>> e4bedb1264fca8a6bc8a41f0ca1ff67b176c6651
response = RedirectResponse("/login?reason=logout", status_code=303)
response.delete_cookie(HOCKEY_COOKIE_NAME, path="/")
return response
@@ -108,6 +160,10 @@ def _login_response(
message: str = "",
username: str = "",
status_code: int = 200,
<<<<<<< HEAD
next_path: str = "/",
=======
>>>>>>> e4bedb1264fca8a6bc8a41f0ca1ff67b176c6651
) -> HTMLResponse:
error_html = f'<div class="notice error">{html.escape(error)}</div>' if error else ""
message_html = (
@@ -149,6 +205,10 @@ def _login_response(
<p>Используйте действующий аккаунт WFL. После входа откроется рабочий интерфейс хоккея.</p>
{message_html}{error_html}
<form method="post" action="/login" autocomplete="on">
<<<<<<< HEAD
<input type="hidden" name="next" value="{html.escape(next_path if next_path == '/logs' else '/')}">
=======
>>>>>>> e4bedb1264fca8a6bc8a41f0ca1ff67b176c6651
<label>Логин<input name="username" value="{html.escape(username)}" autocomplete="username" required autofocus></label>
<label>Пароль<input name="password" type="password" autocomplete="current-password" required></label>
<button type="submit">Войти</button>