user.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. from __future__ import annotations
  2. from datetime import datetime
  3. from typing import TYPE_CHECKING
  4. from sqlalchemy import DateTime, String, func
  5. from sqlalchemy.orm import Mapped, mapped_column, relationship
  6. from backend.app.core.database import Base
  7. if TYPE_CHECKING:
  8. from backend.app.models.group import Group
  9. from backend.app.models.user_email_pref import UserEmailPreference
  10. class User(Base):
  11. """User model for authentication and authorization.
  12. Users can belong to multiple groups, and their permissions are additive
  13. across all groups. The legacy 'role' field is kept for backward compatibility
  14. but is_admin property now also considers group membership.
  15. """
  16. __tablename__ = "users"
  17. id: Mapped[int] = mapped_column(primary_key=True)
  18. username: Mapped[str] = mapped_column(String(100), unique=True, index=True)
  19. email: Mapped[str | None] = mapped_column(String(255), unique=True, index=True, nullable=True)
  20. password_hash: Mapped[str | None] = mapped_column(String(255), nullable=True)
  21. role: Mapped[str] = mapped_column(
  22. String(20), default="user"
  23. ) # "admin" or "user" (legacy, kept for backward compat)
  24. auth_source: Mapped[str] = mapped_column(String(20), default="local") # "local" or "ldap"
  25. is_active: Mapped[bool] = mapped_column(default=True)
  26. created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
  27. updated_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now(), onupdate=func.now())
  28. # Per-user Bambu Cloud credentials (when auth is enabled, each user has their own)
  29. cloud_token: Mapped[str | None] = mapped_column(String(500), nullable=True, default=None)
  30. cloud_email: Mapped[str | None] = mapped_column(String(255), nullable=True, default=None)
  31. # Relationship to groups through association table
  32. groups: Mapped[list[Group]] = relationship(
  33. "Group",
  34. secondary="user_groups",
  35. back_populates="users",
  36. lazy="selectin",
  37. )
  38. # Relationship to email notification preferences
  39. email_preferences: Mapped[UserEmailPreference | None] = relationship(
  40. "UserEmailPreference",
  41. back_populates="user",
  42. uselist=False,
  43. cascade="all, delete-orphan",
  44. lazy="select",
  45. )
  46. @property
  47. def is_admin(self) -> bool:
  48. """Check if user is an admin.
  49. Returns True if:
  50. - User has legacy role='admin', OR
  51. - User belongs to the Administrators group
  52. """
  53. if self.role == "admin":
  54. return True
  55. return any(g.name == "Administrators" for g in self.groups)
  56. def get_permissions(self) -> set[str]:
  57. """Get all permissions from all groups the user belongs to.
  58. Returns a set of permission strings. Permissions are additive across groups.
  59. """
  60. permissions: set[str] = set()
  61. for group in self.groups:
  62. if group.permissions:
  63. permissions.update(group.permissions)
  64. return permissions
  65. def has_permission(self, permission: str) -> bool:
  66. """Check if user has a specific permission.
  67. Admins have all permissions. For other users, checks if the permission
  68. exists in any of their groups.
  69. """
  70. if self.is_admin:
  71. return True
  72. return permission in self.get_permissions()
  73. def has_all_permissions(self, *permissions: str) -> bool:
  74. """Check if user has ALL specified permissions.
  75. Admins have all permissions. For other users, checks if all permissions
  76. exist in their combined group permissions.
  77. """
  78. if self.is_admin:
  79. return True
  80. user_permissions = self.get_permissions()
  81. return all(p in user_permissions for p in permissions)
  82. def has_any_permission(self, *permissions: str) -> bool:
  83. """Check if user has ANY of the specified permissions.
  84. Admins have all permissions. For other users, checks if at least one
  85. permission exists in their combined group permissions.
  86. """
  87. if self.is_admin:
  88. return True
  89. user_permissions = self.get_permissions()
  90. return any(p in user_permissions for p in permissions)
  91. def __repr__(self) -> str:
  92. return f"<User {self.username}>"