Spaces:
3SCError404
/
Paused

nexus / models /user.py
dushyantsingh0
backend stabilization
4405ca9
Raw History Blame Contribute Delete
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")
@property
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
)
@is_platform_admin.setter
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)
@property
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
@role.setter
def role(self, value):
self._active_role = value
@property
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
@workspace_id.setter
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")