| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- from typing import Any, List
- from fastapi import APIRouter, Depends, HTTPException
- from sqlalchemy.orm import Session
- from app.api.v1 import deps
- from app.models.user import User, UserRole
- from app.schemas.system_config import SystemConfig, SystemConfigUpdate, SystemConfigCreate
- from app.services.system_config_service import SystemConfigService
- router = APIRouter()
- @router.get("/", response_model=List[SystemConfig], summary="获取所有系统配置")
- def read_system_configs(
- db: Session = Depends(deps.get_db),
- current_user: User = Depends(deps.get_current_active_user),
- ) -> Any:
- """
- 获取所有系统配置。
- 需要超级管理员权限。
- """
- if current_user.role != UserRole.SUPER_ADMIN:
- raise HTTPException(status_code=403, detail="权限不足")
- return SystemConfigService.get_all_configs(db)
- @router.post("/", response_model=SystemConfig, summary="设置系统配置")
- def update_system_config(
- config_in: SystemConfigCreate,
- db: Session = Depends(deps.get_db),
- current_user: User = Depends(deps.get_current_active_user),
- ) -> Any:
- """
- 创建或更新系统配置。
- 需要超级管理员权限。
- """
- if current_user.role != UserRole.SUPER_ADMIN:
- raise HTTPException(status_code=403, detail="权限不足")
-
- config = SystemConfigService.set_config(db, config_in.key, config_in.value, config_in.description)
- return config
- @router.get("/public", response_model=List[SystemConfig], summary="获取公开系统配置")
- def read_public_system_configs(
- db: Session = Depends(deps.get_db),
- ) -> Any:
- """
- 获取公开的系统配置(如登录开关状态)。
- 无需鉴权。
- """
- # Define which keys are public
- public_keys = ["sms_login_pc_enabled", "sms_login_mobile_enabled"]
-
- configs = []
- for key in public_keys:
- val = SystemConfigService.get_config(db, key)
- # Defaults to 'false' if not set
- if val is None:
- val = "false"
- configs.append({"key": key, "value": val, "description": "Public Config", "id": 0}) # Dummy ID for schema compliance if needed or handle better
-
- return configs
|