Phase B addresses 7 high-severity gaps from the permissions audit: - B1: Enforce tree access check on session start via can_access_tree - B2: Replace all inline permission helpers with centralized permissions.py - B3: Fix require_engineer_or_admin to check is_team_admin before role - B4: Add is_active field on User with enforcement in get_current_active_user - B5: Add admin user management endpoints (list, get, role, team-admin, deactivate, activate) - B6: Add rate limiting on auth/invite endpoints via slowapi (disabled in DEBUG) - B7: Implement refresh token rotation with JTI-based revocation and meaningful logout Also reduces access token TTL from 15 to 5 minutes and updates CLAUDE.md with SaaS/MSP context for future planning sessions. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
68 lines
2.7 KiB
Python
68 lines
2.7 KiB
Python
import uuid
|
|
from datetime import datetime, timezone
|
|
from typing import Optional, TYPE_CHECKING
|
|
from sqlalchemy import String, DateTime, ForeignKey, Boolean, CheckConstraint
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
from sqlalchemy.dialects.postgresql import UUID
|
|
from app.core.database import Base
|
|
|
|
if TYPE_CHECKING:
|
|
from app.models.team import Team
|
|
from app.models.tree import Tree
|
|
from app.models.session import Session
|
|
from app.models.folder import UserFolder
|
|
|
|
|
|
class User(Base):
|
|
__tablename__ = "users"
|
|
__table_args__ = (
|
|
CheckConstraint(
|
|
"role IN ('engineer', 'viewer')",
|
|
name='ck_users_role_enum'
|
|
),
|
|
)
|
|
|
|
id: Mapped[uuid.UUID] = mapped_column(
|
|
UUID(as_uuid=True),
|
|
primary_key=True,
|
|
default=uuid.uuid4
|
|
)
|
|
email: Mapped[str] = mapped_column(String(255), unique=True, nullable=False, index=True)
|
|
password_hash: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
name: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
role: Mapped[str] = mapped_column(String(50), nullable=False, default="engineer")
|
|
is_super_admin: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
|
|
is_team_admin: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
|
|
is_active: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True, server_default="true")
|
|
team_id: Mapped[Optional[uuid.UUID]] = mapped_column(
|
|
UUID(as_uuid=True),
|
|
ForeignKey("teams.id"),
|
|
nullable=True
|
|
)
|
|
invite_code_id: Mapped[Optional[uuid.UUID]] = mapped_column(
|
|
UUID(as_uuid=True),
|
|
ForeignKey("invite_codes.id"),
|
|
nullable=True
|
|
)
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True),
|
|
default=lambda: datetime.now(timezone.utc)
|
|
)
|
|
last_login: Mapped[Optional[datetime]] = mapped_column(DateTime(timezone=True), nullable=True)
|
|
|
|
# Relationships
|
|
team: Mapped[Optional["Team"]] = relationship("Team", back_populates="users")
|
|
trees: Mapped[list["Tree"]] = relationship("Tree", back_populates="author")
|
|
sessions: Mapped[list["Session"]] = relationship("Session", back_populates="user")
|
|
folders: Mapped[list["UserFolder"]] = relationship("UserFolder", back_populates="user")
|
|
|
|
@property
|
|
def is_admin(self) -> bool:
|
|
"""Returns True if user is a super admin (system-wide access)."""
|
|
return self.is_super_admin
|
|
|
|
@property
|
|
def can_manage_team(self) -> bool:
|
|
"""Returns True if user can manage their team (team admin or super admin)."""
|
|
return self.is_super_admin or (self.is_team_admin and self.team_id is not None)
|