bx_data_scope.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. """BX 模块数据权限辅助(create_by / create_dept_id)。"""
  2. from __future__ import annotations
  3. from typing import TYPE_CHECKING
  4. from sqlalchemy.orm import Session
  5. from app.core.data_scope import apply_data_scope, assert_row_in_data_scope
  6. if TYPE_CHECKING:
  7. from app.core.bx_auth import BxUserContext
  8. def apply_bx_scope(query, model, user: "BxUserContext", db: Session, tenant_no: str):
  9. """列表/统计查询应用数据权限。"""
  10. return apply_data_scope(query, model, user.to_user_info(), db, tenant_no)
  11. def assert_bx_row_in_scope(
  12. db: Session,
  13. row,
  14. model,
  15. user: "BxUserContext",
  16. tenant_no: str,
  17. ) -> None:
  18. """详情/改/删前校验行级数据权限。"""
  19. assert_row_in_data_scope(db, row, model, user.to_user_info(), tenant_no)
  20. def assert_bx_biz_in_scope(
  21. db: Session,
  22. biz_type: str,
  23. biz_id: str,
  24. user: "BxUserContext",
  25. tenant_no: str,
  26. ) -> None:
  27. """附件下载/删除/绑定前校验关联业务单在数据权限内。"""
  28. bid = (biz_id or "").strip()
  29. if not bid or bid == "0":
  30. return
  31. btype = (biz_type or "").strip().lower()
  32. if btype in ("advance", "advance_apply", "advance_issue"):
  33. from app.services.bx.advance import AdvanceService
  34. row = AdvanceService.get_by_id(db, bid, tenant_no, user=user)
  35. if not row:
  36. raise HTTPException(status_code=404, detail="关联备用金申请不存在")
  37. return
  38. if btype in ("reimburse", "reimburse_apply"):
  39. from app.services.bx.reimburse import ReimburseService
  40. row = ReimburseService.get_by_id(db, bid, tenant_no, user=user)
  41. if not row:
  42. raise HTTPException(status_code=404, detail="关联报销单不存在")
  43. return
  44. if btype == "workflow":
  45. from app.services.bx.workflow import WorkflowService
  46. row = WorkflowService.get_by_id(db, bid, tenant_no, user=user)
  47. if not row:
  48. raise HTTPException(status_code=404, detail="关联工作流不存在")
  49. def assert_bx_attachment_biz_in_scope(
  50. db: Session,
  51. attachment,
  52. user: "BxUserContext",
  53. tenant_no: str,
  54. ) -> None:
  55. """按附件关联业务单校验数据权限。"""
  56. assert_bx_biz_in_scope(
  57. db,
  58. getattr(attachment, "biz_type", None) or "",
  59. getattr(attachment, "biz_id", None) or "",
  60. user,
  61. tenant_no,
  62. )