data_scope_helpers.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. """CRM 数据权限辅助(负责人 owner_user_id + 标准 create_by/create_dept_id)。"""
  2. from __future__ import annotations
  3. from typing import Any, Dict, Optional
  4. from sqlalchemy.orm import Session
  5. from app.core.data_scope import apply_data_scope, row_in_data_scope
  6. CRM_OWNER_FIELD = "owner_user_id"
  7. def crm_user_info(user: Optional[dict]) -> Optional[Dict[str, Any]]:
  8. if not user:
  9. return None
  10. uid = (user.get("user_id") or user.get("id") or "").strip()
  11. return {
  12. "user_id": uid or None,
  13. "role": user.get("role"),
  14. "dept_id": user.get("dept_id") or user.get("region_id"),
  15. "region_id": user.get("region_id") or user.get("dept_id"),
  16. }
  17. def apply_crm_owner_scope(query, model, user_info, db: Session, tenant_no: str):
  18. if not user_info:
  19. return query
  20. return apply_data_scope(
  21. query, model, user_info, db, tenant_no, owner_field=CRM_OWNER_FIELD
  22. )
  23. def apply_crm_create_by_scope(query, model, user_info, db: Session, tenant_no: str):
  24. if not user_info:
  25. return query
  26. return apply_data_scope(query, model, user_info, db, tenant_no)
  27. def ensure_crm_owner_row_in_scope(
  28. db: Session, row, user_info, tenant_no: str, *, message: str = "无权访问该数据"
  29. ) -> None:
  30. if not user_info or row is None:
  31. return
  32. if not row_in_data_scope(db, row, user_info, tenant_no, owner_field=CRM_OWNER_FIELD):
  33. raise ValueError(message)
  34. def ensure_crm_create_by_row_in_scope(
  35. db: Session, row, user_info, tenant_no: str, *, message: str = "无权访问该数据"
  36. ) -> None:
  37. if not user_info or row is None:
  38. return
  39. if not row_in_data_scope(db, row, user_info, tenant_no):
  40. raise ValueError(message)
  41. def ensure_crm_contact_row_in_scope(
  42. db: Session, row, user_info, tenant_no: str, *, message: str = "无权访问该联系人"
  43. ) -> None:
  44. """联系人:create_by 在范围内,或关联客户在 owner 范围内。"""
  45. if not user_info or row is None:
  46. return
  47. if row_in_data_scope(db, row, user_info, tenant_no):
  48. return
  49. customer_id = getattr(row, "customer_id", None) or ""
  50. if customer_id:
  51. from app.crud.crm.customer import get_customer
  52. customer = get_customer(db, customer_id)
  53. if customer and customer.tenant_no == tenant_no:
  54. if row_in_data_scope(
  55. db, customer, user_info, tenant_no, owner_field=CRM_OWNER_FIELD
  56. ):
  57. return
  58. raise ValueError(message)
  59. def ensure_crm_customer_linked_row_in_scope(
  60. db: Session,
  61. row,
  62. user_info,
  63. tenant_no: str,
  64. *,
  65. customer_id: str = None,
  66. message: str = "无权访问该数据",
  67. ) -> None:
  68. """客户子表(开票信息等):先校验关联客户 owner scope,否则校验 create_by。"""
  69. if not user_info or row is None:
  70. return
  71. cid = customer_id or getattr(row, "customer_id", None) or ""
  72. if cid:
  73. from app.crud.crm.customer import get_customer
  74. customer = get_customer(db, cid)
  75. if customer and customer.tenant_no == tenant_no:
  76. if row_in_data_scope(
  77. db, customer, user_info, tenant_no, owner_field=CRM_OWNER_FIELD
  78. ):
  79. return
  80. if row_in_data_scope(db, row, user_info, tenant_no):
  81. return
  82. raise ValueError(message)