From f8a567615c543ae0388e3ca2864870163ad66739 Mon Sep 17 00:00:00 2001 From: Joshua Oosthuizen Date: Tue, 27 Oct 2026 17:40:32 +0200 Subject: [PATCH] Campus-scoped staff: admins set limits; profile returns the scope PUT /api/users/:id takes scopedCampusIds (supervisors/staff only, audited as user_campus_scope_changed); GET returns scopedCampuses; the profile returns campusScope (null when unscoped or campuses are off). Co-Authored-By: Claude Opus 5.5 --- .../backend/src/controllers/userController.js | 30 ++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/apps/events/backend/src/controllers/userController.js b/apps/events/backend/src/controllers/userController.js index 570e474..5efae0f 100644 --- a/apps/events/backend/src/controllers/userController.js +++ b/apps/events/backend/src/controllers/userController.js @@ -548,7 +548,9 @@ const getUserProfile = async (req, res) => { // newsPromptPending: they haven't answered the news opt-in question yet (asked once). // noticePromptPending: a privacy notice version they must accept has been published. const noticePromptPending = !req.impersonation && await require('../utils/privacyNotice').noticePromptPending({ noticeVersionAccepted }); - res.json({ ...rest, twoFactorRequired: requiresTwoFactor(user), newsPromptPending: !newsAskedAt && !req.impersonation, noticePromptPending }); + // Campus-scoped staff: the campuses they're limited to (null = every campus). + const campusScope = (await require('../utils/campusScope').scopeFor(req))?.campusIds || null; + res.json({ ...rest, twoFactorRequired: requiresTwoFactor(user), newsPromptPending: !newsAskedAt && !req.impersonation, noticePromptPending, campusScope }); } else { res.status(404); throw new Error('User not found'); @@ -783,7 +785,8 @@ const getUserById = async (req, res) => { phoneNumber: true, createdAt: true, updatedAt: true, - isActive: true + isActive: true, + scopedCampuses: { select: { id: true, name: true } } } }); @@ -812,7 +815,7 @@ const updateUser = async (req, res) => { throw new Error('User not found'); } - const { name, email, role, isActive, phoneNumber, password, notificationPreference, twoFactorEnabled } = req.body; + const { name, email, role, isActive, phoneNumber, password, notificationPreference, twoFactorEnabled, scopedCampusIds } = req.body; const newPhone = phoneNumber !== undefined ? (phoneNumber || null) : user.phoneNumber; const { resolveNotificationPreference } = require('../utils/notificationPreference'); @@ -848,6 +851,21 @@ const updateUser = async (req, res) => { data.tokenVersion = { increment: 1 }; } + // Campus-scoped staff (utils/campusScope.js): "Limit to campuses" for supervisors and staff. + // Admins and users are never limited, so a role change away from staff clears the limit. + const before = await prisma.user.findUnique({ where: { id: user.id }, select: { scopedCampuses: { select: { id: true } } } }); + const beforeIds = before.scopedCampuses.map(c => c.id).sort(); + let afterIds = beforeIds; + if (!['supervisor', 'staff'].includes(data.role)) afterIds = []; + else if (Array.isArray(scopedCampusIds)) { + const wanted = [...new Set(scopedCampusIds.filter(x => typeof x === 'string' && x))]; + const found = await prisma.campus.findMany({ where: { id: { in: wanted } }, select: { id: true } }); + if (found.length !== wanted.length) { res.status(400); throw new Error('One of those campuses doesn\'t exist.'); } + afterIds = wanted.sort(); + } + const scopeChanged = afterIds.join(',') !== beforeIds.join(','); + if (scopeChanged) data.scopedCampuses = { set: afterIds.map(id => ({ id })) }; + const updatedUser = await prisma.user.update({ where: { id: req.params.id }, data, @@ -861,9 +879,13 @@ const updateUser = async (req, res) => { twoFactorEnabled: true, createdAt: true, updatedAt: true, - isActive: true + isActive: true, + scopedCampuses: { select: { id: true, name: true } } } }); + if (scopeChanged) { + require('../utils/adminAudit').logAdminAction({ actorId: req.user.id, actorRole: req.user.role, action: 'user_campus_scope_changed', targetType: 'User', targetId: updatedUser.id, metadata: { from: beforeIds, to: afterIds }, ip: getClientIp(req) }); + } if (resetTwoFactor || data.password) { await clearTrustedDevices(updatedUser.id);