permission.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. """主案ACL允许权限模型。"""
  2. from __future__ import annotations
  3. from sqlalchemy import (
  4. BigInteger,
  5. Boolean,
  6. CheckConstraint,
  7. Computed,
  8. ForeignKey,
  9. Index,
  10. Integer,
  11. String,
  12. UniqueConstraint,
  13. text,
  14. )
  15. from sqlalchemy.orm import Mapped, mapped_column
  16. from dms.database.base import BusinessTableMixin
  17. from dms.extensions import db
  18. class Permission(BusinessTableMixin, db.Model):
  19. __tablename__ = "doc_permission"
  20. __table_args__ = (
  21. CheckConstraint(
  22. "subject_type IN ('ORG', 'USER')",
  23. name="permission_subject_type",
  24. ),
  25. UniqueConstraint(
  26. "document_id",
  27. "subject_type",
  28. "subject_id",
  29. "active_marker",
  30. name="uq_doc_permission_active",
  31. ),
  32. Index(
  33. "ix_doc_permission_document_active",
  34. "document_id",
  35. "is_deleted",
  36. ),
  37. Index(
  38. "ix_doc_permission_subject_active",
  39. "subject_type",
  40. "subject_id",
  41. "is_deleted",
  42. ),
  43. )
  44. document_id: Mapped[int] = mapped_column(
  45. BigInteger,
  46. ForeignKey("doc_document.id", ondelete="RESTRICT"),
  47. nullable=False,
  48. )
  49. subject_type: Mapped[str] = mapped_column(String(20), nullable=False)
  50. subject_id: Mapped[int] = mapped_column(BigInteger, nullable=False)
  51. subject_name: Mapped[str] = mapped_column(String(255), nullable=False)
  52. can_view: Mapped[bool] = mapped_column(
  53. Boolean,
  54. nullable=False,
  55. server_default=text("1"),
  56. )
  57. can_download: Mapped[bool] = mapped_column(
  58. Boolean,
  59. nullable=False,
  60. server_default=text("0"),
  61. )
  62. can_edit: Mapped[bool] = mapped_column(
  63. Boolean,
  64. nullable=False,
  65. server_default=text("0"),
  66. )
  67. can_manage_permission: Mapped[bool] = mapped_column(
  68. Boolean,
  69. nullable=False,
  70. server_default=text("0"),
  71. )
  72. can_delete: Mapped[bool] = mapped_column(
  73. Boolean,
  74. nullable=False,
  75. server_default=text("0"),
  76. )
  77. active_marker: Mapped[int | None] = mapped_column(
  78. Integer,
  79. Computed("IF(is_deleted = 0, 1, NULL)", persisted=False),
  80. nullable=True,
  81. )