1. users 表新增 email 列,管理员添加/编辑支持绑定邮箱,修改密码页支持自助绑定 2. 新增 SMTP 邮件发送工具与配置(FRONTEND_URL/SMTP_* 等) 3. 新增忘记密码/重置密码公开接口与页面:一次性令牌、30分钟有效、请求限流、重置后强制重新登录 4. 登录与 /api/auth/me 返回邮箱并同步 PHP Session 5. 版本升级至4.1(sql/upgrades/v4.1.sql)
108 lines
5.3 KiB
Python
108 lines
5.3 KiB
Python
# ===========================================
|
|
# 班级操行分管理系统 - 配置管理
|
|
#
|
|
# 开发者: Canglan
|
|
# 联系方式: admin@sea-studio.top
|
|
# 版权归属: Sea Network Technology Studio
|
|
# 许可证: MIT License
|
|
#
|
|
# 版权所有 © Sea Network Technology Studio
|
|
# ===========================================
|
|
|
|
import os
|
|
from dotenv import load_dotenv
|
|
from typing import List
|
|
|
|
load_dotenv()
|
|
|
|
class Settings:
|
|
APP_NAME: str = os.getenv("APP_NAME", "班级操行分管理系统")
|
|
APP_ENV: str = os.getenv("APP_ENV", "production")
|
|
DEBUG: bool = os.getenv("DEBUG", "False").lower() == "true"
|
|
SECRET_KEY: str = os.getenv("SECRET_KEY", "")
|
|
API_VERSION: str = os.getenv("API_VERSION", "v1")
|
|
|
|
DB_HOST: str = os.getenv("DB_HOST", "127.0.0.1")
|
|
DB_PORT: int = int(os.getenv("DB_PORT", "3306"))
|
|
DB_USER: str = os.getenv("DB_USER", "root")
|
|
DB_PASSWORD: str = os.getenv("DB_PASSWORD", "")
|
|
DB_NAME: str = os.getenv("DB_NAME", "classmanagerdb")
|
|
DB_POOL_SIZE: int = int(os.getenv("DB_POOL_SIZE", "10"))
|
|
DB_MAX_OVERFLOW: int = int(os.getenv("DB_MAX_OVERFLOW", "20"))
|
|
|
|
REDIS_HOST: str = os.getenv("REDIS_HOST", "127.0.0.1")
|
|
REDIS_PORT: int = int(os.getenv("REDIS_PORT", "6379"))
|
|
REDIS_PASSWORD: str = os.getenv("REDIS_PASSWORD", "")
|
|
REDIS_DB: int = int(os.getenv("REDIS_DB", "0"))
|
|
REDIS_MAX_CONNECTIONS: int = int(os.getenv("REDIS_MAX_CONNECTIONS", "50"))
|
|
|
|
@property
|
|
def REDIS_URL(self) -> str:
|
|
if self.REDIS_PASSWORD:
|
|
return f"redis://:{self.REDIS_PASSWORD}@{self.REDIS_HOST}:{self.REDIS_PORT}/{self.REDIS_DB}"
|
|
return f"redis://{self.REDIS_HOST}:{self.REDIS_PORT}/{self.REDIS_DB}"
|
|
|
|
JWT_SECRET_KEY: str = os.getenv("JWT_SECRET_KEY", "")
|
|
JWT_ALGORITHM: str = os.getenv("JWT_ALGORITHM", "HS256")
|
|
JWT_EXPIRE_MINUTES: int = int(os.getenv("JWT_EXPIRE_MINUTES", "60"))
|
|
JWT_IDLE_TIMEOUT_MINUTES: int = int(os.getenv("JWT_IDLE_TIMEOUT_MINUTES", "10"))
|
|
|
|
PASSWORD_SALT: str = os.getenv("PASSWORD_SALT", "")
|
|
DEBUG_ENABLED: bool = os.getenv("DEBUG_ENABLED", "False").lower() == "true"
|
|
DEBUG_PATH: str = os.getenv("DEBUG_PATH", "/debug_add_admin")
|
|
|
|
# ========== 站点地址与邮件(找回密码) ==========
|
|
# 前端站点地址,用于生成密码重置链接,如 https://class.example.com
|
|
FRONTEND_URL: str = os.getenv("FRONTEND_URL", "").rstrip("/")
|
|
SMTP_ENABLED: bool = os.getenv("SMTP_ENABLED", "False").lower() == "true"
|
|
SMTP_HOST: str = os.getenv("SMTP_HOST", "")
|
|
SMTP_PORT: int = int(os.getenv("SMTP_PORT", "465"))
|
|
SMTP_USER: str = os.getenv("SMTP_USER", "")
|
|
SMTP_PASSWORD: str = os.getenv("SMTP_PASSWORD", "")
|
|
SMTP_FROM: str = os.getenv("SMTP_FROM", "")
|
|
SMTP_FROM_NAME: str = os.getenv("SMTP_FROM_NAME", os.getenv("APP_NAME", "班级操行分管理系统"))
|
|
SMTP_SSL: bool = os.getenv("SMTP_SSL", "True").lower() == "true"
|
|
SMTP_STARTTLS: bool = os.getenv("SMTP_STARTTLS", "False").lower() == "true"
|
|
# 密码重置链接有效期(分钟)
|
|
PASSWORD_RESET_EXPIRE_MINUTES: int = int(os.getenv("PASSWORD_RESET_EXPIRE_MINUTES", "30"))
|
|
|
|
DEDUCTION_HOMEWORK_NOT_SUBMIT: int = int(os.getenv("DEDUCTION_HOMEWORK_NOT_SUBMIT", "2"))
|
|
DEDUCTION_HOMEWORK_LATE: int = int(os.getenv("DEDUCTION_HOMEWORK_LATE", "1"))
|
|
DEDUCTION_ATTENDANCE_ABSENT: int = int(os.getenv("DEDUCTION_ATTENDANCE_ABSENT", "3"))
|
|
DEDUCTION_ATTENDANCE_LATE: int = int(os.getenv("DEDUCTION_ATTENDANCE_LATE", "1"))
|
|
DEDUCTION_ATTENDANCE_LEAVE: int = int(os.getenv("DEDUCTION_ATTENDANCE_LEAVE", "0"))
|
|
|
|
LABOR_POINTS_ADD: int = int(os.getenv("LABOR_POINTS_ADD", "1"))
|
|
LABOR_POINTS_SUBTRACT: int = int(os.getenv("LABOR_POINTS_SUBTRACT", "-1"))
|
|
|
|
# 以下加减分上限自 v4.0 起废弃:权限组上限存于数据库 permission_groups 表,
|
|
# 通过「管理员管理 → 权限组」动态配置,保留字段仅为兼容旧 .env 文件。
|
|
MONITOR_MAX_ADD: int = int(os.getenv("MONITOR_MAX_ADD", "5"))
|
|
MONITOR_MAX_SUBTRACT: int = int(os.getenv("MONITOR_MAX_SUBTRACT", "-5"))
|
|
|
|
STUDY_COMMISSIONER_MAX_POINTS: int = int(os.getenv("STUDY_COMMISSIONER_MAX_POINTS", "5"))
|
|
ATTENDANCE_REP_MAX_POINTS: int = int(os.getenv("ATTENDANCE_REP_MAX_POINTS", "8"))
|
|
LABOR_REP_MAX_POINTS: int = int(os.getenv("LABOR_REP_MAX_POINTS", "1"))
|
|
VOLUNTEER_REP_MAX_POINTS: int = int(os.getenv("VOLUNTEER_REP_MAX_POINTS", "5"))
|
|
|
|
LOG_LEVEL: str = os.getenv("LOG_LEVEL", "INFO")
|
|
LOG_MAX_BYTES: int = int(os.getenv("LOG_MAX_BYTES", "104857600"))
|
|
LOG_BACKUP_COUNT: int = int(os.getenv("LOG_BACKUP_COUNT", "30"))
|
|
LOG_RETENTION_DAYS: int = int(os.getenv("LOG_RETENTION_DAYS", "365"))
|
|
|
|
@property
|
|
def CORS_ORIGINS(self) -> List[str]:
|
|
origins = os.getenv("CORS_ORIGINS", "")
|
|
return [origin.strip() for origin in origins.split(",") if origin.strip()]
|
|
|
|
MAX_UPLOAD_SIZE: int = int(os.getenv("MAX_UPLOAD_SIZE", "5242880"))
|
|
ALLOWED_EXTENSIONS: set = set(os.getenv("ALLOWED_EXTENSIONS", "json").split(","))
|
|
STUDENT_INITIAL_POINTS: int = int(os.getenv("STUDENT_INITIAL_POINTS", "60"))
|
|
|
|
def validate(self) -> None:
|
|
required = ["SECRET_KEY", "JWT_SECRET_KEY", "PASSWORD_SALT"]
|
|
for name in required:
|
|
if not getattr(self, name):
|
|
raise ValueError(f"配置 {name} 不能为空")
|
|
|
|
settings = Settings() |