mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-09-28 06:57:54 +00:00
这是一次综合性的项目迭代,包含以下核心变更:
1. **目录与模块重构**
- 调整工作流节点类型模块目录结构,迁移节点类型相关代码
- 重命名platform模块为system模块,更新插件配置信息
- 重构代码生成模块导入路径
2. **数据库与CRUD优化**
- 统一所有CRUD类构造函数,新增数据库会话参数
- 修复权限过滤器数据库会话使用问题
- 更新模板生成器的CRUD代码模板
3. **认证与安全改进**
- 重构JWT密钥配置,移除默认密钥强制要求环境变量
- 重命名密码工具类,统一密码加密校验逻辑
- 优化OAuth认证流程,修复匿名认证使用问题
4. **前端与静态资源**
- 重构前端挂载逻辑,增加目录存在性校验
- 使用标准StaticFiles替换自定义前端挂载实现
5. **工具类与依赖更新**
- 修复导入工具的表名重复检测逻辑
- 优化限流回调代码,移除冗余依赖
- 更新用户、租户等模块的响应模型字段
6. **数据与配置修正**
- 修复系统版本数据字段命名不统一问题
- 简化枚举类校验逻辑,移除冗余注释
- 修复测试用例中的密码工具类导入错误
130 lines
4.7 KiB
Python
130 lines
4.7 KiB
Python
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.core.base_schema import AuthSchema, PageResultSchema
|
|
from app.core.exceptions import CustomException
|
|
from app.core.logger import logger
|
|
|
|
from .crud import LoginLogCRUD, OperationLogCRUD
|
|
from .schema import (
|
|
LoginLogDetailOutSchema,
|
|
LoginLogOutSchema,
|
|
LoginLogQueryParam,
|
|
OperationLogDetailOutSchema,
|
|
OperationLogOutSchema,
|
|
OperationLogQueryParam,
|
|
)
|
|
|
|
|
|
class LoginLogService:
|
|
"""登录日志管理服务"""
|
|
|
|
def __init__(self, auth: AuthSchema, db: AsyncSession) -> None:
|
|
self.auth = auth
|
|
self.db = db
|
|
|
|
async def detail(self, id: int) -> LoginLogDetailOutSchema:
|
|
obj = await LoginLogCRUD(self.auth, self.db).get_or_404(id=id)
|
|
return LoginLogDetailOutSchema.model_validate(obj)
|
|
|
|
async def page(
|
|
self,
|
|
page_no: int,
|
|
page_size: int,
|
|
search: LoginLogQueryParam | None = None,
|
|
order_by: list[dict[str, str]] | None = None,
|
|
) -> PageResultSchema[LoginLogOutSchema]:
|
|
return await LoginLogCRUD(self.auth, self.db).page(
|
|
offset=(page_no - 1) * page_size,
|
|
limit=page_size,
|
|
order_by=order_by or [{"updated_time": "desc"}],
|
|
search=vars(search) if search else None,
|
|
out_schema=LoginLogOutSchema,
|
|
)
|
|
|
|
async def delete(self, ids: list[int]) -> None:
|
|
if len(ids) < 1:
|
|
raise CustomException(msg="删除失败,删除对象不能为空")
|
|
|
|
existing = await LoginLogCRUD(self.auth, self.db).get_list(search={"id": ("in", ids)})
|
|
existing_map = {obj.id for obj in existing}
|
|
for nid in ids:
|
|
if nid not in existing_map:
|
|
raise CustomException(msg=f"删除失败,ID为{nid}的数据不存在")
|
|
|
|
await LoginLogCRUD(self.auth, self.db).delete(ids=ids)
|
|
|
|
|
|
class OperationLogService:
|
|
"""操作日志管理服务"""
|
|
|
|
def __init__(self, auth: AuthSchema, db: AsyncSession) -> None:
|
|
self.auth = auth
|
|
self.db = db
|
|
|
|
@staticmethod
|
|
async def cleanup_operation_log() -> bool:
|
|
from datetime import datetime, timedelta
|
|
from typing import Any
|
|
|
|
from sqlalchemy import delete
|
|
|
|
from app.api.v1.module_system.params.service import ParamsService
|
|
from app.core.ap_scheduler import SchedulerUtil
|
|
from app.core.database import async_db_session
|
|
|
|
from .model import LoginLogModel, OperationLogModel
|
|
|
|
retention_days = 90
|
|
try:
|
|
redis = SchedulerUtil.redis_instance
|
|
if redis:
|
|
# 调度任务是平台级别的,统一使用平台租户(id=1)的配置
|
|
config = await ParamsService.get_system_config_for_middleware(redis, tenant_id=1)
|
|
retention_days = int(config.get("operation_log_retention_days") or 90)
|
|
except Exception:
|
|
pass
|
|
|
|
cutoff = datetime.now() - timedelta(days=retention_days)
|
|
async with async_db_session() as session:
|
|
op_stmt = delete(OperationLogModel).where(OperationLogModel.created_time < cutoff)
|
|
op_result: Any = await session.execute(op_stmt)
|
|
|
|
login_stmt = delete(LoginLogModel).where(LoginLogModel.created_time < cutoff)
|
|
login_result: Any = await session.execute(login_stmt)
|
|
|
|
await session.commit()
|
|
logger.info(f"操作日志清理完成: 操作日志 {op_result.rowcount} 条, 登录日志 {login_result.rowcount} 条")
|
|
return True
|
|
|
|
async def page(
|
|
self,
|
|
page_no: int,
|
|
page_size: int,
|
|
search: OperationLogQueryParam | None = None,
|
|
order_by: list[dict[str, str]] | None = None,
|
|
) -> PageResultSchema[OperationLogOutSchema]:
|
|
crud = OperationLogCRUD(self.auth, self.db)
|
|
return await crud.page(
|
|
offset=(page_no - 1) * page_size,
|
|
limit=page_size,
|
|
order_by=order_by or [{"id": "desc"}],
|
|
search=vars(search) if search else None,
|
|
out_schema=OperationLogOutSchema,
|
|
)
|
|
|
|
async def detail(self, id: int) -> OperationLogDetailOutSchema:
|
|
crud = OperationLogCRUD(self.auth, self.db)
|
|
obj = await crud.get_or_404(id=id)
|
|
return OperationLogDetailOutSchema.model_validate(obj)
|
|
|
|
async def delete(self, ids: list[int]) -> None:
|
|
if len(ids) < 1:
|
|
raise CustomException(msg="删除失败,删除对象不能为空")
|
|
existing = await OperationLogCRUD(self.auth, self.db).get_list(search={"id": ("in", ids)})
|
|
existing_map = {obj.id for obj in existing}
|
|
for nid in ids:
|
|
if nid not in existing_map:
|
|
raise CustomException(msg="删除失败,该数据不存在")
|
|
crud = OperationLogCRUD(self.auth, self.db)
|
|
await crud.delete(ids=ids)
|