feat: trigger LDAP background sync on successful login
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
from fastapi import APIRouter, Depends, Form, Request
|
||||
from fastapi import APIRouter, BackgroundTasks, Depends, Form, Request
|
||||
from fastapi.responses import HTMLResponse, RedirectResponse
|
||||
from fastapi.templating import Jinja2Templates
|
||||
from sqlalchemy.orm import Session
|
||||
@@ -7,6 +7,7 @@ from app.core.auth import clear_auth_cookie, create_access_token, set_auth_cooki
|
||||
from app.core.config import get_settings
|
||||
from app.core.database import get_db
|
||||
from app.modules.auth.dependencies import get_current_user
|
||||
from app.modules.auth.ldap import sync_all_users
|
||||
from app.modules.auth.schemas import UserOut
|
||||
from app.modules.auth.service import authenticate_user
|
||||
|
||||
@@ -27,6 +28,7 @@ async def login_page(request: Request):
|
||||
@router.post("/login", response_class=HTMLResponse)
|
||||
async def login(
|
||||
request: Request,
|
||||
background_tasks: BackgroundTasks,
|
||||
username: str = Form(...),
|
||||
password: str = Form(...),
|
||||
db: Session = Depends(get_db),
|
||||
@@ -39,6 +41,17 @@ async def login(
|
||||
{"nav_items": _NAV, "app_version": "0.1.0", "error": "Ungültige Zugangsdaten."},
|
||||
status_code=200,
|
||||
)
|
||||
if settings.LDAP_ENABLED:
|
||||
background_tasks.add_task(
|
||||
sync_all_users,
|
||||
username,
|
||||
password,
|
||||
settings.LDAP_SERVER,
|
||||
settings.LDAP_DOMAIN,
|
||||
settings.LDAP_SEARCH_BASE,
|
||||
settings.LDAP_SYNC_MIN_INTERVAL_HOURS,
|
||||
settings.LDAP_SYNC_LETTER_DELAY_SECONDS,
|
||||
)
|
||||
token = create_access_token(username=user.username, is_admin=user.is_admin)
|
||||
response = RedirectResponse(url="/", status_code=303)
|
||||
set_auth_cookie(response, token)
|
||||
|
||||
Reference in New Issue
Block a user