85 lines
2.7 KiB
Python
85 lines
2.7 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from pydantic import BaseModel, Field
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.core.deps import require_staff
|
|
from app.db import get_db
|
|
from app.models.home import admin_edu_platforms, save_edu_platforms
|
|
|
|
router = APIRouter(dependencies=[Depends(require_staff)])
|
|
|
|
|
|
class EduPlatformWrite(BaseModel):
|
|
name: str = ""
|
|
url: str = ""
|
|
imageFileId: int | None = None
|
|
sortOrder: int = 0
|
|
|
|
|
|
class EduPlatformsWrite(BaseModel):
|
|
items: list[EduPlatformWrite] = Field(default_factory=list)
|
|
|
|
|
|
@router.get("/edu/platforms")
|
|
def list_edu_platforms(db: Session = Depends(get_db)) -> dict:
|
|
return admin_edu_platforms(db)
|
|
|
|
|
|
@router.put("/edu/platforms")
|
|
def write_edu_platforms(body: EduPlatformsWrite, db: Session = Depends(get_db)) -> dict:
|
|
try:
|
|
save_edu_platforms(db, [item.model_dump() for item in body.items])
|
|
except ValueError as exc:
|
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(exc)) from exc
|
|
return {"ok": True}
|
|
|
|
|
|
class AssociationEducationWrite(BaseModel):
|
|
webUrl: str = Field(max_length=2048)
|
|
mobileUrl: str = Field(max_length=2048)
|
|
|
|
|
|
def _association_access(db: Session, principal: dict, *, write: bool = False) -> None:
|
|
from app.models.rbac import AdminUser
|
|
from app.services.rbac import granted_resources
|
|
|
|
try:
|
|
user_id = int(principal.get("sub", ""))
|
|
except (ValueError, TypeError):
|
|
raise HTTPException(status_code=401, detail="登录已失效") from None
|
|
user = db.get(AdminUser, user_id)
|
|
if user is None or user.status != "active":
|
|
raise HTTPException(status_code=401, detail="登录已失效")
|
|
resources = granted_resources(db, user_id)
|
|
allowed = any(
|
|
(item.type == "button" and item.permission == "edu:edit") if write
|
|
else (item.type == "menu" and item.path == "/edu")
|
|
for item in resources
|
|
)
|
|
if not allowed:
|
|
raise HTTPException(status_code=403, detail="没有继续教育操作权限")
|
|
|
|
|
|
@router.get("/edu/association")
|
|
def read_association(db: Session = Depends(get_db), principal: dict = Depends(require_staff)) -> dict:
|
|
from app.services.association_education import read_links
|
|
|
|
_association_access(db, principal)
|
|
return read_links(db)
|
|
|
|
|
|
@router.put("/edu/association")
|
|
def write_association(
|
|
body: AssociationEducationWrite,
|
|
db: Session = Depends(get_db),
|
|
principal: dict = Depends(require_staff),
|
|
) -> dict:
|
|
from app.services.association_education import save_links
|
|
|
|
_association_access(db, principal, write=True)
|
|
try:
|
|
save_links(db, body.webUrl, body.mobileUrl)
|
|
except ValueError as exc:
|
|
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
|
return {"ok": True}
|