Naalya Handbook
Manage roles

Assigning roles

Attaching a role to a staff user with a campus scope, the assign endpoint, automatic assignment, why scope never lives on a permission, and gotchas.

A role is inert until it's assigned to a user. This page covers how an assignment carries its scope, how some assignments happen automatically, and why you never encode a campus into a permission directly.

Assignment endpoints

Roles attach to users through the user_x_role table, never to permissions directly. The endpoints hang under the user:

Method · PathoperationIdGuard
POST /users/:userId/rolesassignRoleMANAGE ROLE
GET /users/:userId/roleslistUserRolesREAD ROLE
DELETE /users/:userId/roles/:assignmentIdremoveRoleAssignmentMANAGE ROLE

Scope on the assignment

The body that creates an assignment carries the scope, and this is the crux of the whole design. An assignment isn't just "user has role"; it's "user has role at this level". ScopeLevel in libs/shared/src/user/user.enums.ts, read at API fbb63d5178bcfe171292daebb19b6cbdac025a44, has four values: school, campus, department, and subject. The comment on subject says it is derived from the teacher’s subject assignments and has no explicit target.

apps/server/src/app/role/dto/assign-role.dto.ts
export class AssignRoleDto {
  @IsUUID() @IsNotEmpty() roleId: string;
  @IsEnum(ScopeLevel) @IsNotEmpty() scopeLevel: ScopeLevel;   // school | campus | department | subject
  @IsUUID() @IsOptional() campusId?: string;
}

UserRoleService.assignRole enforces a tight set of rules before it writes the row. The target user must be STAFF: roles never go on students or guardians. The role must exist. And the scope/campus pairing must be consistent.

On API staging, not on main d0fbb403, assigning a role is limited by what the caller holds. At 6308a8a1 that limit was manage on all, and giving yourself any role without that pair was refused. At f371dda879fe5933e8970847dfc3af1803c95a58 (merge of PR #689, 28 November 2026, 20:25 GMT) assignRole calls assertCanGrant with the role's stored permission rows, so the assigner must hold every permission in the role. Giving yourself a role still needs manage on all. Both throw 403 You can only grant permissions you hold. assertCanGrant accepts those stored rows as well as DTO pairs. Read in apps/server/src/app/role/user-x-role.service.ts and libs/shared/src/permission/permission-scope/grant-ceiling.ts. The new test was not run. A merge to staging is not a running deploy. The rule is on Enforcing access.

apps/server/src/app/role/user-x-role.service.ts
if (user.type !== UserType.STAFF) {
  throw new ForbiddenException('Roles can only be assigned to staff users');
}
// ...role must exist...
if (dto.scopeLevel === ScopeLevel.CAMPUS) {
  if (!dto.campusId) {
    throw new BadRequestException('campusId is required when scopeLevel is campus');
  }
  const campus = await this.campusService.findOne(dto.campusId);
  if (!campus) throw new NotFoundException('Campus not found');
}

For a CAMPUS assignment a real campusId is required; for SCHOOL it's omitted. A department assignment requires departmentId. Subject scope has no target id. The uniqueness constraint is on (userId, roleId, campusId), so a duplicate for the same scope gets a ConflictException, and the assigner is recorded as assignedBy for the audit trail.

AssignRoleDto was opened at the same pin. It requires roleId and scopeLevel. Optional fields are campusId (the description says required when the scope is campus), departmentId (required when the scope is department), academicYearId (the assignment applies while that year is current), and classes (campus scope only). AssignRoleClassLimitDto requires classId. streamIds is an optional array of UUIDs. The description says to omit it for the whole class. The service throws User not found, Roles can only be assigned to staff users (403, and that 403 is not on this method’s Swagger), Role not found, This role is already assigned to the user for this scope, a sentence that school-level permissions can only be assigned school-wide, Class limits are only allowed on campus-scoped assignments, Academic year not found, campusId is required when scopeLevel is campus, Campus not found, departmentId is required when scopeLevel is department, Department not found, Class plus an id plus not found, Class limits must belong to the assignment campus, and Stream plus an id does not belong to class plus the class id. Swagger’s 404 is User, role, or campus not found. It does not name a department, academic year, or class. Swagger’s 409 is Role already assigned to this user. The thrown conflict string names the scope. The method has no bad-request decorator. The Hub surface that calls assignRole is on Portal routes.

Scope lives on the assignment: that's how 'this campus only' happens

A CAMPUS-scoped assignment is what makes a role's permissions apply to only one campus's rows. The permissions on the role stay generic (READ STUDENT); the assignment supplies the campus. At request time the ability factory turns that campusId into a { campusId: X } condition, see RBAC & Scopes. Nothing about the campus is stored on the permission itself.

Removal is guarded by SuperAdminProtectionService, which keeps the system from locking itself out: you can't remove your own Super Admin role, and the system always keeps at least one user holding Super Admin. Try to drop the last one and you get a ConflictException.

Automatic assignment

Not every assignment is an admin clicking a button. UserRoleService.assignDefaultStaffRole(userId, campusHint?, isAdmin?) runs when a staff user is created. It attaches 'Staff' by default, or 'Super Admin' when isAdmin is set, and, neatly, if a campusHint resolves to a real campus it makes the assignment CAMPUS-scoped; otherwise SCHOOL:

apps/server/src/app/role/user-x-role.service.ts
const roleName = isAdmin ? 'Super Admin' : 'Staff';
const role = await this.roleRepository.findOne({ where: { name: roleName } });
// ...
let scopeLevel = ScopeLevel.SCHOOL;
let campusId: string | undefined;
if (campusHint) {
  const campus = await this.campusRepository.findByLooseName(campusHint);
  if (campus) { scopeLevel = ScopeLevel.CAMPUS; campusId = campus.id; }
}

This is the by-name lookup that makes system-role name immutability matter: if the school's 'Staff' role were renamed, this would log a warning and skip, leaving a new staff member with no role at all. (Whether a staff member becomes a Super Admin on login is decided by the allow-lists covered in Multi-Tenancy; this method is just the mechanism that writes the assignment.)

Conditions, not campuses

A natural question: "how do I give someone read students in campus X?" You don't attach a campus to a permission. You assign READ STUDENT to a role and grant that role at CAMPUS scope with campusId: X. The { campusId: X } condition is derived at request time, by the ability factory, from the assignment's scope. That's why role_x_permission doesn't store a campus: the scope machinery does it, and storing it twice would only let the two disagree.

There is one narrow exception, and it's deliberately fenced in. A role permission may carry explicit conditions (a RoleConditionTriple[] stored as jsonb) for a small, code-defined allow-list of filterable fields: currently just filtering STUDENT by department. Every condition is validated server-side by validateRoleConditions against FILTERABLE_FIELDS, and that allow-list intentionally excludes campus fields because campus is already handled by the assignment:

libs/shared/src/permission/role-conditions/filterable-fields.ts
// Do NOT list a resource's campus-scope field here (campus is scoped via the
// assignment; a stored filter on it would collide).
export const FILTERABLE_FIELDS: ReadonlyMap<Resource, readonly FieldSpec[]> = new Map([
  [Resource.STUDENT, [{ field: 'department', operators: ['eq', 'in'], valueType: 'string' }]],
]);

The takeaway: scope (campus) comes from the assignment; the rare structural filter (like department) is an explicit, allow-listed condition on the permission. Both keep the assignable surface small, pick actions × resources, plus a scope, while still producing rich, row-level access.

Assign to a campus

Now grant a role to a staff user. To restrict it to one campus, send scopeLevel: "campus" and a real campusId. Replace each YOUR_ value: YOUR_ACCESS_TOKEN is a token for a staff user who can manage roles, and the ids are the user, the role (here, the Librarian role), and the campus.

terminal
curl -X POST "https://api.example.com/api/v1/users/YOUR_USER_ID/roles" \
  -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "roleId": "YOUR_ROLE_ID",
    "scopeLevel": "campus",
    "campusId": "YOUR_CAMPUS_ID"
  }'

For a school-wide grant, drop campusId and send scopeLevel: "school". The service checks the user is STAFF, the role exists, and (for campus scope) the campus exists. A second identical assignment for the same (userId, roleId, campusId) returns 409. From here, the user's READ STUDENT applies only to students in that campus, and you never typed "campusId" into a permission.

Gotchas

A handful of things that trip people up, all grounded in the rules above.

System roles: editable permissions, frozen names

isSystem: true blocks renaming (ForbiddenException) and deleting the role, not editing its permissions. Other code (the SystemRoleGuard, automatic 'Staff' / 'Super Admin' assignment) matches roles by literal name, so a renamed system role silently breaks those lookups. Tune permissions freely; leave the name alone.

Roles are per-school: there is no global 'Staff'

Every tenant has its own cloned copy of each role, unique on (school_id, name). Edit "Head teacher" in one school and no other school is affected. Cloning is idempotent, so provisioning or re-seeding never duplicates or resets an existing role.

Scope goes on the assignment, never the permission

To limit access to a campus, set scopeLevel: "campus" + campusId on the assignment, POST /users/:id/roles. The permission itself stays generic. campusId is required when scopeLevel is campus (else 400) and must point at a real campus (else 404). Don't try to encode a campus into a role_x_permission row; the field isn't there, and the ability factory derives it from the assignment.

MANAGE ROLE is a near-admin grant

Holding MANAGE ROLE lets a staff member assign any role at any scope: there is no check that the assigner already holds the permissions they're granting, or that they're confined to their own campus. Treat it as a privileged grant, close to admin.

Where to go next

On this page