From fab5bf755714d709ce4fc36d93011a1aa50077fc Mon Sep 17 00:00:00 2001 From: Rabia Chadha Date: Tue, 17 Nov 2026 00:09:27 -0800 Subject: [PATCH] Show students' Cal 1 Card photos to staff (#4) Staff see each student's photo on the roster cards, the attendance lists and the student's history page, with a placeholder when there's none. - Store each student's SID (Canvas's sis_user_id) at login and when staff add them by email (migration 0005_sid). - c1c_service.py fetches photos from the C1C API the way seating does: /c1c-api/v1/photo/{sid}, basic auth, through C1C_PROXY_URL, with the same settings. Development uses a drawn fake unless MOCK_C1C=false. - GET /api/students//photo serves a photo to staff in the course only, with Cache-Control: private, and nothing is saved. Co-Authored-By: Claude Opus 5.5 --- .env.example | 9 +++ server/c1c_service.py | 55 +++++++++++++++++++ server/canvas_service.py | 9 ++- server/config.py | 7 +++ server/fake_canvas.py | 3 +- server/login.py | 3 + server/migrations/versions/0005_sid.py | 24 ++++++++ server/models.py | 3 + server/state.py | 23 +++++++- src/HistoryPage.tsx | 13 +++-- src/SectionRoster.tsx | 44 ++++++++------- src/SessionAttendance.tsx | 10 +++- src/StudentPhoto.tsx | 43 +++++++++++++++ tests/a11y/test_a11y_pages.py | 1 + tests/unit/test_add_students.py | 23 +++++++- tests/unit/test_dev_login.py | 2 +- tests/unit/test_login.py | 6 ++ tests/unit/test_roster_photos.py | 76 ++++++++++++++++++++++++++ 18 files changed, 320 insertions(+), 34 deletions(-) create mode 100644 server/c1c_service.py create mode 100644 server/migrations/versions/0005_sid.py create mode 100644 src/StudentPhoto.tsx create mode 100644 tests/unit/test_roster_photos.py diff --git a/.env.example b/.env.example index 026759b..633a378 100644 --- a/.env.example +++ b/.env.example @@ -37,6 +37,15 @@ GOOGLE_SERVICE_ACCOUNT_EMAIL= # Optional: enables /api/sudo/* and export_attendance_secret. Use a long random value. API_SECRET= +# Cal 1 Card photo API, for roster photos; the same settings as seating. In +# development the app uses made-up photos unless this is false. C1C only answers +# allowlisted addresses, so requests go through C1C_PROXY_URL (ask a maintainer). +MOCK_C1C=true +C1C_API_DOMAIN= +C1C_API_USERNAME= +C1C_API_PASSWORD= +C1C_PROXY_URL= + # Google Sheets import uses Application Default Credentials. Locally, run: # gcloud auth application-default login \ # --scopes=https://www.googleapis.com/auth/cloud-platform,https://www.googleapis.com/auth/spreadsheets.readonly diff --git a/server/c1c_service.py b/server/c1c_service.py new file mode 100644 index 0000000..bf48c51 --- /dev/null +++ b/server/c1c_service.py @@ -0,0 +1,55 @@ +"""Student photos from the Cal 1 Card office's API (C1C), for roster photos (#4). + +Works like seating's server/services/c1c/__init__.py, which has used this API +since 2024, and takes the same settings. C1C only answers requests from +allowlisted addresses, so requests go through C1C_PROXY_URL when it's set. +""" + +import requests +from flask import current_app + +# Seconds to wait for C1C, so a slow response can't hold up the request; the +# roster shows the placeholder instead. +TIMEOUT = 10 + + +# Input: sid, a student's ID number. +# Output: (image bytes, content type), or None if there's no photo. +# Description: Development uses a made-up picture (fake_photo) unless +# MOCK_C1C=false. Without C1C settings (as on staging today), or if C1C fails, +# there's no photo, so the page shows a placeholder instead of an error. +def get_photo(sid: str) -> tuple[bytes, str] | None: + config = current_app.config + if config.get("MOCK_C1C"): + return fake_photo(sid) + if not config.get("C1C_API_DOMAIN"): + return None + proxy_url = config.get("C1C_PROXY_URL") + try: + response = requests.get( + config["C1C_API_DOMAIN"].rstrip("/") + f"/c1c-api/v1/photo/{sid}", + auth=(config["C1C_API_USERNAME"], config["C1C_API_PASSWORD"]), + proxies={"http": proxy_url, "https": proxy_url} if proxy_url else None, + timeout=TIMEOUT, + ) + except requests.RequestException: + return None + if response.status_code != 200: + return None + return response.content, "image/jpeg" + + +# Input: sid, a student's ID number. +# Output: (SVG bytes, "image/svg+xml"), a plain head-and-shoulders drawing. +# Description: Stands in for a real photo in development and tests. The +# background color comes from the SID, so different students look different. +def fake_photo(sid: str) -> tuple[bytes, str]: + hue = sum(map(ord, sid)) * 47 % 360 + svg = ( + '' + f'' + '' + '' + "" + ) + return svg.encode(), "image/svg+xml" diff --git a/server/canvas_service.py b/server/canvas_service.py index 655f019..9fb3b39 100644 --- a/server/canvas_service.py +++ b/server/canvas_service.py @@ -69,11 +69,14 @@ def get_user_courses(user_id, access_token: str) -> dict: def get_student_from_email(email: str, access_token: str): - """``(canvas user id, name)`` of the student in the current course with - login ``email``, or None. The name is their display name, as at login.""" + """``(canvas user id, name, sid)`` of the student in the current course with + login ``email``, or None. The name is their display name, as at login. The + SID is None if Canvas doesn't show it to this staff member.""" course = _client(access_token).get_course(get_canvas_course_id()) for enrollment in course.get_enrollments(type=["StudentEnrollment"]): user = enrollment.user if user["login_id"] == email: - return str(user["id"]), user.get("short_name") or user["name"] + # Canvas's docs promise the SID on the enrollment; fall back to the user record. + sid = getattr(enrollment, "sis_user_id", None) or user.get("sis_user_id") + return str(user["id"]), user.get("short_name") or user["name"], sid return None diff --git a/server/config.py b/server/config.py index 8830372..f5cbc72 100644 --- a/server/config.py +++ b/server/config.py @@ -83,6 +83,13 @@ def load_config(app: flask.Flask): GOOGLE_SERVICE_ACCOUNT_EMAIL=os.getenv("GOOGLE_SERVICE_ACCOUNT_EMAIL"), # Shared secret for the /api/sudo/* and export_attendance_secret endpoints. API_SECRET=os.getenv("API_SECRET"), + # Cal 1 Card photo API (c1c_service.py), with the same settings as + # seating. Development uses made-up photos unless MOCK_C1C=false. + MOCK_C1C=is_dev and os.getenv("MOCK_C1C", "true").lower() == "true", + C1C_API_DOMAIN=os.getenv("C1C_API_DOMAIN"), + C1C_API_USERNAME=os.getenv("C1C_API_USERNAME"), + C1C_API_PASSWORD=os.getenv("C1C_API_PASSWORD"), + C1C_PROXY_URL=os.getenv("C1C_PROXY_URL"), PERMANENT_SESSION_LIFETIME=7200, SESSION_COOKIE_HTTPONLY=True, SESSION_COOKIE_SECURE=not is_dev, diff --git a/server/fake_canvas.py b/server/fake_canvas.py index e1d8024..da9dfff 100644 --- a/server/fake_canvas.py +++ b/server/fake_canvas.py @@ -28,7 +28,8 @@ def __init__(self, canvas_id): def get_profile(self) -> dict: return {"id": self.id, "name": self.name, "short_name": self.short_name, - "primary_email": self.email, "login_id": self.login_id} + "primary_email": self.email, "login_id": self.login_id, + "sis_user_id": self.sis_user_id} def get_courses(self, *, enrollment_state="active", include=(), per_page=100) -> list[FakeCourse]: return [FakeCourse(course_id, course["enrollments"]) diff --git a/server/login.py b/server/login.py index b4e598e..6296c2e 100644 --- a/server/login.py +++ b/server/login.py @@ -54,6 +54,9 @@ def complete_login(token: dict): user.email = email user.name = name user.pronouns = profile.get("pronouns") or None + # Canvas may leave out the SID, depending on what this person may see; keep + # the one we have (e.g. from when staff added them) rather than erase it. + user.sid = profile.get("sis_user_id") or user.sid user.canvas_courses = courses user.is_global_admin = override_id is not None and str(override_id) in courses db.session.commit() diff --git a/server/migrations/versions/0005_sid.py b/server/migrations/versions/0005_sid.py new file mode 100644 index 0000000..e3e29b9 --- /dev/null +++ b/server/migrations/versions/0005_sid.py @@ -0,0 +1,24 @@ +"""Add users.sid, the student ID number from Canvas, for roster photos. + +Revision ID: 0005_sid +Revises: 0004_pronouns +Create Date: 2026-11-16 +""" +from alembic import op +import sqlalchemy as sa + + +revision = "0005_sid" +down_revision = "0004_pronouns" +branch_labels = None +depends_on = None + + +def upgrade(): + with op.batch_alter_table("users") as batch: + batch.add_column(sa.Column("sid", sa.String(length=255), nullable=True)) + + +def downgrade(): + with op.batch_alter_table("users") as batch: + batch.drop_column("sid") diff --git a/server/models.py b/server/models.py index 13f5f40..d3c2946 100644 --- a/server/models.py +++ b/server/models.py @@ -230,6 +230,9 @@ class User(db.Model, UserMixin): # As shared through CalCentral/UCPath and reported by Canvas at login; None if # the person hasn't shared any (or hasn't logged in). pronouns: str = db.Column(db.String(255), nullable=True) + # Student ID number (Canvas's sis_user_id), used to look up their Cal 1 Card + # photo. None until Canvas has shown it to us, at login or when staff add them. + sid: str = db.Column(db.String(255), nullable=True) # Active Canvas courses at last login, keyed by str(canvas course id): # {"name", "code", "start_at", "is_staff", "is_admin", "is_student"}. canvas_courses: dict = db.Column(db.JSON, nullable=False, default=dict) diff --git a/server/state.py b/server/state.py index f8f7d3a..67a41c4 100644 --- a/server/state.py +++ b/server/state.py @@ -18,6 +18,7 @@ from sqlalchemy import func from sqlalchemy.orm import joinedload, noload +import c1c_service import canvas_service from offering import OFFERING_URL_PREFIX, get_canvas_course_id, get_course, get_course_name, load_offering from import_sheet import check_capacity, import_sections_from_url, import_enrollment_from_url, parse_capacity @@ -707,8 +708,9 @@ def add_students(emails: str, section_id: str): if not found: raise Failure("Could not find email that belongs to this class") - canvas_id, canvasname = found + canvas_id, canvasname, sid = found student = find_or_add_person(email, canvasname, canvas_id=canvas_id) + student.sid = sid or student.sid student.set_role(get_course(), is_staff=False) add_student_helper(student, section) db.session.commit() @@ -835,6 +837,25 @@ def fetch_user(user_id: str): raise Failure(f"No user found with id {user_id}") return user.simple_json + # Input: user_id, the student whose photo to show. + # Output: their Cal 1 Card photo, or 404 if there isn't one. + # Description: A plain GET, not @api, so an tag can load it. Staff + # only, and only for people in this course. The photo isn't saved, and + # "private" keeps shared caches from storing it. + @bp.route("/api/students//photo") + @login_required + def student_photo(user_id: int): + if not current_user.is_staff: + abort(403) + student = offering_member(id=user_id) + photo = c1c_service.get_photo(student.sid) if student is not None and student.sid else None + if photo is None: + abort(404) + image, content_type = photo + response = flask.Response(image, mimetype=content_type) + response.headers["Cache-Control"] = "private, max-age=3600" + return response + @api @staff_required def get_userid(email: str): diff --git a/src/HistoryPage.tsx b/src/HistoryPage.tsx index 2bbd22d..41f8277 100644 --- a/src/HistoryPage.tsx +++ b/src/HistoryPage.tsx @@ -12,6 +12,7 @@ import Pronouns from "./Pronouns"; import type { ID, PersonDetails } from "./models"; import { sectionTitle } from "./models"; import StateContext from "./StateContext"; +import StudentPhoto from "./StudentPhoto"; import useAPI from "./useAPI"; type Props = { @@ -45,10 +46,14 @@ export default function HistoryPage({ userID }: Props): React.ReactNode {
-

- {user.name} - -

+
+ {/* userID is set only when staff look at someone else's history. */} + {userID != null && } +

+ {user.name} + +

+
{userID == null && (

diff --git a/src/SectionRoster.tsx b/src/SectionRoster.tsx index f7ebce2..7958a0c 100644 --- a/src/SectionRoster.tsx +++ b/src/SectionRoster.tsx @@ -7,6 +7,7 @@ import AddStudentModal from "./AddStudentModal"; import Pronouns from "./Pronouns"; import type { SectionDetails } from "./models"; import StateContext from "./StateContext"; +import StudentPhoto from "./StudentPhoto"; import useAPI from "./useStateAPI"; import useSectionAPI from "./useSectionAPI"; @@ -80,26 +81,29 @@ export default function SectionRoster({ section }: Props) { {section.students.map((student) => ( - - - {student.name} - - - - {student.email} - {" "} - + + +

+ + {student.name} + + + + {student.email} + {" "} + +
diff --git a/src/SessionAttendance.tsx b/src/SessionAttendance.tsx index a0d19fa..2c87d7b 100644 --- a/src/SessionAttendance.tsx +++ b/src/SessionAttendance.tsx @@ -8,6 +8,7 @@ import styled from "styled-components"; import AddStudentModal from "./AddStudentModal"; import AttendanceRow from "./AttendanceRow"; import Pronouns from "./Pronouns"; +import StudentPhoto from "./StudentPhoto"; import { nextSessionStartTime } from "./models"; import type { AttendanceStatusType, Person, SectionDetails, Session } from "./models"; import useSectionAPI from "./useSectionAPI"; @@ -110,8 +111,13 @@ export default function SessionAttendance({ section, session }: Props) { {students.map((student) => ( - {student.name} - +
+ + + {student.name} + + +
(null); + + if (missingPhotoID === studentID) { + return ( + + ); + } + return ( + setMissingPhotoID(studentID)} + /> + ); +} diff --git a/tests/a11y/test_a11y_pages.py b/tests/a11y/test_a11y_pages.py index 66a54ac..7e2b576 100644 --- a/tests/a11y/test_a11y_pages.py +++ b/tests/a11y/test_a11y_pages.py @@ -68,6 +68,7 @@ class Page: "/": "not a page (the React build's files)", "/health": "not a page", "/offerings//debug": "not a page", + "/offerings//api/students//photo": "not a page (a student's photo)", "/login/": "redirects to the login page", "/authorized/": "redirects after login", "/logout/": "redirects after logout", diff --git a/tests/unit/test_add_students.py b/tests/unit/test_add_students.py index 5dcdefc..8643dfc 100644 --- a/tests/unit/test_add_students.py +++ b/tests/unit/test_add_students.py @@ -30,13 +30,14 @@ def test_expired_or_missing_token_requests_reauthentication_without_partial_batc def test_fresh_token_is_passed_to_canvas(self): self.authorize() - with patch("state.canvas_service.get_student_from_email", return_value=("555", "New")) as lookup: + with patch("state.canvas_service.get_student_from_email", return_value=("555", "New", "3030000000")) as lookup: self.assertTrue(self.add_students("new@test")["success"]) lookup.assert_called_once_with("new@test", "access") db.session.remove() new = User.query.filter_by(email="new@test").one() # Recorded by Canvas id, so a later email change still finds them. self.assertEqual((new.canvas_id, new.student_offerings), ("555", {"cs61a"})) + self.assertEqual(new.sid, "3030000000") self.assertEqual([s.id for s in new.sections], [self.section_id]) def test_revoked_token_requests_reauthentication(self): @@ -57,9 +58,27 @@ class Enrollment: client.return_value.get_course.return_value.get_enrollments.return_value = [Enrollment()] with self.app.test_request_context(): g.offering = Offering.query.filter_by(canvas_id=CS61A).one() - self.assertEqual(canvas_service.get_student_from_email("new@test", "token"), ("555", "Display Name")) + self.assertEqual(canvas_service.get_student_from_email("new@test", "token"), ("555", "Display Name", None)) self.assertIsNone(canvas_service.get_student_from_email("other@test", "token")) + def test_canvas_lookup_reads_the_sid_from_the_enrollment_or_the_user(self): + import canvas_service + + class Enrollment: + user = {"id": 555, "login_id": "new@test", "name": "Name", "sis_user_id": "3030000001"} + + class EnrollmentWithSid(Enrollment): + sis_user_id = "3030000002" + + client = patch("canvas_service._client").start() + self.addCleanup(patch.stopall) + enrollments = client.return_value.get_course.return_value.get_enrollments + with self.app.test_request_context(): + g.offering = Offering.query.filter_by(canvas_id=CS61A).one() + for enrollment, sid in ((EnrollmentWithSid(), "3030000002"), (Enrollment(), "3030000001")): + enrollments.return_value = [enrollment] + self.assertEqual(canvas_service.get_student_from_email("new@test", "token")[2], sid) + def test_email_lookups_ignore_case(self): with patch("state.canvas_service.get_student_from_email") as lookup: self.assertTrue(self.add_students("KNOWN@Test")["success"]) diff --git a/tests/unit/test_dev_login.py b/tests/unit/test_dev_login.py index 42d3e27..3467e13 100644 --- a/tests/unit/test_dev_login.py +++ b/tests/unit/test_dev_login.py @@ -94,7 +94,7 @@ def test_finds_students_by_email(self): with self.app.test_request_context(): load_offering(CS169A) self.assertEqual(canvas_service.get_student_from_email("jx@berkeley.edu", "dev_access_token"), - (JIMMY_XU, "Jimmy Xu")) + (JIMMY_XU, "Jimmy Xu", "3033033334")) # Staff aren't students. self.assertIsNone(canvas_service.get_student_from_email("long_yu@berkeley.edu", "dev_access_token")) diff --git a/tests/unit/test_login.py b/tests/unit/test_login.py index beee6f2..297ace6 100644 --- a/tests/unit/test_login.py +++ b/tests/unit/test_login.py @@ -59,6 +59,12 @@ def test_login_records_pronouns_as_shared_in_canvas(self): self.log_in_as(2, "known@test", pronouns="") self.assertIsNone(db.session.get(User, self.known_id).pronouns) + def test_login_records_the_sid_and_keeps_it_if_canvas_leaves_it_out(self): + self.log_in_as(2, "known@test", sis_user_id="3030000000") + self.assertEqual(db.session.get(User, self.known_id).sid, "3030000000") + self.log_in_as(2, "known@test") + self.assertEqual(db.session.get(User, self.known_id).sid, "3030000000") + def test_login_never_claims_someone_else_by_email(self): # Canvas user 99 has the email of "staff" (Canvas user 1). with self.assertRaises(Exception) as raised: diff --git a/tests/unit/test_roster_photos.py b/tests/unit/test_roster_photos.py new file mode 100644 index 0000000..92942c4 --- /dev/null +++ b/tests/unit/test_roster_photos.py @@ -0,0 +1,76 @@ +"""Student photos from the Cal 1 Card API: who can see them, and when there are none.""" + +from unittest.mock import patch + +import requests + +from models import User, db +from tests.unit.support import CS61A, DATA8, CourseTestCase + +PHOTO_URL = f"/offerings/{CS61A}/api/students/{{}}/photo" + + +class RosterPhotoTests(CourseTestCase): + config = {"MOCK_C1C": True} + + def setUp(self): + super().setUp() + db.session.get(User, self.known_id).sid = "3030000000" + db.session.commit() + + def test_staff_get_the_photo_and_shared_caches_may_not_keep_it(self): + response = self.get(PHOTO_URL.format(self.known_id)) + self.assertEqual(response.status_code, 200) + self.assertEqual(response.mimetype, "image/svg+xml") + self.assertEqual(response.headers["Cache-Control"], "private, max-age=3600") + + def test_students_cannot_see_photos(self): + self.sign_in(self.known) + self.assertEqual(self.get(PHOTO_URL.format(self.known_id)).status_code, 403) + + def test_signed_out_requests_get_401(self): + self.client.delete_cookie("session") + self.assertEqual(self.get(PHOTO_URL.format(self.known_id)).status_code, 401) + + def test_no_photo_without_an_sid(self): + db.session.get(User, self.known_id).sid = None + db.session.commit() + self.assertEqual(self.get(PHOTO_URL.format(self.known_id)).status_code, 404) + + def test_no_photo_of_someone_outside_the_course(self): + outsider = User(email="outsider@test", name="Outsider", sid="3039999999") + outsider.set_role(str(DATA8), is_staff=False) + db.session.add(outsider) + db.session.commit() + self.assertEqual(self.get(PHOTO_URL.format(outsider.id)).status_code, 404) + + def test_no_photos_without_c1c_settings(self): + self.app.config["MOCK_C1C"] = False + with patch("c1c_service.requests.get") as request: + self.assertEqual(self.get(PHOTO_URL.format(self.known_id)).status_code, 404) + request.assert_not_called() + + def use_real_c1c(self): + self.app.config.update(MOCK_C1C=False, C1C_API_DOMAIN="https://c1c.test/", + C1C_API_USERNAME="user", C1C_API_PASSWORD="password", + C1C_PROXY_URL="http://proxy.test") + + def test_real_api_is_asked_through_the_proxy_as_seating_does(self): + self.use_real_c1c() + with patch("c1c_service.requests.get") as request: + request.return_value.status_code = 200 + request.return_value.content = b"jpeg" + response = self.get(PHOTO_URL.format(self.known_id)) + self.assertEqual((response.status_code, response.mimetype, response.data), + (200, "image/jpeg", b"jpeg")) + request.assert_called_once_with( + "https://c1c.test/c1c-api/v1/photo/3030000000", auth=("user", "password"), + proxies={"http": "http://proxy.test", "https": "http://proxy.test"}, timeout=10) + + def test_c1c_errors_and_timeouts_mean_no_photo(self): + self.use_real_c1c() + with patch("c1c_service.requests.get") as request: + request.return_value.status_code = 500 + self.assertEqual(self.get(PHOTO_URL.format(self.known_id)).status_code, 404) + request.side_effect = requests.Timeout() + self.assertEqual(self.get(PHOTO_URL.format(self.known_id)).status_code, 404)