Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix(learner): complete surface map for first-party learner screens + flip preset on restrictions assignment (#1222)

larryluozhang0c2666c010dd
diff --git a/deeptutor/api/routers/auth.py b/deeptutor/api/routers/auth.py
index 0cbf58105..45630dd47 100644
--- a/deeptutor/api/routers/auth.py
+++ b/deeptutor/api/routers/auth.py
@@ -469,7 +469,7 @@ async def require_admin(
return payload
-def _learning_surface_for_path(path: str) -> str:
+def _learning_surface_for_path(path: str, method: str = "GET") -> str:
normalized = "/" + str(path or "").lstrip("/")
for root, surface in (
("/api/reading", "reading"),
@@ -478,9 +478,20 @@ def _learning_surface_for_path(path: str) -> str:
("/api/question", "chat"),
("/api/question-notebook", "chat"),
("/api/sessions", "chat"),
+ # Mastery Path progress/topics are the learner's own per-user data;
+ # the router already scopes every record to the current account, so
+ # all methods (including progress PATCH/POST) belong to "chat".
+ ("/api/mastery-paths", "chat"),
):
if normalized == root or normalized.startswith(f"{root}/"):
return surface
+ # Knowledge-center browsing is a legitimate learner activity, but KB
+ # mutations affect shared/admin-owned resources — read-only methods only.
+ if method.upper() in ("GET", "HEAD", "OPTIONS") and (
+ normalized == "/api/knowledge-bases"
+ or normalized.startswith("/api/knowledge-bases/")
+ ):
+ return "reading"
return ""
@@ -492,7 +503,7 @@ async def require_learning_surface(
from deeptutor.multi_user.learning_access import assert_learning_surface
try:
- assert_learning_surface(_learning_surface_for_path(request.url.path))
+ assert_learning_surface(_learning_surface_for_path(request.url.path, request.method))
except PermissionError as exc:
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail=str(exc)) from exc
diff --git a/deeptutor/api/routers/multi_user.py b/deeptutor/api/routers/multi_user.py
index 9d310c513..055ff80f6 100644
--- a/deeptutor/api/routers/multi_user.py
+++ b/deeptutor/api/routers/multi_user.py
@@ -45,6 +45,7 @@ from deeptutor.multi_user.identity import (
list_user_info,
set_book_permission,
set_password,
+ set_preset,
)
from deeptutor.multi_user.knowledge_access import admin_kb_base_dir
from deeptutor.multi_user.model_access import is_owner_bound
@@ -670,6 +671,16 @@ async def put_guardian_restrictions(
)
grant = deepcopy(_restriction_grant(learner_user_id, learner_record))
policy = grant.get("learning_policy")
+ if not isinstance(policy, dict):
+ # Assigning guardian restrictions is what makes an account a learning
+ # account. Seed the default learning policy and flip the preset to
+ # "learner" so the frontend renders the scoped learner shell instead
+ # of the full app (a `standard` preset + `learning_policy` mix makes
+ # the client load admin surfaces that all default-deny to 403, #1222).
+ grant = deepcopy(learner_grant(learner_user_id))
+ policy = grant.get("learning_policy")
+ if isinstance(policy, dict) and _learner_username:
+ set_preset(_learner_username, "learner")
if not isinstance(policy, dict):
raise HTTPException(status_code=409, detail="Learner account has no learning policy")
reading = policy.get("reading")
diff --git a/tests/multi_user/test_learning_surface_map.py b/tests/multi_user/test_learning_surface_map.py
new file mode 100644
index 000000000..b4c662554
--- /dev/null
+++ b/tests/multi_user/test_learning_surface_map.py
@@ -0,0 +1,89 @@
+"""Regression tests for the learner surface map and restrictions preset flip (#1222).
+
+Two failure modes covered:
+
+1. ``_learning_surface_for_path`` used to map only five prefixes, so the
+ learner shell's own Knowledge Center (`/api/knowledge-bases/*`) and Mastery
+ Path (`/api/mastery-paths/*`) endpoints default-denied to 403.
+2. Assigning guardian restrictions to a `standard`-preset account left the
+ account in a `standard` preset + `learning_policy` mix: the frontend then
+ rendered the full admin shell and every surface request 403'd. The PUT
+ restrictions handler now seeds the default policy and flips the preset to
+ ``learner``.
+"""
+
+from __future__ import annotations
+
+import pytest
+
+from deeptutor.api.routers.auth import _learning_surface_for_path
+
+
+ ("path", "method", "expected"),
+ [
+ # Pre-existing mappings keep working.
+ ("/api/reading/materials", "GET", "reading"),
+ ("/api/courses", "GET", "reading"),
+ ("/api/chat/sessions", "GET", "chat"),
+ ("/api/question/generate", "POST", "chat"),
+ ("/api/sessions/abc", "GET", "chat"),
+ # Mastery Path: the learner's own per-user progress, all methods.
+ ("/api/mastery-paths/topics", "GET", "chat"),
+ ("/api/mastery-paths/topics/index", "GET", "chat"),
+ ("/api/mastery-paths/progress/book-1", "GET", "chat"),
+ ("/api/mastery-paths/progress/book-1", "PATCH", "chat"),
+ ("/api/mastery-paths/progress/book-1/redo", "POST", "chat"),
+ # Knowledge Center: read-only for learners.
+ ("/api/knowledge-bases", "GET", "reading"),
+ ("/api/knowledge-bases/kb1/files", "GET", "reading"),
+ ("/api/knowledge-bases/kb1/files/a.pdf", "GET", "reading"),
+ # KB mutations stay denied — they affect shared/admin resources.
+ ("/api/knowledge-bases", "POST", ""),
+ ("/api/knowledge-bases/kb1/upload", "POST", ""),
+ ("/api/knowledge-bases/kb1/files/a.pdf", "DELETE", ""),
+ # Everything else still default-denies.
+ ("/api/settings", "GET", ""),
+ ("/api/system/status", "GET", ""),
+ ("/api/partners", "GET", ""),
+ ("/api/memory/overview", "GET", ""),
+ ("", "GET", ""),
+ ],
+)
+def test_learning_surface_map(path: str, method: str, expected: str) -> None:
+ assert _learning_surface_for_path(path, method) == expected
+
+
+def test_put_restrictions_flips_standard_preset_to_learner(
+ mu_isolated_root, seed_user
+):
+ """PUT restrictions on a standard-preset account seeds the policy and
+ flips the account preset to ``learner`` (#1222 preset/policy mix)."""
+ import asyncio
+ from types import SimpleNamespace
+
+ from deeptutor.api.routers.multi_user import (
+ GuardianRestrictionsPayload,
+ put_guardian_restrictions,
+ )
+ from deeptutor.multi_user.identity import get_user_by_id
+
+ # The first account in an empty store is promoted to admin by save_user;
+ # seed a bootstrap account first so the learner keeps role="user".
+ seed_user("bootstrap-admin")
+ record = seed_user("student-standard")
+ learner_user_id = record["id"]
+ admin = SimpleNamespace(user_id="u_admin", role="admin")
+
+ payload = GuardianRestrictionsPayload(
+ age_band="13-15",
+ allow_upload=True,
+ allowed_surfaces=["chat", "reading"],
+ extensions=[],
+ )
+
+ result = asyncio.run(put_guardian_restrictions(learner_user_id, payload, admin))
+ assert result["restrictions"]["age_band"] == "13-15"
+
+ _username, updated = get_user_by_id(learner_user_id)
+ assert updated.get("preset") == "learner"