1
0
forked from erp-dev/erp
Files
erpnew/docs/2026-01-11_summary.md
2026-01-12 11:52:41 +08:00

71 lines
1.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 2026-01-11 工作日志
## 完成事项
### API 审计日志中间件
实现了一个用于记录特定模块POST请求的中间件用于保存"创建"操作的历史现场。
#### 1. 新增 `ApiAuditLog` 模型 (`api_v1/models.py`)
记录以下信息:
- `url`: 请求URL路径
- `method`: HTTP方法
- `request_data`: 请求体数据JSON格式
- `query_params`: URL查询参数
- `user_id`: 操作用户ID
- `username`: 操作用户的用户名快照
- `response_status`: HTTP响应状态码
- `created_at`: 创建时间
#### 2. 新增 Celery Task (`api_v1/tasks.py`)
- `save_api_audit_log`: 异步保存审计日志任务
- 通过Celery队列异步执行避免阻塞API响应
#### 3. 新增中间件 (`flower/middleware.py`)
- `ApiAuditLogMiddleware`: API审计日志中间件
- 只记录POST请求
- 通过URL前缀白名单过滤
- 支持multipart/form-data请求文件字段只记录元信息文件名、大小、类型
- 在AuthenticationMiddleware之后运行可获取request.user
- 对于JWT认证手动调用JWTAuthentication获取用户信息
#### 4. 配置更新 (`flower/settings.py`)
新增配置项:
```python
# API 审计日志配置
API_AUDIT_LOG_ENABLED = True
API_AUDIT_LOG_URL_PREFIXES = [
'/api/v1/',
'/api/v2/',
'/api/backend/',
]
```
中间件注册:
```python
MIDDLEWARE = [
...
'flower.middleware.ApiAuditLogMiddleware', # 放在最后
]
```
#### 5. 数据库迁移
- 迁移文件: `api_v1/migrations/0009_apiauditlog.py`
#### 6. Admin 注册 (`api_v1/admin.py`)
- 新增 `ApiAuditLogAdmin` 管理类
- 支持按 URL、用户名搜索
- 支持按方法、响应状态码、时间过滤
- 所有字段只读,禁止添加和修改(只允许查看和删除)
## 待办事项
- [ ] 执行数据库迁移 (`uv run python manage.py migrate`)
- [ ] 测试中间件功能