user.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. class User(Base):
  10. """User model for authentication and authorization.
  11. Users can belong to multiple groups, and their permissions are additive
  12. across all groups. The legacy 'role' field is kept for backward compatibility
  13. but is_admin property now also considers group membership.
  14. """
  15. __tablename__ = "users"
  16. id: Mapped[int] = mapped_column(primary_key=True)
  17. username: Mapped[str] = mapped_column(String(100), unique=True, index=True)
  18. email: Mapped[str | None] = mapped_column(String(255), unique=True, index=True, nullable=True)
  19. password_hash: Mapped[str] = mapped_column(String(255))
  20. role: Mapped[str] = mapped_column(
  21. String(20), default="user"
  22. ) # "admin" or "user" (legacy, kept for backward compat)
  23. is_active: Mapped[bool] = mapped_column(default=True)
  24. created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
  25. updated_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now(), onupdate=func.now())
  26. # Relationship to groups through association table
  27. groups: Mapped[list[Group]] = relationship(
  28. "Group",
  29. secondary="user_groups",
  30. back_populates="users",
  31. lazy="selectin",
  32. )
  33. @property
  34. def is_admin(self) -> bool:
  35. """Check if user is an admin.
  36. Returns True if:
  37. - User has legacy role='admin', OR
  38. - User belongs to the Administrators group
  39. """
  40. if self.role == "admin":
  41. return True
  42. return any(g.name == "Administrators" for g in self.groups)
  43. def get_permissions(self) -> set[str]:
  44. """Get all permissions from all groups the user belongs to.
  45. Returns a set of permission strings. Permissions are additive across groups.
  46. """
  47. permissions: set[str] = set()
  48. for group in self.groups:
  49. if group.permissions:
  50. permissions.update(group.permissions)
  51. return permissions
  52. def has_permission(self, permission: str) -> bool:
  53. """Check if user has a specific permission.
  54. Admins have all permissions. For other users, checks if the permission
  55. exists in any of their groups.
  56. """
  57. if self.is_admin:
  58. return True
  59. return permission in self.get_permissions()
  60. def has_all_permissions(self, *permissions: str) -> bool:
  61. """Check if user has ALL specified permissions.
  62. Admins have all permissions. For other users, checks if all permissions
  63. exist in their combined group permissions.
  64. """
  65. if self.is_admin:
  66. return True
  67. user_permissions = self.get_permissions()
  68. return all(p in user_permissions for p in permissions)
  69. def has_any_permission(self, *permissions: str) -> bool:
  70. """Check if user has ANY of the specified permissions.
  71. Admins have all permissions. For other users, checks if at least one
  72. permission exists in their combined group permissions.
  73. """
  74. if self.is_admin:
  75. return True
  76. user_permissions = self.get_permissions()
  77. return any(p in user_permissions for p in permissions)
  78. def __repr__(self) -> str:
  79. return f"<User {self.username}>"