Spaces:
3SCError404
/
Paused

File size: 3,742 Bytes
5eb4857
 
 
 
 
 
4405ca9
5eb4857
 
 
 
 
 
 
 
 
 
 
 
3143c25
 
b5a977c
5eb4857
 
 
 
 
b7980f7
3143c25
 
b7980f7
 
3143c25
 
 
e3c5d41
 
 
 
 
 
3143c25
 
 
 
 
 
 
e3c5d41
 
 
 
3143c25
b7980f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5eb4857
 
 
b7980f7
5eb4857
b5a977c
5eb4857
 
 
 
b7980f7
5eb4857
 
b7980f7
5eb4857
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"""
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")