group.py 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. """Group model for permission-based access control."""
  2. from __future__ import annotations
  3. from datetime import datetime
  4. from typing import TYPE_CHECKING
  5. from sqlalchemy import Boolean, Column, DateTime, ForeignKey, Integer, String, Table, func
  6. from sqlalchemy.orm import Mapped, mapped_column, relationship
  7. from sqlalchemy.types import JSON
  8. from backend.app.core.database import Base
  9. if TYPE_CHECKING:
  10. from backend.app.models.user import User
  11. # Many-to-many association table between users and groups
  12. user_groups = Table(
  13. "user_groups",
  14. Base.metadata,
  15. Column("user_id", Integer, ForeignKey("users.id", ondelete="CASCADE"), primary_key=True),
  16. Column("group_id", Integer, ForeignKey("groups.id", ondelete="CASCADE"), primary_key=True),
  17. )
  18. # Printers a group with ``restrict_printers`` set is allowed to see and control
  19. # (#1727). Rows are only meaningful while the flag is on; turning it off keeps
  20. # them so the selection survives toggling. SQLite doesn't enforce the FK
  21. # cascades, so printer and group deletes remove their rows explicitly.
  22. group_printers = Table(
  23. "group_printers",
  24. Base.metadata,
  25. Column("group_id", Integer, ForeignKey("groups.id", ondelete="CASCADE"), primary_key=True),
  26. Column("printer_id", Integer, ForeignKey("printers.id", ondelete="CASCADE"), primary_key=True),
  27. )
  28. # Locations a restricted group may use (#1727): every printer whose
  29. # ``location`` matches, now or later, so a printer added to "Lab A" reaches
  30. # the Lab A team without anyone ticking it. Matched exactly against
  31. # ``printers.location``; a name no printer carries any more simply grants
  32. # nothing. Like ``group_printers``, kept while the flag is off.
  33. group_locations = Table(
  34. "group_locations",
  35. Base.metadata,
  36. Column("group_id", Integer, ForeignKey("groups.id", ondelete="CASCADE"), primary_key=True),
  37. Column("location", String(100), primary_key=True),
  38. )
  39. class Group(Base):
  40. """Group model for organizing users and assigning permissions.
  41. Groups contain a list of permissions that are granted to all members.
  42. Users can belong to multiple groups, and their permissions are additive.
  43. System groups (Administrators, Operators, Viewers) cannot be deleted.
  44. Permissions say *what* a member may do; ``restrict_printers`` says *where*
  45. and ``restrict_filaments`` *with which filaments*. A group without a flag
  46. doesn't narrow that dimension at all, so it can be combined with a team
  47. group that does. See ``core/printer_scope.py`` and ``core/filament_scope.py``.
  48. """
  49. __tablename__ = "groups"
  50. id: Mapped[int] = mapped_column(primary_key=True)
  51. name: Mapped[str] = mapped_column(String(100), unique=True, index=True)
  52. description: Mapped[str | None] = mapped_column(String(500), nullable=True)
  53. permissions: Mapped[list[str]] = mapped_column(JSON, default=list)
  54. is_system: Mapped[bool] = mapped_column(Boolean, default=False)
  55. restrict_printers: Mapped[bool] = mapped_column(Boolean, default=False, server_default="0")
  56. # Filament scope (#3522): when set, members may only print with the
  57. # filaments this group's rules allow (see ``core/filament_scope.py``). The
  58. # mode and rules are kept while the flag is off, like the printer picks.
  59. restrict_filaments: Mapped[bool] = mapped_column(Boolean, default=False, server_default="0")
  60. filament_rule_mode: Mapped[str] = mapped_column(String(10), default="block", server_default="block")
  61. filament_rules: Mapped[list[dict] | None] = mapped_column(JSON, nullable=True)
  62. created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
  63. updated_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now(), onupdate=func.now())
  64. # Relationship to users through association table
  65. users: Mapped[list[User]] = relationship(
  66. "User",
  67. secondary=user_groups,
  68. back_populates="groups",
  69. lazy="selectin",
  70. )
  71. def __repr__(self) -> str:
  72. return f"<Group {self.name}>"