feat: 增加管理后台的会话、配置和用户管理
This commit is contained in:
+46
-13
@@ -11,7 +11,12 @@ from fastapi.staticfiles import StaticFiles
|
||||
from pydantic import BaseModel, Field
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from admin.identity import identity
|
||||
from admin.routes import admin_router, public_router
|
||||
from admin.session import admin_session_active, clear_admin_session, write_admin_session
|
||||
from admin.settings import ensure_admin_seed, jigsaw_plan, load_admin_identity
|
||||
from euniv.db import get_db, init_db
|
||||
from euniv.models import Account
|
||||
from euniv.service import (
|
||||
fetch_exam_detail,
|
||||
fetch_home,
|
||||
@@ -27,6 +32,8 @@ from euniv.session import clear_session, read_employee_id, write_session
|
||||
@asynccontextmanager
|
||||
async def lifespan(_app: FastAPI):
|
||||
init_db()
|
||||
ensure_admin_seed()
|
||||
load_admin_identity()
|
||||
stop = None
|
||||
if os.environ.get("EUNIV_DISABLE_REFRESH") != "1":
|
||||
stop = start_credential_refresh()
|
||||
@@ -36,10 +43,12 @@ async def lifespan(_app: FastAPI):
|
||||
|
||||
|
||||
app = FastAPI(title="EunivTools", lifespan=lifespan)
|
||||
app.include_router(public_router)
|
||||
app.include_router(admin_router)
|
||||
|
||||
|
||||
class LoginBody(BaseModel):
|
||||
employee_id: str = Field(min_length=1, max_length=32)
|
||||
employee_id: str = Field(min_length=1, max_length=64)
|
||||
password: str = Field(min_length=1, max_length=128)
|
||||
manual_jigsaw: bool = False
|
||||
|
||||
@@ -56,23 +65,45 @@ def health() -> dict:
|
||||
|
||||
def _open_session(response: Response, payload: dict) -> dict:
|
||||
employee_id = payload.get("employee_id")
|
||||
if employee_id:
|
||||
write_session(response, employee_id)
|
||||
return payload
|
||||
if not employee_id:
|
||||
return payload
|
||||
write_session(response, str(employee_id))
|
||||
clear_admin_session(response)
|
||||
opened = dict(payload)
|
||||
opened["role"] = "user"
|
||||
opened["employee_id"] = str(employee_id)
|
||||
return opened
|
||||
|
||||
|
||||
def _active_employee(request: Request, db: Session) -> str | None:
|
||||
employee_id = read_employee_id(request.cookies)
|
||||
if not employee_id or db.get(Account, employee_id) is None:
|
||||
return None
|
||||
return employee_id
|
||||
|
||||
|
||||
@app.get("/api/session")
|
||||
def session(request: Request) -> dict:
|
||||
employee_id = read_employee_id(request.cookies)
|
||||
def session(request: Request, db: Session = Depends(get_db)) -> dict:
|
||||
if admin_session_active(request.cookies):
|
||||
return {"role": "admin", "username": identity.username()}
|
||||
employee_id = _active_employee(request, db)
|
||||
if not employee_id:
|
||||
raise HTTPException(status_code=401, detail="未登录")
|
||||
return {"employee_id": employee_id}
|
||||
return {"role": "user", "employee_id": employee_id}
|
||||
|
||||
|
||||
@app.post("/api/login")
|
||||
def login(body: LoginBody, response: Response, db: Session = Depends(get_db)) -> dict:
|
||||
account = body.employee_id.strip()
|
||||
if identity.matches(account):
|
||||
if not identity.password_ok(body.password):
|
||||
raise HTTPException(status_code=400, detail="登录失败")
|
||||
write_admin_session(response)
|
||||
clear_session(response)
|
||||
return {"role": "admin", "username": identity.username()}
|
||||
manual, attempts = jigsaw_plan(db, body.manual_jigsaw)
|
||||
try:
|
||||
payload = login_and_store(db, body.employee_id.strip(), body.password, manual=body.manual_jigsaw)
|
||||
payload = login_and_store(db, account, body.password, manual=manual, auto_attempts=attempts)
|
||||
except RuntimeError as exc:
|
||||
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
||||
return _open_session(response, payload)
|
||||
@@ -103,8 +134,10 @@ def revoke(request: Request, response: Response, db: Session = Depends(get_db))
|
||||
return {"ok": True}
|
||||
|
||||
|
||||
def _signed_in(request: Request) -> str:
|
||||
employee_id = read_employee_id(request.cookies)
|
||||
def _signed_in(request: Request, db: Session) -> str:
|
||||
if admin_session_active(request.cookies):
|
||||
raise HTTPException(status_code=403, detail="管理员不能访问该接口")
|
||||
employee_id = _active_employee(request, db)
|
||||
if not employee_id:
|
||||
raise HTTPException(status_code=401, detail="未登录")
|
||||
return employee_id
|
||||
@@ -113,7 +146,7 @@ def _signed_in(request: Request) -> str:
|
||||
@app.get("/api/home")
|
||||
def home(request: Request, db: Session = Depends(get_db)) -> dict:
|
||||
try:
|
||||
return fetch_home(db, _signed_in(request))
|
||||
return fetch_home(db, _signed_in(request, db))
|
||||
except RuntimeError as exc:
|
||||
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
||||
|
||||
@@ -121,7 +154,7 @@ def home(request: Request, db: Session = Depends(get_db)) -> dict:
|
||||
@app.get("/api/exam")
|
||||
def exam_detail(path: str, request: Request, db: Session = Depends(get_db)) -> dict:
|
||||
try:
|
||||
return fetch_exam_detail(db, _signed_in(request), path)
|
||||
return fetch_exam_detail(db, _signed_in(request, db), path)
|
||||
except RuntimeError as exc:
|
||||
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
||||
|
||||
@@ -129,7 +162,7 @@ def exam_detail(path: str, request: Request, db: Session = Depends(get_db)) -> d
|
||||
@app.get("/api/training")
|
||||
def training_detail(path: str, request: Request, db: Session = Depends(get_db)) -> dict:
|
||||
try:
|
||||
return fetch_training_detail(db, _signed_in(request), path)
|
||||
return fetch_training_detail(db, _signed_in(request, db), path)
|
||||
except RuntimeError as exc:
|
||||
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
||||
|
||||
|
||||
Reference in New Issue
Block a user