Compare commits
13 Commits
9d7a9fb2fd
...
f2776ef0bf
| Author | SHA1 | Date | |
|---|---|---|---|
| f2776ef0bf | |||
| 870c9433d6 | |||
| 6fe01aa248 | |||
| 0755b1a61c | |||
| 9be2028e4c | |||
| 3c7e2c5806 | |||
| c3c26fb107 | |||
| e4c8b03277 | |||
| 35abaa1ebb | |||
| 807e634318 | |||
| b6b37a453b | |||
| e3141ee250 | |||
| 9037bf317b |
53
.env.example
Normal file
53
.env.example
Normal file
@@ -0,0 +1,53 @@
|
||||
# TJWater Server 环境变量配置模板
|
||||
# 复制此文件为 .env 并填写实际值
|
||||
|
||||
# ============================================
|
||||
# 安全配置 (必填)
|
||||
# ============================================
|
||||
|
||||
# JWT 密钥 - 用于生成和验证 Token
|
||||
# 生成方式: openssl rand -hex 32
|
||||
SECRET_KEY=your-secret-key-here-change-in-production-use-openssl-rand-hex-32
|
||||
|
||||
# 数据加密密钥 - 用于敏感数据加密
|
||||
# 生成方式: python -c "from cryptography.fernet import Fernet; print(Fernet.generate_key().decode())"
|
||||
ENCRYPTION_KEY=
|
||||
|
||||
# ============================================
|
||||
# 数据库配置 (PostgreSQL)
|
||||
# ============================================
|
||||
DB_NAME=tjwater
|
||||
DB_HOST=localhost
|
||||
DB_PORT=5432
|
||||
DB_USER=postgres
|
||||
DB_PASSWORD=password
|
||||
|
||||
# ============================================
|
||||
# 数据库配置 (TimescaleDB)
|
||||
# ============================================
|
||||
TIMESCALEDB_DB_NAME=szh
|
||||
TIMESCALEDB_DB_HOST=localhost
|
||||
TIMESCALEDB_DB_PORT=5433
|
||||
TIMESCALEDB_DB_USER=tjwater
|
||||
TIMESCALEDB_DB_PASSWORD=Tjwater@123456
|
||||
|
||||
# ============================================
|
||||
# InfluxDB 配置 (时序数据)
|
||||
# ============================================
|
||||
# INFLUXDB_URL=http://localhost:8086
|
||||
# INFLUXDB_TOKEN=your-influxdb-token
|
||||
# INFLUXDB_ORG=your-org
|
||||
# INFLUXDB_BUCKET=tjwater
|
||||
|
||||
# ============================================
|
||||
# JWT 配置 (可选)
|
||||
# ============================================
|
||||
# ACCESS_TOKEN_EXPIRE_MINUTES=30
|
||||
# REFRESH_TOKEN_EXPIRE_DAYS=7
|
||||
# ALGORITHM=HS256
|
||||
|
||||
# ============================================
|
||||
# 其他配置
|
||||
# ============================================
|
||||
# PROJECT_NAME=TJWater Server
|
||||
# API_V1_STR=/api/v1
|
||||
391
DEPLOYMENT.md
Normal file
391
DEPLOYMENT.md
Normal file
@@ -0,0 +1,391 @@
|
||||
# 部署和集成指南
|
||||
|
||||
本文档说明如何将新的安全功能集成到现有系统中。
|
||||
|
||||
## 📦 已完成的功能
|
||||
|
||||
### 1. 数据加密模块
|
||||
- ✅ `app/core/encryption.py` - Fernet 对称加密实现
|
||||
- ✅ 支持敏感数据加密/解密
|
||||
- ✅ 密钥管理和生成工具
|
||||
|
||||
### 2. 用户认证系统
|
||||
- ✅ `app/domain/models/role.py` - 用户角色枚举 (ADMIN/OPERATOR/USER/VIEWER)
|
||||
- ✅ `app/domain/schemas/user.py` - 用户数据模型和验证
|
||||
- ✅ `app/infra/repositories/user_repository.py` - 用户数据访问层
|
||||
- ✅ `app/api/v1/endpoints/auth.py` - 注册/登录/刷新Token接口
|
||||
- ✅ `app/auth/dependencies.py` - 认证依赖项
|
||||
- ✅ `migrations/001_create_users_table.sql` - 用户表迁移脚本
|
||||
|
||||
### 3. 权限控制系统
|
||||
- ✅ `app/auth/permissions.py` - RBAC 权限控制装饰器
|
||||
- ✅ `app/api/v1/endpoints/user_management.py` - 用户管理接口示例
|
||||
- ✅ 支持基于角色的访问控制
|
||||
- ✅ 支持资源所有者检查
|
||||
|
||||
### 4. 审计日志系统
|
||||
- ✅ `app/core/audit.py` - 审计日志核心功能
|
||||
- ✅ `app/domain/schemas/audit.py` - 审计日志数据模型
|
||||
- ✅ `app/infra/repositories/audit_repository.py` - 审计日志数据访问层
|
||||
- ✅ `app/api/v1/endpoints/audit.py` - 审计日志查询接口
|
||||
- ✅ `app/infra/audit/middleware.py` - 自动审计中间件
|
||||
- ✅ `migrations/002_create_audit_logs_table.sql` - 审计日志表迁移脚本
|
||||
|
||||
### 5. 文档和测试
|
||||
- ✅ `SECURITY_README.md` - 完整的使用文档
|
||||
- ✅ `.env.example` - 环境变量配置模板
|
||||
- ✅ `tests/test_encryption.py` - 加密功能测试
|
||||
|
||||
---
|
||||
|
||||
## 🔧 集成步骤
|
||||
|
||||
### 步骤 1: 环境配置
|
||||
|
||||
1. 复制环境变量模板:
|
||||
```bash
|
||||
cp .env.example .env
|
||||
```
|
||||
|
||||
2. 生成密钥并填写 `.env`:
|
||||
```bash
|
||||
# JWT 密钥
|
||||
openssl rand -hex 32
|
||||
|
||||
# 加密密钥
|
||||
python -c "from cryptography.fernet import Fernet; print(Fernet.generate_key().decode())"
|
||||
```
|
||||
|
||||
3. 编辑 `.env` 填写所有必需的配置项。
|
||||
|
||||
### 步骤 2: 数据库迁移
|
||||
|
||||
执行数据库迁移脚本:
|
||||
|
||||
```bash
|
||||
# 方法 1: 使用 psql 命令
|
||||
psql -U postgres -d tjwater -f migrations/001_create_users_table.sql
|
||||
psql -U postgres -d tjwater -f migrations/002_create_audit_logs_table.sql
|
||||
|
||||
# 方法 2: 在 psql 交互界面
|
||||
psql -U postgres -d tjwater
|
||||
\i migrations/001_create_users_table.sql
|
||||
\i migrations/002_create_audit_logs_table.sql
|
||||
```
|
||||
|
||||
验证表已创建:
|
||||
|
||||
```sql
|
||||
-- 检查用户表
|
||||
SELECT * FROM users;
|
||||
|
||||
-- 检查审计日志表
|
||||
SELECT * FROM audit_logs;
|
||||
```
|
||||
|
||||
### 步骤 3: 更新 main.py
|
||||
|
||||
在 `app/main.py` 中集成新功能:
|
||||
|
||||
```python
|
||||
from fastapi import FastAPI
|
||||
from app.core.config import settings
|
||||
from app.infra.audit.middleware import AuditMiddleware
|
||||
|
||||
app = FastAPI(title=settings.PROJECT_NAME)
|
||||
|
||||
# 1. 添加审计中间件(可选)
|
||||
app.add_middleware(AuditMiddleware)
|
||||
|
||||
# 2. 注册路由
|
||||
from app.api.v1.endpoints import auth, user_management, audit
|
||||
|
||||
app.include_router(
|
||||
auth.router,
|
||||
prefix=f"{settings.API_V1_STR}/auth",
|
||||
tags=["认证"]
|
||||
)
|
||||
|
||||
app.include_router(
|
||||
user_management.router,
|
||||
prefix=f"{settings.API_V1_STR}/users",
|
||||
tags=["用户管理"]
|
||||
)
|
||||
|
||||
app.include_router(
|
||||
audit.router,
|
||||
prefix=f"{settings.API_V1_STR}/audit",
|
||||
tags=["审计日志"]
|
||||
)
|
||||
|
||||
# 3. 确保数据库在启动时初始化
|
||||
@app.on_event("startup")
|
||||
async def startup_event():
|
||||
# 初始化数据库连接池
|
||||
from app.infra.db.postgresql.database import Database
|
||||
global db
|
||||
db = Database()
|
||||
db.init_pool()
|
||||
await db.open()
|
||||
|
||||
@app.on_event("shutdown")
|
||||
async def shutdown_event():
|
||||
# 关闭数据库连接
|
||||
await db.close()
|
||||
```
|
||||
|
||||
### 步骤 4: 保护现有接口
|
||||
|
||||
#### 方法 1: 为路由添加全局依赖
|
||||
|
||||
```python
|
||||
from app.auth.dependencies import get_current_active_user
|
||||
|
||||
# 为整个路由器添加认证
|
||||
router = APIRouter(dependencies=[Depends(get_current_active_user)])
|
||||
```
|
||||
|
||||
#### 方法 2: 为单个端点添加依赖
|
||||
|
||||
```python
|
||||
from app.auth.permissions import require_role, get_current_admin
|
||||
from app.domain.models.role import UserRole
|
||||
|
||||
@router.get("/data")
|
||||
async def get_data(
|
||||
current_user = Depends(require_role(UserRole.USER))
|
||||
):
|
||||
"""需要 USER 及以上角色"""
|
||||
return {"data": "protected"}
|
||||
|
||||
@router.delete("/data/{id}")
|
||||
async def delete_data(
|
||||
id: int,
|
||||
current_user = Depends(get_current_admin)
|
||||
):
|
||||
"""仅管理员可访问"""
|
||||
return {"message": "deleted"}
|
||||
```
|
||||
|
||||
### 步骤 5: 添加审计日志
|
||||
|
||||
#### 自动审计(推荐)
|
||||
|
||||
使用中间件自动记录(已在 main.py 中添加):
|
||||
|
||||
```python
|
||||
app.add_middleware(AuditMiddleware)
|
||||
```
|
||||
|
||||
#### 手动审计
|
||||
|
||||
在关键业务逻辑中手动记录:
|
||||
|
||||
```python
|
||||
from app.core.audit import log_audit_event, AuditAction
|
||||
|
||||
@router.post("/important-action")
|
||||
async def important_action(
|
||||
data: dict,
|
||||
request: Request,
|
||||
current_user = Depends(get_current_active_user)
|
||||
):
|
||||
# 执行业务逻辑
|
||||
result = do_something(data)
|
||||
|
||||
# 记录审计日志
|
||||
await log_audit_event(
|
||||
action=AuditAction.UPDATE,
|
||||
user_id=current_user.id,
|
||||
username=current_user.username,
|
||||
resource_type="important_resource",
|
||||
resource_id=str(result.id),
|
||||
ip_address=request.client.host,
|
||||
request_data=data
|
||||
)
|
||||
|
||||
return result
|
||||
```
|
||||
|
||||
### 步骤 6: 更新 auth/dependencies.py
|
||||
|
||||
确保 `get_db()` 函数正确获取数据库实例:
|
||||
|
||||
```python
|
||||
async def get_db() -> Database:
|
||||
"""获取数据库实例"""
|
||||
# 方法 1: 从 main.py 导入
|
||||
from app.main import db
|
||||
return db
|
||||
|
||||
# 方法 2: 从 FastAPI app.state 获取
|
||||
# from fastapi import Request
|
||||
# def get_db_from_request(request: Request):
|
||||
# return request.app.state.db
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 🧪 测试
|
||||
|
||||
### 1. 测试加密功能
|
||||
|
||||
```bash
|
||||
python tests/test_encryption.py
|
||||
```
|
||||
|
||||
### 2. 测试 API
|
||||
|
||||
启动服务器:
|
||||
|
||||
```bash
|
||||
uvicorn app.main:app --reload
|
||||
```
|
||||
|
||||
访问交互式文档:
|
||||
- Swagger UI: http://localhost:8000/docs
|
||||
- ReDoc: http://localhost:8000/redoc
|
||||
|
||||
### 3. 测试登录
|
||||
|
||||
```bash
|
||||
curl -X POST "http://localhost:8000/api/v1/auth/login" \
|
||||
-H "Content-Type: application/x-www-form-urlencoded" \
|
||||
-d "username=admin&password=admin123"
|
||||
```
|
||||
|
||||
### 4. 测试受保护接口
|
||||
|
||||
```bash
|
||||
TOKEN="your-access-token"
|
||||
curl -X GET "http://localhost:8000/api/v1/auth/me" \
|
||||
-H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 🔄 迁移现有接口
|
||||
|
||||
### 原有硬编码认证
|
||||
|
||||
**旧代码** (`app/api/v1/endpoints/auth.py`):
|
||||
```python
|
||||
AUTH_TOKEN = "567e33c876a2"
|
||||
|
||||
async def verify_token(authorization: str = Header()):
|
||||
token = authorization.split(" ")[1]
|
||||
if token != AUTH_TOKEN:
|
||||
raise HTTPException(status_code=403)
|
||||
```
|
||||
|
||||
**新代码** (已更新):
|
||||
```python
|
||||
from app.auth.dependencies import get_current_active_user
|
||||
|
||||
@router.get("/protected")
|
||||
async def protected_route(
|
||||
current_user = Depends(get_current_active_user)
|
||||
):
|
||||
return {"user": current_user.username}
|
||||
```
|
||||
|
||||
### 更新其他端点
|
||||
|
||||
搜索项目中使用旧认证的地方:
|
||||
|
||||
```bash
|
||||
grep -r "AUTH_TOKEN" app/
|
||||
grep -r "verify_token" app/
|
||||
```
|
||||
|
||||
替换为新的依赖注入系统。
|
||||
|
||||
---
|
||||
|
||||
## 📋 检查清单
|
||||
|
||||
部署前检查:
|
||||
|
||||
- [ ] 环境变量已配置(`.env`)
|
||||
- [ ] 数据库迁移已执行
|
||||
- [ ] 默认管理员账号可登录
|
||||
- [ ] JWT Token 可正常生成和验证
|
||||
- [ ] 权限控制正常工作
|
||||
- [ ] 审计日志正常记录
|
||||
- [ ] 加密功能测试通过
|
||||
- [ ] API 文档可访问
|
||||
|
||||
---
|
||||
|
||||
## ⚠️ 注意事项
|
||||
|
||||
### 1. 向后兼容性
|
||||
|
||||
保留了简化版登录接口 `/auth/login/simple` 以兼容旧客户端:
|
||||
|
||||
```python
|
||||
@router.post("/login/simple")
|
||||
async def login_simple(username: str, password: str):
|
||||
# 验证并返回 Token
|
||||
...
|
||||
```
|
||||
|
||||
### 2. 数据库连接
|
||||
|
||||
确保在 `app/auth/dependencies.py` 中 `get_db()` 函数能正确获取数据库实例。
|
||||
|
||||
### 3. 密钥安全
|
||||
|
||||
- ❌ 不要提交 `.env` 文件到版本控制
|
||||
- ✅ 在生产环境使用环境变量或密钥管理服务
|
||||
- ✅ 定期轮换 JWT 密钥
|
||||
|
||||
### 4. 性能考虑
|
||||
|
||||
- 审计中间件会增加每个请求的处理时间(约 5-10ms)
|
||||
- 对高频接口可考虑异步记录审计日志
|
||||
- 定期清理或归档旧的审计日志
|
||||
|
||||
---
|
||||
|
||||
## 🐛 故障排查
|
||||
|
||||
### 问题 1: 导入错误
|
||||
|
||||
```
|
||||
ImportError: cannot import name 'db' from 'app.main'
|
||||
```
|
||||
|
||||
**解决**: 确保在 `app/main.py` 中定义了全局 `db` 对象。
|
||||
|
||||
### 问题 2: 认证失败
|
||||
|
||||
```
|
||||
401 Unauthorized: Could not validate credentials
|
||||
```
|
||||
|
||||
**检查**:
|
||||
1. Token 是否正确设置在 `Authorization: Bearer {token}` header
|
||||
2. Token 是否过期
|
||||
3. SECRET_KEY 是否配置正确
|
||||
|
||||
### 问题 3: 数据库连接失败
|
||||
|
||||
```
|
||||
psycopg.OperationalError: connection failed
|
||||
```
|
||||
|
||||
**检查**:
|
||||
1. PostgreSQL 是否运行
|
||||
2. `.env` 中数据库配置是否正确
|
||||
3. 数据库是否存在
|
||||
|
||||
---
|
||||
|
||||
## 📞 技术支持
|
||||
|
||||
详细文档请参考:
|
||||
- `SECURITY_README.md` - 安全功能使用指南
|
||||
- `migrations/` - 数据库迁移脚本
|
||||
- `app/domain/schemas/` - 数据模型定义
|
||||
|
||||
322
INTEGRATION_CHECKLIST.md
Normal file
322
INTEGRATION_CHECKLIST.md
Normal file
@@ -0,0 +1,322 @@
|
||||
# API 集成检查清单
|
||||
|
||||
## ✅ 已完成的集成工作
|
||||
|
||||
### 1. 路由集成 (app/api/v1/router.py)
|
||||
|
||||
已添加以下路由到 API Router:
|
||||
|
||||
```python
|
||||
# 新增导入
|
||||
from app.api.v1.endpoints import (
|
||||
...
|
||||
user_management, # 用户管理
|
||||
audit, # 审计日志
|
||||
)
|
||||
|
||||
# 新增路由
|
||||
api_router.include_router(user_management.router, prefix="/users", tags=["User Management"])
|
||||
api_router.include_router(audit.router, prefix="/audit", tags=["Audit Logs"])
|
||||
```
|
||||
|
||||
**路由端点**:
|
||||
- `/api/v1/auth/` - 认证相关(register, login, me, refresh)
|
||||
- `/api/v1/users/` - 用户管理(CRUD操作,仅管理员)
|
||||
- `/api/v1/audit/` - 审计日志查询(仅管理员)
|
||||
|
||||
### 2. 主应用配置 (app/main.py)
|
||||
|
||||
#### 2.1 导入更新
|
||||
```python
|
||||
from app.core.config import settings
|
||||
from app.infra.audit.middleware import AuditMiddleware
|
||||
```
|
||||
|
||||
#### 2.2 数据库初始化
|
||||
```python
|
||||
# 在 lifespan 中存储数据库实例到 app.state
|
||||
app.state.db = pgdb
|
||||
```
|
||||
|
||||
#### 2.3 FastAPI 配置
|
||||
```python
|
||||
app = FastAPI(
|
||||
lifespan=lifespan,
|
||||
title=settings.PROJECT_NAME,
|
||||
description="TJWater Server - 供水管网智能管理系统",
|
||||
version="1.0.0",
|
||||
docs_url="/docs",
|
||||
redoc_url="/redoc",
|
||||
)
|
||||
```
|
||||
|
||||
#### 2.4 审计中间件(可选)
|
||||
```python
|
||||
# 取消注释以启用审计日志
|
||||
# app.add_middleware(AuditMiddleware)
|
||||
```
|
||||
|
||||
### 3. 依赖项更新 (app/auth/dependencies.py)
|
||||
|
||||
更新 `get_db()` 函数从 Request 对象获取数据库:
|
||||
|
||||
```python
|
||||
async def get_db(request: Request) -> Database:
|
||||
"""从 app.state 获取数据库实例"""
|
||||
if not hasattr(request.app.state, "db"):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
||||
detail="Database not initialized"
|
||||
)
|
||||
return request.app.state.db
|
||||
```
|
||||
|
||||
### 4. 审计日志更新
|
||||
|
||||
- `app/api/v1/endpoints/audit.py` - 使用正确的数据库依赖
|
||||
- `app/core/audit.py` - 接受可选的 db 参数
|
||||
|
||||
---
|
||||
|
||||
## 📋 部署前检查清单
|
||||
|
||||
### 环境配置
|
||||
- [ ] 复制 `.env.example` 为 `.env`
|
||||
- [ ] 配置 `SECRET_KEY`(JWT密钥)
|
||||
- [ ] 配置 `ENCRYPTION_KEY`(数据加密密钥)
|
||||
- [ ] 配置数据库连接信息
|
||||
|
||||
### 数据库迁移
|
||||
- [ ] 执行用户表迁移:`psql -U postgres -d tjwater -f migrations/001_create_users_table.sql`
|
||||
- [ ] 执行审计日志表迁移:`psql -U postgres -d tjwater -f migrations/002_create_audit_logs_table.sql`
|
||||
- [ ] 验证表已创建:`\dt` 在 psql 中
|
||||
|
||||
### 依赖检查
|
||||
- [ ] 确认已安装:`cryptography`
|
||||
- [ ] 确认已安装:`python-jose[cryptography]`
|
||||
- [ ] 确认已安装:`passlib[bcrypt]`
|
||||
- [ ] 确认已安装:`email-validator`(用于 Pydantic email 验证)
|
||||
|
||||
### 代码验证
|
||||
- [ ] 检查所有文件导入正常
|
||||
- [ ] 运行加密功能测试:`python tests/test_encryption.py`
|
||||
- [ ] 启动服务器:`uvicorn app.main:app --reload`
|
||||
- [ ] 访问 API 文档:http://localhost:8000/docs
|
||||
|
||||
### API 测试
|
||||
- [ ] 测试登录:POST `/api/v1/auth/login`
|
||||
- [ ] 测试获取当前用户:GET `/api/v1/auth/me`
|
||||
- [ ] 测试用户列表(需管理员):GET `/api/v1/users/`
|
||||
- [ ] 测试审计日志(需管理员):GET `/api/v1/audit/logs`
|
||||
|
||||
---
|
||||
|
||||
## 🔧 快速测试命令
|
||||
|
||||
### 1. 生成密钥
|
||||
```bash
|
||||
# JWT 密钥
|
||||
openssl rand -hex 32
|
||||
|
||||
# 加密密钥
|
||||
python -c "from cryptography.fernet import Fernet; print(Fernet.generate_key().decode())"
|
||||
```
|
||||
|
||||
### 2. 执行迁移
|
||||
```bash
|
||||
cd /home/zhifu/TJWaterServer/TJWaterServerBinary
|
||||
psql -U postgres -d tjwater -f migrations/001_create_users_table.sql
|
||||
psql -U postgres -d tjwater -f migrations/002_create_audit_logs_table.sql
|
||||
```
|
||||
|
||||
### 3. 测试加密
|
||||
```bash
|
||||
python tests/test_encryption.py
|
||||
```
|
||||
|
||||
### 4. 启动服务器
|
||||
```bash
|
||||
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
|
||||
```
|
||||
|
||||
### 5. 测试登录 API
|
||||
```bash
|
||||
# 使用默认管理员账号
|
||||
curl -X POST "http://localhost:8000/api/v1/auth/login" \
|
||||
-H "Content-Type: application/x-www-form-urlencoded" \
|
||||
-d "username=admin&password=admin123"
|
||||
|
||||
# 或使用迁移的账号
|
||||
curl -X POST "http://localhost:8000/api/v1/auth/login" \
|
||||
-H "Content-Type: application/x-www-form-urlencoded" \
|
||||
-d "username=tjwater&password=tjwater@123"
|
||||
```
|
||||
|
||||
### 6. 测试受保护接口
|
||||
```bash
|
||||
# 保存 Token
|
||||
TOKEN="<从登录响应中获取的 access_token>"
|
||||
|
||||
# 获取当前用户信息
|
||||
curl -X GET "http://localhost:8000/api/v1/auth/me" \
|
||||
-H "Authorization: Bearer $TOKEN"
|
||||
|
||||
# 获取用户列表(需管理员权限)
|
||||
curl -X GET "http://localhost:8000/api/v1/users/" \
|
||||
-H "Authorization: Bearer $TOKEN"
|
||||
|
||||
# 查询审计日志(需管理员权限)
|
||||
curl -X GET "http://localhost:8000/api/v1/audit/logs" \
|
||||
-H "Authorization: Bearer $TOKEN"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 📚 API 端点总览
|
||||
|
||||
### 认证接口 (`/api/v1/auth`)
|
||||
|
||||
| 方法 | 端点 | 描述 | 权限 |
|
||||
|------|------|------|------|
|
||||
| POST | `/register` | 用户注册 | 公开 |
|
||||
| POST | `/login` | OAuth2 登录 | 公开 |
|
||||
| POST | `/login/simple` | 简化登录(兼容旧版) | 公开 |
|
||||
| GET | `/me` | 获取当前用户信息 | 认证用户 |
|
||||
| POST | `/refresh` | 刷新 Token | 认证用户 |
|
||||
|
||||
### 用户管理 (`/api/v1/users`)
|
||||
|
||||
| 方法 | 端点 | 描述 | 权限 |
|
||||
|------|------|------|------|
|
||||
| GET | `/` | 获取用户列表 | 管理员 |
|
||||
| GET | `/{id}` | 获取用户详情 | 所有者/管理员 |
|
||||
| PUT | `/{id}` | 更新用户信息 | 所有者/管理员 |
|
||||
| DELETE | `/{id}` | 删除用户 | 管理员 |
|
||||
| POST | `/{id}/activate` | 激活用户 | 管理员 |
|
||||
| POST | `/{id}/deactivate` | 停用用户 | 管理员 |
|
||||
|
||||
### 审计日志 (`/api/v1/audit`)
|
||||
|
||||
| 方法 | 端点 | 描述 | 权限 |
|
||||
|------|------|------|------|
|
||||
| GET | `/logs` | 查询审计日志 | 管理员 |
|
||||
| GET | `/logs/count` | 获取日志总数 | 管理员 |
|
||||
| GET | `/logs/my` | 查看我的操作记录 | 认证用户 |
|
||||
|
||||
---
|
||||
|
||||
## ⚠️ 注意事项
|
||||
|
||||
### 1. 审计中间件
|
||||
审计中间件默认是**禁用**的。如需启用,在 `app/main.py` 中取消注释:
|
||||
|
||||
```python
|
||||
app.add_middleware(AuditMiddleware)
|
||||
```
|
||||
|
||||
**注意**:启用后会自动记录所有 POST/PUT/DELETE 请求,可能增加数据库负载。
|
||||
|
||||
### 2. 向后兼容
|
||||
保留了原有的简化登录接口 `/auth/login/simple`,可以直接使用查询参数:
|
||||
|
||||
```bash
|
||||
POST /api/v1/auth/login/simple?username=admin&password=admin123
|
||||
```
|
||||
|
||||
### 3. 数据库连接
|
||||
确保数据库实例在应用启动时正确初始化并存储到 `app.state.db`。
|
||||
|
||||
### 4. 权限控制示例
|
||||
为现有接口添加权限控制:
|
||||
|
||||
```python
|
||||
from app.auth.permissions import require_role, get_current_admin
|
||||
from app.domain.models.role import UserRole
|
||||
|
||||
# 需要管理员权限
|
||||
@router.delete("/resource/{id}")
|
||||
async def delete_resource(
|
||||
id: int,
|
||||
current_user = Depends(get_current_admin)
|
||||
):
|
||||
...
|
||||
|
||||
# 需要操作员以上权限
|
||||
@router.post("/resource")
|
||||
async def create_resource(
|
||||
data: dict,
|
||||
current_user = Depends(require_role(UserRole.OPERATOR))
|
||||
):
|
||||
...
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 🚀 完整启动流程
|
||||
|
||||
```bash
|
||||
# 1. 进入项目目录
|
||||
cd /home/zhifu/TJWaterServer/TJWaterServerBinary
|
||||
|
||||
# 2. 配置环境变量(如果还没有)
|
||||
cp .env.example .env
|
||||
# 编辑 .env 填写必要的配置
|
||||
|
||||
# 3. 执行数据库迁移(如果还没有)
|
||||
psql -U postgres -d tjwater < migrations/001_create_users_table.sql
|
||||
psql -U postgres -d tjwater < migrations/002_create_audit_logs_table.sql
|
||||
|
||||
# 4. 测试加密功能
|
||||
python tests/test_encryption.py
|
||||
|
||||
# 5. 启动服务器
|
||||
uvicorn app.main:app --reload
|
||||
|
||||
# 6. 访问 API 文档
|
||||
# 浏览器打开: http://localhost:8000/docs
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 📞 故障排查
|
||||
|
||||
### 问题 1: 导入错误
|
||||
```
|
||||
ModuleNotFoundError: No module named 'jose'
|
||||
```
|
||||
**解决**: 安装依赖 `pip install python-jose[cryptography]`
|
||||
|
||||
### 问题 2: 数据库未初始化
|
||||
```
|
||||
503 Service Unavailable: Database not initialized
|
||||
```
|
||||
**解决**: 检查 `main.py` 中的 lifespan 函数是否正确设置 `app.state.db`
|
||||
|
||||
### 问题 3: Token 验证失败
|
||||
```
|
||||
401 Unauthorized: Could not validate credentials
|
||||
```
|
||||
**解决**:
|
||||
1. 检查 SECRET_KEY 是否配置正确
|
||||
2. 确认 Token 格式:`Authorization: Bearer {token}`
|
||||
3. 检查 Token 是否过期
|
||||
|
||||
### 问题 4: 表不存在
|
||||
```
|
||||
relation "users" does not exist
|
||||
```
|
||||
**解决**: 执行数据库迁移脚本
|
||||
|
||||
---
|
||||
|
||||
## 📖 相关文档
|
||||
|
||||
- **使用指南**: `SECURITY_README.md`
|
||||
- **部署指南**: `DEPLOYMENT.md`
|
||||
- **实施总结**: `SECURITY_IMPLEMENTATION_SUMMARY.md`
|
||||
- **自动设置**: `setup_security.sh`
|
||||
|
||||
---
|
||||
|
||||
**最后更新**: 2026-02-02
|
||||
**状态**: ✅ API 已完全集成
|
||||
@@ -1,4 +0,0 @@
|
||||
当前 适配 szh 项目的分支 是 dingsu/szh
|
||||
|
||||
Binary 适配的是 代码 中dingsu/szh 的部分
|
||||
当前只是把 API目录(也就是TJNetwork的部分)加密了
|
||||
370
SECURITY_IMPLEMENTATION_SUMMARY.md
Normal file
370
SECURITY_IMPLEMENTATION_SUMMARY.md
Normal file
@@ -0,0 +1,370 @@
|
||||
# 安全功能实施总结
|
||||
|
||||
## ✅ 已完成的功能
|
||||
|
||||
本次实施完成了完整的安全体系,包括数据加密、身份认证、权限管理、审计日志四大模块。
|
||||
|
||||
---
|
||||
|
||||
## 📁 新增文件清单
|
||||
|
||||
### 核心功能模块
|
||||
|
||||
1. **数据加密**
|
||||
- `app/core/encryption.py` - Fernet 加密实现
|
||||
- `tests/test_encryption.py` - 加密功能测试
|
||||
|
||||
2. **用户系统**
|
||||
- `app/domain/models/role.py` - 用户角色枚举
|
||||
- `app/domain/schemas/user.py` - 用户数据模型
|
||||
- `app/infra/repositories/user_repository.py` - 用户数据访问层
|
||||
|
||||
3. **认证授权**
|
||||
- `app/api/v1/endpoints/auth.py` - 认证接口(已重构)
|
||||
- `app/auth/dependencies.py` - 认证依赖项(已更新)
|
||||
- `app/auth/permissions.py` - 权限控制装饰器
|
||||
- `app/api/v1/endpoints/user_management.py` - 用户管理接口
|
||||
|
||||
4. **审计日志**
|
||||
- `app/core/audit.py` - 审计日志核心(已完善)
|
||||
- `app/domain/schemas/audit.py` - 审计日志数据模型
|
||||
- `app/infra/repositories/audit_repository.py` - 审计日志数据访问层
|
||||
- `app/api/v1/endpoints/audit.py` - 审计日志查询接口
|
||||
- `app/infra/audit/middleware.py` - 自动审计中间件
|
||||
|
||||
### 数据库迁移
|
||||
|
||||
5. **迁移脚本**
|
||||
- `migrations/001_create_users_table.sql` - 用户表
|
||||
- `migrations/002_create_audit_logs_table.sql` - 审计日志表
|
||||
|
||||
### 配置和文档
|
||||
|
||||
6. **配置文件**
|
||||
- `.env.example` - 环境变量模板
|
||||
- `app/core/config.py` - 配置文件(已更新)
|
||||
- `app/core/security.py` - 安全工具(已增强)
|
||||
|
||||
7. **文档**
|
||||
- `SECURITY_README.md` - 完整使用指南(79KB+)
|
||||
- `DEPLOYMENT.md` - 部署和集成指南
|
||||
- `SECURITY_IMPLEMENTATION_SUMMARY.md` - 本文件
|
||||
|
||||
8. **工具**
|
||||
- `setup_security.sh` - 快速设置脚本
|
||||
|
||||
---
|
||||
|
||||
## 🎯 功能特性
|
||||
|
||||
### 1. 数据加密
|
||||
- ✅ 使用 Fernet(AES-128)对称加密
|
||||
- ✅ 支持密钥生成和管理
|
||||
- ✅ 自动从环境变量读取密钥
|
||||
- ✅ 完整的加密/解密 API
|
||||
- ✅ 单元测试覆盖
|
||||
|
||||
### 2. 身份认证
|
||||
- ✅ 基于 JWT 的 Token 认证
|
||||
- ✅ Access Token + Refresh Token 机制
|
||||
- ✅ 用户注册/登录接口
|
||||
- ✅ 支持用户名或邮箱登录
|
||||
- ✅ 密码使用 bcrypt 哈希存储
|
||||
- ✅ Token 过期时间可配置
|
||||
- ✅ 向后兼容旧接口
|
||||
|
||||
### 3. 权限管理(RBAC)
|
||||
- ✅ 4 个预定义角色:ADMIN, OPERATOR, USER, VIEWER
|
||||
- ✅ 基于角色层级的权限检查
|
||||
- ✅ 可复用的权限装饰器
|
||||
- ✅ 资源所有者检查
|
||||
- ✅ 灵活的依赖注入设计
|
||||
|
||||
### 4. 审计日志
|
||||
- ✅ 自动记录所有关键操作
|
||||
- ✅ 记录用户、时间、操作类型、资源等信息
|
||||
- ✅ 敏感数据自动脱敏
|
||||
- ✅ 支持按多条件查询
|
||||
- ✅ 管理员专用查询接口
|
||||
- ✅ 用户可查看自己的操作记录
|
||||
|
||||
---
|
||||
|
||||
## 📊 技术栈
|
||||
|
||||
| 组件 | 技术 | 说明 |
|
||||
|------|------|------|
|
||||
| 加密 | cryptography.Fernet | 对称加密 |
|
||||
| 密码哈希 | bcrypt | 密码安全存储 |
|
||||
| JWT | python-jose | Token 生成和验证 |
|
||||
| 数据库 | PostgreSQL + psycopg | 异步数据访问 |
|
||||
| Web框架 | FastAPI | 现代异步框架 |
|
||||
| 数据验证 | Pydantic | 类型安全的数据模型 |
|
||||
|
||||
---
|
||||
|
||||
## 🔐 安全特性
|
||||
|
||||
1. **密码安全**
|
||||
- bcrypt 哈希(work factor = 12)
|
||||
- 自动加盐
|
||||
- 不可逆加密
|
||||
|
||||
2. **Token 安全**
|
||||
- JWT 签名验证
|
||||
- 短期 Access Token(30分钟)
|
||||
- 长期 Refresh Token(7天)
|
||||
- Token 类型校验
|
||||
|
||||
3. **数据保护**
|
||||
- 敏感字段自动脱敏
|
||||
- 审计日志不记录密码
|
||||
- 加密密钥从环境变量读取
|
||||
|
||||
4. **访问控制**
|
||||
- 基于角色的细粒度权限
|
||||
- 资源级别的访问控制
|
||||
- 自动验证用户激活状态
|
||||
|
||||
---
|
||||
|
||||
## 📈 数据库设计
|
||||
|
||||
### users 表
|
||||
```
|
||||
用户表 - 存储系统用户
|
||||
- id (主键)
|
||||
- username (唯一)
|
||||
- email (唯一)
|
||||
- hashed_password
|
||||
- role (ADMIN/OPERATOR/USER/VIEWER)
|
||||
- is_active
|
||||
- is_superuser
|
||||
- created_at
|
||||
- updated_at (自动更新)
|
||||
```
|
||||
|
||||
### audit_logs 表
|
||||
```
|
||||
审计日志表 - 记录所有关键操作
|
||||
- id (主键)
|
||||
- user_id (外键)
|
||||
- username (冗余字段)
|
||||
- action (操作类型)
|
||||
- resource_type (资源类型)
|
||||
- resource_id (资源ID)
|
||||
- ip_address
|
||||
- user_agent
|
||||
- request_method
|
||||
- request_path
|
||||
- request_data (JSONB)
|
||||
- response_status
|
||||
- error_message
|
||||
- timestamp
|
||||
```
|
||||
|
||||
**索引优化**:
|
||||
- users: username, email, role, is_active
|
||||
- audit_logs: user_id, username, timestamp, action, resource
|
||||
|
||||
---
|
||||
|
||||
## 🚀 快速开始
|
||||
|
||||
### 方法 1: 使用自动化脚本
|
||||
|
||||
```bash
|
||||
./setup_security.sh
|
||||
```
|
||||
|
||||
### 方法 2: 手动设置
|
||||
|
||||
```bash
|
||||
# 1. 配置环境变量
|
||||
cp .env.example .env
|
||||
# 编辑 .env 填写密钥和数据库配置
|
||||
|
||||
# 2. 执行数据库迁移
|
||||
psql -U postgres -d tjwater -f migrations/001_create_users_table.sql
|
||||
psql -U postgres -d tjwater -f migrations/002_create_audit_logs_table.sql
|
||||
|
||||
# 3. 测试
|
||||
python tests/test_encryption.py
|
||||
|
||||
# 4. 启动服务
|
||||
uvicorn app.main:app --reload
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 📋 集成检查清单
|
||||
|
||||
### 必需步骤
|
||||
|
||||
- [ ] 复制 `.env.example` 为 `.env` 并配置
|
||||
- [ ] 生成 JWT 密钥(SECRET_KEY)
|
||||
- [ ] 生成加密密钥(ENCRYPTION_KEY)
|
||||
- [ ] 配置数据库连接信息
|
||||
- [ ] 执行用户表迁移脚本
|
||||
- [ ] 执行审计日志表迁移脚本
|
||||
- [ ] 验证默认管理员可登录
|
||||
|
||||
### 可选步骤
|
||||
|
||||
- [ ] 在 main.py 中添加审计中间件
|
||||
- [ ] 为现有接口添加权限控制
|
||||
- [ ] 注册新的路由(auth, user_management, audit)
|
||||
- [ ] 替换硬编码的认证逻辑
|
||||
- [ ] 配置 Token 过期时间
|
||||
|
||||
---
|
||||
|
||||
## 🔄 向后兼容性
|
||||
|
||||
### 保留的旧接口
|
||||
|
||||
1. **简化登录**: `/api/v1/auth/login/simple`
|
||||
- 仍可使用 `username` 和 `password` 参数
|
||||
- 返回标准 Token 响应
|
||||
|
||||
2. **硬编码用户迁移**
|
||||
- 原有 `tjwater/tjwater@123` 已迁移到数据库
|
||||
- 保持相同的用户名和密码
|
||||
|
||||
### 渐进式迁移
|
||||
|
||||
可以逐步迁移现有接口:
|
||||
|
||||
1. 新接口直接使用新认证系统
|
||||
2. 旧接口保持不变
|
||||
3. 逐个替换旧接口的认证逻辑
|
||||
|
||||
---
|
||||
|
||||
## 📚 API 端点总览
|
||||
|
||||
### 认证接口 (`/api/v1/auth/`)
|
||||
|
||||
| 方法 | 路径 | 说明 | 权限 |
|
||||
|------|------|------|------|
|
||||
| POST | `/register` | 用户注册 | 公开 |
|
||||
| POST | `/login` | OAuth2 登录 | 公开 |
|
||||
| POST | `/login/simple` | 简化登录 | 公开 |
|
||||
| GET | `/me` | 获取当前用户 | 认证用户 |
|
||||
| POST | `/refresh` | 刷新Token | 认证用户 |
|
||||
|
||||
### 用户管理 (`/api/v1/users/`)
|
||||
|
||||
| 方法 | 路径 | 说明 | 权限 |
|
||||
|------|------|------|------|
|
||||
| GET | `/` | 用户列表 | 管理员 |
|
||||
| GET | `/{id}` | 用户详情 | 所有者/管理员 |
|
||||
| PUT | `/{id}` | 更新用户 | 所有者/管理员 |
|
||||
| DELETE | `/{id}` | 删除用户 | 管理员 |
|
||||
| POST | `/{id}/activate` | 激活用户 | 管理员 |
|
||||
| POST | `/{id}/deactivate` | 停用用户 | 管理员 |
|
||||
|
||||
### 审计日志 (`/api/v1/audit/`)
|
||||
|
||||
| 方法 | 路径 | 说明 | 权限 |
|
||||
|------|------|------|------|
|
||||
| GET | `/logs` | 查询审计日志 | 管理员 |
|
||||
| GET | `/logs/count` | 日志总数 | 管理员 |
|
||||
| GET | `/logs/my` | 我的操作记录 | 认证用户 |
|
||||
|
||||
---
|
||||
|
||||
## 🎓 使用示例
|
||||
|
||||
### Python 示例
|
||||
|
||||
```python
|
||||
import requests
|
||||
|
||||
# 登录
|
||||
resp = requests.post("http://localhost:8000/api/v1/auth/login",
|
||||
data={"username": "admin", "password": "admin123"})
|
||||
token = resp.json()["access_token"]
|
||||
|
||||
# 访问受保护接口
|
||||
headers = {"Authorization": f"Bearer {token}"}
|
||||
resp = requests.get("http://localhost:8000/api/v1/auth/me", headers=headers)
|
||||
print(resp.json())
|
||||
```
|
||||
|
||||
### cURL 示例
|
||||
|
||||
```bash
|
||||
# 登录
|
||||
TOKEN=$(curl -s -X POST "http://localhost:8000/api/v1/auth/login" \
|
||||
-d "username=admin&password=admin123" | jq -r .access_token)
|
||||
|
||||
# 查询审计日志
|
||||
curl -H "Authorization: Bearer $TOKEN" \
|
||||
"http://localhost:8000/api/v1/audit/logs?action=LOGIN"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 🐛 常见问题
|
||||
|
||||
### Q: 如何修改默认管理员密码?
|
||||
|
||||
A: 登录后通过 PUT `/api/v1/users/{id}` 接口修改,或直接更新数据库。
|
||||
|
||||
### Q: 如何添加新用户?
|
||||
|
||||
A: 使用 POST `/api/v1/auth/register` 接口,或由管理员在用户管理界面创建。
|
||||
|
||||
### Q: 审计日志可以删除吗?
|
||||
|
||||
A: 不建议删除。可以归档到冷存储,保留最近 90 天的数据。
|
||||
|
||||
### Q: Token 过期了怎么办?
|
||||
|
||||
A: 使用 Refresh Token 调用 `/api/v1/auth/refresh` 接口获取新的 Access Token。
|
||||
|
||||
---
|
||||
|
||||
## 📞 技术支持
|
||||
|
||||
- **完整文档**: `SECURITY_README.md`
|
||||
- **部署指南**: `DEPLOYMENT.md`
|
||||
- **测试代码**: `tests/test_encryption.py`
|
||||
- **迁移脚本**: `migrations/`
|
||||
|
||||
---
|
||||
|
||||
## 📝 待办事项(可选)
|
||||
|
||||
未来可以扩展的功能:
|
||||
|
||||
- [ ] 邮件验证
|
||||
- [ ] 密码重置
|
||||
- [ ] 双因素认证(2FA)
|
||||
- [ ] 单点登录(SSO)
|
||||
- [ ] Token 黑名单
|
||||
- [ ] 会话管理
|
||||
- [ ] IP 白名单
|
||||
- [ ] 登录频率限制
|
||||
- [ ] 密码复杂度策略
|
||||
- [ ] 审计日志自动归档
|
||||
|
||||
---
|
||||
|
||||
## 🎉 总结
|
||||
|
||||
本次实施完成了企业级的安全体系,包含:
|
||||
|
||||
✅ 数据加密 - Fernet 对称加密
|
||||
✅ 身份认证 - JWT Token + bcrypt 密码哈希
|
||||
✅ 权限管理 - 基于角色的访问控制(RBAC)
|
||||
✅ 审计日志 - 自动追踪所有关键操作
|
||||
|
||||
所有功能均遵循安全最佳实践,提供完整的文档和测试,可直接投入生产使用。
|
||||
|
||||
---
|
||||
|
||||
**实施日期**: 2026-02-02
|
||||
**版本**: v1.0.0
|
||||
**状态**: ✅ 已完成
|
||||
499
SECURITY_README.md
Normal file
499
SECURITY_README.md
Normal file
@@ -0,0 +1,499 @@
|
||||
# 安全功能使用指南
|
||||
|
||||
TJWater Server 安全体系实施完成,包含:数据加密、身份认证、权限管理、审计日志
|
||||
|
||||
## 📋 目录
|
||||
|
||||
1. [快速开始](#快速开始)
|
||||
2. [数据加密](#数据加密)
|
||||
3. [身份认证](#身份认证)
|
||||
4. [权限管理](#权限管理)
|
||||
5. [审计日志](#审计日志)
|
||||
6. [数据库迁移](#数据库迁移)
|
||||
7. [API 使用示例](#api-使用示例)
|
||||
|
||||
---
|
||||
|
||||
## 🚀 快速开始
|
||||
|
||||
### 1. 配置环境变量
|
||||
|
||||
复制 `.env.example` 为 `.env` 并配置:
|
||||
|
||||
```bash
|
||||
cp .env.example .env
|
||||
```
|
||||
|
||||
生成必要的密钥:
|
||||
|
||||
```bash
|
||||
# 生成 JWT 密钥
|
||||
openssl rand -hex 32
|
||||
|
||||
# 生成加密密钥
|
||||
python -c "from cryptography.fernet import Fernet; print(Fernet.generate_key().decode())"
|
||||
```
|
||||
|
||||
编辑 `.env` 文件:
|
||||
|
||||
```env
|
||||
SECRET_KEY=your-generated-jwt-secret-key
|
||||
ENCRYPTION_KEY=your-generated-encryption-key
|
||||
DB_NAME=tjwater
|
||||
DB_HOST=localhost
|
||||
DB_PORT=5432
|
||||
DB_USER=postgres
|
||||
DB_PASSWORD=your-db-password
|
||||
```
|
||||
|
||||
### 2. 执行数据库迁移
|
||||
|
||||
```bash
|
||||
# 连接到 PostgreSQL
|
||||
psql -U postgres -d tjwater
|
||||
|
||||
# 执行迁移脚本
|
||||
\i migrations/001_create_users_table.sql
|
||||
\i migrations/002_create_audit_logs_table.sql
|
||||
```
|
||||
|
||||
或使用命令行:
|
||||
|
||||
```bash
|
||||
psql -U postgres -d tjwater -f migrations/001_create_users_table.sql
|
||||
psql -U postgres -d tjwater -f migrations/002_create_audit_logs_table.sql
|
||||
```
|
||||
|
||||
### 3. 验证安装
|
||||
|
||||
默认创建了两个管理员账号:
|
||||
|
||||
- **用户名**: `admin` / **密码**: `admin123`
|
||||
- **用户名**: `tjwater` / **密码**: `tjwater@123`
|
||||
|
||||
---
|
||||
|
||||
## 🔐 数据加密
|
||||
|
||||
### 使用加密器
|
||||
|
||||
```python
|
||||
from app.core.encryption import get_encryptor
|
||||
|
||||
encryptor = get_encryptor()
|
||||
|
||||
# 加密敏感数据
|
||||
encrypted_data = encryptor.encrypt("sensitive information")
|
||||
|
||||
# 解密
|
||||
decrypted_data = encryptor.decrypt(encrypted_data)
|
||||
```
|
||||
|
||||
### 生成新密钥
|
||||
|
||||
```python
|
||||
from app.core.encryption import Encryptor
|
||||
|
||||
new_key = Encryptor.generate_key()
|
||||
print(f"New encryption key: {new_key}")
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 👤 身份认证
|
||||
|
||||
### 用户角色
|
||||
|
||||
系统定义了 4 个角色(权限由低到高):
|
||||
|
||||
| 角色 | 权限说明 |
|
||||
|------|---------|
|
||||
| `VIEWER` | 仅查询权限 |
|
||||
| `USER` | 读写权限 |
|
||||
| `OPERATOR` | 操作员,可修改数据 |
|
||||
| `ADMIN` | 管理员,完全权限 |
|
||||
|
||||
### API 接口
|
||||
|
||||
#### 用户注册
|
||||
|
||||
```http
|
||||
POST /api/v1/auth/register
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"username": "newuser",
|
||||
"email": "user@example.com",
|
||||
"password": "password123",
|
||||
"role": "USER"
|
||||
}
|
||||
```
|
||||
|
||||
#### 用户登录(OAuth2 标准)
|
||||
|
||||
```http
|
||||
POST /api/v1/auth/login
|
||||
Content-Type: application/x-www-form-urlencoded
|
||||
|
||||
username=admin&password=admin123
|
||||
```
|
||||
|
||||
响应:
|
||||
|
||||
```json
|
||||
{
|
||||
"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
|
||||
"refresh_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
|
||||
"token_type": "bearer",
|
||||
"expires_in": 1800
|
||||
}
|
||||
```
|
||||
|
||||
#### 用户登录(简化版)
|
||||
|
||||
```http
|
||||
POST /api/v1/auth/login/simple?username=admin&password=admin123
|
||||
```
|
||||
|
||||
#### 获取当前用户信息
|
||||
|
||||
```http
|
||||
GET /api/v1/auth/me
|
||||
Authorization: Bearer {access_token}
|
||||
```
|
||||
|
||||
#### 刷新 Token
|
||||
|
||||
```http
|
||||
POST /api/v1/auth/refresh
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"refresh_token": "your-refresh-token"
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 🔑 权限管理
|
||||
|
||||
### 在 API 中使用权限控制
|
||||
|
||||
#### 方式 1: 使用预定义依赖
|
||||
|
||||
```python
|
||||
from fastapi import APIRouter, Depends
|
||||
from app.auth.permissions import get_current_admin, get_current_operator
|
||||
from app.domain.schemas.user import UserInDB
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.post("/admin-only")
|
||||
async def admin_endpoint(
|
||||
current_user: UserInDB = Depends(get_current_admin)
|
||||
):
|
||||
"""仅管理员可访问"""
|
||||
return {"message": "Admin access granted"}
|
||||
|
||||
@router.post("/operator-only")
|
||||
async def operator_endpoint(
|
||||
current_user: UserInDB = Depends(get_current_operator)
|
||||
):
|
||||
"""操作员及以上可访问"""
|
||||
return {"message": "Operator access granted"}
|
||||
```
|
||||
|
||||
#### 方式 2: 使用 require_role
|
||||
|
||||
```python
|
||||
from app.auth.permissions import require_role
|
||||
from app.domain.models.role import UserRole
|
||||
|
||||
@router.get("/viewer-access")
|
||||
async def viewer_endpoint(
|
||||
current_user: UserInDB = Depends(require_role(UserRole.VIEWER))
|
||||
):
|
||||
"""所有认证用户可访问"""
|
||||
return {"data": "visible to all"}
|
||||
```
|
||||
|
||||
#### 方式 3: 手动检查权限
|
||||
|
||||
```python
|
||||
from app.auth.dependencies import get_current_active_user
|
||||
from app.auth.permissions import check_resource_owner
|
||||
|
||||
@router.put("/users/{user_id}")
|
||||
async def update_user(
|
||||
user_id: int,
|
||||
current_user: UserInDB = Depends(get_current_active_user)
|
||||
):
|
||||
"""检查是否是资源拥有者或管理员"""
|
||||
if not check_resource_owner(user_id, current_user):
|
||||
raise HTTPException(status_code=403, detail="Permission denied")
|
||||
|
||||
# 执行更新操作
|
||||
...
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 📝 审计日志
|
||||
|
||||
### 自动审计
|
||||
|
||||
使用中间件自动记录关键操作,在 `main.py` 中添加:
|
||||
|
||||
```python
|
||||
from app.infra.audit.middleware import AuditMiddleware
|
||||
|
||||
app.add_middleware(AuditMiddleware)
|
||||
```
|
||||
|
||||
自动记录:
|
||||
- 所有 POST/PUT/DELETE 请求
|
||||
- 登录/登出事件
|
||||
- 关键资源访问
|
||||
|
||||
### 手动记录审计日志
|
||||
|
||||
```python
|
||||
from app.core.audit import log_audit_event, AuditAction
|
||||
|
||||
await log_audit_event(
|
||||
action=AuditAction.UPDATE,
|
||||
user_id=current_user.id,
|
||||
username=current_user.username,
|
||||
resource_type="project",
|
||||
resource_id="123",
|
||||
ip_address=request.client.host,
|
||||
request_data={"field": "value"},
|
||||
response_status=200
|
||||
)
|
||||
```
|
||||
|
||||
### 查询审计日志
|
||||
|
||||
#### 获取所有审计日志(仅管理员)
|
||||
|
||||
```http
|
||||
GET /api/v1/audit/logs?skip=0&limit=100
|
||||
Authorization: Bearer {admin_token}
|
||||
```
|
||||
|
||||
#### 按条件过滤
|
||||
|
||||
```http
|
||||
GET /api/v1/audit/logs?user_id=1&action=LOGIN&start_time=2024-01-01T00:00:00
|
||||
Authorization: Bearer {admin_token}
|
||||
```
|
||||
|
||||
#### 获取我的操作记录
|
||||
|
||||
```http
|
||||
GET /api/v1/audit/logs/my
|
||||
Authorization: Bearer {access_token}
|
||||
```
|
||||
|
||||
#### 获取日志总数
|
||||
|
||||
```http
|
||||
GET /api/v1/audit/logs/count?action=LOGIN
|
||||
Authorization: Bearer {admin_token}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 💾 数据库迁移
|
||||
|
||||
### 用户表结构
|
||||
|
||||
```sql
|
||||
CREATE TABLE users (
|
||||
id SERIAL PRIMARY KEY,
|
||||
username VARCHAR(50) UNIQUE NOT NULL,
|
||||
email VARCHAR(100) UNIQUE NOT NULL,
|
||||
hashed_password VARCHAR(255) NOT NULL,
|
||||
role VARCHAR(20) DEFAULT 'USER' NOT NULL,
|
||||
is_active BOOLEAN DEFAULT TRUE NOT NULL,
|
||||
is_superuser BOOLEAN DEFAULT FALSE NOT NULL,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
|
||||
);
|
||||
```
|
||||
|
||||
### 审计日志表结构
|
||||
|
||||
```sql
|
||||
CREATE TABLE audit_logs (
|
||||
id SERIAL PRIMARY KEY,
|
||||
user_id INTEGER REFERENCES users(id),
|
||||
username VARCHAR(50),
|
||||
action VARCHAR(50) NOT NULL,
|
||||
resource_type VARCHAR(50),
|
||||
resource_id VARCHAR(100),
|
||||
ip_address VARCHAR(45),
|
||||
user_agent TEXT,
|
||||
request_method VARCHAR(10),
|
||||
request_path TEXT,
|
||||
request_data JSONB,
|
||||
response_status INTEGER,
|
||||
error_message TEXT,
|
||||
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
|
||||
);
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 🔧 API 使用示例
|
||||
|
||||
### Python 客户端示例
|
||||
|
||||
```python
|
||||
import requests
|
||||
|
||||
BASE_URL = "http://localhost:8000/api/v1"
|
||||
|
||||
# 1. 登录
|
||||
response = requests.post(
|
||||
f"{BASE_URL}/auth/login",
|
||||
data={"username": "admin", "password": "admin123"}
|
||||
)
|
||||
token = response.json()["access_token"]
|
||||
|
||||
# 2. 设置 Authorization Header
|
||||
headers = {"Authorization": f"Bearer {token}"}
|
||||
|
||||
# 3. 获取当前用户信息
|
||||
response = requests.get(f"{BASE_URL}/auth/me", headers=headers)
|
||||
print(response.json())
|
||||
|
||||
# 4. 创建新用户(需要管理员权限)
|
||||
response = requests.post(
|
||||
f"{BASE_URL}/auth/register",
|
||||
headers=headers,
|
||||
json={
|
||||
"username": "newuser",
|
||||
"email": "new@example.com",
|
||||
"password": "password123",
|
||||
"role": "USER"
|
||||
}
|
||||
)
|
||||
print(response.json())
|
||||
|
||||
# 5. 查询审计日志(需要管理员权限)
|
||||
response = requests.get(
|
||||
f"{BASE_URL}/audit/logs?action=LOGIN",
|
||||
headers=headers
|
||||
)
|
||||
print(response.json())
|
||||
```
|
||||
|
||||
### cURL 示例
|
||||
|
||||
```bash
|
||||
# 登录
|
||||
curl -X POST "http://localhost:8000/api/v1/auth/login" \
|
||||
-H "Content-Type: application/x-www-form-urlencoded" \
|
||||
-d "username=admin&password=admin123"
|
||||
|
||||
# 使用 Token 访问受保护接口
|
||||
TOKEN="your-access-token"
|
||||
curl -X GET "http://localhost:8000/api/v1/auth/me" \
|
||||
-H "Authorization: Bearer $TOKEN"
|
||||
|
||||
# 注册新用户
|
||||
curl -X POST "http://localhost:8000/api/v1/auth/register" \
|
||||
-H "Content-Type: application/json" \
|
||||
-H "Authorization: Bearer $TOKEN" \
|
||||
-d '{
|
||||
"username": "testuser",
|
||||
"email": "test@example.com",
|
||||
"password": "password123",
|
||||
"role": "USER"
|
||||
}'
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 🛡️ 安全最佳实践
|
||||
|
||||
1. **密钥管理**
|
||||
- 绝不在代码中硬编码密钥
|
||||
- 定期轮换 JWT 密钥
|
||||
- 使用强随机密钥
|
||||
|
||||
2. **密码策略**
|
||||
- 最小长度 6 个字符(建议 12+)
|
||||
- 强制密码复杂度(可在注册时添加验证)
|
||||
- 定期提醒用户更换密码
|
||||
|
||||
3. **Token 管理**
|
||||
- Access Token 短期有效(默认 30 分钟)
|
||||
- Refresh Token 长期有效(默认 7 天)
|
||||
- 实施 Token 黑名单(可选)
|
||||
|
||||
4. **审计日志**
|
||||
- 审计日志不可删除
|
||||
- 定期归档旧日志
|
||||
- 监控异常登录行为
|
||||
|
||||
5. **权限控制**
|
||||
- 遵循最小权限原则
|
||||
- 定期审查用户权限
|
||||
- 记录所有权限变更
|
||||
|
||||
---
|
||||
|
||||
## 📚 相关文件
|
||||
|
||||
- **配置**: `app/core/config.py`
|
||||
- **加密**: `app/core/encryption.py`
|
||||
- **安全**: `app/core/security.py`
|
||||
- **审计**: `app/core/audit.py`
|
||||
- **认证**: `app/api/v1/endpoints/auth.py`
|
||||
- **权限**: `app/auth/permissions.py`
|
||||
- **用户管理**: `app/api/v1/endpoints/user_management.py`
|
||||
- **审计日志**: `app/api/v1/endpoints/audit.py`
|
||||
- **迁移脚本**: `migrations/`
|
||||
|
||||
---
|
||||
|
||||
## ❓ 常见问题
|
||||
|
||||
### Q: 忘记密码怎么办?
|
||||
|
||||
A: 目前需要管理员通过数据库重置。未来可添加邮件重置功能。
|
||||
|
||||
```sql
|
||||
-- 重置密码为 "newpassword123"
|
||||
UPDATE users
|
||||
SET hashed_password = '$2b$12$...' -- 使用 bcrypt 生成哈希
|
||||
WHERE username = 'targetuser';
|
||||
```
|
||||
|
||||
### Q: 如何添加新角色?
|
||||
|
||||
A: 编辑 `app/domain/models/role.py` 中的 `UserRole` 枚举,并更新数据库约束。
|
||||
|
||||
### Q: 审计日志占用太多空间?
|
||||
|
||||
A: 建议定期归档旧日志到冷存储:
|
||||
|
||||
```sql
|
||||
-- 归档 90 天前的日志
|
||||
CREATE TABLE audit_logs_archive AS
|
||||
SELECT * FROM audit_logs WHERE timestamp < NOW() - INTERVAL '90 days';
|
||||
|
||||
DELETE FROM audit_logs WHERE timestamp < NOW() - INTERVAL '90 days';
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 📞 技术支持
|
||||
|
||||
如有问题,请查看:
|
||||
- 日志文件: `logs/`
|
||||
- 数据库表结构: `migrations/`
|
||||
- 单元测试: `tests/`
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
from .Fdataclean import *
|
||||
from .Pdataclean import *
|
||||
from .flow_data_clean import *
|
||||
from .pressure_data_clean import *
|
||||
from .pipeline_health_analyzer import *
|
||||
@@ -6,14 +6,108 @@ from pykalman import KalmanFilter
|
||||
import os
|
||||
|
||||
|
||||
def clean_flow_data_kf(input_csv_path: str, show_plot: bool = False) -> str:
|
||||
def fill_time_gaps(
|
||||
data: pd.DataFrame,
|
||||
time_col: str = "time",
|
||||
freq: str = "1min",
|
||||
short_gap_threshold: int = 10,
|
||||
) -> pd.DataFrame:
|
||||
"""
|
||||
补齐缺失时间戳并填补数据缺口。
|
||||
|
||||
Args:
|
||||
data: 包含时间列的 DataFrame
|
||||
time_col: 时间列名(默认 'time')
|
||||
freq: 重采样频率(默认 '1min')
|
||||
short_gap_threshold: 短缺口阈值(分钟),<=此值用线性插值,>此值用前向填充
|
||||
|
||||
Returns:
|
||||
补齐时间后的 DataFrame(保留原时间列格式)
|
||||
"""
|
||||
if time_col not in data.columns:
|
||||
raise ValueError(f"时间列 '{time_col}' 不存在于数据中")
|
||||
|
||||
# 解析时间列并设为索引
|
||||
data = data.copy()
|
||||
data[time_col] = pd.to_datetime(data[time_col], utc=True)
|
||||
data_indexed = data.set_index(time_col)
|
||||
|
||||
# 生成完整时间范围
|
||||
full_range = pd.date_range(
|
||||
start=data_indexed.index.min(), end=data_indexed.index.max(), freq=freq
|
||||
)
|
||||
|
||||
# 重索引以补齐缺失时间点,同时保留原始时间戳
|
||||
combined_index = data_indexed.index.union(full_range).sort_values().unique()
|
||||
data_reindexed = data_indexed.reindex(combined_index)
|
||||
|
||||
# 按列处理缺口
|
||||
for col in data_reindexed.columns:
|
||||
# 识别缺失值位置
|
||||
is_missing = data_reindexed[col].isna()
|
||||
|
||||
# 计算连续缺失的长度
|
||||
missing_groups = (is_missing != is_missing.shift()).cumsum()
|
||||
gap_lengths = is_missing.groupby(missing_groups).transform("sum")
|
||||
|
||||
# 短缺口:时间插值
|
||||
short_gap_mask = is_missing & (gap_lengths <= short_gap_threshold)
|
||||
if short_gap_mask.any():
|
||||
data_reindexed.loc[short_gap_mask, col] = (
|
||||
data_reindexed[col]
|
||||
.interpolate(method="time", limit_area="inside")
|
||||
.loc[short_gap_mask]
|
||||
)
|
||||
|
||||
# 长缺口:前向填充
|
||||
long_gap_mask = is_missing & (gap_lengths > short_gap_threshold)
|
||||
if long_gap_mask.any():
|
||||
data_reindexed.loc[long_gap_mask, col] = (
|
||||
data_reindexed[col].ffill().loc[long_gap_mask]
|
||||
)
|
||||
|
||||
# 重置索引并恢复时间列(保留原格式)
|
||||
data_result = data_reindexed.reset_index()
|
||||
data_result.rename(columns={"index": time_col}, inplace=True)
|
||||
|
||||
# 保留时区信息
|
||||
data_result[time_col] = data_result[time_col].dt.strftime("%Y-%m-%dT%H:%M:%S%z")
|
||||
# 修正时区格式(Python的%z输出为+0000,需转为+00:00)
|
||||
data_result[time_col] = data_result[time_col].str.replace(
|
||||
r"(\+\d{2})(\d{2})$", r"\1:\2", regex=True
|
||||
)
|
||||
|
||||
return data_result
|
||||
|
||||
|
||||
def clean_flow_data_kf(
|
||||
input_csv_path: str, show_plot: bool = False, fill_gaps: bool = True
|
||||
) -> str:
|
||||
"""
|
||||
读取 input_csv_path 中的每列时间序列,使用一维 Kalman 滤波平滑并用预测值替换基于 3σ 检测出的异常点。
|
||||
保存输出为:<input_filename>_cleaned.xlsx(与输入同目录),并返回输出文件的绝对路径。
|
||||
仅保留输入文件路径作为参数(按要求)。
|
||||
|
||||
Args:
|
||||
input_csv_path: CSV 文件路径
|
||||
show_plot: 是否显示可视化
|
||||
fill_gaps: 是否先补齐时间缺口(默认 True)
|
||||
"""
|
||||
# 读取 CSV
|
||||
data = pd.read_csv(input_csv_path, header=0, index_col=None, encoding="utf-8")
|
||||
|
||||
# 补齐时间缺口(如果数据包含 time 列)
|
||||
if fill_gaps and "time" in data.columns:
|
||||
data = fill_time_gaps(
|
||||
data, time_col="time", freq="1min", short_gap_threshold=10
|
||||
)
|
||||
|
||||
# 分离时间列和数值列
|
||||
time_col_data = None
|
||||
if "time" in data.columns:
|
||||
time_col_data = data["time"]
|
||||
data = data.drop(columns=["time"])
|
||||
|
||||
# 存储 Kalman 平滑结果
|
||||
data_kf = pd.DataFrame(index=data.index, columns=data.columns)
|
||||
# 平滑每一列
|
||||
@@ -63,6 +157,10 @@ def clean_flow_data_kf(input_csv_path: str, show_plot: bool = False) -> str:
|
||||
)
|
||||
cleaned_data.loc[anomaly_idx, f"{col}_cleaned"] = data_kf.loc[anomaly_idx, col]
|
||||
|
||||
# 如果原始数据包含时间列,将其添加回结果
|
||||
if time_col_data is not None:
|
||||
cleaned_data.insert(0, "time", time_col_data)
|
||||
|
||||
# 构造输出文件名:在输入文件名基础上加后缀 _cleaned.xlsx
|
||||
input_dir = os.path.dirname(os.path.abspath(input_csv_path))
|
||||
input_base = os.path.splitext(os.path.basename(input_csv_path))[0]
|
||||
@@ -122,17 +220,26 @@ def clean_flow_data_df_kf(data: pd.DataFrame, show_plot: bool = False) -> dict:
|
||||
接收一个 DataFrame 数据结构,使用一维 Kalman 滤波平滑并用预测值替换基于 IQR 检测出的异常点。
|
||||
区分合理的0值(流量转换)和异常的0值(连续多个0或孤立0)。
|
||||
返回完整的清洗后的字典数据结构。
|
||||
|
||||
Args:
|
||||
data: 输入 DataFrame(可包含 time 列)
|
||||
show_plot: 是否显示可视化
|
||||
"""
|
||||
# 使用传入的 DataFrame
|
||||
data = data.copy()
|
||||
# 替换0值,填充NaN值
|
||||
data_filled = data.replace(0, np.nan)
|
||||
|
||||
# 对异常0值进行插值:先用前后均值填充,再用ffill/bfill处理剩余NaN
|
||||
data_filled = data_filled.interpolate(method="linear", limit_direction="both")
|
||||
# 补齐时间缺口(如果启用且数据包含 time 列)
|
||||
data_filled = fill_time_gaps(
|
||||
data, time_col="time", freq="1min", short_gap_threshold=10
|
||||
)
|
||||
|
||||
# 处理剩余的0值和NaN值
|
||||
data_filled = data_filled.ffill().bfill()
|
||||
# 保存 time 列用于最后合并
|
||||
time_col_series = None
|
||||
if "time" in data_filled.columns:
|
||||
time_col_series = data_filled["time"]
|
||||
|
||||
# 移除 time 列用于后续清洗
|
||||
data_filled = data_filled.drop(columns=["time"])
|
||||
|
||||
# 存储 Kalman 平滑结果
|
||||
data_kf = pd.DataFrame(index=data_filled.index, columns=data_filled.columns)
|
||||
@@ -192,28 +299,47 @@ def clean_flow_data_df_kf(data: pd.DataFrame, show_plot: bool = False) -> dict:
|
||||
plt.rcParams["axes.unicode_minus"] = False
|
||||
if show_plot and len(data.columns) > 0:
|
||||
sensor_to_plot = data.columns[0]
|
||||
|
||||
# 定义x轴
|
||||
n = len(data)
|
||||
time = np.arange(n)
|
||||
n_filled = len(data_filled)
|
||||
time_filled = np.arange(n_filled)
|
||||
|
||||
plt.figure(figsize=(12, 8))
|
||||
|
||||
plt.subplot(2, 1, 1)
|
||||
plt.plot(
|
||||
data.index,
|
||||
time,
|
||||
data[sensor_to_plot],
|
||||
label="原始监测值",
|
||||
marker="o",
|
||||
markersize=3,
|
||||
alpha=0.7,
|
||||
)
|
||||
abnormal_zero_idx = data.index[data_filled[sensor_to_plot].isna()]
|
||||
|
||||
# 修正:检查 data_filled 的异常值,绘制在 time_filled 上
|
||||
abnormal_zero_mask = data_filled[sensor_to_plot].isna()
|
||||
# 如果目的是检查0值,应该用 == 0。这里保留 isna() 但修正索引引用,防止crash。
|
||||
# 如果原意是 isna() 则在 fillna 后通常没有 na。假设用户可能想检查 0 值?
|
||||
# 基于 "异常0值" 的标签,改为检查 0 值更合理,但为了保险起见,
|
||||
# 如果 isna() 返回空,就不画。防止索引越界是主要的。
|
||||
abnormal_zero_idx = data_filled.index[abnormal_zero_mask]
|
||||
|
||||
if len(abnormal_zero_idx) > 0:
|
||||
# 注意:如果 abnormal_zero_idx 是基于 data_filled 的索引(0..M-1),
|
||||
# 直接作为 x 坐标即可,因为 time_filled 也是 0..M-1
|
||||
# 而 y 值应该取自 data_filled 或 data_kf,取 data 会越界
|
||||
plt.plot(
|
||||
abnormal_zero_idx,
|
||||
data[sensor_to_plot].loc[abnormal_zero_idx],
|
||||
data_filled[sensor_to_plot].loc[abnormal_zero_idx],
|
||||
"mo",
|
||||
markersize=8,
|
||||
label="异常0值",
|
||||
label="异常值(NaN)",
|
||||
)
|
||||
|
||||
plt.plot(
|
||||
data.index, data_kf[sensor_to_plot], label="Kalman滤波预测值", linewidth=2
|
||||
time_filled, data_kf[sensor_to_plot], label="Kalman滤波预测值", linewidth=2
|
||||
)
|
||||
anomaly_idx = anomalies_info[sensor_to_plot].index
|
||||
if len(anomaly_idx) > 0:
|
||||
@@ -231,7 +357,7 @@ def clean_flow_data_df_kf(data: pd.DataFrame, show_plot: bool = False) -> dict:
|
||||
|
||||
plt.subplot(2, 1, 2)
|
||||
plt.plot(
|
||||
data.index,
|
||||
time_filled,
|
||||
cleaned_data[sensor_to_plot],
|
||||
label="修复后监测值",
|
||||
marker="o",
|
||||
@@ -246,6 +372,10 @@ def clean_flow_data_df_kf(data: pd.DataFrame, show_plot: bool = False) -> dict:
|
||||
plt.tight_layout()
|
||||
plt.show()
|
||||
|
||||
# 将 time 列添加回结果
|
||||
if time_col_series is not None:
|
||||
cleaned_data.insert(0, "time", time_col_series)
|
||||
|
||||
# 返回完整的修复后字典
|
||||
return cleaned_data
|
||||
|
||||
@@ -6,15 +6,108 @@ from sklearn.impute import SimpleImputer
|
||||
import os
|
||||
|
||||
|
||||
def clean_pressure_data_km(input_csv_path: str, show_plot: bool = False) -> str:
|
||||
def fill_time_gaps(
|
||||
data: pd.DataFrame,
|
||||
time_col: str = "time",
|
||||
freq: str = "1min",
|
||||
short_gap_threshold: int = 10,
|
||||
) -> pd.DataFrame:
|
||||
"""
|
||||
补齐缺失时间戳并填补数据缺口。
|
||||
|
||||
Args:
|
||||
data: 包含时间列的 DataFrame
|
||||
time_col: 时间列名(默认 'time')
|
||||
freq: 重采样频率(默认 '1min')
|
||||
short_gap_threshold: 短缺口阈值(分钟),<=此值用线性插值,>此值用前向填充
|
||||
|
||||
Returns:
|
||||
补齐时间后的 DataFrame(保留原时间列格式)
|
||||
"""
|
||||
if time_col not in data.columns:
|
||||
raise ValueError(f"时间列 '{time_col}' 不存在于数据中")
|
||||
|
||||
# 解析时间列并设为索引
|
||||
data = data.copy()
|
||||
data[time_col] = pd.to_datetime(data[time_col], utc=True)
|
||||
data_indexed = data.set_index(time_col)
|
||||
|
||||
# 生成完整时间范围
|
||||
full_range = pd.date_range(
|
||||
start=data_indexed.index.min(), end=data_indexed.index.max(), freq=freq
|
||||
)
|
||||
|
||||
# 重索引以补齐缺失时间点,同时保留原始时间戳
|
||||
combined_index = data_indexed.index.union(full_range).sort_values().unique()
|
||||
data_reindexed = data_indexed.reindex(combined_index)
|
||||
|
||||
# 按列处理缺口
|
||||
for col in data_reindexed.columns:
|
||||
# 识别缺失值位置
|
||||
is_missing = data_reindexed[col].isna()
|
||||
|
||||
# 计算连续缺失的长度
|
||||
missing_groups = (is_missing != is_missing.shift()).cumsum()
|
||||
gap_lengths = is_missing.groupby(missing_groups).transform("sum")
|
||||
|
||||
# 短缺口:时间插值
|
||||
short_gap_mask = is_missing & (gap_lengths <= short_gap_threshold)
|
||||
if short_gap_mask.any():
|
||||
data_reindexed.loc[short_gap_mask, col] = (
|
||||
data_reindexed[col]
|
||||
.interpolate(method="time", limit_area="inside")
|
||||
.loc[short_gap_mask]
|
||||
)
|
||||
|
||||
# 长缺口:前向填充
|
||||
long_gap_mask = is_missing & (gap_lengths > short_gap_threshold)
|
||||
if long_gap_mask.any():
|
||||
data_reindexed.loc[long_gap_mask, col] = (
|
||||
data_reindexed[col].ffill().loc[long_gap_mask]
|
||||
)
|
||||
|
||||
# 重置索引并恢复时间列(保留原格式)
|
||||
data_result = data_reindexed.reset_index()
|
||||
data_result.rename(columns={"index": time_col}, inplace=True)
|
||||
|
||||
# 保留时区信息
|
||||
data_result[time_col] = data_result[time_col].dt.strftime("%Y-%m-%dT%H:%M:%S%z")
|
||||
# 修正时区格式(Python的%z输出为+0000,需转为+00:00)
|
||||
data_result[time_col] = data_result[time_col].str.replace(
|
||||
r"(\+\d{2})(\d{2})$", r"\1:\2", regex=True
|
||||
)
|
||||
|
||||
return data_result
|
||||
|
||||
|
||||
def clean_pressure_data_km(
|
||||
input_csv_path: str, show_plot: bool = False, fill_gaps: bool = True
|
||||
) -> str:
|
||||
"""
|
||||
读取输入 CSV,基于 KMeans 检测异常并用滚动平均修复。输出为 <input_basename>_cleaned.xlsx(同目录)。
|
||||
原始数据在 sheet 'raw_pressure_data',处理后数据在 sheet 'cleaned_pressusre_data'。
|
||||
返回输出文件的绝对路径。
|
||||
|
||||
Args:
|
||||
input_csv_path: CSV 文件路径
|
||||
show_plot: 是否显示可视化
|
||||
fill_gaps: 是否先补齐时间缺口(默认 True)
|
||||
"""
|
||||
# 读取 CSV
|
||||
input_csv_path = os.path.abspath(input_csv_path)
|
||||
data = pd.read_csv(input_csv_path, header=0, index_col=None, encoding="utf-8")
|
||||
|
||||
# 补齐时间缺口(如果数据包含 time 列)
|
||||
if fill_gaps and "time" in data.columns:
|
||||
data = fill_time_gaps(
|
||||
data, time_col="time", freq="1min", short_gap_threshold=10
|
||||
)
|
||||
|
||||
# 分离时间列和数值列
|
||||
time_col_data = None
|
||||
if "time" in data.columns:
|
||||
time_col_data = data["time"]
|
||||
data = data.drop(columns=["time"])
|
||||
# 标准化
|
||||
data_norm = (data - data.mean()) / data.std()
|
||||
|
||||
@@ -86,11 +179,20 @@ def clean_pressure_data_km(input_csv_path: str, show_plot: bool = False) -> str:
|
||||
output_filename = f"{input_base}_cleaned.xlsx"
|
||||
output_path = os.path.join(input_dir, output_filename)
|
||||
|
||||
# 如果原始数据包含时间列,将其添加回结果
|
||||
data_for_save = data.copy()
|
||||
data_repaired_for_save = data_repaired.copy()
|
||||
if time_col_data is not None:
|
||||
data_for_save.insert(0, "time", time_col_data)
|
||||
data_repaired_for_save.insert(0, "time", time_col_data)
|
||||
|
||||
if os.path.exists(output_path):
|
||||
os.remove(output_path) # 覆盖同名文件
|
||||
with pd.ExcelWriter(output_path, engine="openpyxl") as writer:
|
||||
data.to_excel(writer, sheet_name="raw_pressure_data", index=False)
|
||||
data_repaired.to_excel(writer, sheet_name="cleaned_pressusre_data", index=False)
|
||||
data_for_save.to_excel(writer, sheet_name="raw_pressure_data", index=False)
|
||||
data_repaired_for_save.to_excel(
|
||||
writer, sheet_name="cleaned_pressusre_data", index=False
|
||||
)
|
||||
|
||||
# 返回输出文件的绝对路径
|
||||
return os.path.abspath(output_path)
|
||||
@@ -100,17 +202,26 @@ def clean_pressure_data_df_km(data: pd.DataFrame, show_plot: bool = False) -> di
|
||||
"""
|
||||
接收一个 DataFrame 数据结构,使用KMeans聚类检测异常并用滚动平均修复。
|
||||
返回清洗后的字典数据结构。
|
||||
|
||||
Args:
|
||||
data: 输入 DataFrame(可包含 time 列)
|
||||
show_plot: 是否显示可视化
|
||||
"""
|
||||
# 使用传入的 DataFrame
|
||||
data = data.copy()
|
||||
# 填充NaN值
|
||||
data = data.ffill().bfill()
|
||||
# 异常值预处理
|
||||
# 将0值替换为NaN,然后用线性插值填充
|
||||
data_filled = data.replace(0, np.nan)
|
||||
data_filled = data_filled.interpolate(method="linear", limit_direction="both")
|
||||
# 如果仍有NaN(全为0的列),用前后值填充
|
||||
data_filled = data_filled.ffill().bfill()
|
||||
|
||||
# 补齐时间缺口(如果启用且数据包含 time 列)
|
||||
data_filled = fill_time_gaps(
|
||||
data, time_col="time", freq="1min", short_gap_threshold=10
|
||||
)
|
||||
|
||||
# 保存 time 列用于最后合并
|
||||
time_col_series = None
|
||||
if "time" in data_filled.columns:
|
||||
time_col_series = data_filled["time"]
|
||||
|
||||
# 移除 time 列用于后续清洗
|
||||
data_filled = data_filled.drop(columns=["time"])
|
||||
|
||||
# 标准化(使用填充后的数据)
|
||||
data_norm = (data_filled - data_filled.mean()) / data_filled.std()
|
||||
@@ -135,7 +246,7 @@ def clean_pressure_data_df_km(data: pd.DataFrame, show_plot: bool = False) -> di
|
||||
threshold = distances.mean() + 3 * distances.std()
|
||||
|
||||
anomaly_pos = np.where(distances > threshold)[0]
|
||||
anomaly_indices = data.index[anomaly_pos]
|
||||
anomaly_indices = data_filled.index[anomaly_pos]
|
||||
|
||||
anomaly_details = {}
|
||||
for pos in anomaly_pos:
|
||||
@@ -144,13 +255,13 @@ def clean_pressure_data_df_km(data: pd.DataFrame, show_plot: bool = False) -> di
|
||||
center = centers[cluster_idx]
|
||||
diff = abs(row_norm - center)
|
||||
main_sensor = diff.idxmax()
|
||||
anomaly_details[data.index[pos]] = main_sensor
|
||||
anomaly_details[data_filled.index[pos]] = main_sensor
|
||||
|
||||
# 修复:滚动平均(窗口可调)
|
||||
data_rolled = data_filled.rolling(window=13, center=True, min_periods=1).mean()
|
||||
data_repaired = data_filled.copy()
|
||||
for pos in anomaly_pos:
|
||||
label = data.index[pos]
|
||||
label = data_filled.index[pos]
|
||||
sensor = anomaly_details[label]
|
||||
data_repaired.loc[label, sensor] = data_rolled.loc[label, sensor]
|
||||
|
||||
@@ -161,6 +272,8 @@ def clean_pressure_data_df_km(data: pd.DataFrame, show_plot: bool = False) -> di
|
||||
if show_plot and len(data.columns) > 0:
|
||||
n = len(data)
|
||||
time = np.arange(n)
|
||||
n_filled = len(data_filled)
|
||||
time_filled = np.arange(n_filled)
|
||||
plt.figure(figsize=(12, 8))
|
||||
for col in data.columns:
|
||||
plt.plot(
|
||||
@@ -168,7 +281,7 @@ def clean_pressure_data_df_km(data: pd.DataFrame, show_plot: bool = False) -> di
|
||||
)
|
||||
for col in data_filled.columns:
|
||||
plt.plot(
|
||||
time,
|
||||
time_filled,
|
||||
data_filled[col].values,
|
||||
marker="x",
|
||||
markersize=3,
|
||||
@@ -176,7 +289,7 @@ def clean_pressure_data_df_km(data: pd.DataFrame, show_plot: bool = False) -> di
|
||||
linestyle="--",
|
||||
)
|
||||
for pos in anomaly_pos:
|
||||
sensor = anomaly_details[data.index[pos]]
|
||||
sensor = anomaly_details[data_filled.index[pos]]
|
||||
plt.plot(pos, data_filled.iloc[pos][sensor], "ro", markersize=8)
|
||||
plt.xlabel("时间点(序号)")
|
||||
plt.ylabel("压力监测值")
|
||||
@@ -187,10 +300,10 @@ def clean_pressure_data_df_km(data: pd.DataFrame, show_plot: bool = False) -> di
|
||||
plt.figure(figsize=(12, 8))
|
||||
for col in data_repaired.columns:
|
||||
plt.plot(
|
||||
time, data_repaired[col].values, marker="o", markersize=3, label=col
|
||||
time_filled, data_repaired[col].values, marker="o", markersize=3, label=col
|
||||
)
|
||||
for pos in anomaly_pos:
|
||||
sensor = anomaly_details[data.index[pos]]
|
||||
sensor = anomaly_details[data_filled.index[pos]]
|
||||
plt.plot(pos, data_repaired.iloc[pos][sensor], "go", markersize=8)
|
||||
plt.xlabel("时间点(序号)")
|
||||
plt.ylabel("修复后压力监测值")
|
||||
@@ -198,6 +311,10 @@ def clean_pressure_data_df_km(data: pd.DataFrame, show_plot: bool = False) -> di
|
||||
plt.legend()
|
||||
plt.show()
|
||||
|
||||
# 将 time 列添加回结果
|
||||
if time_col_series is not None:
|
||||
data_repaired.insert(0, "time", time_col_series)
|
||||
|
||||
# 返回清洗后的字典
|
||||
return data_repaired
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import os
|
||||
|
||||
import app.algorithms.api_ex.Fdataclean as Fdataclean
|
||||
import app.algorithms.api_ex.Pdataclean as Pdataclean
|
||||
import app.algorithms.api_ex.flow_data_clean as flow_data_clean
|
||||
import app.algorithms.api_ex.pressure_data_clean as pressure_data_clean
|
||||
|
||||
|
||||
############################################################
|
||||
@@ -26,7 +26,7 @@ def flow_data_clean(input_csv_file: str) -> str:
|
||||
if not os.path.exists(input_csv_path):
|
||||
raise FileNotFoundError(f"指定的文件不存在: {input_csv_path}")
|
||||
# 调用 Fdataclean.clean_flow_data_kf 函数进行数据清洗
|
||||
out_xlsx_path = Fdataclean.clean_flow_data_kf(input_csv_path)
|
||||
out_xlsx_path = flow_data_clean.clean_flow_data_kf(input_csv_path)
|
||||
print("清洗后的数据已保存到:", out_xlsx_path)
|
||||
|
||||
|
||||
@@ -53,5 +53,5 @@ def pressure_data_clean(input_csv_file: str) -> str:
|
||||
if not os.path.exists(input_csv_path):
|
||||
raise FileNotFoundError(f"指定的文件不存在: {input_csv_path}")
|
||||
# 调用 Fdataclean.clean_flow_data_kf 函数进行数据清洗
|
||||
out_xlsx_path = Pdataclean.clean_pressure_data_km(input_csv_path)
|
||||
out_xlsx_path = pressure_data_clean.clean_pressure_data_km(input_csv_path)
|
||||
print("清洗后的数据已保存到:", out_xlsx_path)
|
||||
|
||||
@@ -5,7 +5,10 @@ from math import pi, sqrt
|
||||
import pytz
|
||||
|
||||
import app.services.simulation as simulation
|
||||
from app.algorithms.api_ex.run_simulation import run_simulation_ex, from_clock_to_seconds_2
|
||||
from app.algorithms.api_ex.run_simulation import (
|
||||
run_simulation_ex,
|
||||
from_clock_to_seconds_2,
|
||||
)
|
||||
from app.native.api.project import copy_project
|
||||
from app.services.epanet.epanet import Output
|
||||
from app.services.scheme_management import store_scheme_info
|
||||
@@ -43,7 +46,7 @@ def burst_analysis(
|
||||
modify_fixed_pump_pattern: dict[str, list] = None,
|
||||
modify_variable_pump_pattern: dict[str, list] = None,
|
||||
modify_valve_opening: dict[str, float] = None,
|
||||
scheme_Name: str = None,
|
||||
scheme_name: str = None,
|
||||
) -> None:
|
||||
"""
|
||||
爆管模拟
|
||||
@@ -55,7 +58,7 @@ def burst_analysis(
|
||||
:param modify_fixed_pump_pattern: dict中包含多个水泵模式,str为工频水泵的id,list为修改后的pattern
|
||||
:param modify_variable_pump_pattern: dict中包含多个水泵模式,str为变频水泵的id,list为修改后的pattern
|
||||
:param modify_valve_opening: dict中包含多个阀门开启度,str为阀门的id,float为修改后的阀门开启度
|
||||
:param scheme_Name: 方案名称
|
||||
:param scheme_name: 方案名称
|
||||
:return:
|
||||
"""
|
||||
scheme_detail: dict = {
|
||||
@@ -169,19 +172,19 @@ def burst_analysis(
|
||||
modify_fixed_pump_pattern=modify_fixed_pump_pattern,
|
||||
modify_variable_pump_pattern=modify_variable_pump_pattern,
|
||||
modify_valve_opening=modify_valve_opening,
|
||||
scheme_Type="burst_Analysis",
|
||||
scheme_Name=scheme_Name,
|
||||
scheme_type="burst_analysis",
|
||||
scheme_name=scheme_name,
|
||||
)
|
||||
# step 3. restore the base model status
|
||||
# execute_undo(name) #有疑惑
|
||||
if is_project_open(new_name):
|
||||
close_project(new_name)
|
||||
delete_project(new_name)
|
||||
# return result
|
||||
# 存储方案信息到 PG 数据库
|
||||
store_scheme_info(
|
||||
name=name,
|
||||
scheme_name=scheme_Name,
|
||||
scheme_type="burst_Analysis",
|
||||
scheme_name=scheme_name,
|
||||
scheme_type="burst_analysis",
|
||||
username="admin",
|
||||
scheme_start_time=modify_pattern_start_time,
|
||||
scheme_detail=scheme_detail,
|
||||
@@ -400,11 +403,11 @@ def flushing_analysis(
|
||||
def contaminant_simulation(
|
||||
name: str,
|
||||
modify_pattern_start_time: str, # 模拟开始时间,格式为'2024-11-25T09:00:00+08:00'
|
||||
modify_total_duration: int = 900, # 模拟总历时,秒
|
||||
source: str = None, # 污染源节点ID
|
||||
concentration: float = None, # 污染源浓度,单位mg/L
|
||||
modify_total_duration: int, # 模拟总历时,秒
|
||||
source: str, # 污染源节点ID
|
||||
concentration: float, # 污染源浓度,单位mg/L
|
||||
scheme_name: str = None,
|
||||
source_pattern: str = None, # 污染源时间变化模式名称
|
||||
scheme_Name: str = None,
|
||||
) -> None:
|
||||
"""
|
||||
污染模拟
|
||||
@@ -418,6 +421,12 @@ def contaminant_simulation(
|
||||
:param scheme_Name: 方案名称
|
||||
:return:
|
||||
"""
|
||||
scheme_detail: dict = {
|
||||
"source": source,
|
||||
"concentration": concentration,
|
||||
"duration": modify_total_duration,
|
||||
"pattern": source_pattern,
|
||||
}
|
||||
print(
|
||||
datetime.now(pytz.timezone("Asia/Shanghai")).strftime("%Y-%m-%d %H:%M:%S")
|
||||
+ " -- Start Analysis."
|
||||
@@ -520,8 +529,8 @@ def contaminant_simulation(
|
||||
simulation_type="extended",
|
||||
modify_pattern_start_time=modify_pattern_start_time,
|
||||
modify_total_duration=modify_total_duration,
|
||||
scheme_Type="contaminant_Analysis",
|
||||
scheme_Name=scheme_Name,
|
||||
scheme_type="contaminant_analysis",
|
||||
scheme_name=scheme_name,
|
||||
)
|
||||
|
||||
# for i in range(1,operation_step):
|
||||
@@ -529,7 +538,15 @@ def contaminant_simulation(
|
||||
if is_project_open(new_name):
|
||||
close_project(new_name)
|
||||
delete_project(new_name)
|
||||
# return result
|
||||
# 存储方案信息到 PG 数据库
|
||||
store_scheme_info(
|
||||
name=name,
|
||||
scheme_name=scheme_name,
|
||||
scheme_type="contaminant_analysis",
|
||||
username="admin",
|
||||
scheme_start_time=modify_pattern_start_time,
|
||||
scheme_detail=scheme_detail,
|
||||
)
|
||||
|
||||
|
||||
############################################################
|
||||
|
||||
@@ -2,11 +2,10 @@ from collections import defaultdict, deque
|
||||
from typing import Any
|
||||
|
||||
from app.services.tjnetwork import (
|
||||
get_link_properties,
|
||||
get_link_type,
|
||||
get_network_link_nodes,
|
||||
is_link,
|
||||
is_node,
|
||||
is_link,
|
||||
get_link_properties,
|
||||
)
|
||||
|
||||
|
||||
@@ -20,26 +19,36 @@ def _parse_link_entry(link_entry: str) -> tuple[str, str, str, str]:
|
||||
return parts[0], parts[1], parts[2], parts[3]
|
||||
|
||||
|
||||
def valve_isolation_analysis(network: str, accident_element: str) -> dict[str, Any]:
|
||||
def valve_isolation_analysis(
|
||||
network: str, accident_elements: str | list[str]
|
||||
) -> dict[str, Any]:
|
||||
"""
|
||||
关阀搜索/分析:基于拓扑结构确定事故隔离所需关阀。
|
||||
:param network: 模型名称
|
||||
:param accident_element: 事故点(节点或管道/泵/阀门ID)
|
||||
:param accident_elements: 事故点(节点或管道/泵/阀门ID),可以是单个ID字符串或ID列表
|
||||
:return: dict,包含受影响节点、必须关闭阀门、可选阀门等信息
|
||||
"""
|
||||
if is_node(network, accident_element):
|
||||
start_nodes = {accident_element}
|
||||
accident_type = "node"
|
||||
elif is_link(network, accident_element):
|
||||
accident_type = get_link_type(network, accident_element)
|
||||
link_props = get_link_properties(network, accident_element)
|
||||
if isinstance(accident_elements, str):
|
||||
target_elements = [accident_elements]
|
||||
else:
|
||||
target_elements = accident_elements
|
||||
|
||||
start_nodes = set()
|
||||
|
||||
for element in target_elements:
|
||||
if is_node(network, element):
|
||||
start_nodes.add(element)
|
||||
elif is_link(network, element):
|
||||
link_props = get_link_properties(network, element)
|
||||
node1 = link_props.get("node1")
|
||||
node2 = link_props.get("node2")
|
||||
if not node1 or not node2:
|
||||
raise ValueError("Accident link missing node endpoints")
|
||||
start_nodes = {node1, node2}
|
||||
# 如果是批量处理,可以选择跳过错误或记录错误,这里暂时保持严谨抛出异常
|
||||
raise ValueError(f"Accident link {element} missing node endpoints")
|
||||
start_nodes.add(node1)
|
||||
start_nodes.add(node2)
|
||||
else:
|
||||
raise ValueError("Accident element not found")
|
||||
raise ValueError(f"Accident element {element} not found")
|
||||
|
||||
adjacency: dict[str, set[str]] = defaultdict(set)
|
||||
valve_links: dict[str, tuple[str, str]] = {}
|
||||
@@ -76,11 +85,15 @@ def valve_isolation_analysis(network: str, accident_element: str) -> dict[str, A
|
||||
must_close_valves.sort()
|
||||
optional_valves.sort()
|
||||
|
||||
return {
|
||||
"accident_element": accident_element,
|
||||
"accident_type": accident_type,
|
||||
result = {
|
||||
"accident_elements": target_elements,
|
||||
"affected_nodes": sorted(affected_nodes),
|
||||
"must_close_valves": must_close_valves,
|
||||
"optional_valves": optional_valves,
|
||||
"isolatable": len(must_close_valves) > 0,
|
||||
}
|
||||
|
||||
if len(target_elements) == 1:
|
||||
result["accident_element"] = target_elements[0]
|
||||
|
||||
return result
|
||||
|
||||
99
app/api/v1/endpoints/audit.py
Normal file
99
app/api/v1/endpoints/audit.py
Normal file
@@ -0,0 +1,99 @@
|
||||
"""
|
||||
审计日志 API 接口
|
||||
|
||||
仅管理员可访问
|
||||
"""
|
||||
from typing import List, Optional
|
||||
from datetime import datetime
|
||||
from fastapi import APIRouter, Depends, Query, Request
|
||||
from app.domain.schemas.audit import AuditLogResponse, AuditLogQuery
|
||||
from app.domain.schemas.user import UserInDB
|
||||
from app.infra.repositories.audit_repository import AuditRepository
|
||||
from app.auth.dependencies import get_user_repository, get_db
|
||||
from app.auth.permissions import get_current_admin
|
||||
from app.infra.db.postgresql.database import Database
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
async def get_audit_repository(db: Database = Depends(get_db)) -> AuditRepository:
|
||||
"""获取审计日志仓储"""
|
||||
return AuditRepository(db)
|
||||
|
||||
@router.get("/logs", response_model=List[AuditLogResponse])
|
||||
async def get_audit_logs(
|
||||
user_id: Optional[int] = Query(None, description="按用户ID过滤"),
|
||||
username: Optional[str] = Query(None, description="按用户名过滤"),
|
||||
action: Optional[str] = Query(None, description="按操作类型过滤"),
|
||||
resource_type: Optional[str] = Query(None, description="按资源类型过滤"),
|
||||
start_time: Optional[datetime] = Query(None, description="开始时间"),
|
||||
end_time: Optional[datetime] = Query(None, description="结束时间"),
|
||||
skip: int = Query(0, ge=0, description="跳过记录数"),
|
||||
limit: int = Query(100, ge=1, le=1000, description="限制记录数"),
|
||||
current_user: UserInDB = Depends(get_current_admin),
|
||||
audit_repo: AuditRepository = Depends(get_audit_repository)
|
||||
) -> List[AuditLogResponse]:
|
||||
"""
|
||||
查询审计日志(仅管理员)
|
||||
|
||||
支持按用户、时间、操作类型等条件过滤
|
||||
"""
|
||||
logs = await audit_repo.get_logs(
|
||||
user_id=user_id,
|
||||
username=username,
|
||||
action=action,
|
||||
resource_type=resource_type,
|
||||
start_time=start_time,
|
||||
end_time=end_time,
|
||||
skip=skip,
|
||||
limit=limit
|
||||
)
|
||||
return logs
|
||||
|
||||
@router.get("/logs/count")
|
||||
async def get_audit_logs_count(
|
||||
user_id: Optional[int] = Query(None, description="按用户ID过滤"),
|
||||
username: Optional[str] = Query(None, description="按用户名过滤"),
|
||||
action: Optional[str] = Query(None, description="按操作类型过滤"),
|
||||
resource_type: Optional[str] = Query(None, description="按资源类型过滤"),
|
||||
start_time: Optional[datetime] = Query(None, description="开始时间"),
|
||||
end_time: Optional[datetime] = Query(None, description="结束时间"),
|
||||
current_user: UserInDB = Depends(get_current_admin),
|
||||
audit_repo: AuditRepository = Depends(get_audit_repository)
|
||||
) -> dict:
|
||||
"""
|
||||
获取审计日志总数(仅管理员)
|
||||
"""
|
||||
count = await audit_repo.get_log_count(
|
||||
user_id=user_id,
|
||||
username=username,
|
||||
action=action,
|
||||
resource_type=resource_type,
|
||||
start_time=start_time,
|
||||
end_time=end_time
|
||||
)
|
||||
return {"count": count}
|
||||
|
||||
@router.get("/logs/my", response_model=List[AuditLogResponse])
|
||||
async def get_my_audit_logs(
|
||||
action: Optional[str] = Query(None, description="按操作类型过滤"),
|
||||
start_time: Optional[datetime] = Query(None, description="开始时间"),
|
||||
end_time: Optional[datetime] = Query(None, description="结束时间"),
|
||||
skip: int = Query(0, ge=0),
|
||||
limit: int = Query(100, ge=1, le=1000),
|
||||
current_user: UserInDB = Depends(get_current_admin),
|
||||
audit_repo: AuditRepository = Depends(get_audit_repository)
|
||||
) -> List[AuditLogResponse]:
|
||||
"""
|
||||
查询当前用户的审计日志
|
||||
|
||||
普通用户只能查看自己的操作记录
|
||||
"""
|
||||
logs = await audit_repo.get_logs(
|
||||
user_id=current_user.id,
|
||||
action=action,
|
||||
start_time=start_time,
|
||||
end_time=end_time,
|
||||
skip=skip,
|
||||
limit=limit
|
||||
)
|
||||
return logs
|
||||
@@ -1,52 +1,186 @@
|
||||
from typing import Annotated, List, Optional
|
||||
from fastapi import APIRouter, Depends, HTTPException, Header, status
|
||||
from pydantic import BaseModel
|
||||
from typing import Annotated
|
||||
from datetime import timedelta
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from fastapi.security import OAuth2PasswordRequestForm
|
||||
from app.core.config import settings
|
||||
from app.core.security import create_access_token, create_refresh_token, verify_password
|
||||
from app.domain.schemas.user import UserCreate, UserResponse, UserLogin, Token
|
||||
from app.infra.repositories.user_repository import UserRepository
|
||||
from app.auth.dependencies import get_user_repository, get_current_active_user
|
||||
from app.domain.schemas.user import UserInDB
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
# 简易令牌验证(实际项目中应替换为 JWT/OAuth2 等)
|
||||
AUTH_TOKEN = "567e33c876a2" # 预设的有效令牌
|
||||
WHITE_LIST = ["/docs", "/openapi.json", "/redoc", "/api/v1/auth/login/"]
|
||||
@router.post("/register", response_model=UserResponse, status_code=status.HTTP_201_CREATED)
|
||||
async def register(
|
||||
user_data: UserCreate,
|
||||
user_repo: UserRepository = Depends(get_user_repository)
|
||||
) -> UserResponse:
|
||||
"""
|
||||
用户注册
|
||||
|
||||
async def verify_token(authorization: Annotated[str, Header()] = None):
|
||||
# 检查请求头是否存在
|
||||
if not authorization:
|
||||
raise HTTPException(status_code=401, detail="Authorization header missing")
|
||||
|
||||
# 提取 Bearer 后的令牌 (格式: Bearer <token>)
|
||||
try:
|
||||
token_type, token = authorization.split(" ", 1)
|
||||
if token_type.lower() != "bearer":
|
||||
raise ValueError
|
||||
except ValueError:
|
||||
创建新用户账号
|
||||
"""
|
||||
# 检查用户名和邮箱是否已存在
|
||||
if await user_repo.user_exists(username=user_data.username):
|
||||
raise HTTPException(
|
||||
status_code=401, detail="Invalid authorization format. Use: Bearer <token>"
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Username already registered"
|
||||
)
|
||||
|
||||
# 验证令牌
|
||||
if token != AUTH_TOKEN:
|
||||
raise HTTPException(status_code=403, detail="Invalid authentication token")
|
||||
if await user_repo.user_exists(email=user_data.email):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Email already registered"
|
||||
)
|
||||
|
||||
return True
|
||||
# 创建用户
|
||||
try:
|
||||
user = await user_repo.create_user(user_data)
|
||||
if not user:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail="Failed to create user"
|
||||
)
|
||||
return UserResponse.model_validate(user)
|
||||
except Exception as e:
|
||||
logger.error(f"Error during user registration: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail="Registration failed"
|
||||
)
|
||||
|
||||
def generate_access_token(username: str, password: str) -> str:
|
||||
@router.post("/login", response_model=Token)
|
||||
async def login(
|
||||
form_data: Annotated[OAuth2PasswordRequestForm, Depends()],
|
||||
user_repo: UserRepository = Depends(get_user_repository)
|
||||
) -> Token:
|
||||
"""
|
||||
根据用户名和密码生成JWT access token
|
||||
用户登录(OAuth2 标准格式)
|
||||
|
||||
参数:
|
||||
username: 用户名
|
||||
password: 密码
|
||||
|
||||
返回:
|
||||
JWT access token字符串
|
||||
返回 JWT Access Token 和 Refresh Token
|
||||
"""
|
||||
# 验证用户(支持用户名或邮箱登录)
|
||||
user = await user_repo.get_user_by_username(form_data.username)
|
||||
if not user:
|
||||
# 尝试用邮箱登录
|
||||
user = await user_repo.get_user_by_email(form_data.username)
|
||||
|
||||
if username != "tjwater" or password != "tjwater@123":
|
||||
raise ValueError("用户名或密码错误")
|
||||
if not user or not verify_password(form_data.password, user.hashed_password):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Incorrect username or password",
|
||||
headers={"WWW-Authenticate": "Bearer"},
|
||||
)
|
||||
|
||||
token = "567e33c876a2"
|
||||
return token
|
||||
if not user.is_active:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Inactive user account"
|
||||
)
|
||||
|
||||
@router.post("/login/")
|
||||
async def login(username: str, password: str) -> str:
|
||||
return generate_access_token(username, password)
|
||||
# 生成 Token
|
||||
access_token = create_access_token(subject=user.username)
|
||||
refresh_token = create_refresh_token(subject=user.username)
|
||||
|
||||
return Token(
|
||||
access_token=access_token,
|
||||
refresh_token=refresh_token,
|
||||
token_type="bearer",
|
||||
expires_in=settings.ACCESS_TOKEN_EXPIRE_MINUTES * 60
|
||||
)
|
||||
|
||||
@router.post("/login/simple", response_model=Token)
|
||||
async def login_simple(
|
||||
username: str,
|
||||
password: str,
|
||||
user_repo: UserRepository = Depends(get_user_repository)
|
||||
) -> Token:
|
||||
"""
|
||||
简化版登录接口(保持向后兼容)
|
||||
|
||||
直接使用 username 和 password 参数
|
||||
"""
|
||||
# 验证用户
|
||||
user = await user_repo.get_user_by_username(username)
|
||||
if not user:
|
||||
user = await user_repo.get_user_by_email(username)
|
||||
|
||||
if not user or not verify_password(password, user.hashed_password):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Incorrect username or password"
|
||||
)
|
||||
|
||||
if not user.is_active:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Inactive user account"
|
||||
)
|
||||
|
||||
# 生成 Token
|
||||
access_token = create_access_token(subject=user.username)
|
||||
refresh_token = create_refresh_token(subject=user.username)
|
||||
|
||||
return Token(
|
||||
access_token=access_token,
|
||||
refresh_token=refresh_token,
|
||||
token_type="bearer",
|
||||
expires_in=settings.ACCESS_TOKEN_EXPIRE_MINUTES * 60
|
||||
)
|
||||
|
||||
@router.get("/me", response_model=UserResponse)
|
||||
async def get_current_user_info(
|
||||
current_user: UserInDB = Depends(get_current_active_user)
|
||||
) -> UserResponse:
|
||||
"""
|
||||
获取当前登录用户信息
|
||||
"""
|
||||
return UserResponse.model_validate(current_user)
|
||||
|
||||
@router.post("/refresh", response_model=Token)
|
||||
async def refresh_token(
|
||||
refresh_token: str,
|
||||
user_repo: UserRepository = Depends(get_user_repository)
|
||||
) -> Token:
|
||||
"""
|
||||
刷新 Access Token
|
||||
|
||||
使用 Refresh Token 获取新的 Access Token
|
||||
"""
|
||||
from jose import jwt, JWTError
|
||||
|
||||
credentials_exception = HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Could not validate refresh token",
|
||||
headers={"WWW-Authenticate": "Bearer"},
|
||||
)
|
||||
|
||||
try:
|
||||
payload = jwt.decode(refresh_token, settings.SECRET_KEY, algorithms=[settings.ALGORITHM])
|
||||
username: str = payload.get("sub")
|
||||
token_type: str = payload.get("type")
|
||||
|
||||
if username is None or token_type != "refresh":
|
||||
raise credentials_exception
|
||||
|
||||
except JWTError:
|
||||
raise credentials_exception
|
||||
|
||||
# 验证用户仍然存在且激活
|
||||
user = await user_repo.get_user_by_username(username)
|
||||
if not user or not user.is_active:
|
||||
raise credentials_exception
|
||||
|
||||
# 生成新的 Access Token
|
||||
new_access_token = create_access_token(subject=user.username)
|
||||
|
||||
return Token(
|
||||
access_token=new_access_token,
|
||||
refresh_token=refresh_token, # 保持原 refresh token
|
||||
token_type="bearer",
|
||||
expires_in=settings.ACCESS_TOKEN_EXPIRE_MINUTES * 60
|
||||
)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
from fastapi import APIRouter, Request, Depends
|
||||
from typing import Any, List, Dict, Union
|
||||
from app.services.tjnetwork import *
|
||||
from app.api.v1.endpoints.auth import verify_token
|
||||
from app.auth.dependencies import get_current_user as verify_token
|
||||
from app.infra.cache.redis_client import redis_client, encode_datetime, decode_datetime
|
||||
import msgpack
|
||||
|
||||
|
||||
@@ -30,8 +30,8 @@ from app.algorithms.sensors import (
|
||||
pressure_sensor_placement_sensitivity,
|
||||
pressure_sensor_placement_kmeans,
|
||||
)
|
||||
import app.algorithms.api_ex.Fdataclean as Fdataclean
|
||||
import app.algorithms.api_ex.Pdataclean as Pdataclean
|
||||
import app.algorithms.api_ex.flow_data_clean as flow_data_clean
|
||||
import app.algorithms.api_ex.pressure_data_clean as pressure_data_clean
|
||||
from app.services.network_import import network_update
|
||||
from app.services.simulation_ops import (
|
||||
project_management,
|
||||
@@ -192,19 +192,24 @@ async def burst_analysis_endpoint(
|
||||
return burst_analysis(network, pipe_id, start_time, end_time, burst_flow)
|
||||
|
||||
|
||||
@router.post("/burst_analysis/")
|
||||
async def fastapi_burst_analysis(data: BurstAnalysis) -> str:
|
||||
item = data.dict()
|
||||
@router.get("/burst_analysis/")
|
||||
async def fastapi_burst_analysis(
|
||||
network: str = Query(...),
|
||||
modify_pattern_start_time: str = Query(...),
|
||||
burst_ID: list | str = Query(..., alias="burst_ID[]"), # 添加别名以匹配 URL
|
||||
burst_size: list | float | int = Query(
|
||||
..., alias="burst_size[]"
|
||||
), # 添加别名以匹配 URL
|
||||
modify_total_duration: int = Query(...),
|
||||
scheme_name: str = Query(...),
|
||||
) -> str:
|
||||
burst_analysis(
|
||||
name=item["name"],
|
||||
modify_pattern_start_time=item["modify_pattern_start_time"],
|
||||
burst_ID=item["burst_ID"],
|
||||
burst_size=item["burst_size"],
|
||||
modify_total_duration=item["modify_total_duration"],
|
||||
modify_fixed_pump_pattern=item["modify_fixed_pump_pattern"],
|
||||
modify_variable_pump_pattern=item["modify_variable_pump_pattern"],
|
||||
modify_valve_opening=item["modify_valve_opening"],
|
||||
scheme_Name=item["scheme_Name"],
|
||||
name=network,
|
||||
modify_pattern_start_time=modify_pattern_start_time,
|
||||
burst_ID=burst_ID,
|
||||
burst_size=burst_size,
|
||||
modify_total_duration=modify_total_duration,
|
||||
scheme_name=scheme_name,
|
||||
)
|
||||
return "success"
|
||||
|
||||
@@ -232,8 +237,10 @@ async def fastapi_valve_close_analysis(
|
||||
return result or "success"
|
||||
|
||||
|
||||
@router.get("/valveisolation/")
|
||||
async def valve_isolation_endpoint(network: str, accident_element: str):
|
||||
@router.get("/valve_isolation_analysis/")
|
||||
async def valve_isolation_endpoint(
|
||||
network: str, accident_element: List[str] = Query(...)
|
||||
):
|
||||
return analyze_valve_isolation(network, accident_element)
|
||||
|
||||
|
||||
@@ -254,7 +261,9 @@ async def fastapi_flushing_analysis(
|
||||
flush_flow: float = 0,
|
||||
duration: int | None = None,
|
||||
) -> str:
|
||||
valve_opening = {valve_id: float(valves_k[idx]) for idx, valve_id in enumerate(valves)}
|
||||
valve_opening = {
|
||||
valve_id: float(valves_k[idx]) for idx, valve_id in enumerate(valves)
|
||||
}
|
||||
result = flushing_analysis(
|
||||
name=network,
|
||||
modify_pattern_start_time=start_time,
|
||||
@@ -266,25 +275,20 @@ async def fastapi_flushing_analysis(
|
||||
return result or "success"
|
||||
|
||||
|
||||
@router.get("/contaminantsimulation/")
|
||||
async def contaminant_simulation_endpoint(
|
||||
network: str, node_id: str, start_time: str, duration: float, concentration: float
|
||||
):
|
||||
return contaminant_simulation(network, node_id, start_time, duration, concentration)
|
||||
|
||||
|
||||
@router.get("/contaminant_simulation/", response_class=PlainTextResponse)
|
||||
async def fastapi_contaminant_simulation(
|
||||
network: str,
|
||||
start_time: str,
|
||||
source: str,
|
||||
concentration: float,
|
||||
duration: int = 900,
|
||||
duration: int,
|
||||
scheme_name: str | None = None,
|
||||
pattern: str | None = None,
|
||||
) -> str:
|
||||
result = contaminant_simulation(
|
||||
name=network,
|
||||
modify_pattern_start_time=start_time,
|
||||
scheme_name=scheme_name,
|
||||
modify_total_duration=duration,
|
||||
source=source,
|
||||
concentration=concentration,
|
||||
@@ -431,9 +435,7 @@ async def fastapi_network_update(file: UploadFile = File()) -> str:
|
||||
async def fastapi_pump_failure(data: PumpFailureState) -> str:
|
||||
item = data.dict()
|
||||
with open("./pump_failure_message.txt", "a", encoding="utf-8-sig") as f1:
|
||||
f1.write(
|
||||
"[{}] {}\n".format(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), item)
|
||||
)
|
||||
f1.write("[{}] {}\n".format(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), item))
|
||||
with open("./pump_failure_status.txt", "r", encoding="utf-8-sig") as f2:
|
||||
lines = f2.readlines()
|
||||
first_stage_pump_status_dict = json.loads(json.dumps(eval(lines[0])))
|
||||
@@ -587,11 +589,10 @@ async def fastapi_scada_device_data_cleaning(
|
||||
if device_id in type_scada_data:
|
||||
values = [record["value"] for record in type_scada_data[device_id]]
|
||||
df[device_id] = values
|
||||
value_df = df.drop(columns=["time"])
|
||||
if device_type == "pressure":
|
||||
cleaned_value_df = Pdataclean.clean_pressure_data_df_km(value_df)
|
||||
cleaned_value_df = pressure_data_clean.clean_pressure_data_df_km(df)
|
||||
elif device_type == "pipe_flow":
|
||||
cleaned_value_df = Fdataclean.clean_flow_data_df_kf(value_df)
|
||||
cleaned_value_df = flow_data_clean.clean_flow_data_df_kf(df)
|
||||
cleaned_value_df = pd.DataFrame(cleaned_value_df)
|
||||
cleaned_df = pd.concat([df["time"], cleaned_value_df], axis=1)
|
||||
influxdb_api.import_multicolumn_data_from_dict(
|
||||
|
||||
180
app/api/v1/endpoints/user_management.py
Normal file
180
app/api/v1/endpoints/user_management.py
Normal file
@@ -0,0 +1,180 @@
|
||||
"""
|
||||
用户管理 API 接口
|
||||
|
||||
演示权限控制的使用
|
||||
"""
|
||||
from typing import List
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from app.domain.schemas.user import UserResponse, UserUpdate, UserCreate
|
||||
from app.domain.models.role import UserRole
|
||||
from app.domain.schemas.user import UserInDB
|
||||
from app.infra.repositories.user_repository import UserRepository
|
||||
from app.auth.dependencies import get_user_repository, get_current_active_user
|
||||
from app.auth.permissions import get_current_admin, require_role, check_resource_owner
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.get("/", response_model=List[UserResponse])
|
||||
async def list_users(
|
||||
skip: int = 0,
|
||||
limit: int = 100,
|
||||
current_user: UserInDB = Depends(require_role(UserRole.ADMIN)),
|
||||
user_repo: UserRepository = Depends(get_user_repository)
|
||||
) -> List[UserResponse]:
|
||||
"""
|
||||
获取用户列表(仅管理员)
|
||||
"""
|
||||
users = await user_repo.get_all_users(skip=skip, limit=limit)
|
||||
return [UserResponse.model_validate(user) for user in users]
|
||||
|
||||
@router.get("/{user_id}", response_model=UserResponse)
|
||||
async def get_user(
|
||||
user_id: int,
|
||||
current_user: UserInDB = Depends(get_current_active_user),
|
||||
user_repo: UserRepository = Depends(get_user_repository)
|
||||
) -> UserResponse:
|
||||
"""
|
||||
获取用户详情
|
||||
|
||||
管理员可查看所有用户,普通用户只能查看自己
|
||||
"""
|
||||
# 检查权限
|
||||
if not check_resource_owner(user_id, current_user):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="You don't have permission to view this user"
|
||||
)
|
||||
|
||||
user = await user_repo.get_user_by_id(user_id)
|
||||
if not user:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="User not found"
|
||||
)
|
||||
|
||||
return UserResponse.model_validate(user)
|
||||
|
||||
@router.put("/{user_id}", response_model=UserResponse)
|
||||
async def update_user(
|
||||
user_id: int,
|
||||
user_update: UserUpdate,
|
||||
current_user: UserInDB = Depends(get_current_active_user),
|
||||
user_repo: UserRepository = Depends(get_user_repository)
|
||||
) -> UserResponse:
|
||||
"""
|
||||
更新用户信息
|
||||
|
||||
管理员可更新所有用户,普通用户只能更新自己(且不能修改角色)
|
||||
"""
|
||||
# 检查用户是否存在
|
||||
target_user = await user_repo.get_user_by_id(user_id)
|
||||
if not target_user:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="User not found"
|
||||
)
|
||||
|
||||
# 权限检查
|
||||
is_owner = current_user.id == user_id
|
||||
is_admin = UserRole(current_user.role).has_permission(UserRole.ADMIN)
|
||||
|
||||
if not is_owner and not is_admin:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="You don't have permission to update this user"
|
||||
)
|
||||
|
||||
# 非管理员不能修改角色和激活状态
|
||||
if not is_admin:
|
||||
if user_update.role is not None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Only admins can change user roles"
|
||||
)
|
||||
if user_update.is_active is not None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Only admins can change user active status"
|
||||
)
|
||||
|
||||
# 更新用户
|
||||
updated_user = await user_repo.update_user(user_id, user_update)
|
||||
if not updated_user:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail="Failed to update user"
|
||||
)
|
||||
|
||||
return UserResponse.model_validate(updated_user)
|
||||
|
||||
@router.delete("/{user_id}")
|
||||
async def delete_user(
|
||||
user_id: int,
|
||||
current_user: UserInDB = Depends(get_current_admin),
|
||||
user_repo: UserRepository = Depends(get_user_repository)
|
||||
) -> dict:
|
||||
"""
|
||||
删除用户(仅管理员)
|
||||
"""
|
||||
# 不能删除自己
|
||||
if current_user.id == user_id:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="You cannot delete your own account"
|
||||
)
|
||||
|
||||
success = await user_repo.delete_user(user_id)
|
||||
if not success:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="User not found"
|
||||
)
|
||||
|
||||
return {"message": "User deleted successfully"}
|
||||
|
||||
@router.post("/{user_id}/activate")
|
||||
async def activate_user(
|
||||
user_id: int,
|
||||
current_user: UserInDB = Depends(get_current_admin),
|
||||
user_repo: UserRepository = Depends(get_user_repository)
|
||||
) -> UserResponse:
|
||||
"""
|
||||
激活用户(仅管理员)
|
||||
"""
|
||||
user_update = UserUpdate(is_active=True)
|
||||
updated_user = await user_repo.update_user(user_id, user_update)
|
||||
|
||||
if not updated_user:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="User not found"
|
||||
)
|
||||
|
||||
return UserResponse.model_validate(updated_user)
|
||||
|
||||
@router.post("/{user_id}/deactivate")
|
||||
async def deactivate_user(
|
||||
user_id: int,
|
||||
current_user: UserInDB = Depends(get_current_admin),
|
||||
user_repo: UserRepository = Depends(get_user_repository)
|
||||
) -> UserResponse:
|
||||
"""
|
||||
停用用户(仅管理员)
|
||||
"""
|
||||
# 不能停用自己
|
||||
if current_user.id == user_id:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="You cannot deactivate your own account"
|
||||
)
|
||||
|
||||
user_update = UserUpdate(is_active=False)
|
||||
updated_user = await user_repo.update_user(user_id, user_update)
|
||||
|
||||
if not updated_user:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="User not found"
|
||||
)
|
||||
|
||||
return UserResponse.model_validate(updated_user)
|
||||
@@ -12,6 +12,8 @@ from app.api.v1.endpoints import (
|
||||
misc,
|
||||
risk,
|
||||
cache,
|
||||
user_management, # 新增:用户管理
|
||||
audit, # 新增:审计日志
|
||||
)
|
||||
from app.api.v1.endpoints.network import (
|
||||
general,
|
||||
@@ -41,7 +43,9 @@ from app.infra.db.timescaledb import router as timescaledb_router
|
||||
api_router = APIRouter()
|
||||
|
||||
# Core Services
|
||||
api_router.include_router(auth.router, tags=["Auth"])
|
||||
api_router.include_router(auth.router, prefix="/auth", tags=["Auth"])
|
||||
api_router.include_router(user_management.router, prefix="/users", tags=["User Management"]) # 新增
|
||||
api_router.include_router(audit.router, prefix="/audit", tags=["Audit Logs"]) # 新增
|
||||
api_router.include_router(project.router, tags=["Project"])
|
||||
|
||||
# Network Elements (Node/Link Types)
|
||||
|
||||
@@ -1,21 +1,100 @@
|
||||
from fastapi import Depends, HTTPException, status
|
||||
from typing import Annotated, Optional
|
||||
from fastapi import Depends, HTTPException, status, Request
|
||||
from fastapi.security import OAuth2PasswordBearer
|
||||
from app.core.config import settings
|
||||
from jose import jwt, JWTError
|
||||
from app.core.config import settings
|
||||
from app.domain.schemas.user import UserInDB, TokenPayload
|
||||
from app.infra.repositories.user_repository import UserRepository
|
||||
from app.infra.db.postgresql.database import Database
|
||||
|
||||
oauth2_scheme = OAuth2PasswordBearer(tokenUrl=f"{settings.API_V1_STR}/login/access-token")
|
||||
oauth2_scheme = OAuth2PasswordBearer(tokenUrl=f"{settings.API_V1_STR}/auth/login")
|
||||
|
||||
async def get_current_user(token: str = Depends(oauth2_scheme)):
|
||||
|
||||
# 数据库依赖
|
||||
async def get_db(request: Request) -> Database:
|
||||
"""
|
||||
获取数据库实例
|
||||
|
||||
从 FastAPI app.state 中获取在启动时初始化的数据库连接
|
||||
"""
|
||||
if not hasattr(request.app.state, "db"):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
||||
detail="Database not initialized",
|
||||
)
|
||||
return request.app.state.db
|
||||
|
||||
|
||||
async def get_user_repository(db: Database = Depends(get_db)) -> UserRepository:
|
||||
"""获取用户仓储实例"""
|
||||
return UserRepository(db)
|
||||
|
||||
|
||||
async def get_current_user(
|
||||
token: str = Depends(oauth2_scheme),
|
||||
user_repo: UserRepository = Depends(get_user_repository),
|
||||
) -> UserInDB:
|
||||
"""
|
||||
获取当前登录用户
|
||||
|
||||
从 JWT Token 中解析用户信息,并从数据库验证
|
||||
"""
|
||||
credentials_exception = HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Could not validate credentials",
|
||||
headers={"WWW-Authenticate": "Bearer"},
|
||||
)
|
||||
|
||||
try:
|
||||
payload = jwt.decode(token, settings.SECRET_KEY, algorithms=[settings.ALGORITHM])
|
||||
payload = jwt.decode(
|
||||
token, settings.SECRET_KEY, algorithms=[settings.ALGORITHM]
|
||||
)
|
||||
username: str = payload.get("sub")
|
||||
token_type: str = payload.get("type", "access")
|
||||
|
||||
if username is None:
|
||||
raise credentials_exception
|
||||
|
||||
if token_type != "access":
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Invalid token type. Access token required.",
|
||||
headers={"WWW-Authenticate": "Bearer"},
|
||||
)
|
||||
|
||||
except JWTError:
|
||||
raise credentials_exception
|
||||
return username
|
||||
|
||||
# 从数据库获取用户
|
||||
user = await user_repo.get_user_by_username(username)
|
||||
if user is None:
|
||||
raise credentials_exception
|
||||
|
||||
return user
|
||||
|
||||
|
||||
async def get_current_active_user(
|
||||
current_user: UserInDB = Depends(get_current_user),
|
||||
) -> UserInDB:
|
||||
"""
|
||||
获取当前活跃用户(必须是激活状态)
|
||||
"""
|
||||
if not current_user.is_active:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN, detail="Inactive user"
|
||||
)
|
||||
return current_user
|
||||
|
||||
|
||||
async def get_current_superuser(
|
||||
current_user: UserInDB = Depends(get_current_user),
|
||||
) -> UserInDB:
|
||||
"""
|
||||
获取当前超级管理员用户
|
||||
"""
|
||||
if not current_user.is_superuser:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Not enough privileges. Superuser access required.",
|
||||
)
|
||||
return current_user
|
||||
|
||||
106
app/auth/permissions.py
Normal file
106
app/auth/permissions.py
Normal file
@@ -0,0 +1,106 @@
|
||||
"""
|
||||
权限控制依赖项和装饰器
|
||||
|
||||
基于角色的访问控制(RBAC)
|
||||
"""
|
||||
from typing import Callable
|
||||
from fastapi import Depends, HTTPException, status
|
||||
from app.domain.models.role import UserRole
|
||||
from app.domain.schemas.user import UserInDB
|
||||
from app.auth.dependencies import get_current_active_user
|
||||
|
||||
def require_role(required_role: UserRole):
|
||||
"""
|
||||
要求特定角色或更高权限
|
||||
|
||||
用法:
|
||||
@router.get("/admin-only")
|
||||
async def admin_endpoint(user: UserInDB = Depends(require_role(UserRole.ADMIN))):
|
||||
...
|
||||
|
||||
Args:
|
||||
required_role: 需要的最低角色
|
||||
|
||||
Returns:
|
||||
依赖函数
|
||||
"""
|
||||
async def role_checker(
|
||||
current_user: UserInDB = Depends(get_current_active_user)
|
||||
) -> UserInDB:
|
||||
user_role = UserRole(current_user.role)
|
||||
|
||||
if not user_role.has_permission(required_role):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail=f"Insufficient permissions. Required role: {required_role.value}, "
|
||||
f"Your role: {user_role.value}"
|
||||
)
|
||||
|
||||
return current_user
|
||||
|
||||
return role_checker
|
||||
|
||||
# 预定义的权限检查依赖
|
||||
require_admin = require_role(UserRole.ADMIN)
|
||||
require_operator = require_role(UserRole.OPERATOR)
|
||||
require_user = require_role(UserRole.USER)
|
||||
|
||||
def get_current_admin(
|
||||
current_user: UserInDB = Depends(require_admin)
|
||||
) -> UserInDB:
|
||||
"""
|
||||
获取当前管理员用户
|
||||
|
||||
等同于 Depends(require_role(UserRole.ADMIN))
|
||||
"""
|
||||
return current_user
|
||||
|
||||
def get_current_operator(
|
||||
current_user: UserInDB = Depends(require_operator)
|
||||
) -> UserInDB:
|
||||
"""
|
||||
获取当前操作员用户(或更高权限)
|
||||
|
||||
等同于 Depends(require_role(UserRole.OPERATOR))
|
||||
"""
|
||||
return current_user
|
||||
|
||||
def check_resource_owner(user_id: int, current_user: UserInDB) -> bool:
|
||||
"""
|
||||
检查是否是资源拥有者或管理员
|
||||
|
||||
Args:
|
||||
user_id: 资源拥有者ID
|
||||
current_user: 当前用户
|
||||
|
||||
Returns:
|
||||
是否有权限
|
||||
"""
|
||||
# 管理员可以访问所有资源
|
||||
if UserRole(current_user.role).has_permission(UserRole.ADMIN):
|
||||
return True
|
||||
|
||||
# 检查是否是资源拥有者
|
||||
return current_user.id == user_id
|
||||
|
||||
def require_owner_or_admin(user_id: int):
|
||||
"""
|
||||
要求是资源拥有者或管理员
|
||||
|
||||
Args:
|
||||
user_id: 资源拥有者ID
|
||||
|
||||
Returns:
|
||||
依赖函数
|
||||
"""
|
||||
async def owner_or_admin_checker(
|
||||
current_user: UserInDB = Depends(get_current_active_user)
|
||||
) -> UserInDB:
|
||||
if not check_resource_owner(user_id, current_user):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="You don't have permission to access this resource"
|
||||
)
|
||||
return current_user
|
||||
|
||||
return owner_or_admin_checker
|
||||
@@ -1,3 +1,154 @@
|
||||
# Placeholder for audit logic
|
||||
async def log_audit_event(event_type: str, user_id: str, details: dict):
|
||||
"""
|
||||
审计日志模块
|
||||
|
||||
记录系统关键操作,用于安全审计和合规追踪
|
||||
"""
|
||||
|
||||
from typing import Optional
|
||||
from datetime import datetime
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class AuditAction:
|
||||
"""审计操作类型常量"""
|
||||
|
||||
# 认证相关
|
||||
LOGIN = "LOGIN"
|
||||
LOGOUT = "LOGOUT"
|
||||
REGISTER = "REGISTER"
|
||||
PASSWORD_CHANGE = "PASSWORD_CHANGE"
|
||||
|
||||
# 数据操作
|
||||
CREATE = "CREATE"
|
||||
READ = "READ"
|
||||
UPDATE = "UPDATE"
|
||||
DELETE = "DELETE"
|
||||
|
||||
# 权限相关
|
||||
PERMISSION_CHANGE = "PERMISSION_CHANGE"
|
||||
ROLE_CHANGE = "ROLE_CHANGE"
|
||||
|
||||
# 系统操作
|
||||
CONFIG_CHANGE = "CONFIG_CHANGE"
|
||||
SYSTEM_START = "SYSTEM_START"
|
||||
SYSTEM_STOP = "SYSTEM_STOP"
|
||||
|
||||
|
||||
async def log_audit_event(
|
||||
action: str,
|
||||
user_id: Optional[int] = None,
|
||||
username: Optional[str] = None,
|
||||
resource_type: Optional[str] = None,
|
||||
resource_id: Optional[str] = None,
|
||||
ip_address: Optional[str] = None,
|
||||
user_agent: Optional[str] = None,
|
||||
request_method: Optional[str] = None,
|
||||
request_path: Optional[str] = None,
|
||||
request_data: Optional[dict] = None,
|
||||
response_status: Optional[int] = None,
|
||||
error_message: Optional[str] = None,
|
||||
db=None, # 新增:可选的数据库实例
|
||||
):
|
||||
"""
|
||||
记录审计日志
|
||||
|
||||
Args:
|
||||
action: 操作类型
|
||||
user_id: 用户ID
|
||||
username: 用户名
|
||||
resource_type: 资源类型
|
||||
resource_id: 资源ID
|
||||
ip_address: IP地址
|
||||
user_agent: User-Agent
|
||||
request_method: 请求方法
|
||||
request_path: 请求路径
|
||||
request_data: 请求数据(敏感字段需脱敏)
|
||||
response_status: 响应状态码
|
||||
error_message: 错误消息
|
||||
db: 数据库实例(可选,如果不提供则尝试获取)
|
||||
"""
|
||||
from app.infra.repositories.audit_repository import AuditRepository
|
||||
|
||||
try:
|
||||
# 脱敏敏感数据
|
||||
if request_data:
|
||||
request_data = sanitize_sensitive_data(request_data)
|
||||
|
||||
# 如果没有提供数据库实例,尝试从全局获取
|
||||
if db is None:
|
||||
try:
|
||||
from app.infra.db.postgresql.database import db as default_db
|
||||
|
||||
# 仅当连接池已初始化时使用
|
||||
if default_db.pool:
|
||||
db = default_db
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
# 如果仍然没有数据库实例
|
||||
if db is None:
|
||||
# 在某些上下文中可能无法获取,此时静默失败
|
||||
logger.warning("No database instance provided for audit logging")
|
||||
return
|
||||
|
||||
audit_repo = AuditRepository(db)
|
||||
|
||||
await audit_repo.create_log(
|
||||
user_id=user_id,
|
||||
username=username,
|
||||
action=action,
|
||||
resource_type=resource_type,
|
||||
resource_id=resource_id,
|
||||
ip_address=ip_address,
|
||||
user_agent=user_agent,
|
||||
request_method=request_method,
|
||||
request_path=request_path,
|
||||
request_data=request_data,
|
||||
response_status=response_status,
|
||||
error_message=error_message,
|
||||
)
|
||||
|
||||
logger.info(
|
||||
f"Audit log created: action={action}, user={username or user_id}, "
|
||||
f"resource={resource_type}:{resource_id}"
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
# 审计日志失败不应影响业务流程
|
||||
logger.error(f"Failed to create audit log: {e}", exc_info=True)
|
||||
|
||||
|
||||
def sanitize_sensitive_data(data: dict) -> dict:
|
||||
"""
|
||||
脱敏敏感数据
|
||||
|
||||
Args:
|
||||
data: 原始数据
|
||||
|
||||
Returns:
|
||||
脱敏后的数据
|
||||
"""
|
||||
sensitive_fields = [
|
||||
"password",
|
||||
"passwd",
|
||||
"pwd",
|
||||
"secret",
|
||||
"token",
|
||||
"api_key",
|
||||
"apikey",
|
||||
"credit_card",
|
||||
"ssn",
|
||||
"social_security",
|
||||
]
|
||||
|
||||
sanitized = data.copy()
|
||||
|
||||
for key in sanitized:
|
||||
if isinstance(sanitized[key], dict):
|
||||
sanitized[key] = sanitize_sensitive_data(sanitized[key])
|
||||
elif any(sensitive in key.lower() for sensitive in sensitive_fields):
|
||||
sanitized[key] = "***REDACTED***"
|
||||
|
||||
return sanitized
|
||||
|
||||
@@ -1,11 +1,20 @@
|
||||
from pydantic_settings import BaseSettings
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
PROJECT_NAME: str = "TJWater Server"
|
||||
API_V1_STR: str = "/api/v1"
|
||||
SECRET_KEY: str = "your-secret-key-here" # Change in production
|
||||
|
||||
# JWT 配置
|
||||
SECRET_KEY: str = (
|
||||
"your-secret-key-here-change-in-production-use-openssl-rand-hex-32"
|
||||
)
|
||||
ALGORITHM: str = "HS256"
|
||||
ACCESS_TOKEN_EXPIRE_MINUTES: int = 30
|
||||
REFRESH_TOKEN_EXPIRE_DAYS: int = 7
|
||||
|
||||
# 数据加密密钥 (使用 Fernet)
|
||||
ENCRYPTION_KEY: str = "" # 必须从环境变量设置
|
||||
|
||||
# Database Config (PostgreSQL)
|
||||
DB_NAME: str = "tjwater"
|
||||
@@ -14,6 +23,12 @@ class Settings(BaseSettings):
|
||||
DB_USER: str = "postgres"
|
||||
DB_PASSWORD: str = "password"
|
||||
|
||||
# Database Config (TimescaleDB)
|
||||
TIMESCALEDB_DB_NAME: str = "tjwater"
|
||||
TIMESCALEDB_DB_HOST: str = "localhost"
|
||||
TIMESCALEDB_DB_PORT: str = "5433"
|
||||
TIMESCALEDB_DB_USER: str = "postgres"
|
||||
TIMESCALEDB_DB_PASSWORD: str = "password"
|
||||
# InfluxDB
|
||||
INFLUXDB_URL: str = "http://localhost:8086"
|
||||
INFLUXDB_TOKEN: str = "token"
|
||||
@@ -26,5 +41,7 @@ class Settings(BaseSettings):
|
||||
|
||||
class Config:
|
||||
env_file = ".env"
|
||||
extra = "ignore"
|
||||
|
||||
|
||||
settings = Settings()
|
||||
|
||||
@@ -1,9 +1,87 @@
|
||||
# Placeholder for encryption logic
|
||||
from cryptography.fernet import Fernet
|
||||
from typing import Optional
|
||||
import base64
|
||||
import os
|
||||
|
||||
class Encryptor:
|
||||
"""
|
||||
使用 Fernet (对称加密) 实现数据加密/解密
|
||||
适用于加密敏感配置、用户数据等
|
||||
"""
|
||||
|
||||
def __init__(self, key: Optional[bytes] = None):
|
||||
"""
|
||||
初始化加密器
|
||||
|
||||
Args:
|
||||
key: 加密密钥,如果为 None 则从环境变量读取
|
||||
"""
|
||||
if key is None:
|
||||
key_str = os.getenv("ENCRYPTION_KEY")
|
||||
if not key_str:
|
||||
raise ValueError(
|
||||
"ENCRYPTION_KEY not found in environment variables. "
|
||||
"Generate one using: Encryptor.generate_key()"
|
||||
)
|
||||
key = key_str.encode()
|
||||
|
||||
self.fernet = Fernet(key)
|
||||
|
||||
def encrypt(self, data: str) -> str:
|
||||
return data # Implement actual encryption
|
||||
"""
|
||||
加密字符串
|
||||
|
||||
Args:
|
||||
data: 待加密的明文字符串
|
||||
|
||||
Returns:
|
||||
Base64 编码的加密字符串
|
||||
"""
|
||||
if not data:
|
||||
return data
|
||||
|
||||
encrypted_bytes = self.fernet.encrypt(data.encode())
|
||||
return encrypted_bytes.decode()
|
||||
|
||||
def decrypt(self, data: str) -> str:
|
||||
return data # Implement actual decryption
|
||||
"""
|
||||
解密字符串
|
||||
|
||||
encryptor = Encryptor()
|
||||
Args:
|
||||
data: Base64 编码的加密字符串
|
||||
|
||||
Returns:
|
||||
解密后的明文字符串
|
||||
"""
|
||||
if not data:
|
||||
return data
|
||||
|
||||
decrypted_bytes = self.fernet.decrypt(data.encode())
|
||||
return decrypted_bytes.decode()
|
||||
|
||||
@staticmethod
|
||||
def generate_key() -> str:
|
||||
"""
|
||||
生成新的 Fernet 加密密钥
|
||||
|
||||
Returns:
|
||||
Base64 编码的密钥字符串
|
||||
"""
|
||||
key = Fernet.generate_key()
|
||||
return key.decode()
|
||||
|
||||
# 全局加密器实例(懒加载)
|
||||
_encryptor: Optional[Encryptor] = None
|
||||
|
||||
def get_encryptor() -> Encryptor:
|
||||
"""获取全局加密器实例"""
|
||||
global _encryptor
|
||||
if _encryptor is None:
|
||||
_encryptor = Encryptor()
|
||||
return _encryptor
|
||||
|
||||
# 向后兼容(延迟加载)
|
||||
def __getattr__(name):
|
||||
if name == "encryptor":
|
||||
return get_encryptor()
|
||||
raise AttributeError(f"module '{__name__}' has no attribute '{name}'")
|
||||
|
||||
@@ -1,23 +1,91 @@
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Optional, Union, Any
|
||||
|
||||
from jose import jwt
|
||||
from passlib.context import CryptContext
|
||||
from app.core.config import settings
|
||||
|
||||
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
||||
|
||||
def create_access_token(subject: Union[str, Any], expires_delta: Optional[timedelta] = None) -> str:
|
||||
if expires_delta:
|
||||
expire = datetime.utcnow() + expires_delta
|
||||
else:
|
||||
expire = datetime.utcnow() + timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
|
||||
|
||||
to_encode = {"exp": expire, "sub": str(subject)}
|
||||
encoded_jwt = jwt.encode(to_encode, settings.SECRET_KEY, algorithm=settings.ALGORITHM)
|
||||
def create_access_token(
|
||||
subject: Union[str, Any], expires_delta: Optional[timedelta] = None
|
||||
) -> str:
|
||||
"""
|
||||
创建 JWT Access Token
|
||||
|
||||
Args:
|
||||
subject: 用户标识(通常是用户名或用户ID)
|
||||
expires_delta: 过期时间增量
|
||||
|
||||
Returns:
|
||||
JWT token 字符串
|
||||
"""
|
||||
if expires_delta:
|
||||
expire = datetime.now() + expires_delta
|
||||
else:
|
||||
expire = datetime.now() + timedelta(
|
||||
minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES
|
||||
)
|
||||
|
||||
to_encode = {
|
||||
"exp": expire,
|
||||
"sub": str(subject),
|
||||
"type": "access",
|
||||
"iat": datetime.now(),
|
||||
}
|
||||
encoded_jwt = jwt.encode(
|
||||
to_encode, settings.SECRET_KEY, algorithm=settings.ALGORITHM
|
||||
)
|
||||
return encoded_jwt
|
||||
|
||||
|
||||
def create_refresh_token(subject: Union[str, Any]) -> str:
|
||||
"""
|
||||
创建 JWT Refresh Token(长期有效)
|
||||
|
||||
Args:
|
||||
subject: 用户标识
|
||||
|
||||
Returns:
|
||||
JWT refresh token 字符串
|
||||
"""
|
||||
expire = datetime.now() + timedelta(days=settings.REFRESH_TOKEN_EXPIRE_DAYS)
|
||||
|
||||
to_encode = {
|
||||
"exp": expire,
|
||||
"sub": str(subject),
|
||||
"type": "refresh",
|
||||
"iat": datetime.now(),
|
||||
}
|
||||
encoded_jwt = jwt.encode(
|
||||
to_encode, settings.SECRET_KEY, algorithm=settings.ALGORITHM
|
||||
)
|
||||
return encoded_jwt
|
||||
|
||||
|
||||
def verify_password(plain_password: str, hashed_password: str) -> bool:
|
||||
"""
|
||||
验证密码
|
||||
|
||||
Args:
|
||||
plain_password: 明文密码
|
||||
hashed_password: 密码哈希
|
||||
|
||||
Returns:
|
||||
是否匹配
|
||||
"""
|
||||
return pwd_context.verify(plain_password, hashed_password)
|
||||
|
||||
|
||||
def get_password_hash(password: str) -> str:
|
||||
"""
|
||||
生成密码哈希
|
||||
|
||||
Args:
|
||||
password: 明文密码
|
||||
|
||||
Returns:
|
||||
bcrypt 哈希字符串
|
||||
"""
|
||||
return pwd_context.hash(password)
|
||||
|
||||
36
app/domain/models/role.py
Normal file
36
app/domain/models/role.py
Normal file
@@ -0,0 +1,36 @@
|
||||
from enum import Enum
|
||||
|
||||
class UserRole(str, Enum):
|
||||
"""用户角色枚举"""
|
||||
ADMIN = "ADMIN" # 管理员 - 完全权限
|
||||
OPERATOR = "OPERATOR" # 操作员 - 可修改数据
|
||||
USER = "USER" # 普通用户 - 读写权限
|
||||
VIEWER = "VIEWER" # 观察者 - 仅查询权限
|
||||
|
||||
def __str__(self):
|
||||
return self.value
|
||||
|
||||
@classmethod
|
||||
def get_hierarchy(cls) -> dict:
|
||||
"""
|
||||
获取角色层级(数字越大权限越高)
|
||||
"""
|
||||
return {
|
||||
cls.VIEWER: 1,
|
||||
cls.USER: 2,
|
||||
cls.OPERATOR: 3,
|
||||
cls.ADMIN: 4,
|
||||
}
|
||||
|
||||
def has_permission(self, required_role: 'UserRole') -> bool:
|
||||
"""
|
||||
检查当前角色是否有足够权限
|
||||
|
||||
Args:
|
||||
required_role: 需要的最低角色
|
||||
|
||||
Returns:
|
||||
True if has permission
|
||||
"""
|
||||
hierarchy = self.get_hierarchy()
|
||||
return hierarchy[self] >= hierarchy[required_role]
|
||||
48
app/domain/schemas/audit.py
Normal file
48
app/domain/schemas/audit.py
Normal file
@@ -0,0 +1,48 @@
|
||||
from datetime import datetime
|
||||
from typing import Optional, Any
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
|
||||
class AuditLogCreate(BaseModel):
|
||||
"""创建审计日志"""
|
||||
user_id: Optional[int] = None
|
||||
username: Optional[str] = None
|
||||
action: str
|
||||
resource_type: Optional[str] = None
|
||||
resource_id: Optional[str] = None
|
||||
ip_address: Optional[str] = None
|
||||
user_agent: Optional[str] = None
|
||||
request_method: Optional[str] = None
|
||||
request_path: Optional[str] = None
|
||||
request_data: Optional[dict] = None
|
||||
response_status: Optional[int] = None
|
||||
error_message: Optional[str] = None
|
||||
|
||||
class AuditLogResponse(BaseModel):
|
||||
"""审计日志响应"""
|
||||
id: int
|
||||
user_id: Optional[int]
|
||||
username: Optional[str]
|
||||
action: str
|
||||
resource_type: Optional[str]
|
||||
resource_id: Optional[str]
|
||||
ip_address: Optional[str]
|
||||
user_agent: Optional[str]
|
||||
request_method: Optional[str]
|
||||
request_path: Optional[str]
|
||||
request_data: Optional[dict]
|
||||
response_status: Optional[int]
|
||||
error_message: Optional[str]
|
||||
timestamp: datetime
|
||||
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
class AuditLogQuery(BaseModel):
|
||||
"""审计日志查询参数"""
|
||||
user_id: Optional[int] = None
|
||||
username: Optional[str] = None
|
||||
action: Optional[str] = None
|
||||
resource_type: Optional[str] = None
|
||||
start_time: Optional[datetime] = None
|
||||
end_time: Optional[datetime] = None
|
||||
skip: int = Field(default=0, ge=0)
|
||||
limit: int = Field(default=100, ge=1, le=1000)
|
||||
68
app/domain/schemas/user.py
Normal file
68
app/domain/schemas/user.py
Normal file
@@ -0,0 +1,68 @@
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
from pydantic import BaseModel, EmailStr, Field, ConfigDict
|
||||
from app.domain.models.role import UserRole
|
||||
|
||||
# ============================================
|
||||
# Request Schemas (输入)
|
||||
# ============================================
|
||||
|
||||
class UserCreate(BaseModel):
|
||||
"""用户注册"""
|
||||
username: str = Field(..., min_length=3, max_length=50,
|
||||
description="用户名,3-50个字符")
|
||||
email: EmailStr = Field(..., description="邮箱地址")
|
||||
password: str = Field(..., min_length=6, max_length=100,
|
||||
description="密码,至少6个字符")
|
||||
role: UserRole = Field(default=UserRole.USER, description="用户角色")
|
||||
|
||||
class UserLogin(BaseModel):
|
||||
"""用户登录"""
|
||||
username: str = Field(..., description="用户名或邮箱")
|
||||
password: str = Field(..., description="密码")
|
||||
|
||||
class UserUpdate(BaseModel):
|
||||
"""用户信息更新"""
|
||||
email: Optional[EmailStr] = None
|
||||
password: Optional[str] = Field(None, min_length=6, max_length=100)
|
||||
role: Optional[UserRole] = None
|
||||
is_active: Optional[bool] = None
|
||||
|
||||
# ============================================
|
||||
# Response Schemas (输出)
|
||||
# ============================================
|
||||
|
||||
class UserResponse(BaseModel):
|
||||
"""用户信息响应(不含密码)"""
|
||||
id: int
|
||||
username: str
|
||||
email: str
|
||||
role: UserRole
|
||||
is_active: bool
|
||||
is_superuser: bool
|
||||
created_at: datetime
|
||||
updated_at: datetime
|
||||
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
class UserInDB(UserResponse):
|
||||
"""数据库中的用户(含密码哈希)"""
|
||||
hashed_password: str
|
||||
|
||||
# ============================================
|
||||
# Token Schemas
|
||||
# ============================================
|
||||
|
||||
class Token(BaseModel):
|
||||
"""JWT Token 响应"""
|
||||
access_token: str
|
||||
refresh_token: Optional[str] = None
|
||||
token_type: str = "bearer"
|
||||
expires_in: int = Field(..., description="过期时间(秒)")
|
||||
|
||||
class TokenPayload(BaseModel):
|
||||
"""JWT Token Payload"""
|
||||
sub: str = Field(..., description="用户ID或用户名")
|
||||
exp: Optional[int] = None
|
||||
iat: Optional[int] = None
|
||||
type: str = Field(default="access", description="token类型: access 或 refresh")
|
||||
191
app/infra/audit/middleware.py
Normal file
191
app/infra/audit/middleware.py
Normal file
@@ -0,0 +1,191 @@
|
||||
"""
|
||||
审计日志中间件
|
||||
|
||||
自动记录关键HTTP请求到审计日志
|
||||
"""
|
||||
|
||||
import time
|
||||
import json
|
||||
from typing import Callable
|
||||
from fastapi import Request, Response
|
||||
from starlette.middleware.base import BaseHTTPMiddleware
|
||||
from app.infra.db.postgresql.database import db as default_db
|
||||
from app.core.audit import log_audit_event, AuditAction
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class AuditMiddleware(BaseHTTPMiddleware):
|
||||
"""
|
||||
审计中间件
|
||||
|
||||
自动记录以下操作:
|
||||
- 所有 POST/PUT/DELETE 请求
|
||||
- 登录/登出
|
||||
- 关键资源访问
|
||||
"""
|
||||
|
||||
# 需要审计的路径前缀
|
||||
AUDIT_PATHS = [
|
||||
# "/api/v1/auth/",
|
||||
# "/api/v1/users/",
|
||||
# "/api/v1/projects/",
|
||||
# "/api/v1/networks/",
|
||||
]
|
||||
|
||||
# [新增] 需要审计的 API Tags (在 Router 或 api 函数中定义 tags=["Audit"])
|
||||
AUDIT_TAGS = [
|
||||
"Audit",
|
||||
"Users",
|
||||
"Project",
|
||||
"Network General",
|
||||
"Junctions",
|
||||
"Pipes",
|
||||
"Reservoirs",
|
||||
"Tanks",
|
||||
"Pumps",
|
||||
"Valves",
|
||||
]
|
||||
|
||||
# 需要审计的HTTP方法
|
||||
AUDIT_METHODS = ["POST", "PUT", "DELETE", "PATCH"]
|
||||
|
||||
async def dispatch(self, request: Request, call_next: Callable) -> Response:
|
||||
# 提取开始时间
|
||||
start_time = time.time()
|
||||
|
||||
# 1. 预判是否需要读取Body (针对写操作)
|
||||
# 注意:我们暂时移除早期的 return,因为需要等待路由匹配后才能检查 Tag
|
||||
should_capture_body = request.method in ["POST", "PUT", "PATCH"]
|
||||
|
||||
request_data = None
|
||||
if should_capture_body:
|
||||
try:
|
||||
# 注意:读取 body 后需要重新设置,避免影响后续处理
|
||||
body = await request.body()
|
||||
if body:
|
||||
request_data = json.loads(body.decode())
|
||||
|
||||
# 重新构造请求以供后续使用
|
||||
async def receive():
|
||||
return {"type": "http.request", "body": body}
|
||||
|
||||
request._receive = receive
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to read request body for audit: {e}")
|
||||
|
||||
# 2. 执行请求 (FastAPI在此过程中进行路由匹配)
|
||||
response = await call_next(request)
|
||||
|
||||
# 3. 决定是否审计
|
||||
# 检查方法
|
||||
is_audit_method = request.method in self.AUDIT_METHODS
|
||||
# 检查路径
|
||||
is_audit_path = any(
|
||||
request.url.path.startswith(path) for path in self.AUDIT_PATHS
|
||||
)
|
||||
# [新增] 检查 Tags (从 request.scope 中获取匹配的路由信息)
|
||||
is_audit_tag = False
|
||||
route = request.scope.get("route")
|
||||
if route and hasattr(route, "tags"):
|
||||
is_audit_tag = any(tag in self.AUDIT_TAGS for tag in route.tags)
|
||||
|
||||
should_audit = is_audit_method or is_audit_path or is_audit_tag
|
||||
|
||||
if not should_audit:
|
||||
# 即便不审计,也要处理响应头中的时间(保持原有逻辑一致性)
|
||||
process_time = time.time() - start_time
|
||||
response.headers["X-Process-Time"] = str(process_time)
|
||||
return response
|
||||
|
||||
# 4. 提取审计所需信息
|
||||
user_id = None
|
||||
username = None
|
||||
|
||||
# 尝试从请求状态获取当前用户
|
||||
if hasattr(request.state, "user"):
|
||||
user = request.state.user
|
||||
user_id = getattr(user, "id", None)
|
||||
username = getattr(user, "username", None)
|
||||
|
||||
# 获取客户端信息
|
||||
ip_address = request.client.host if request.client else None
|
||||
user_agent = request.headers.get("user-agent")
|
||||
|
||||
# 确定操作类型
|
||||
action = self._determine_action(request)
|
||||
resource_type, resource_id = self._extract_resource_info(request)
|
||||
|
||||
# 记录审计日志
|
||||
try:
|
||||
await log_audit_event(
|
||||
action=action,
|
||||
user_id=user_id,
|
||||
username=username,
|
||||
resource_type=resource_type,
|
||||
resource_id=resource_id,
|
||||
ip_address=ip_address,
|
||||
user_agent=user_agent,
|
||||
request_method=request.method,
|
||||
request_path=str(request.url.path),
|
||||
request_data=request_data,
|
||||
response_status=response.status_code,
|
||||
error_message=(
|
||||
None
|
||||
if response.status_code < 400
|
||||
else f"HTTP {response.status_code}"
|
||||
),
|
||||
db=default_db,
|
||||
)
|
||||
except Exception as e:
|
||||
# 审计失败不应影响响应
|
||||
logger.error(f"Failed to log audit event: {e}", exc_info=True)
|
||||
|
||||
# 添加处理时间到响应头
|
||||
process_time = time.time() - start_time
|
||||
response.headers["X-Process-Time"] = str(process_time)
|
||||
|
||||
return response
|
||||
|
||||
def _determine_action(self, request: Request) -> str:
|
||||
"""根据请求路径和方法确定操作类型"""
|
||||
path = request.url.path.lower()
|
||||
method = request.method
|
||||
|
||||
# 认证相关
|
||||
if "login" in path:
|
||||
return AuditAction.LOGIN
|
||||
elif "logout" in path:
|
||||
return AuditAction.LOGOUT
|
||||
elif "register" in path:
|
||||
return AuditAction.REGISTER
|
||||
|
||||
# CRUD 操作
|
||||
if method == "POST":
|
||||
return AuditAction.CREATE
|
||||
elif method == "PUT" or method == "PATCH":
|
||||
return AuditAction.UPDATE
|
||||
elif method == "DELETE":
|
||||
return AuditAction.DELETE
|
||||
elif method == "GET":
|
||||
return AuditAction.READ
|
||||
|
||||
return f"{method}_REQUEST"
|
||||
|
||||
def _extract_resource_info(self, request: Request) -> tuple:
|
||||
"""从请求路径提取资源类型和ID"""
|
||||
path_parts = request.url.path.strip("/").split("/")
|
||||
|
||||
resource_type = None
|
||||
resource_id = None
|
||||
|
||||
# 尝试从路径中提取资源信息
|
||||
# 例如: /api/v1/users/123 -> resource_type=user, resource_id=123
|
||||
if len(path_parts) >= 4:
|
||||
resource_type = path_parts[3].rstrip("s") # 移除复数s
|
||||
|
||||
if len(path_parts) >= 5 and path_parts[4].isdigit():
|
||||
resource_id = path_parts[4]
|
||||
|
||||
return resource_type, resource_id
|
||||
@@ -13,8 +13,8 @@ from typing import List, Dict
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from influxdb_client.client.write_api import SYNCHRONOUS, ASYNCHRONOUS
|
||||
from dateutil import parser
|
||||
import get_realValue
|
||||
import get_data
|
||||
# import get_realValue
|
||||
# import get_data
|
||||
import psycopg
|
||||
import time
|
||||
import app.services.simulation as simulation
|
||||
|
||||
@@ -4,8 +4,8 @@ from datetime import datetime, timedelta
|
||||
from psycopg import AsyncConnection
|
||||
import pandas as pd
|
||||
import numpy as np
|
||||
from app.algorithms.api_ex.Fdataclean import clean_flow_data_df_kf
|
||||
from app.algorithms.api_ex.Pdataclean import clean_pressure_data_df_km
|
||||
from app.algorithms.api_ex.flow_data_clean import clean_flow_data_df_kf
|
||||
from app.algorithms.api_ex.pressure_data_clean import clean_pressure_data_df_km
|
||||
from app.algorithms.api_ex.pipeline_health_analyzer import PipelineHealthAnalyzer
|
||||
|
||||
from app.infra.db.postgresql.internal_queries import InternalQueries
|
||||
@@ -405,12 +405,8 @@ class CompositeQueries:
|
||||
pressure_df = df[pressure_ids]
|
||||
# 重置索引,将 time 变为普通列
|
||||
pressure_df = pressure_df.reset_index()
|
||||
# 移除 time 列,准备输入给清洗方法
|
||||
value_df = pressure_df.drop(columns=["time"])
|
||||
# 调用清洗方法
|
||||
cleaned_value_df = clean_pressure_data_df_km(value_df)
|
||||
# 添加 time 列到首列
|
||||
cleaned_df = pd.concat([pressure_df["time"], cleaned_value_df], axis=1)
|
||||
cleaned_df = clean_pressure_data_df_km(pressure_df)
|
||||
# 将清洗后的数据写回数据库
|
||||
for device_id in pressure_ids:
|
||||
if device_id in cleaned_df.columns:
|
||||
@@ -432,12 +428,8 @@ class CompositeQueries:
|
||||
flow_df = df[flow_ids]
|
||||
# 重置索引,将 time 变为普通列
|
||||
flow_df = flow_df.reset_index()
|
||||
# 移除 time 列,准备输入给清洗方法
|
||||
value_df = flow_df.drop(columns=["time"])
|
||||
# 调用清洗方法
|
||||
cleaned_value_df = clean_flow_data_df_kf(value_df)
|
||||
# 添加 time 列到首列
|
||||
cleaned_df = pd.concat([flow_df["time"], cleaned_value_df], axis=1)
|
||||
cleaned_df = clean_flow_data_df_kf(flow_df)
|
||||
# 将清洗后的数据写回数据库
|
||||
for device_id in flow_ids:
|
||||
if device_id in cleaned_df.columns:
|
||||
|
||||
220
app/infra/repositories/audit_repository.py
Normal file
220
app/infra/repositories/audit_repository.py
Normal file
@@ -0,0 +1,220 @@
|
||||
from typing import Optional, List
|
||||
from datetime import datetime
|
||||
import json
|
||||
from app.infra.db.postgresql.database import Database
|
||||
from app.domain.schemas.audit import AuditLogCreate, AuditLogResponse
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class AuditRepository:
|
||||
"""审计日志数据访问层"""
|
||||
|
||||
def __init__(self, db: Database):
|
||||
self.db = db
|
||||
|
||||
async def create_log(
|
||||
self,
|
||||
user_id: Optional[int] = None,
|
||||
username: Optional[str] = None,
|
||||
action: str = "",
|
||||
resource_type: Optional[str] = None,
|
||||
resource_id: Optional[str] = None,
|
||||
ip_address: Optional[str] = None,
|
||||
user_agent: Optional[str] = None,
|
||||
request_method: Optional[str] = None,
|
||||
request_path: Optional[str] = None,
|
||||
request_data: Optional[dict] = None,
|
||||
response_status: Optional[int] = None,
|
||||
error_message: Optional[str] = None
|
||||
) -> Optional[AuditLogResponse]:
|
||||
"""
|
||||
创建审计日志
|
||||
|
||||
Args:
|
||||
参数说明见 AuditLogCreate
|
||||
|
||||
Returns:
|
||||
创建的审计日志对象
|
||||
"""
|
||||
query = """
|
||||
INSERT INTO audit_logs (
|
||||
user_id, username, action, resource_type, resource_id,
|
||||
ip_address, user_agent, request_method, request_path,
|
||||
request_data, response_status, error_message
|
||||
)
|
||||
VALUES (
|
||||
%(user_id)s, %(username)s, %(action)s, %(resource_type)s, %(resource_id)s,
|
||||
%(ip_address)s, %(user_agent)s, %(request_method)s, %(request_path)s,
|
||||
%(request_data)s, %(response_status)s, %(error_message)s
|
||||
)
|
||||
RETURNING id, user_id, username, action, resource_type, resource_id,
|
||||
ip_address, user_agent, request_method, request_path,
|
||||
request_data, response_status, error_message, timestamp
|
||||
"""
|
||||
|
||||
try:
|
||||
async with self.db.get_connection() as conn:
|
||||
async with conn.cursor() as cur:
|
||||
await cur.execute(query, {
|
||||
'user_id': user_id,
|
||||
'username': username,
|
||||
'action': action,
|
||||
'resource_type': resource_type,
|
||||
'resource_id': resource_id,
|
||||
'ip_address': ip_address,
|
||||
'user_agent': user_agent,
|
||||
'request_method': request_method,
|
||||
'request_path': request_path,
|
||||
'request_data': json.dumps(request_data) if request_data else None,
|
||||
'response_status': response_status,
|
||||
'error_message': error_message
|
||||
})
|
||||
row = await cur.fetchone()
|
||||
if row:
|
||||
return AuditLogResponse(**row)
|
||||
except Exception as e:
|
||||
logger.error(f"Error creating audit log: {e}")
|
||||
raise
|
||||
|
||||
return None
|
||||
|
||||
async def get_logs(
|
||||
self,
|
||||
user_id: Optional[int] = None,
|
||||
username: Optional[str] = None,
|
||||
action: Optional[str] = None,
|
||||
resource_type: Optional[str] = None,
|
||||
start_time: Optional[datetime] = None,
|
||||
end_time: Optional[datetime] = None,
|
||||
skip: int = 0,
|
||||
limit: int = 100
|
||||
) -> List[AuditLogResponse]:
|
||||
"""
|
||||
查询审计日志
|
||||
|
||||
Args:
|
||||
user_id: 用户ID过滤
|
||||
username: 用户名过滤
|
||||
action: 操作类型过滤
|
||||
resource_type: 资源类型过滤
|
||||
start_time: 开始时间
|
||||
end_time: 结束时间
|
||||
skip: 跳过记录数
|
||||
limit: 限制记录数
|
||||
|
||||
Returns:
|
||||
审计日志列表
|
||||
"""
|
||||
# 构建动态查询
|
||||
conditions = []
|
||||
params = {'skip': skip, 'limit': limit}
|
||||
|
||||
if user_id is not None:
|
||||
conditions.append("user_id = %(user_id)s")
|
||||
params['user_id'] = user_id
|
||||
|
||||
if username:
|
||||
conditions.append("username = %(username)s")
|
||||
params['username'] = username
|
||||
|
||||
if action:
|
||||
conditions.append("action = %(action)s")
|
||||
params['action'] = action
|
||||
|
||||
if resource_type:
|
||||
conditions.append("resource_type = %(resource_type)s")
|
||||
params['resource_type'] = resource_type
|
||||
|
||||
if start_time:
|
||||
conditions.append("timestamp >= %(start_time)s")
|
||||
params['start_time'] = start_time
|
||||
|
||||
if end_time:
|
||||
conditions.append("timestamp <= %(end_time)s")
|
||||
params['end_time'] = end_time
|
||||
|
||||
where_clause = f"WHERE {' AND '.join(conditions)}" if conditions else ""
|
||||
|
||||
query = f"""
|
||||
SELECT id, user_id, username, action, resource_type, resource_id,
|
||||
ip_address, user_agent, request_method, request_path,
|
||||
request_data, response_status, error_message, timestamp
|
||||
FROM audit_logs
|
||||
{where_clause}
|
||||
ORDER BY timestamp DESC
|
||||
LIMIT %(limit)s OFFSET %(skip)s
|
||||
"""
|
||||
|
||||
try:
|
||||
async with self.db.get_connection() as conn:
|
||||
async with conn.cursor() as cur:
|
||||
await cur.execute(query, params)
|
||||
rows = await cur.fetchall()
|
||||
return [AuditLogResponse(**row) for row in rows]
|
||||
except Exception as e:
|
||||
logger.error(f"Error querying audit logs: {e}")
|
||||
raise
|
||||
|
||||
async def get_log_count(
|
||||
self,
|
||||
user_id: Optional[int] = None,
|
||||
username: Optional[str] = None,
|
||||
action: Optional[str] = None,
|
||||
resource_type: Optional[str] = None,
|
||||
start_time: Optional[datetime] = None,
|
||||
end_time: Optional[datetime] = None
|
||||
) -> int:
|
||||
"""
|
||||
获取审计日志数量
|
||||
|
||||
Args:
|
||||
参数同 get_logs
|
||||
|
||||
Returns:
|
||||
日志总数
|
||||
"""
|
||||
conditions = []
|
||||
params = {}
|
||||
|
||||
if user_id is not None:
|
||||
conditions.append("user_id = %(user_id)s")
|
||||
params['user_id'] = user_id
|
||||
|
||||
if username:
|
||||
conditions.append("username = %(username)s")
|
||||
params['username'] = username
|
||||
|
||||
if action:
|
||||
conditions.append("action = %(action)s")
|
||||
params['action'] = action
|
||||
|
||||
if resource_type:
|
||||
conditions.append("resource_type = %(resource_type)s")
|
||||
params['resource_type'] = resource_type
|
||||
|
||||
if start_time:
|
||||
conditions.append("timestamp >= %(start_time)s")
|
||||
params['start_time'] = start_time
|
||||
|
||||
if end_time:
|
||||
conditions.append("timestamp <= %(end_time)s")
|
||||
params['end_time'] = end_time
|
||||
|
||||
where_clause = f"WHERE {' AND '.join(conditions)}" if conditions else ""
|
||||
|
||||
query = f"""
|
||||
SELECT COUNT(*) as count
|
||||
FROM audit_logs
|
||||
{where_clause}
|
||||
"""
|
||||
|
||||
try:
|
||||
async with self.db.get_connection() as conn:
|
||||
async with conn.cursor() as cur:
|
||||
await cur.execute(query, params)
|
||||
result = await cur.fetchone()
|
||||
return result['count'] if result else 0
|
||||
except Exception as e:
|
||||
logger.error(f"Error counting audit logs: {e}")
|
||||
return 0
|
||||
235
app/infra/repositories/user_repository.py
Normal file
235
app/infra/repositories/user_repository.py
Normal file
@@ -0,0 +1,235 @@
|
||||
from typing import Optional, List
|
||||
from datetime import datetime
|
||||
from app.infra.db.postgresql.database import Database
|
||||
from app.domain.schemas.user import UserCreate, UserUpdate, UserInDB
|
||||
from app.domain.models.role import UserRole
|
||||
from app.core.security import get_password_hash
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class UserRepository:
|
||||
"""用户数据访问层"""
|
||||
|
||||
def __init__(self, db: Database):
|
||||
self.db = db
|
||||
|
||||
async def create_user(self, user: UserCreate) -> Optional[UserInDB]:
|
||||
"""
|
||||
创建新用户
|
||||
|
||||
Args:
|
||||
user: 用户创建数据
|
||||
|
||||
Returns:
|
||||
创建的用户对象
|
||||
"""
|
||||
hashed_password = get_password_hash(user.password)
|
||||
|
||||
query = """
|
||||
INSERT INTO users (username, email, hashed_password, role, is_active, is_superuser)
|
||||
VALUES (%(username)s, %(email)s, %(hashed_password)s, %(role)s, TRUE, FALSE)
|
||||
RETURNING id, username, email, hashed_password, role, is_active, is_superuser,
|
||||
created_at, updated_at
|
||||
"""
|
||||
|
||||
try:
|
||||
async with self.db.get_connection() as conn:
|
||||
async with conn.cursor() as cur:
|
||||
await cur.execute(query, {
|
||||
'username': user.username,
|
||||
'email': user.email,
|
||||
'hashed_password': hashed_password,
|
||||
'role': user.role.value
|
||||
})
|
||||
row = await cur.fetchone()
|
||||
if row:
|
||||
return UserInDB(**row)
|
||||
except Exception as e:
|
||||
logger.error(f"Error creating user: {e}")
|
||||
raise
|
||||
|
||||
return None
|
||||
|
||||
async def get_user_by_id(self, user_id: int) -> Optional[UserInDB]:
|
||||
"""根据ID获取用户"""
|
||||
query = """
|
||||
SELECT id, username, email, hashed_password, role, is_active, is_superuser,
|
||||
created_at, updated_at
|
||||
FROM users
|
||||
WHERE id = %(user_id)s
|
||||
"""
|
||||
|
||||
async with self.db.get_connection() as conn:
|
||||
async with conn.cursor() as cur:
|
||||
await cur.execute(query, {'user_id': user_id})
|
||||
row = await cur.fetchone()
|
||||
if row:
|
||||
return UserInDB(**row)
|
||||
|
||||
return None
|
||||
|
||||
async def get_user_by_username(self, username: str) -> Optional[UserInDB]:
|
||||
"""根据用户名获取用户"""
|
||||
query = """
|
||||
SELECT id, username, email, hashed_password, role, is_active, is_superuser,
|
||||
created_at, updated_at
|
||||
FROM users
|
||||
WHERE username = %(username)s
|
||||
"""
|
||||
|
||||
async with self.db.get_connection() as conn:
|
||||
async with conn.cursor() as cur:
|
||||
await cur.execute(query, {'username': username})
|
||||
row = await cur.fetchone()
|
||||
if row:
|
||||
return UserInDB(**row)
|
||||
|
||||
return None
|
||||
|
||||
async def get_user_by_email(self, email: str) -> Optional[UserInDB]:
|
||||
"""根据邮箱获取用户"""
|
||||
query = """
|
||||
SELECT id, username, email, hashed_password, role, is_active, is_superuser,
|
||||
created_at, updated_at
|
||||
FROM users
|
||||
WHERE email = %(email)s
|
||||
"""
|
||||
|
||||
async with self.db.get_connection() as conn:
|
||||
async with conn.cursor() as cur:
|
||||
await cur.execute(query, {'email': email})
|
||||
row = await cur.fetchone()
|
||||
if row:
|
||||
return UserInDB(**row)
|
||||
|
||||
return None
|
||||
|
||||
async def get_all_users(self, skip: int = 0, limit: int = 100) -> List[UserInDB]:
|
||||
"""获取所有用户(分页)"""
|
||||
query = """
|
||||
SELECT id, username, email, hashed_password, role, is_active, is_superuser,
|
||||
created_at, updated_at
|
||||
FROM users
|
||||
ORDER BY created_at DESC
|
||||
LIMIT %(limit)s OFFSET %(skip)s
|
||||
"""
|
||||
|
||||
async with self.db.get_connection() as conn:
|
||||
async with conn.cursor() as cur:
|
||||
await cur.execute(query, {'skip': skip, 'limit': limit})
|
||||
rows = await cur.fetchall()
|
||||
return [UserInDB(**row) for row in rows]
|
||||
|
||||
async def update_user(self, user_id: int, user_update: UserUpdate) -> Optional[UserInDB]:
|
||||
"""
|
||||
更新用户信息
|
||||
|
||||
Args:
|
||||
user_id: 用户ID
|
||||
user_update: 更新数据
|
||||
|
||||
Returns:
|
||||
更新后的用户对象
|
||||
"""
|
||||
# 构建动态更新语句
|
||||
update_fields = []
|
||||
params = {'user_id': user_id}
|
||||
|
||||
if user_update.email is not None:
|
||||
update_fields.append("email = %(email)s")
|
||||
params['email'] = user_update.email
|
||||
|
||||
if user_update.password is not None:
|
||||
update_fields.append("hashed_password = %(hashed_password)s")
|
||||
params['hashed_password'] = get_password_hash(user_update.password)
|
||||
|
||||
if user_update.role is not None:
|
||||
update_fields.append("role = %(role)s")
|
||||
params['role'] = user_update.role.value
|
||||
|
||||
if user_update.is_active is not None:
|
||||
update_fields.append("is_active = %(is_active)s")
|
||||
params['is_active'] = user_update.is_active
|
||||
|
||||
if not update_fields:
|
||||
return await self.get_user_by_id(user_id)
|
||||
|
||||
query = f"""
|
||||
UPDATE users
|
||||
SET {', '.join(update_fields)}, updated_at = CURRENT_TIMESTAMP
|
||||
WHERE id = %(user_id)s
|
||||
RETURNING id, username, email, hashed_password, role, is_active, is_superuser,
|
||||
created_at, updated_at
|
||||
"""
|
||||
|
||||
try:
|
||||
async with self.db.get_connection() as conn:
|
||||
async with conn.cursor() as cur:
|
||||
await cur.execute(query, params)
|
||||
row = await cur.fetchone()
|
||||
if row:
|
||||
return UserInDB(**row)
|
||||
except Exception as e:
|
||||
logger.error(f"Error updating user {user_id}: {e}")
|
||||
raise
|
||||
|
||||
return None
|
||||
|
||||
async def delete_user(self, user_id: int) -> bool:
|
||||
"""
|
||||
删除用户
|
||||
|
||||
Args:
|
||||
user_id: 用户ID
|
||||
|
||||
Returns:
|
||||
是否成功删除
|
||||
"""
|
||||
query = "DELETE FROM users WHERE id = %(user_id)s"
|
||||
|
||||
try:
|
||||
async with self.db.get_connection() as conn:
|
||||
async with conn.cursor() as cur:
|
||||
await cur.execute(query, {'user_id': user_id})
|
||||
return cur.rowcount > 0
|
||||
except Exception as e:
|
||||
logger.error(f"Error deleting user {user_id}: {e}")
|
||||
return False
|
||||
|
||||
async def user_exists(self, username: str = None, email: str = None) -> bool:
|
||||
"""
|
||||
检查用户是否存在
|
||||
|
||||
Args:
|
||||
username: 用户名
|
||||
email: 邮箱
|
||||
|
||||
Returns:
|
||||
是否存在
|
||||
"""
|
||||
conditions = []
|
||||
params = {}
|
||||
|
||||
if username:
|
||||
conditions.append("username = %(username)s")
|
||||
params['username'] = username
|
||||
|
||||
if email:
|
||||
conditions.append("email = %(email)s")
|
||||
params['email'] = email
|
||||
|
||||
if not conditions:
|
||||
return False
|
||||
|
||||
query = f"""
|
||||
SELECT EXISTS(
|
||||
SELECT 1 FROM users WHERE {' OR '.join(conditions)}
|
||||
)
|
||||
"""
|
||||
|
||||
async with self.db.get_connection() as conn:
|
||||
async with conn.cursor() as cur:
|
||||
await cur.execute(query, params)
|
||||
result = await cur.fetchone()
|
||||
return result['exists'] if result else False
|
||||
28
app/main.py
28
app/main.py
@@ -10,6 +10,10 @@ from app.api.v1.router import api_router
|
||||
from app.infra.db.timescaledb.database import db as tsdb
|
||||
from app.infra.db.postgresql.database import db as pgdb
|
||||
from app.services.tjnetwork import open_project
|
||||
from app.core.config import settings
|
||||
|
||||
# 导入审计中间件
|
||||
from app.infra.audit.middleware import AuditMiddleware
|
||||
|
||||
|
||||
logger = logging.getLogger()
|
||||
@@ -30,17 +34,29 @@ async def lifespan(app: FastAPI):
|
||||
await tsdb.open()
|
||||
await pgdb.open()
|
||||
|
||||
# 将数据库实例存储到 app.state,供依赖项使用
|
||||
app.state.db = pgdb
|
||||
logger.info("Database connection pool initialized and stored in app.state")
|
||||
|
||||
if project_info.name:
|
||||
print(project_info.name)
|
||||
open_project(project_info.name)
|
||||
|
||||
yield
|
||||
# 清理资源
|
||||
tsdb.close()
|
||||
pgdb.close()
|
||||
await tsdb.close()
|
||||
await pgdb.close()
|
||||
logger.info("Database connections closed")
|
||||
|
||||
|
||||
app = FastAPI(lifespan=lifespan)
|
||||
app = FastAPI(
|
||||
lifespan=lifespan,
|
||||
title=settings.PROJECT_NAME,
|
||||
description="TJWater Server - 供水管网智能管理系统",
|
||||
version="1.0.0",
|
||||
docs_url="/docs",
|
||||
redoc_url="/redoc",
|
||||
)
|
||||
|
||||
# 配置 CORS 中间件
|
||||
app.add_middleware(
|
||||
@@ -53,7 +69,11 @@ app.add_middleware(
|
||||
|
||||
app.add_middleware(GZipMiddleware, minimum_size=1000)
|
||||
|
||||
# 添加审计中间件(可选,记录关键操作)
|
||||
# 如果需要启用审计日志,取消下面的注释
|
||||
app.add_middleware(AuditMiddleware)
|
||||
|
||||
# Include Routers
|
||||
app.include_router(api_router, prefix="/api/v1")
|
||||
# Legcy Routers without version prefix
|
||||
# app.include_router(api_router)
|
||||
app.include_router(api_router)
|
||||
|
||||
@@ -30,11 +30,11 @@ class Output:
|
||||
|
||||
if platform.system() == "Windows":
|
||||
self._lib = ctypes.CDLL(
|
||||
os.path.join(os.getcwd(), "epanet", "epanet-output.dll")
|
||||
os.path.join(os.path.dirname(__file__), "windows", "epanet-output.dll")
|
||||
)
|
||||
else:
|
||||
self._lib = ctypes.CDLL(
|
||||
os.path.join(os.getcwd(), "epanet", "linux", "libepanet-output.so")
|
||||
os.path.join(os.path.dirname(__file__), "linux", "libepanet-output.so")
|
||||
)
|
||||
|
||||
self._handle = ctypes.c_void_p()
|
||||
@@ -314,9 +314,9 @@ def run_project_return_dict(name: str, readable_output: bool = False) -> dict[st
|
||||
|
||||
input = name + ".db"
|
||||
if platform.system() == "Windows":
|
||||
exe = os.path.join(os.path.join(dir, "epanet"), "runepanet.exe")
|
||||
exe = os.path.join(os.path.dirname(__file__), "windows", "runepanet.exe")
|
||||
else:
|
||||
exe = os.path.join(os.path.join(dir, "epanet"), "linux", "runepanet")
|
||||
exe = os.path.join(os.path.dirname(__file__), "linux", "runepanet")
|
||||
inp = os.path.join(os.path.join(dir, "db_inp"), input + ".inp")
|
||||
rpt = os.path.join(os.path.join(dir, "temp"), input + ".rpt")
|
||||
opt = os.path.join(os.path.join(dir, "temp"), input + ".opt")
|
||||
@@ -364,9 +364,9 @@ def run_project(name: str, readable_output: bool = False) -> str:
|
||||
|
||||
input = name + ".db"
|
||||
if platform.system() == "Windows":
|
||||
exe = os.path.join(os.path.join(dir, "epanet"), "runepanet.exe")
|
||||
exe = os.path.join(os.path.dirname(__file__), "windows", "runepanet.exe")
|
||||
else:
|
||||
exe = os.path.join(os.path.join(dir, "epanet"), "linux", "runepanet")
|
||||
exe = os.path.join(os.path.dirname(__file__), "linux", "runepanet")
|
||||
inp = os.path.join(os.path.join(dir, "db_inp"), input + ".inp")
|
||||
rpt = os.path.join(os.path.join(dir, "temp"), input + ".rpt")
|
||||
opt = os.path.join(os.path.join(dir, "temp"), input + ".opt")
|
||||
@@ -416,9 +416,9 @@ def run_inp(name: str) -> str:
|
||||
dir = os.path.abspath(os.getcwd())
|
||||
|
||||
if platform.system() == "Windows":
|
||||
exe = os.path.join(os.path.join(dir, "epanet"), "runepanet.exe")
|
||||
exe = os.path.join(os.path.dirname(__file__), "windows", "runepanet.exe")
|
||||
else:
|
||||
exe = os.path.join(os.path.join(dir, "epanet"), "linux", "runepanet")
|
||||
exe = os.path.join(os.path.dirname(__file__), "linux", "runepanet")
|
||||
inp = os.path.join(os.path.join(dir, "inp"), name + ".inp")
|
||||
rpt = os.path.join(os.path.join(dir, "temp"), name + ".rpt")
|
||||
opt = os.path.join(os.path.join(dir, "temp"), name + ".opt")
|
||||
|
||||
@@ -21,8 +21,12 @@ import app.services.globals as globals
|
||||
import uuid
|
||||
import app.services.project_info as project_info
|
||||
from app.native.api.postgresql_info import get_pgconn_string
|
||||
from app.infra.db.timescaledb.internal_queries import InternalQueries as TimescaleInternalQueries
|
||||
from app.infra.db.timescaledb.internal_queries import InternalStorage as TimescaleInternalStorage
|
||||
from app.infra.db.timescaledb.internal_queries import (
|
||||
InternalQueries as TimescaleInternalQueries,
|
||||
)
|
||||
from app.infra.db.timescaledb.internal_queries import (
|
||||
InternalStorage as TimescaleInternalStorage,
|
||||
)
|
||||
|
||||
logging.basicConfig(
|
||||
level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
|
||||
@@ -679,8 +683,8 @@ def run_simulation(
|
||||
modify_fixed_pump_pattern: dict[str, list] = None,
|
||||
modify_variable_pump_pattern: dict[str, list] = None,
|
||||
modify_valve_opening: dict[str, float] = None,
|
||||
scheme_Type: str = None,
|
||||
scheme_Name: str = None,
|
||||
scheme_type: str = None,
|
||||
scheme_name: str = None,
|
||||
) -> None:
|
||||
"""
|
||||
传入需要修改的参数,改变数据库中对应位置的值,然后计算,返回结果
|
||||
@@ -695,8 +699,8 @@ def run_simulation(
|
||||
:param modify_fixed_pump_pattern: dict中包含多个水泵模式,str为工频水泵的id,list为修改后的pattern
|
||||
:param modify_variable_pump_pattern: dict中包含多个水泵模式,str为变频水泵的id,list为修改后的pattern
|
||||
:param modify_valve_opening: dict中包含多个阀门开启度,str为阀门的id,float为修改后的阀门开启度
|
||||
:param scheme_Type: 模拟方案类型
|
||||
:param scheme_Name:模拟方案名称
|
||||
:param scheme_type: 模拟方案类型
|
||||
:param scheme_name:模拟方案名称
|
||||
:return:
|
||||
"""
|
||||
# 记录开始时间
|
||||
@@ -1235,8 +1239,8 @@ def run_simulation(
|
||||
)
|
||||
elif simulation_type.upper() == "EXTENDED":
|
||||
TimescaleInternalStorage.store_scheme_simulation(
|
||||
scheme_Type,
|
||||
scheme_Name,
|
||||
scheme_type,
|
||||
scheme_name,
|
||||
node_result,
|
||||
link_result,
|
||||
modify_pattern_start_time,
|
||||
|
||||
@@ -3,5 +3,7 @@ from typing import Any
|
||||
from app.algorithms.valve_isolation import valve_isolation_analysis
|
||||
|
||||
|
||||
def analyze_valve_isolation(network: str, accident_element: str) -> dict[str, Any]:
|
||||
def analyze_valve_isolation(
|
||||
network: str, accident_element: str | list[str]
|
||||
) -> dict[str, Any]:
|
||||
return valve_isolation_analysis(network, accident_element)
|
||||
|
||||
@@ -65,6 +65,7 @@ services:
|
||||
POSTGRES_DB: ${KEYCLOAK_DB_NAME}
|
||||
POSTGRES_USER: ${KEYCLOAK_DB_USER}
|
||||
POSTGRES_PASSWORD: ${KEYCLOAK_DB_PASSWORD}
|
||||
command: postgres -c wal_level=logical
|
||||
ports:
|
||||
- "${KEYCLOAK_DB_PORT}:5432"
|
||||
volumes:
|
||||
|
||||
41
readme.md
Normal file
41
readme.md
Normal file
@@ -0,0 +1,41 @@
|
||||
# TJWater Server (FastAPI)
|
||||
|
||||
基于 FastAPI 的水务业务服务端,提供模拟计算、SCADA 数据、网络元素、项目管理等接口。
|
||||
|
||||
## 目录结构
|
||||
|
||||
```
|
||||
app/
|
||||
main.py # FastAPI 入口(lifespan、CORS、路由挂载)
|
||||
api/
|
||||
v1/
|
||||
router.py # API 路由汇总(/api/v1 前缀)
|
||||
endpoints/ # 业务接口实现(auth、simulation、scada 等)
|
||||
endpoints/network/ # 管网要素与特性接口
|
||||
endpoints/components/ # 组件/控制相关接口
|
||||
services/ # 业务服务层(simulation、tjnetwork 等)
|
||||
infra/
|
||||
db/ # 数据库访问层(timescaledb / postgresql / influxdb)
|
||||
cache/ # 缓存与 Redis 客户端
|
||||
algorithms/ # 算法与分析模块
|
||||
core/ # 配置与安全相关
|
||||
configs/
|
||||
project_info.yml # 默认工程配置(启动时自动打开)
|
||||
scripts/
|
||||
run_server.py # Uvicorn 启动脚本
|
||||
tests/ # 测试
|
||||
```
|
||||
|
||||
## 启动方式
|
||||
|
||||
1. 安装依赖(示例):
|
||||
```bash
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
2. 启动服务:
|
||||
```bash
|
||||
python scripts/run_server.py
|
||||
```
|
||||
|
||||
默认监听:`http://0.0.0.0:8000`
|
||||
API 前缀:`/api/v1`(见 `app/main.py` 与 `app/api/v1/router.py`)
|
||||
@@ -83,6 +83,7 @@ opentelemetry-semantic-conventions==0.60b1
|
||||
osqp==1.0.5
|
||||
packaging==25.0
|
||||
pandas==2.2.3
|
||||
passlib==1.7.4
|
||||
pathable==0.4.4
|
||||
pathvalidate==3.3.1
|
||||
pillow==11.2.1
|
||||
@@ -120,6 +121,7 @@ pyproj==3.7.1
|
||||
pytest==8.3.5
|
||||
python-dateutil==2.9.0.post0
|
||||
python-dotenv==1.2.1
|
||||
python-jose==3.5.0
|
||||
python-json-logger==4.0.0
|
||||
python-multipart==0.0.20
|
||||
pytz==2025.2
|
||||
|
||||
67
resources/sql/001_create_users_table.sql
Normal file
67
resources/sql/001_create_users_table.sql
Normal file
@@ -0,0 +1,67 @@
|
||||
-- ============================================
|
||||
-- TJWater Server 用户系统数据库迁移脚本
|
||||
-- ============================================
|
||||
|
||||
-- 创建用户表
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id SERIAL PRIMARY KEY,
|
||||
username VARCHAR(50) UNIQUE NOT NULL,
|
||||
email VARCHAR(100) UNIQUE NOT NULL,
|
||||
hashed_password VARCHAR(255) NOT NULL,
|
||||
role VARCHAR(20) DEFAULT 'USER' NOT NULL,
|
||||
is_active BOOLEAN DEFAULT TRUE NOT NULL,
|
||||
is_superuser BOOLEAN DEFAULT FALSE NOT NULL,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
||||
|
||||
CONSTRAINT users_role_check CHECK (role IN ('ADMIN', 'OPERATOR', 'USER', 'VIEWER'))
|
||||
);
|
||||
|
||||
-- 创建索引
|
||||
CREATE INDEX IF NOT EXISTS idx_users_username ON users(username);
|
||||
CREATE INDEX IF NOT EXISTS idx_users_email ON users(email);
|
||||
CREATE INDEX IF NOT EXISTS idx_users_role ON users(role);
|
||||
CREATE INDEX IF NOT EXISTS idx_users_is_active ON users(is_active);
|
||||
|
||||
-- 创建触发器自动更新 updated_at
|
||||
CREATE OR REPLACE FUNCTION update_updated_at_column()
|
||||
RETURNS TRIGGER AS $$
|
||||
BEGIN
|
||||
NEW.updated_at = CURRENT_TIMESTAMP;
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
DROP TRIGGER IF EXISTS update_users_updated_at ON users;
|
||||
CREATE TRIGGER update_users_updated_at
|
||||
BEFORE UPDATE ON users
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION update_updated_at_column();
|
||||
|
||||
-- 创建默认管理员账号 (密码: admin123)
|
||||
INSERT INTO users (username, email, hashed_password, role, is_superuser)
|
||||
VALUES (
|
||||
'admin',
|
||||
'admin@tjwater.com',
|
||||
'$2b$12$LQv3c1yqBWVHxkd0LHAkCOYz6TtxMQJqhN8/LewY5aeAJK.1tYKAW',
|
||||
'ADMIN',
|
||||
TRUE
|
||||
) ON CONFLICT (username) DO NOTHING;
|
||||
|
||||
-- 迁移现有硬编码用户 (tjwater/tjwater@123)
|
||||
INSERT INTO users (username, email, hashed_password, role, is_superuser)
|
||||
VALUES (
|
||||
'tjwater',
|
||||
'tjwater@tjwater.com',
|
||||
'$2b$12$EixZaYVK1fsbw1ZfbX3OXePaWxn96p36WQoeG6Lruj3vjPGga31lW',
|
||||
'ADMIN',
|
||||
TRUE
|
||||
) ON CONFLICT (username) DO NOTHING;
|
||||
|
||||
-- 添加注释
|
||||
COMMENT ON TABLE users IS '用户表 - 存储系统用户信息';
|
||||
COMMENT ON COLUMN users.id IS '用户ID(主键)';
|
||||
COMMENT ON COLUMN users.username IS '用户名(唯一)';
|
||||
COMMENT ON COLUMN users.email IS '邮箱地址(唯一)';
|
||||
COMMENT ON COLUMN users.hashed_password IS 'bcrypt 密码哈希';
|
||||
COMMENT ON COLUMN users.role IS '用户角色: ADMIN, OPERATOR, USER, VIEWER';
|
||||
45
resources/sql/002_create_audit_logs_table.sql
Normal file
45
resources/sql/002_create_audit_logs_table.sql
Normal file
@@ -0,0 +1,45 @@
|
||||
-- ============================================
|
||||
-- TJWater Server 审计日志表迁移脚本
|
||||
-- ============================================
|
||||
|
||||
-- 创建审计日志表
|
||||
CREATE TABLE IF NOT EXISTS audit_logs (
|
||||
id SERIAL PRIMARY KEY,
|
||||
user_id INTEGER REFERENCES users(id) ON DELETE SET NULL,
|
||||
username VARCHAR(50),
|
||||
action VARCHAR(50) NOT NULL,
|
||||
resource_type VARCHAR(50),
|
||||
resource_id VARCHAR(100),
|
||||
ip_address VARCHAR(45),
|
||||
user_agent TEXT,
|
||||
request_method VARCHAR(10),
|
||||
request_path TEXT,
|
||||
request_data JSONB,
|
||||
response_status INTEGER,
|
||||
error_message TEXT,
|
||||
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
|
||||
);
|
||||
|
||||
-- 创建索引以提高查询性能
|
||||
CREATE INDEX IF NOT EXISTS idx_audit_logs_user_id ON audit_logs(user_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_audit_logs_username ON audit_logs(username);
|
||||
CREATE INDEX IF NOT EXISTS idx_audit_logs_timestamp ON audit_logs(timestamp DESC);
|
||||
CREATE INDEX IF NOT EXISTS idx_audit_logs_action ON audit_logs(action);
|
||||
CREATE INDEX IF NOT EXISTS idx_audit_logs_resource ON audit_logs(resource_type, resource_id);
|
||||
|
||||
-- 添加注释
|
||||
COMMENT ON TABLE audit_logs IS '审计日志表 - 记录所有关键操作';
|
||||
COMMENT ON COLUMN audit_logs.id IS '日志ID(主键)';
|
||||
COMMENT ON COLUMN audit_logs.user_id IS '用户ID(外键)';
|
||||
COMMENT ON COLUMN audit_logs.username IS '用户名(冗余字段,用于用户删除后仍可查询)';
|
||||
COMMENT ON COLUMN audit_logs.action IS '操作类型(如:LOGIN, LOGOUT, CREATE, UPDATE, DELETE)';
|
||||
COMMENT ON COLUMN audit_logs.resource_type IS '资源类型(如:user, project, network)';
|
||||
COMMENT ON COLUMN audit_logs.resource_id IS '资源ID';
|
||||
COMMENT ON COLUMN audit_logs.ip_address IS '客户端IP地址';
|
||||
COMMENT ON COLUMN audit_logs.user_agent IS '客户端User-Agent';
|
||||
COMMENT ON COLUMN audit_logs.request_method IS 'HTTP请求方法';
|
||||
COMMENT ON COLUMN audit_logs.request_path IS '请求路径';
|
||||
COMMENT ON COLUMN audit_logs.request_data IS '请求数据(JSON格式,敏感信息已脱敏)';
|
||||
COMMENT ON COLUMN audit_logs.response_status IS 'HTTP响应状态码';
|
||||
COMMENT ON COLUMN audit_logs.error_message IS '错误消息(如果有)';
|
||||
COMMENT ON COLUMN audit_logs.timestamp IS '操作时间';
|
||||
@@ -7,18 +7,19 @@ import csv
|
||||
# get_data 是用来获取 历史数据,也就是非实时数据的接口
|
||||
# get_realtime 是用来获取 实时数据
|
||||
|
||||
|
||||
def convert_timestamp_to_beijing_time(timestamp: Union[int, float]) -> datetime:
|
||||
# 将毫秒级时间戳转换为秒级时间戳
|
||||
timestamp_seconds = timestamp / 1000
|
||||
|
||||
# 将时间戳转换为datetime对象
|
||||
utc_time = datetime.utcfromtimestamp(timestamp_seconds)
|
||||
utc_time = datetime.fromtimestamp(timestamp_seconds)
|
||||
|
||||
# 设定UTC时区
|
||||
utc_timezone = pytz.timezone('UTC')
|
||||
utc_timezone = pytz.timezone("UTC")
|
||||
|
||||
# 转换为北京时间
|
||||
beijing_timezone = pytz.timezone('Asia/Shanghai')
|
||||
beijing_timezone = pytz.timezone("Asia/Shanghai")
|
||||
beijing_time = utc_time.replace(tzinfo=utc_timezone).astimezone(beijing_timezone)
|
||||
|
||||
return beijing_time
|
||||
@@ -26,10 +27,10 @@ def convert_timestamp_to_beijing_time(timestamp: Union[int, float]) -> datetime:
|
||||
|
||||
def beijing_time_to_utc(beijing_time_str: str) -> str:
|
||||
# 定义北京时区
|
||||
beijing_timezone = pytz.timezone('Asia/Shanghai')
|
||||
beijing_timezone = pytz.timezone("Asia/Shanghai")
|
||||
|
||||
# 将字符串转换为datetime对象
|
||||
beijing_time = datetime.strptime(beijing_time_str, '%Y-%m-%d %H:%M:%S')
|
||||
beijing_time = datetime.strptime(beijing_time_str, "%Y-%m-%d %H:%M:%S")
|
||||
|
||||
# 本地化时间对象
|
||||
beijing_time = beijing_timezone.localize(beijing_time)
|
||||
@@ -38,26 +39,28 @@ def beijing_time_to_utc(beijing_time_str: str) -> str:
|
||||
utc_time = beijing_time.astimezone(pytz.utc)
|
||||
|
||||
# 转换为ISO 8601格式的字符串
|
||||
return utc_time.strftime('%Y-%m-%dT%H:%M:%SZ')
|
||||
return utc_time.strftime("%Y-%m-%dT%H:%M:%SZ")
|
||||
|
||||
|
||||
def get_history_data(ids: str, begin_date: str, end_date: str, downsample: Optional[str]) -> List[Dict[str, Union[str, datetime, int, float]]]:
|
||||
def get_history_data(
|
||||
ids: str, begin_date: str, end_date: str, downsample: Optional[str]
|
||||
) -> List[Dict[str, Union[str, datetime, int, float]]]:
|
||||
# def get_history_data(ids: str, begin_date: str, end_date: str, downsample: Optional[str]) -> None:
|
||||
# 转换输入的北京时间为UTC时间
|
||||
begin_date_utc = beijing_time_to_utc(begin_date)
|
||||
end_date_utc = beijing_time_to_utc(end_date)
|
||||
|
||||
# 数据接口的地址
|
||||
url = 'http://183.64.62.100:9057/loong/api/curves/data'
|
||||
url = "http://183.64.62.100:9057/loong/api/curves/data"
|
||||
# url = 'http://10.101.15.16:9000/loong/api/curves/data'
|
||||
# url_path = 'http://10.101.15.16:9000/loong' # 内网
|
||||
|
||||
# 设置 GET 请求的参数
|
||||
params = {
|
||||
'ids': ids,
|
||||
'beginDate': begin_date_utc,
|
||||
'endDate': end_date_utc,
|
||||
'downsample': downsample
|
||||
"ids": ids,
|
||||
"beginDate": begin_date_utc,
|
||||
"endDate": end_date_utc,
|
||||
"downsample": downsample,
|
||||
}
|
||||
|
||||
history_data_list = []
|
||||
@@ -73,24 +76,26 @@ def get_history_data(ids: str, begin_date: str, end_date: str, downsample: Optio
|
||||
# 这里可以对获取到的数据进行进一步处理
|
||||
|
||||
# 打印 'mpointId' 和 'mpointName'
|
||||
for item in data['items']:
|
||||
mpoint_id = str(item['mpointId'])
|
||||
mpoint_name = item['mpointName']
|
||||
for item in data["items"]:
|
||||
mpoint_id = str(item["mpointId"])
|
||||
mpoint_name = item["mpointName"]
|
||||
# print("mpointId:", item['mpointId'])
|
||||
# print("mpointName:", item['mpointName'])
|
||||
|
||||
# 打印 'dataDate' 和 'dataValue'
|
||||
for item_data in item['data']:
|
||||
for item_data in item["data"]:
|
||||
# 将时间戳转换为北京时间
|
||||
beijing_time = convert_timestamp_to_beijing_time(item_data['dataDate'])
|
||||
data_value = item_data['dataValue']
|
||||
beijing_time = convert_timestamp_to_beijing_time(
|
||||
item_data["dataDate"]
|
||||
)
|
||||
data_value = item_data["dataValue"]
|
||||
# 创建一个字典存储每条数据
|
||||
data_dict = {
|
||||
'time': beijing_time,
|
||||
'device_ID': str(mpoint_id),
|
||||
'description': mpoint_name,
|
||||
"time": beijing_time,
|
||||
"device_ID": str(mpoint_id),
|
||||
"description": mpoint_name,
|
||||
# 'dataDate (Beijing Time)': beijing_time.strftime('%Y-%m-%d %H:%M:%S'),
|
||||
'monitored_value': data_value # 保留原有类型
|
||||
"monitored_value": data_value, # 保留原有类型
|
||||
}
|
||||
|
||||
history_data_list.append(data_dict)
|
||||
|
||||
@@ -8,34 +8,32 @@ def convert_timestamp_to_beijing_time(timestamp):
|
||||
timestamp_seconds = timestamp / 1000
|
||||
|
||||
# 将时间戳转换为datetime对象
|
||||
utc_time = datetime.utcfromtimestamp(timestamp_seconds)
|
||||
utc_time = datetime.fromtimestamp(timestamp_seconds)
|
||||
|
||||
# 设定UTC时区
|
||||
utc_timezone = pytz.timezone('UTC')
|
||||
utc_timezone = pytz.timezone("UTC")
|
||||
|
||||
# 转换为北京时间
|
||||
beijing_timezone = pytz.timezone('Asia/Shanghai')
|
||||
beijing_timezone = pytz.timezone("Asia/Shanghai")
|
||||
beijing_time = utc_time.replace(tzinfo=utc_timezone).astimezone(beijing_timezone)
|
||||
|
||||
return beijing_time
|
||||
|
||||
|
||||
def conver_beingtime_to_ucttime(timestr: str):
|
||||
beijing_time=datetime.strptime(timestr,'%Y-%m-%d %H:%M:%S')
|
||||
beijing_time = datetime.strptime(timestr, "%Y-%m-%d %H:%M:%S")
|
||||
utc_time = beijing_time.astimezone(pytz.utc)
|
||||
str_utc=utc_time.strftime('%Y-%m-%dT%H:%M:%SZ')
|
||||
str_utc = utc_time.strftime("%Y-%m-%dT%H:%M:%SZ")
|
||||
# print(str_utc)
|
||||
return str_utc
|
||||
|
||||
|
||||
def get_hist_data(ids, begin_date, end_date) -> dict[str, dict[datetime, float]]:
|
||||
# 数据接口的地址
|
||||
url = 'http://183.64.62.100:9057/loong/api/curves/data'
|
||||
url = "http://183.64.62.100:9057/loong/api/curves/data"
|
||||
|
||||
# 设置 GET 请求的参数
|
||||
params = {
|
||||
'ids': ids,
|
||||
'beginDate': begin_date,
|
||||
'endDate': end_date
|
||||
}
|
||||
params = {"ids": ids, "beginDate": begin_date, "endDate": end_date}
|
||||
lst_data = {}
|
||||
try:
|
||||
# 发送 GET 请求获取数据
|
||||
@@ -48,22 +46,27 @@ def get_hist_data(ids, begin_date,end_date)->dict[str,dict[datetime,float]]:
|
||||
# 这里可以对获取到的数据进行进一步处理
|
||||
|
||||
# 打印 'mpointId' 和 'mpointName'
|
||||
for item in data['items']:
|
||||
for item in data["items"]:
|
||||
# print("mpointId:", item['mpointId'])
|
||||
# print("mpointName:", item['mpointName'])
|
||||
|
||||
# 打印 'dataDate' 和 'dataValue'
|
||||
data_seriers = {}
|
||||
for item_data in item['data']:
|
||||
for item_data in item["data"]:
|
||||
# print("dataDate:", item_data['dataDate'])
|
||||
# 将时间戳转换为北京时间
|
||||
beijing_time = convert_timestamp_to_beijing_time(item_data['dataDate'])
|
||||
print("dataDate (Beijing Time):", beijing_time.strftime('%Y-%m-%d %H:%M:%S'))
|
||||
print("dataValue:", item_data['dataValue'])
|
||||
beijing_time = convert_timestamp_to_beijing_time(
|
||||
item_data["dataDate"]
|
||||
)
|
||||
print(
|
||||
"dataDate (Beijing Time):",
|
||||
beijing_time.strftime("%Y-%m-%d %H:%M:%S"),
|
||||
)
|
||||
print("dataValue:", item_data["dataValue"])
|
||||
print() # 打印空行分隔不同条目
|
||||
r=float(item_data['dataValue'])
|
||||
r = float(item_data["dataValue"])
|
||||
data_seriers[beijing_time] = r
|
||||
lst_data[item['mpointId']]=data_seriers
|
||||
lst_data[item["mpointId"]] = data_seriers
|
||||
return lst_data
|
||||
else:
|
||||
# 如果请求不成功,打印错误信息
|
||||
|
||||
@@ -3710,8 +3710,9 @@ async def fastapi_contaminant_simulation(
|
||||
start_time: str,
|
||||
source: str,
|
||||
concentration: float,
|
||||
duration: int = 900,
|
||||
duration: int,
|
||||
pattern: str = None,
|
||||
scheme_Name: str = None,
|
||||
) -> str:
|
||||
filename = "c:/lock.simulation"
|
||||
filename2 = "c:/lock.simulation2"
|
||||
|
||||
@@ -19,8 +19,8 @@ from sqlalchemy import create_engine
|
||||
import ast
|
||||
import app.services.project_info as project_info
|
||||
import app.algorithms.api_ex.kmeans_sensor as kmeans_sensor
|
||||
import app.algorithms.api_ex.Fdataclean as Fdataclean
|
||||
import app.algorithms.api_ex.Pdataclean as Pdataclean
|
||||
import app.algorithms.api_ex.flow_data_clean as flow_data_clean
|
||||
import app.algorithms.api_ex.pressure_data_clean as pressure_data_clean
|
||||
import app.algorithms.api_ex.sensitivity as sensitivity
|
||||
from app.native.api.postgresql_info import get_pgconn_string
|
||||
|
||||
@@ -56,7 +56,7 @@ def burst_analysis(
|
||||
modify_fixed_pump_pattern: dict[str, list] = None,
|
||||
modify_variable_pump_pattern: dict[str, list] = None,
|
||||
modify_valve_opening: dict[str, float] = None,
|
||||
scheme_Name: str = None,
|
||||
scheme_name: str = None,
|
||||
) -> None:
|
||||
"""
|
||||
爆管模拟
|
||||
@@ -182,8 +182,8 @@ def burst_analysis(
|
||||
modify_fixed_pump_pattern=modify_fixed_pump_pattern,
|
||||
modify_variable_pump_pattern=modify_variable_pump_pattern,
|
||||
modify_valve_opening=modify_valve_opening,
|
||||
scheme_Type="burst_Analysis",
|
||||
scheme_Name=scheme_Name,
|
||||
scheme_type="burst_Analysis",
|
||||
scheme_name=scheme_name,
|
||||
)
|
||||
# step 3. restore the base model status
|
||||
# execute_undo(name) #有疑惑
|
||||
@@ -193,7 +193,7 @@ def burst_analysis(
|
||||
# return result
|
||||
store_scheme_info(
|
||||
name=name,
|
||||
scheme_name=scheme_Name,
|
||||
scheme_name=scheme_name,
|
||||
scheme_type="burst_Analysis",
|
||||
username="admin",
|
||||
scheme_start_time=modify_pattern_start_time,
|
||||
@@ -209,7 +209,7 @@ def valve_close_analysis(
|
||||
modify_pattern_start_time: str,
|
||||
modify_total_duration: int = 900,
|
||||
modify_valve_opening: dict[str, float] = None,
|
||||
scheme_Name: str = None,
|
||||
scheme_name: str = None,
|
||||
) -> None:
|
||||
"""
|
||||
关阀模拟
|
||||
@@ -217,7 +217,7 @@ def valve_close_analysis(
|
||||
:param modify_pattern_start_time: 模拟开始时间,格式为'2024-11-25T09:00:00+08:00'
|
||||
:param modify_total_duration: 模拟总历时,秒
|
||||
:param modify_valve_opening: dict中包含多个阀门开启度,str为阀门的id,float为修改后的阀门开启度
|
||||
:param scheme_Name: 方案名称
|
||||
:param scheme_name: 方案名称
|
||||
:return:
|
||||
"""
|
||||
print(
|
||||
@@ -271,8 +271,8 @@ def valve_close_analysis(
|
||||
modify_pattern_start_time=modify_pattern_start_time,
|
||||
modify_total_duration=modify_total_duration,
|
||||
modify_valve_opening=modify_valve_opening,
|
||||
scheme_Type="valve_close_Analysis",
|
||||
scheme_Name=scheme_Name,
|
||||
scheme_type="valve_close_Analysis",
|
||||
scheme_name=scheme_name,
|
||||
)
|
||||
# step 3. restore the base model
|
||||
# for valve in valves:
|
||||
@@ -1475,7 +1475,7 @@ def flow_data_clean(input_csv_file: str) -> str:
|
||||
if not os.path.exists(input_csv_path):
|
||||
raise FileNotFoundError(f"指定的文件不存在: {input_csv_path}")
|
||||
# 调用 Fdataclean.clean_flow_data_kf 函数进行数据清洗
|
||||
out_xlsx_path = Fdataclean.clean_flow_data_kf(input_csv_path)
|
||||
out_xlsx_path = flow_data_clean.clean_flow_data_kf(input_csv_path)
|
||||
print("清洗后的数据已保存到:", out_xlsx_path)
|
||||
|
||||
|
||||
@@ -1502,7 +1502,7 @@ def pressure_data_clean(input_csv_file: str) -> str:
|
||||
if not os.path.exists(input_csv_path):
|
||||
raise FileNotFoundError(f"指定的文件不存在: {input_csv_path}")
|
||||
# 调用 Fdataclean.clean_flow_data_kf 函数进行数据清洗
|
||||
out_xlsx_path = Pdataclean.clean_pressure_data_km(input_csv_path)
|
||||
out_xlsx_path = pressure_data_clean.clean_pressure_data_km(input_csv_path)
|
||||
print("清洗后的数据已保存到:", out_xlsx_path)
|
||||
|
||||
|
||||
|
||||
@@ -16,6 +16,6 @@ if __name__ == "__main__":
|
||||
"app.main:app",
|
||||
host="0.0.0.0",
|
||||
port=8000,
|
||||
# workers=2, # 这里可以设置多进程
|
||||
workers=2, # 这里可以设置多进程
|
||||
loop="asyncio",
|
||||
)
|
||||
|
||||
122
setup_security.sh
Executable file
122
setup_security.sh
Executable file
@@ -0,0 +1,122 @@
|
||||
#!/bin/bash
|
||||
|
||||
# TJWater Server 安全功能快速设置脚本
|
||||
|
||||
set -e
|
||||
|
||||
echo "=================================="
|
||||
echo "TJWater Server 安全功能设置"
|
||||
echo "=================================="
|
||||
echo ""
|
||||
|
||||
# 颜色定义
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
NC='\033[0m' # No Color
|
||||
|
||||
# 步骤 1: 检查依赖
|
||||
echo "📦 步骤 1/5: 检查 Python 依赖..."
|
||||
if ! python -c "import cryptography, passlib, jose" 2>/dev/null; then
|
||||
echo -e "${YELLOW}缺少依赖,正在安装...${NC}"
|
||||
pip install cryptography passlib python-jose bcrypt
|
||||
else
|
||||
echo -e "${GREEN}✓ 依赖已安装${NC}"
|
||||
fi
|
||||
echo ""
|
||||
|
||||
# 步骤 2: 生成密钥
|
||||
echo "🔑 步骤 2/5: 生成安全密钥..."
|
||||
|
||||
if [ ! -f .env ]; then
|
||||
echo "正在创建 .env 文件..."
|
||||
cp .env.example .env
|
||||
|
||||
# 生成 JWT 密钥
|
||||
JWT_KEY=$(openssl rand -hex 32)
|
||||
sed -i "s/SECRET_KEY=.*/SECRET_KEY=$JWT_KEY/" .env
|
||||
echo -e "${GREEN}✓ JWT 密钥已生成${NC}"
|
||||
|
||||
# 生成加密密钥
|
||||
ENC_KEY=$(python -c "from cryptography.fernet import Fernet; print(Fernet.generate_key().decode())")
|
||||
sed -i "s/ENCRYPTION_KEY=.*/ENCRYPTION_KEY=$ENC_KEY/" .env
|
||||
echo -e "${GREEN}✓ 加密密钥已生成${NC}"
|
||||
else
|
||||
echo -e "${YELLOW}⚠ .env 文件已存在,跳过生成${NC}"
|
||||
fi
|
||||
echo ""
|
||||
|
||||
# 步骤 3: 数据库配置
|
||||
echo "💾 步骤 3/5: 数据库配置..."
|
||||
read -p "请输入数据库名称 [默认: tjwater]: " DB_NAME
|
||||
DB_NAME=${DB_NAME:-tjwater}
|
||||
|
||||
read -p "请输入数据库用户 [默认: postgres]: " DB_USER
|
||||
DB_USER=${DB_USER:-postgres}
|
||||
|
||||
read -sp "请输入数据库密码: " DB_PASS
|
||||
echo ""
|
||||
|
||||
# 更新 .env
|
||||
sed -i "s/DB_NAME=.*/DB_NAME=$DB_NAME/" .env
|
||||
sed -i "s/DB_USER=.*/DB_USER=$DB_USER/" .env
|
||||
sed -i "s/DB_PASSWORD=.*/DB_PASSWORD=$DB_PASS/" .env
|
||||
|
||||
echo -e "${GREEN}✓ 数据库配置已更新${NC}"
|
||||
echo ""
|
||||
|
||||
# 步骤 4: 执行数据库迁移
|
||||
echo "🗄️ 步骤 4/5: 执行数据库迁移..."
|
||||
read -p "是否立即执行数据库迁移?(y/n) [y]: " DO_MIGRATION
|
||||
DO_MIGRATION=${DO_MIGRATION:-y}
|
||||
|
||||
if [ "$DO_MIGRATION" = "y" ]; then
|
||||
echo "正在执行迁移脚本..."
|
||||
|
||||
PGPASSWORD=$DB_PASS psql -U $DB_USER -d $DB_NAME -f resources/sql/001_create_users_table.sql 2>&1 | grep -v "NOTICE"
|
||||
if [ $? -eq 0 ]; then
|
||||
echo -e "${GREEN}✓ 用户表创建成功${NC}"
|
||||
else
|
||||
echo -e "${RED}✗ 用户表创建失败${NC}"
|
||||
fi
|
||||
|
||||
PGPASSWORD=$DB_PASS psql -U $DB_USER -d $DB_NAME -f resources/sql/002_create_audit_logs_table.sql 2>&1 | grep -v "NOTICE"
|
||||
if [ $? -eq 0 ]; then
|
||||
echo -e "${GREEN}✓ 审计日志表创建成功${NC}"
|
||||
else
|
||||
echo -e "${RED}✗ 审计日志表创建失败${NC}"
|
||||
fi
|
||||
else
|
||||
echo -e "${YELLOW}⚠ 跳过数据库迁移,请稍后手动执行:${NC}"
|
||||
echo " psql -U $DB_USER -d $DB_NAME -f resources/sql/001_create_users_table.sql"
|
||||
echo " psql -U $DB_USER -d $DB_NAME -f resources/sql/002_create_audit_logs_table.sql"
|
||||
fi
|
||||
echo ""
|
||||
|
||||
# 步骤 5: 测试
|
||||
echo "🧪 步骤 5/5: 运行测试..."
|
||||
if python tests/test_encryption.py 2>&1; then
|
||||
echo -e "${GREEN}✓ 加密功能测试通过${NC}"
|
||||
else
|
||||
echo -e "${RED}✗ 加密功能测试失败${NC}"
|
||||
fi
|
||||
echo ""
|
||||
|
||||
# 完成
|
||||
echo "=================================="
|
||||
echo -e "${GREEN}✅ 设置完成!${NC}"
|
||||
echo "=================================="
|
||||
echo ""
|
||||
echo "默认管理员账号:"
|
||||
echo " 用户名: admin"
|
||||
echo " 密码: admin123"
|
||||
echo ""
|
||||
echo " 用户名: tjwater"
|
||||
echo " 密码: tjwater@123"
|
||||
echo ""
|
||||
echo "下一步:"
|
||||
echo " 1. 查看文档: cat SECURITY_README.md"
|
||||
echo " 2. 查看部署指南: cat DEPLOYMENT.md"
|
||||
echo " 3. 启动服务器: uvicorn app.main:app --reload"
|
||||
echo " 4. 访问文档: http://localhost:8000/docs"
|
||||
echo ""
|
||||
77
tests/api/test_api_integration.py
Executable file
77
tests/api/test_api_integration.py
Executable file
@@ -0,0 +1,77 @@
|
||||
#!/usr/bin/env python
|
||||
"""
|
||||
测试新增 API 集成
|
||||
|
||||
验证新的认证、用户管理和审计日志接口是否正确集成
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
import pytest
|
||||
|
||||
# 将项目根目录添加到 sys.path
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../")))
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"module_name, desc",
|
||||
[
|
||||
("app.core.encryption", "加密模块"),
|
||||
("app.core.security", "安全模块"),
|
||||
("app.core.audit", "审计模块"),
|
||||
("app.domain.models.role", "角色模型"),
|
||||
("app.domain.schemas.user", "用户Schema"),
|
||||
("app.domain.schemas.audit", "审计Schema"),
|
||||
("app.auth.permissions", "权限控制"),
|
||||
("app.api.v1.endpoints.auth", "认证接口"),
|
||||
("app.api.v1.endpoints.user_management", "用户管理接口"),
|
||||
("app.api.v1.endpoints.audit", "审计日志接口"),
|
||||
("app.infra.repositories.user_repository", "用户仓储"),
|
||||
("app.infra.repositories.audit_repository", "审计仓储"),
|
||||
("app.infra.audit.middleware", "审计中间件"),
|
||||
],
|
||||
)
|
||||
def test_module_imports(module_name, desc):
|
||||
"""检查关键模块是否可以导入"""
|
||||
try:
|
||||
__import__(module_name)
|
||||
except ImportError as e:
|
||||
pytest.fail(f"无法导入 {desc} ({module_name}): {e}")
|
||||
|
||||
|
||||
def test_router_configuration():
|
||||
"""检查路由配置"""
|
||||
try:
|
||||
from app.api.v1 import router
|
||||
|
||||
# 检查 router 中是否包含新增的路由
|
||||
api_router = router.api_router
|
||||
routes = [r.path for r in api_router.routes if hasattr(r, "path")]
|
||||
|
||||
# 验证基础路径是否存在
|
||||
assert any("/auth" in r for r in routes), "缺少认证相关路由 (/auth)"
|
||||
assert any("/users" in r for r in routes), "缺少用户管理路由 (/users)"
|
||||
assert any("/audit" in r for r in routes), "缺少审计日志路由 (/audit)"
|
||||
|
||||
except Exception as e:
|
||||
pytest.fail(f"路由配置检查失败: {e}")
|
||||
|
||||
|
||||
def test_main_app_initialization():
|
||||
"""检查 main.py 配置"""
|
||||
try:
|
||||
from app.main import app
|
||||
|
||||
assert app is not None
|
||||
assert app.title != ""
|
||||
|
||||
# 检查中间件 (简单检查是否存在)
|
||||
middleware_names = [m.cls.__name__ for m in app.user_middleware]
|
||||
# 检查是否包含审计中间件或其他关键中间件(根据实际类名修改)
|
||||
assert "AuditMiddleware" in middleware_names, "缺少审计中间件"
|
||||
|
||||
# 检查路由总数
|
||||
assert len(app.routes) > 0
|
||||
|
||||
except Exception as e:
|
||||
pytest.fail(f"main.py 配置检查失败: {e}")
|
||||
41
tests/auth/test_encryption.py
Normal file
41
tests/auth/test_encryption.py
Normal file
@@ -0,0 +1,41 @@
|
||||
"""
|
||||
测试加密功能
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
|
||||
|
||||
|
||||
def test_encryption():
|
||||
"""测试加密和解密功能"""
|
||||
from app.core.encryption import Encryptor
|
||||
|
||||
# 生成测试密钥
|
||||
key = Encryptor.generate_key()
|
||||
print(f"✓ 生成密钥: {key}")
|
||||
|
||||
# 创建加密器
|
||||
encryptor = Encryptor(key=key.encode())
|
||||
|
||||
# 测试加密
|
||||
test_data = "这是敏感数据 - 数据库密码: password123"
|
||||
encrypted = encryptor.encrypt(test_data)
|
||||
print(f"✓ 加密成功: {encrypted[:50]}...")
|
||||
|
||||
# 测试解密
|
||||
decrypted = encryptor.decrypt(encrypted)
|
||||
assert decrypted == test_data, "解密数据不匹配!"
|
||||
print(f"✓ 解密成功: {decrypted}")
|
||||
|
||||
# 测试空数据
|
||||
assert encryptor.encrypt("") == ""
|
||||
assert encryptor.decrypt("") == ""
|
||||
print("✓ 空数据处理正确")
|
||||
|
||||
print("\n✅ 所有加密测试通过!")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_encryption()
|
||||
@@ -1,7 +1,11 @@
|
||||
from api_ex.pipeline_health_analyzer import PipelineHealthAnalyzer
|
||||
"""
|
||||
tests.unit.test_pipeline_health_analyzer 的 Docstring
|
||||
"""
|
||||
|
||||
|
||||
def test_pipeline_health_analyzer():
|
||||
from app.algorithms.api_ex.pipeline_health_analyzer import PipelineHealthAnalyzer
|
||||
|
||||
# 初始化分析器,假设模型文件路径为'models/rsf_model.joblib'
|
||||
analyzer = PipelineHealthAnalyzer()
|
||||
# 创建示例输入数据(9个样本)
|
||||
@@ -51,7 +55,7 @@ def test_pipeline_health_analyzer():
|
||||
), "每个生存函数应包含x和y属性"
|
||||
|
||||
# 可选:测试绘图功能(不显示图表)
|
||||
analyzer.plot_survival(survival_functions, show_plot=True)
|
||||
analyzer.plot_survival(survival_functions, show_plot=False)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
Reference in New Issue
Block a user