Spaces:
Paused
Paused
Download models/user.py from 3SCError404/miracle: direct link, hf CLI and curl.
- Browser
- Download file 3.74 kB
-
https://huggingface.co/spaces/3SCError404/miracle/resolve/main/models/user.py
- Command line
-
hf download hf://spaces/3SCError404/miracle/models/user.py
-
curl -L -o user.py https://huggingface.co/spaces/3SCError404/miracle/resolve/main/models/user.py
3.74 kB
| """ | |
| User model — supports 5 roles across customer and 3SC staff. | |
| Roles: customer_admin, customer_user, 3sc_agent, 3sc_lead, 3sc_admin | |
| """ | |
| import enum | |
| from sqlalchemy import Column, String, Boolean, ForeignKey | |
| from sqlalchemy.dialects.postgresql import UUID, JSONB | |
| from sqlalchemy.orm import relationship | |
| from core.database import Base | |
| from models.base import PrimaryKeyMixin, TimestampMixin, SoftDeleteMixin | |
| class UserRole(str, enum.Enum): | |
| CUSTOMER_ADMIN = "customer_admin" | |
| CUSTOMER_USER = "customer_user" | |
| SC_AGENT = "3sc_agent" | |
| SC_LEAD = "3sc_lead" | |
| SC_ADMIN = "3sc_admin" | |
| SUPERADMIN = "superadmin" | |
| TEAM_LIST = ["ENGINEERING", "FINANCE", "SUCCESS", "ACCOUNT", "SECURITY", "COMPLIANCE", "GENERAL", "OTHER"] | |
| class User(Base, PrimaryKeyMixin, TimestampMixin, SoftDeleteMixin): | |
| __tablename__ = "users" | |
| # NEW Many-to-Many Architecture | |
| global_role = Column(String, nullable=True) # superadmin only | |
| real_email = Column(String, nullable=True) # For notifications | |
| workspace_links = relationship("UserWorkspace", back_populates="user", cascade="all, delete-orphan") | |
| def is_platform_admin(self) -> bool: | |
| """Strict check for global platform authority (Context-aware).""" | |
| # 1. Check for manual injection (e.g. from deps.py context) | |
| if hasattr(self, "_is_platform_admin"): | |
| return self._is_platform_admin | |
| # 2. Default calculated logic | |
| from core.config import get_settings | |
| settings = get_settings() | |
| return ( | |
| self.email.lower() == settings.SUPER_ADMIN_EMAIL.lower() and | |
| self.global_role == UserRole.SUPERADMIN | |
| ) | |
| def is_platform_admin(self, value: bool): | |
| self._is_platform_admin = value | |
| # DEPRECATED Legacy Columns (Aliased for safety) | |
| _legacy_role = Column("role", String, nullable=True) | |
| _legacy_workspace_id = Column("workspace_id", UUID(as_uuid=True), ForeignKey("workspaces.id"), nullable=True) | |
| def role(self): | |
| """Context-aware role shim (injected by deps.py)""" | |
| # If Deps has already set an active role, use it | |
| if hasattr(self, "_active_role"): | |
| return self._active_role | |
| # Fallback to global role or legacy column (only for migration compatibility) | |
| return self.global_role or self._legacy_role | |
| def role(self, value): | |
| self._active_role = value | |
| def workspace_id(self): | |
| """Context-aware workspace_id shim (injected by deps.py)""" | |
| if hasattr(self, "_active_workspace_id"): | |
| return self._active_workspace_id | |
| return self._legacy_workspace_id | |
| def workspace_id(self, value): | |
| self._active_workspace_id = value | |
| email = Column(String(255), unique=True, nullable=False, index=True) | |
| hashed_password = Column(String(255), nullable=False) | |
| full_name = Column(String(255), nullable=False) | |
| is_active = Column(Boolean, default=True, nullable=False) | |
| team = Column(String(100), nullable=True) | |
| avatar_url = Column(String(512), nullable=True) | |
| ai_gist = Column(JSONB, default={}, nullable=False) | |
| # Relationships | |
| workspace = relationship("Workspace", back_populates="users", foreign_keys=[_legacy_workspace_id]) | |
| created_tickets = relationship("Ticket", back_populates="creator", foreign_keys="Ticket.created_by") | |
| assigned_tickets = relationship("Ticket", back_populates="assignee", foreign_keys="Ticket.assigned_to") | |
| resolved_tickets = relationship("Ticket", back_populates="resolver", foreign_keys="Ticket.resolved_by") | |
| messages = relationship("Message", back_populates="sender") | |