Default per-user main.py; invite-only by default
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -12,6 +12,7 @@ from sqlalchemy import func, select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from editor_app.db.models import AuthSession, InviteToken, User
|
||||
from editor_app.services import user_workspace
|
||||
|
||||
if TYPE_CHECKING:
|
||||
pass
|
||||
@@ -67,6 +68,10 @@ def create_user(db: Session, username: str, password: str, *, is_superuser: bool
|
||||
db.add(user)
|
||||
db.commit()
|
||||
db.refresh(user)
|
||||
if auth_enabled():
|
||||
user_workspace.ensure_default_code_main(
|
||||
user_workspace.user_workspace_root(user.id, user.username)
|
||||
)
|
||||
return user
|
||||
|
||||
|
||||
@@ -145,7 +150,7 @@ def delete_user(db: Session, user_id: int) -> bool:
|
||||
|
||||
|
||||
def invite_required() -> bool:
|
||||
return os.environ.get("AUTH_INVITE_ONLY", "false").strip().lower() in ("1", "true", "yes", "on")
|
||||
return os.environ.get("AUTH_INVITE_ONLY", "true").strip().lower() in ("1", "true", "yes", "on")
|
||||
|
||||
|
||||
def create_invite(db: Session, email: str, invited_by_user_id: int | None = None, expires_days: int = 7) -> InviteToken:
|
||||
|
||||
27
src/editor_app/services/user_workspace.py
Normal file
27
src/editor_app/services/user_workspace.py
Normal file
@@ -0,0 +1,27 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
from editor_app import config
|
||||
|
||||
DEFAULT_MAIN_PY = 'print("Hello, World!")\n'
|
||||
|
||||
|
||||
def safe_workspace_leaf(username: str, user_id: int) -> str:
|
||||
base = re.sub(r"[^a-zA-Z0-9._-]+", "-", username.strip()).strip("-").lower() or "user"
|
||||
return f"{base}-{user_id}"
|
||||
|
||||
|
||||
def user_workspace_root(user_id: int, username: str, workspace_root: Path | None = None) -> Path:
|
||||
root = (workspace_root or config.WORKSPACE_ROOT).resolve()
|
||||
return root / "users" / safe_workspace_leaf(username, user_id)
|
||||
|
||||
|
||||
def ensure_default_code_main(user_root: Path) -> None:
|
||||
"""Ensure code/ exists and add a starter main.py when missing."""
|
||||
code_dir = user_root / "code"
|
||||
code_dir.mkdir(parents=True, exist_ok=True)
|
||||
main_py = code_dir / "main.py"
|
||||
if not main_py.exists():
|
||||
main_py.write_text(DEFAULT_MAIN_PY, encoding="utf-8")
|
||||
Reference in New Issue
Block a user