Scoping rules
How abstract staff grants become campus-scoped row filters, applyStaffAbilities, the campus-scope map, stored conditions, and rule merging.
Scoping is the half of role-based access control (RBAC) that decides which rows a staff member sees. This page follows a single permission from a role assignment all the way to a CASL condition, then shows how to extend the system safely.
applyStaffAbilities
This is the function that turns abstract grants into scoped rules, and it's worth reading slowly because it encodes the project's whole scoping policy. For each role assignment, for each (action, resource) permission on it, three things can happen.
The method takes role assignments, department assignments, teaching pairs, and class-teacher pairs. For each permission on a role assignment, in order:
- If the assignment is not school-scoped and
isSchoolOnlyGrantis true, the permission is skipped.SCHOOL_ONLY_RESOURCESisall,school,school_administration,school_dashboard,template,api_key,audit_log, andschool_calendar.SCHOOL_ONLY_WRITE_RESOURCESisrole,academic_year,knowledge_base,school_report, andreferral, and only forcreate,update,delete, andmanage. Read and list on those five still pass this check. manageonallcallscan(Action.MANAGE, Resource.ALL)andreturns from the whole method. Department grants later in the same method do not run.- A role named
CLASS_TEACHERwhose resource is inCLASS_TEACHER_SCOPE_FIELDusesapplyClassTeacherScopedRuleand does not also take the scope switch below. - Otherwise the scope level decides:
schoolgrants the permission with its stored conditions and no campus condition.campuscallsapplyCampusScopedRulewhencampusIdis set, including any class limits on the assignment.departmentcallsapplyUnitScopedRule. A resource missing fromDEPARTMENT_SCOPE_FIELDis not granted.subjectcallsapplySubjectScopedRule, one rule per teaching pair. A resource missing fromSUBJECT_SCOPE_FIELDis not granted.
After the role loop, department memberships are an additive union. A school-level department (no campusId) grants the permission with no campus condition. A campus department uses applyCampusScopedRule. The same school-only skip and the same manage on all early return apply there.
A staff member's JWT does not have to carry every campus. Two campus assignments produce two passes and OR-ed rules.
Campus scoping
"Restrict to campus" means different things for different resources. A student has a non-null campusId. A department can belong to a campus or to the whole school (nullable). A guardian belongs to an array of campuses. And a user has no campus column at all. The factory can't guess this: it looks it up in a map.
[Resource.STUDENT, 'campusId'],
[Resource.DEPARTMENT, { field: 'campusId', nullable: true }],
[Resource.GUARDIAN, { field: 'campusIds', isArray: true }],
[Resource.CAMPUS, 'id'],
[Resource.REPORT_CARD, { field: 'campusId', nullable: true }],The full map at API staging 38525cf9, by shape:
| Field shape | Resources |
|---|---|
campusId, required | student, class, notice_board, cbt_exam, election, grading_config, payment_gateway, ledger, enrollment, scheme_of_work, lesson_plan, subject, school_report, library_catalogue, library_management, timetable, social_connection |
campusId, nullable (school-wide rows also match) | department, application, staff, report_card, job_vacancy, payment_item, transaction, calendar |
campusIds array | guardian |
id | campus |
| Not in the map | user, academic_report, and every resource not listed |
A ledger account with no campus (an applicant not yet placed) is reachable only by school-scoped roles.
The other scope maps
These live in libs/shared/src/permission/permission-scope/scope-fields.ts. A resource that is not a key is not granted by that scope. The comment on each map says so.
Department (DEPARTMENT_SCOPE_FIELD). department matches id. subject matches departmentId. scheme_of_work, lesson_plan, cbt_exam, and grade match subjectId with viaSubjects: true.
Class teacher (CLASS_TEACHER_SCOPE_FIELD), used when the role name is CLASS_TEACHER. class matches class id. student matches stream field currentStreamId. enrollment matches stream field streamId. scheme_of_work and lesson_plan match stream field streamId and are nullable, so a group document with no stream stays readable.
Class limit (CLASS_LIMIT_SCOPE_FIELD), used when a campus assignment carries a class limit. Unlisted resources stay campus-wide. class uses id. student uses currentClassId and currentStreamId. enrollment uses classId and streamId.
Subject teacher (SUBJECT_SCOPE_FIELD), one rule per teaching pair. scheme_of_work and lesson_plan are subjectAndStream. grade is subject. cbt_exam is subjectAndCampus. class is class. enrollment is stream. student is currentStream.
How applyClassTeacherScopedRule turns a nullable stream field into a CASL condition was not re-read. The keys above are the map.
applyCampusScopedRule reads that map and behaves accordingly:
const mapping = CAMPUS_SCOPE_FIELD.get(resource);
if (!mapping) {
can(action, resource, extraConditions); // no campus column → unconditioned
return;
}
const field = typeof mapping === 'string' ? mapping : mapping.field;
const isNullable = typeof mapping === 'object' && 'nullable' in mapping && mapping.nullable;
const isReadAction = action === Action.READ || action === Action.LIST;
can(action, resource, { [field]: campusId, ...extraConditions }); // your campus
if (isNullable && isReadAction) {
can(action, resource, { [field]: null, ...extraConditions }); // + school-level rows
}The behavior, summarized:
| Map entry | Example | Rule produced for a campus-scoped grant |
|---|---|---|
| Not in the map | USER, ROLE, SUBJECT | Unconditioned: full access if they have the permission at all |
| Required scalar | STUDENT.campusId | { campusId: <yourCampus> } |
| Nullable scalar | DEPARTMENT.campusId | { campusId: <yourCampus> }, plus { campusId: null } for READ/LIST |
| Array field | GUARDIAN.campusIds | { campusIds: <yourCampus> } |
That nullable-plus-null pair is a deliberate policy: campus staff can read school-level items (a department that belongs to no campus) but can only write to their own campus, because the extra campusId: null rule is added for READ/LIST only.
Conditions are never assigned to users directly
Notice you never write { campusId: X } anywhere outside this factory. An admin assigns a role at a campus scope; the condition is the factory's job. There is no UI and no table for "give this user a campusId condition": it falls out of the assignment automatically. Departments work the same way via the parallel departmentAssignments loop.
Role conditions
There is one narrow channel where an admin can add a custom condition, and it's deliberately fenced. A role's permission can carry stored "filter" conditions: e.g. "this Grade Teacher role only sees students in the Science department." Those are stored as triples and compiled into CASL conditions at request time.
for (const triple of triples) {
conditions[triple.field] =
triple.operator === 'in' ? { $in: triple.value } : triple.value;
}The security boundary is FILTERABLE_FIELDS: a code-defined allow-list of which fields a role may filter on, per resource. An admin can't filter on an arbitrary column; the field has to be on the list, and the operator is limited to eq/in because the converter only handles those two safely.
export const FILTERABLE_FIELDS: ReadonlyMap<Resource, readonly FieldSpec[]> = new Map([
[Resource.STUDENT, [{ field: 'department', operators: ['eq', 'in'], valueType: 'string' }]],
]);These stored conditions ride into applyStaffAbilities as perm.conditions and get merged with the campus condition ({ [field]: campusId, ...extraConditions }), so a campus-scoped Science teacher ends up with both filters AND-ed together.
Merging rules
After all the can(...) calls, the rule list often has duplicates and overlaps: two campus assignments both granting read student, say. mergeCaslRules collapses them. This isn't cosmetic: the default Mongo matcher behind createMongoAbility cannot evaluate a top-level $or, so the merger has to combine rules without producing one.
It groups rules by (inverted, action, subject, fields) and, per group, applies a few rules in priority order:
- A conditionless rule wins. If any rule in the group has no conditions, the whole group collapses to that one: broader access beats narrower.
- Same-field values merge into
$in. Two rules that both constrain onlycampusIdcollapse to{ campusId: { $in: [a, b] } }. - Otherwise, keep them separate. Different fields or non-
$inoperators stay as distinct duplicate rules: CASL evaluates duplicates as OR, which the matcher does support (it just can't do an explicit$orkey).
You rarely call this directly, but knowing it exists explains why your /auth/me rules look tidier than the raw can() calls would suggest.
Adding a resource
Adding to the enums is a rare, deliberate change, and a new Resource ripples outward. The one step you must not skip is the campus-scope map.
- Add the value to
Resourceinlibs/shared/src/permission/permission.enum.ts. - Register it in the permission registry with a
kind(fullorreadonly) and label, so roles are allowed to grant it. Roles & Permissions Admin covers the registry. - If the resource has a campus column, add an entry to
CAMPUS_SCOPE_FIELDincampus-scope.tsso campus-scoped grants restrict correctly. - If non-staff identities should have static access, add the
can(...)calls to the relevant branch ofbuildAbility. - Use it:
@RequirePermissions({ action, resource })on the controller,getScope/assertCanAccessin the service.
A campus-scoped resource with no scope entry leaks
This is the trap in step 3. If a resource has a campusId column but you forget to add it to CAMPUS_SCOPE_FIELD, applyCampusScopedRule falls into its "not in the map → grant unconditioned" branch. A campus-scoped staffer then sees every campus's rows. The map is the safety boundary: keep it in sync with your entities.
Where to go next
Building the Ability
Where applyStaffAbilities is called from: resolveContext and buildAbility.
Enforcing Access
How these scoped rules get read back into a WHERE clause at the service layer.
Add a Resource Module
Build a resource end to end, including picking the right @RequirePermissions pair.
Roles & Permissions Admin
The registry, role CRUD, and how assignments are managed and scoped.