feat(后端): 添加授权体系、系统监控与试用期计提

按 2026-11-02 需求补齐计划交付后端能力:
- 角色授权:权限目录化,接口统一准入校验,未登记默认拒绝;
  管理员判断改为按数据库角色重新判定,防止权限码持有者绕过
- 系统监控:单进程采集资源、请求与异常指标,分钟级聚合,
  阈值配置化;敏感配置增加 repr 保护,Redis 密码 URL 转义
- 试用期计提:按回款日判定并写入快照,年度结清与调整单联动;
  人员模型补试用期、离职日期与工资版本历史字段
- 计提模板:公式骨架受控,页面维护名称与参数绑定
- T+ 采集:自动化配置表与进度查询

内置角色禁停用保护对齐模块注释;核算规则版本提为常量并
补充批量核算失败堆栈日志。

影响范围:后端全部模块、数据库迁移 V22-V28
验证:uv run ruff check 通过;922 个单元测试全部通过;
集成测试因本机 MySQL 数据卷被其他容器锁定暂未运行
This commit is contained in:
liuwenxin committed 2026-11-03 11:20:55 +08:00
1 parent 02fb06e0af
commit 1461a8d113
149 files changed
+8736 -368

No files matched your search

+108 -1
View File
@@ -24,7 +24,7 @@ from app.models import (
TPlusSyncRecord,
TPlusWorkRun,
)
from app.schemas.tplus_workbench import FactIn, WorkCreateIn
from app.schemas.tplus_workbench import FactIn, WorkCreateIn, WorkQuery
from app.services.tplus import workbench
from app.services.tplus.mapping import get_mapping
from app.services.tplus.master_import import execute_master
@@ -47,6 +47,30 @@ def work_session(monkeypatch):
monkeypatch.setattr(workbench, "_source_database", lambda: "test_source")
monkeypatch.setattr(workbench, "sync_lock", lambda s: nullcontext())
with Session(engine, expire_on_commit=False) as session:
from app.models import (
AuthorizationSetting,
Permission,
Role,
RolePermission,
User,
UserRole,
)
from app.services.permission_catalog import CATALOG, ROLE_DEFAULTS
session.add(AuthorizationSetting(id=1, revision=1))
actor = User(id=1, username="test_operator", password="test_hash_only", status=1)
session.add(actor)
permission_rows = {code: Permission(perm_code=code, perm_name=code, perm_type=2, status=1)
for code in CATALOG}
session.add_all(permission_rows.values())
session.flush()
for code, grants in ROLE_DEFAULTS.items():
role = Role(role_code=code, role_name=code, status=1)
session.add(role)
session.flush()
session.add_all([RolePermission(role_id=role.id, permission_id=permission_rows[p].id) for p in grants])
if code == "admin":
actor.user_roles.append(UserRole(role_id=role.id))
session.add(
BusinessRole(
role_name="营销员",
@@ -139,6 +163,89 @@ def prepare_person(session):
)
def test_关闭源读取仍可查询历史资料和任务(work_session, monkeypatch):
from app.core.config import Settings
from app.services.tplus import task_service
s = work_session
prepare_person(s)
run = preview(s, ["personnel"])
settings = Settings(_env_file=None, app_secret_key="x" * 32)
monkeypatch.setattr(task_service, "get_settings", lambda: settings)
def disabled_source():
raise RuntimeError("T+ 数据源未启用")
monkeypatch.setattr(workbench, "_source_database", disabled_source)
facts = workbench.list_facts(s, WorkQuery())
assert facts.total == 1
assert workbench.list_work(s, WorkQuery()).total == 1
assert workbench.get_run(s, run.run_no).run_no == run.run_no
assert workbench.fact_history(s, facts.records[0]["id"], WorkQuery()).total == 1
assert workbench.list_lines(s, run.run_no, WorkQuery()).total == 1
assert "仅供核对" in "".join(workbench.report_csv(s, run.run_no))
def test_关闭源读取有多个账套时拒绝猜测(work_session, monkeypatch):
from app.core.config import Settings
from app.core.exceptions import BadRequestError
from app.services.tplus import task_service
prepare_person(work_session)
work_session.add(TPlusFact(
source_database="another_source", kind="PRODUCT", fact_key="9", version=1,
data_json={"category": "municipal_water", "unit": "件"},
evidence="其他账套测试", requested_by=1,
))
work_session.commit()
settings = Settings(_env_file=None, app_secret_key="x" * 32)
monkeypatch.setattr(task_service, "get_settings", lambda: settings)
with pytest.raises(BadRequestError, match="多个账套"):
workbench.list_facts(work_session, WorkQuery())
def test_历史查询显式账套范围不能串账(work_session, monkeypatch):
from app.core.config import Settings
from app.services.tplus import task_service
prepare_person(work_session)
settings = Settings(
_env_file=None, app_secret_key="x" * 32, t_plus_database="another_source"
)
monkeypatch.setattr(task_service, "get_settings", lambda: settings)
assert workbench.list_facts(work_session, WorkQuery()).total == 0
def test_关闭读取时概况仍显示最近成功采集(work_session, monkeypatch):
from app.core.config import Settings
from app.services.tplus import query_service, task_service
prepare_person(work_session)
settings = Settings(_env_file=None, app_secret_key="x" * 32)
monkeypatch.setattr(task_service, "get_settings", lambda: settings)
overview = query_service.overview(work_session)
assert overview.is_configured is False
assert "仍可查看" in overview.configuration_message
assert overview.automation.enabled is False
assert overview.automation.last_success_at is not None
assert overview.total_staged_records >= 0
def test_关闭SQL源读取仍可维护已存账套补充资料(work_session, monkeypatch):
from app.core.config import Settings
from app.core.exceptions import BadRequestError
from app.services.tplus import task_service
prepare_person(work_session)
settings = Settings(_env_file=None, app_secret_key="x" * 32)
monkeypatch.setattr(task_service, "get_settings", lambda: settings)
def disabled_source():
raise BadRequestError("T+ 数据源未启用")
monkeypatch.setattr(workbench, "_source_database", disabled_source)
data = fact(work_session, "PRODUCT", "9", {"category":"municipal_water", "unit":"件"})
assert data["version"] == 1
def test_人员真实导入且重跑不重复_保留人工工资与岗位(work_session):
s = work_session
prepare_person(s)