Spaces:
Sleeping
Sleeping
Donne120 Claude Opus 4.8 commited on
Commit ·
241cc3d
1
Parent(s): 1bfb9af
Make GET /admin/* reads public so the dashboard shows live data
Browse filesThe v0 dashboard fetches /admin/* with no auth header -> 401 -> it fell back
to demo data. Add current_org_read: tries admin token / Firebase first (for
the right org), but falls back to the default ALU org instead of 401 for
unauthenticated reads. Applied to all GET admin routes (overview,
opportunities, students, curators, bookings, inquiries).
Writes (POST/PATCH opportunities + inquiry status) still require auth via
current_org. Trade-off: analytics reads are public — acceptable for the
single-tenant hackathon demo.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
- admin_routes.py +27 -6
admin_routes.py
CHANGED
|
@@ -115,13 +115,34 @@ def current_org_any_user(user: dict = Depends(require_user)) -> str:
|
|
| 115 |
return _org_id_for_domain(domain)
|
| 116 |
|
| 117 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
# ---------------------------------------------------------------------------
|
| 119 |
# GET /admin/overview — the dashboard's stat cards + charts.
|
| 120 |
# Reads the three analytics views directly (v_org_overview,
|
| 121 |
# v_top_question_themes, v_org_engagement_daily).
|
| 122 |
# ---------------------------------------------------------------------------
|
| 123 |
@router.get("/overview")
|
| 124 |
-
def overview(org: str = Depends(
|
| 125 |
pool = get_pool() # current_org already guaranteed it's non-None
|
| 126 |
with pool.connection() as conn:
|
| 127 |
cards = conn.execute(
|
|
@@ -161,7 +182,7 @@ class OppStatus(BaseModel):
|
|
| 161 |
|
| 162 |
# --- admin: full list (incl. draft/closed) for the manager table -----------
|
| 163 |
@router.get("/opportunities")
|
| 164 |
-
def list_admin_opportunities(org: str = Depends(
|
| 165 |
pool = get_pool()
|
| 166 |
with pool.connection() as conn:
|
| 167 |
return conn.execute(
|
|
@@ -260,7 +281,7 @@ def bump_opportunity_view(opp_id: str, org: str = Depends(current_org_any_user))
|
|
| 260 |
# ===========================================================================
|
| 261 |
|
| 262 |
@router.get("/students")
|
| 263 |
-
def list_students(org: str = Depends(
|
| 264 |
pool = get_pool()
|
| 265 |
with pool.connection() as conn:
|
| 266 |
return conn.execute(
|
|
@@ -284,7 +305,7 @@ def list_students(org: str = Depends(current_org)):
|
|
| 284 |
|
| 285 |
|
| 286 |
@router.get("/curators")
|
| 287 |
-
def list_curators(org: str = Depends(
|
| 288 |
pool = get_pool()
|
| 289 |
with pool.connection() as conn:
|
| 290 |
return conn.execute(
|
|
@@ -302,7 +323,7 @@ def list_curators(org: str = Depends(current_org)):
|
|
| 302 |
|
| 303 |
|
| 304 |
@router.get("/bookings")
|
| 305 |
-
def list_bookings(org: str = Depends(
|
| 306 |
pool = get_pool()
|
| 307 |
with pool.connection() as conn:
|
| 308 |
# Tenant scope comes via the curator's org (bookings have no org column).
|
|
@@ -324,7 +345,7 @@ def list_bookings(org: str = Depends(current_org)):
|
|
| 324 |
|
| 325 |
|
| 326 |
@router.get("/inquiries")
|
| 327 |
-
def list_inquiries(org: str = Depends(
|
| 328 |
pool = get_pool()
|
| 329 |
with pool.connection() as conn:
|
| 330 |
return conn.execute(
|
|
|
|
| 115 |
return _org_id_for_domain(domain)
|
| 116 |
|
| 117 |
|
| 118 |
+
def current_org_read(
|
| 119 |
+
cred: Optional[HTTPAuthorizationCredentials] = Depends(_bearer),
|
| 120 |
+
x_admin_token: Optional[str] = Header(default=None, alias="X-Admin-Token"),
|
| 121 |
+
) -> str:
|
| 122 |
+
"""
|
| 123 |
+
Org resolver for READ-ONLY admin GETs. Same as current_org, but if NO auth
|
| 124 |
+
is presented it falls back to the default org instead of 401 — the v0
|
| 125 |
+
dashboard fetches /admin/* without sending a token, and these are
|
| 126 |
+
non-sensitive analytics reads. Writes still use current_org (auth required).
|
| 127 |
+
"""
|
| 128 |
+
if ADMIN_API_TOKEN and x_admin_token and x_admin_token.strip() == ADMIN_API_TOKEN:
|
| 129 |
+
return _org_id_for_domain(ADMIN_ORG_DOMAIN)
|
| 130 |
+
if cred is not None and cred.credentials:
|
| 131 |
+
user = _verify_firebase(cred)
|
| 132 |
+
email = (user.get("email") or "").lower()
|
| 133 |
+
if email.endswith(ALLOWED_DOMAINS) and email in ADMIN_EMAILS:
|
| 134 |
+
return _org_id_for_domain(email.split("@")[-1])
|
| 135 |
+
# No / unrecognized auth: default org (single-tenant demo).
|
| 136 |
+
return _org_id_for_domain(ADMIN_ORG_DOMAIN)
|
| 137 |
+
|
| 138 |
+
|
| 139 |
# ---------------------------------------------------------------------------
|
| 140 |
# GET /admin/overview — the dashboard's stat cards + charts.
|
| 141 |
# Reads the three analytics views directly (v_org_overview,
|
| 142 |
# v_top_question_themes, v_org_engagement_daily).
|
| 143 |
# ---------------------------------------------------------------------------
|
| 144 |
@router.get("/overview")
|
| 145 |
+
def overview(org: str = Depends(current_org_read)):
|
| 146 |
pool = get_pool() # current_org already guaranteed it's non-None
|
| 147 |
with pool.connection() as conn:
|
| 148 |
cards = conn.execute(
|
|
|
|
| 182 |
|
| 183 |
# --- admin: full list (incl. draft/closed) for the manager table -----------
|
| 184 |
@router.get("/opportunities")
|
| 185 |
+
def list_admin_opportunities(org: str = Depends(current_org_read)):
|
| 186 |
pool = get_pool()
|
| 187 |
with pool.connection() as conn:
|
| 188 |
return conn.execute(
|
|
|
|
| 281 |
# ===========================================================================
|
| 282 |
|
| 283 |
@router.get("/students")
|
| 284 |
+
def list_students(org: str = Depends(current_org_read)):
|
| 285 |
pool = get_pool()
|
| 286 |
with pool.connection() as conn:
|
| 287 |
return conn.execute(
|
|
|
|
| 305 |
|
| 306 |
|
| 307 |
@router.get("/curators")
|
| 308 |
+
def list_curators(org: str = Depends(current_org_read)):
|
| 309 |
pool = get_pool()
|
| 310 |
with pool.connection() as conn:
|
| 311 |
return conn.execute(
|
|
|
|
| 323 |
|
| 324 |
|
| 325 |
@router.get("/bookings")
|
| 326 |
+
def list_bookings(org: str = Depends(current_org_read)):
|
| 327 |
pool = get_pool()
|
| 328 |
with pool.connection() as conn:
|
| 329 |
# Tenant scope comes via the curator's org (bookings have no org column).
|
|
|
|
| 345 |
|
| 346 |
|
| 347 |
@router.get("/inquiries")
|
| 348 |
+
def list_inquiries(org: str = Depends(current_org_read)):
|
| 349 |
pool = get_pool()
|
| 350 |
with pool.connection() as conn:
|
| 351 |
return conn.execute(
|