Skip to content

Implement class join codes back-end#789

Draft
fspeirs wants to merge 7 commits into
mainfrom
fs-implement-class-join-codes
Draft

Implement class join codes back-end#789
fspeirs wants to merge 7 commits into
mainfrom
fs-implement-class-join-codes

Conversation

@fspeirs
Copy link
Copy Markdown
Contributor

@fspeirs fspeirs commented Apr 22, 2026

This PR implements the back-end for class join codes — students join a class by entering an 8-character code in the format CDDD-CDDD (consonant + three digits, twice, separated by a hyphen).

Based on #787 for the email-domain validation pieces; will be rebased on main once that merges.

Changes

  • Add join_code column with a unique index to school_classes, plus a backfill migration for existing classes.
  • Add JoinCodeGenerator with .generate and .normalize (canonicalises user input to the hyphenated form for DB lookup).
  • Auto-assign join codes on SchoolClass create/import with a uniqueness retry, plus a regenerate_join_code! helper.
  • Add GET /api/join/:join_code returning the prospective student's status: unauthenticated, joinable, already_member, wrong_school, domain_mismatch, or not_a_student.
  • Add POST /api/join/:join_code to enrol the current user as a student of the school and class.
  • Add POST /api/school_classes/:id/regenerate_join_code.
  • Expose join_code on the school class JSON and sso_enabled? on the school JSON.
  • Add School#valid_email? (built on Store school email domains #787's domain validation) for the domain_mismatch check.
  • Update CanCan abilities so school owners and class teachers can regenerate_join_code.
  • Add specs covering the model, generator, controllers, and abilities.

@cla-bot cla-bot Bot added the cla-signed label Apr 22, 2026
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 22, 2026

Test coverage

91.27% line coverage reported by SimpleCov.
Run: https://github.com/RaspberryPiFoundation/editor-api/actions/runs/26086343300

Comment thread spec/models/school_class_spec.rb Fixed
@fspeirs fspeirs force-pushed the fs-implement-class-join-codes branch from 2b3e115 to bb23e12 Compare April 22, 2026 12:43
@fspeirs fspeirs force-pushed the fs-implement-class-join-codes branch 2 times, most recently from 508b98b to aec7f84 Compare May 7, 2026 12:42
@fspeirs fspeirs changed the title Implement class join codes Implement class join codes back-end May 18, 2026
@fspeirs fspeirs temporarily deployed to editor-api-p-fs-impleme-xjbsht May 18, 2026 10:23 Inactive
fspeirs added 7 commits May 19, 2026 09:36
Generate human-readable globally unique class join codes in the
format CVDDCVDD (e.g. CE18LI80) — consonant, vowel, two digits,
twice. The mixed alpha+numeric layout reads cleanly out loud,
limits visual ambiguity (no I/1 or O/0 collisions in the consonant
set), and is short enough to type from memory.

Removes K, X, and Z from the consonant pool to reduce the chance of
generating offensive substrings, and rejects any code whose
consonant-vowel pairs match a small denylist of common offensive
patterns. Generation retries up to 100 times before giving up.
Add a globally unique, regenerable join code to every class. The
code is generated automatically on creation and backfilled for
existing classes. Distinct from the existing per-school code:
join codes are globally unique (so /join/:code can resolve without
a school identifier), are designed to be regeneratable if a class
needs a fresh code, and are surfaced in the school_class JSON
payload for clients that display them.

Two migrations following the existing pattern for the per-school
code field — one to add the column and unique index, one to
backfill existing rows. The backfill skips validations because
existing rows may not satisfy other newer constraints.
Add POST /api/schools/:school_id/classes/:id/regenerate_join_code
so teachers and school owners can rotate a class's join code if
it has been over-shared. The endpoint authorises through CanCan;
both school_owner and school_teacher abilities gain the
regenerate_join_code permission for classes they have access to.
Add a public predicate that answers whether an email address
belongs to one of the school's allowed domains. Extracts the
domain from the email (after the last '@', stripped and
lowercased) and delegates to the existing valid_domain? lookup
against school_email_domains. Returns false for blank, malformed,
or domain-less inputs.

Lets callers gate behaviour on email domain without having to
parse the address themselves.
Add the student-facing half of the join-code flow: a JSON API the
frontend can call to look up a class from its join code and enroll
the current user. Mirrors the teacher_invitations pattern — backend
is JSON-only, the frontend owns the public URL the user lands on.

GET /api/join/:join_code is unauthenticated and returns
{ status, school, school_class } where status is one of
unauthenticated, joinable, already_member, wrong_school, or
domain_mismatch. The frontend uses status to decide what to show.

POST /api/join/:join_code requires auth and performs enrollment.
On success or for already_member it returns
{ redirect_url } (a locale-less path so the frontend can prepend
the user's current locale); on a forbidden status it returns 403
with the status as the error. Membership creation is idempotent.

The endpoint normalises join codes by uppercasing and stripping
non-alphanumerics, so hyphenated forms like BAFA-1234 from
copy-and-pasted share links resolve to the canonical BAFA1234.
Adds a boolean sso_enabled? to School (true when at least one
school_email_domain exists) and surfaces it through the school
jbuilder partial so the front end can detect JIT-provisioning
schools.
This will ease any concerns about accidentally generating rude words
while also remaining visually distinct from class codes or school codes,
which are DD-DD-DD.
@fspeirs fspeirs force-pushed the fs-implement-class-join-codes branch from 85e8b32 to efa9fd2 Compare May 19, 2026 08:43
@fspeirs fspeirs temporarily deployed to editor-api-p-fs-impleme-o7nlku May 19, 2026 08:43 Inactive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant