版本1.0.4 - 发布前准备
- 修复system.py缩进错误 - 优化前端页面样式(待重构) - 改进API接口结构 - 完善文档和自动化脚本 - 平台基本功能稳定运行
This commit is contained in:
@@ -3,7 +3,6 @@
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Optional
|
||||
from jose import JWTError, jwt
|
||||
from passlib.context import CryptContext
|
||||
from fastapi.security import OAuth2PasswordBearer
|
||||
from fastapi import Depends, HTTPException, status
|
||||
from sqlalchemy.orm import Session
|
||||
@@ -11,8 +10,7 @@ from sqlalchemy.orm import Session
|
||||
from app.models import User
|
||||
from app.database import get_db
|
||||
|
||||
# 密码加密
|
||||
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
||||
import bcrypt
|
||||
|
||||
# JWT配置
|
||||
SECRET_KEY = "your-secret-key-here" # 生产环境应从环境变量读取
|
||||
@@ -21,13 +19,27 @@ ACCESS_TOKEN_EXPIRE_MINUTES = 10080 # 7天
|
||||
|
||||
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/api/auth/login")
|
||||
|
||||
|
||||
def verify_password(plain_password: str, hashed_password: str) -> bool:
|
||||
"""验证密码"""
|
||||
return pwd_context.verify(plain_password, hashed_password)
|
||||
"""验证密码(使用 bcrypt 直接比较)"""
|
||||
try:
|
||||
# 确保输入为字节
|
||||
if isinstance(plain_password, str):
|
||||
plain_password = plain_password.encode('utf-8')
|
||||
if isinstance(hashed_password, str):
|
||||
hashed_password = hashed_password.encode('utf-8')
|
||||
return bcrypt.checkpw(plain_password, hashed_password)
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
def get_password_hash(password: str) -> str:
|
||||
"""生成密码哈希"""
|
||||
return pwd_context.hash(password)
|
||||
"""生成密码哈希(使用 bcrypt)"""
|
||||
if isinstance(password, str):
|
||||
password = password.encode('utf-8')
|
||||
hashed = bcrypt.hashpw(password, bcrypt.gensalt())
|
||||
return hashed.decode('utf-8')
|
||||
|
||||
|
||||
def create_access_token(data: dict, expires_delta: Optional[timedelta] = None):
|
||||
"""创建JWT token"""
|
||||
@@ -40,6 +52,7 @@ def create_access_token(data: dict, expires_delta: Optional[timedelta] = None):
|
||||
encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
|
||||
return encoded_jwt
|
||||
|
||||
|
||||
async def get_current_user(
|
||||
token: str = Depends(oauth2_scheme),
|
||||
db: Session = Depends(get_db)
|
||||
@@ -57,17 +70,19 @@ async def get_current_user(
|
||||
raise credentials_exception
|
||||
except JWTError:
|
||||
raise credentials_exception
|
||||
|
||||
|
||||
user = db.query(User).filter(User.username == username).first()
|
||||
if user is None:
|
||||
raise credentials_exception
|
||||
return user
|
||||
|
||||
|
||||
async def get_current_active_user(current_user: User = Depends(get_current_user)):
|
||||
"""获取活跃用户(简单检查)"""
|
||||
# 这里可以添加更多活跃性检查逻辑
|
||||
return current_user
|
||||
|
||||
|
||||
async def get_current_admin_user(current_user: User = Depends(get_current_user)):
|
||||
"""获取管理员用户"""
|
||||
if current_user.role != "admin":
|
||||
@@ -77,10 +92,12 @@ async def get_current_admin_user(current_user: User = Depends(get_current_user))
|
||||
)
|
||||
return current_user
|
||||
|
||||
|
||||
def create_audit_log(
|
||||
db: Session,
|
||||
user_id: int,
|
||||
action: str,
|
||||
username: str = "",
|
||||
resource_type: str = "",
|
||||
resource_id: int = None,
|
||||
details: str = "",
|
||||
@@ -88,9 +105,20 @@ def create_audit_log(
|
||||
user_agent: str = ""
|
||||
):
|
||||
"""创建审计日志"""
|
||||
from ..models import AuditLog
|
||||
from app.models import AuditLog, User
|
||||
|
||||
# 如果未提供 username,尝试从 user_id 查询
|
||||
if not username:
|
||||
if user_id and user_id != 0:
|
||||
user = db.query(User).filter(User.id == user_id).first()
|
||||
if user:
|
||||
username = user.username
|
||||
if not username:
|
||||
username = "unknown"
|
||||
|
||||
audit_log = AuditLog(
|
||||
user_id=user_id,
|
||||
username=username,
|
||||
action=action,
|
||||
resource_type=resource_type,
|
||||
resource_id=resource_id,
|
||||
@@ -99,4 +127,4 @@ def create_audit_log(
|
||||
user_agent=user_agent
|
||||
)
|
||||
db.add(audit_log)
|
||||
db.commit()
|
||||
db.commit()
|
||||
|
||||
Reference in New Issue
Block a user