Files
LinkDesk/backend/main.py
T
indigo db2c414c1a Add multi-role permission system with a Role Management admin page
Users can now hold multiple roles, each with its own editable set of
create/edit/delete-style permissions across assets, shots, tasks, task
assignment, review approve/retake, submissions, uploads, and notes
(including internal vs. client note visibility). The 4 existing roles
(coordinator/director/artist/developer) are migrated into the new system
as system roles, seeded to reproduce today's actual behavior exactly;
admins can create custom roles (e.g. "Reviewer", "Outsourcing") via the
new Role Management page and assign multiple roles to a user via a new
"Manage Roles" action on the Team page.

Backend:
- New Role/Permission models and role_permissions/user_roles tables,
  plus a one-off, idempotent seed/backfill migration script.
- New require_permission()/user_has_permission() dependency, wired into
  the actual mutation endpoints across shots/assets/tasks/reviews,
  always preserving existing ownership- and self-service-based access
  (e.g. artists editing their own task status, own notes, own uploads,
  own submissions) as an unconditional fallback alongside the new
  permission checks - nothing that worked before now requires a role.
- New endpoints: PUT/DELETE on task submissions (wires up soft-deletion
  columns that existed on the model but were never exposed), plus full
  role CRUD and per-user role assignment.
- Along the way: fixed newly-created users not being linked to their
  matching system role (silently leaving them with zero permissions),
  and unified an inconsistency between the single vs. bulk task status
  endpoints that allowed different roles to bulk-update status.

Frontend:
- Role Management page with a grouped, human-readable permission editor
  (icons, plain-language action labels, per-resource select-all, live
  selected count) replacing an earlier dense matrix prototype.
- hasPermission() added to the existing usePermission() composable
  without touching its current isAdmin/isCoordinatorOrAdmin consumers.
- Note composer gets an Internal/Client toggle; submissions gain inline
  edit/delete actions gated the same ownership-or-permission way as notes.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-18 20:41:17 +08:00

122 lines
4.4 KiB
Python

from fastapi import FastAPI, Request, Response
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from contextlib import asynccontextmanager
import time
import logging
import json
import os
from database import engine, Base
from routers import auth, users, projects, episodes, assets, shots, tasks, reviews, files, developer, settings, notifications, activities, admin, data_consistency, roles
# Import models to ensure they are registered with SQLAlchemy
import models
# # Configure logging
# logging.basicConfig(
# level=logging.DEBUG,
# format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
# )
# logger = logging.getLogger("vfx_api")
# # logger = logging.getLogger("uvicorn")
# logger.setLevel(logging.DEBUG)
# # Also set the auth logger to debug
# auth_logger = logging.getLogger("vfx_auth")
# auth_logger.setLevel(logging.DEBUG)
@asynccontextmanager
async def lifespan(app: FastAPI):
# Create database tables
Base.metadata.create_all(bind=engine)
yield
app = FastAPI(
title="LinkDesk API",
description="A comprehensive project management system for animation and VFX production",
version="1.0.0",
lifespan=lifespan
)
# Configure CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["http://localhost:5173", "http://localhost:5174"], # Vue.js dev server
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Add logging middleware
# @app.middleware("http")
# async def log_requests(request: Request, call_next):
# """Log all HTTP requests and responses."""
# start_time = time.time()
# # Log request
# client_ip = request.client.host if request.client else "unknown"
# auth_header = request.headers.get("authorization", "none")
# # Mask the token for security
# if auth_header and auth_header.startswith("Bearer "):
# auth_display = f"Bearer {auth_header[7:15]}..."
# else:
# auth_display = auth_header
# log_message = f"🔵 {request.method} {request.url.path} from {client_ip} | Auth: {auth_display}"
# logger.info(log_message)
# print(log_message) # Also print to console for debugging
# # Process the request
# try:
# response = await call_next(request)
# process_time = time.time() - start_time
# # Log response
# status_emoji = "🟢" if response.status_code < 400 else "🔴" if response.status_code >= 500 else "🟡"
# response_message = f"{status_emoji} {response.status_code} - {process_time:.3f}s"
# logger.info(response_message)
# print(response_message) # Also print to console for debugging
# return response
# except Exception as e:
# process_time = time.time() - start_time
# logger.error(f"🔴 ERROR: {str(e)} - {process_time:.3f}s")
# raise
# Mount static files for uploads
uploads_dir = os.path.join(os.path.dirname(__file__), "uploads")
if not os.path.exists(uploads_dir):
os.makedirs(uploads_dir)
app.mount("/uploads", StaticFiles(directory=uploads_dir), name="uploads")
# Include routers
app.include_router(auth.router, prefix="/auth", tags=["authentication"])
app.include_router(users.router, prefix="/users", tags=["users"])
app.include_router(projects.router, prefix="/projects", tags=["projects"])
app.include_router(episodes.router, prefix="/episodes", tags=["episodes"])
app.include_router(assets.router, prefix="/assets", tags=["assets"])
app.include_router(shots.router, prefix="/shots", tags=["shots"])
app.include_router(tasks.router, prefix="/tasks", tags=["tasks"])
app.include_router(reviews.router, prefix="/reviews", tags=["reviews"])
app.include_router(files.router, prefix="/files", tags=["files"])
app.include_router(developer.router, prefix="/developer", tags=["developer"])
app.include_router(settings.router, prefix="/settings", tags=["settings"])
app.include_router(notifications.router, tags=["notifications"])
app.include_router(activities.router, tags=["activities"])
app.include_router(admin.router, prefix="/admin", tags=["admin"])
app.include_router(data_consistency.router, prefix="/data-consistency", tags=["data-consistency"])
app.include_router(roles.router, prefix="/roles", tags=["roles"])
@app.get("/")
async def root():
return {"message": "VFX Project Management System API"}
@app.get("/health")
async def health_check():
return {"status": "healthy"}