|
| 1 | +import { ForbiddenException, Injectable } from '@nestjs/common'; |
| 2 | +import type { UserRole } from '@opennota/shared'; |
| 3 | +import type { JwtPayload } from '../auth/jwt-payload'; |
| 4 | +import { PrismaService } from '../prisma/prisma.service'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Central authority for resource-level (ownership) permission checks. |
| 8 | + * |
| 9 | + * Coarse role gating lives in `@Roles(...)` metadata enforced by the |
| 10 | + * {@link RolesGuard}. This service answers the finer questions a role alone |
| 11 | + * cannot: *which* subjects a teacher may grade, *which* students' academic |
| 12 | + * data a user may read. Keeping that logic here means every module enforces |
| 13 | + * the same rules instead of re-deriving them. |
| 14 | + */ |
| 15 | +@Injectable() |
| 16 | +export class AccessControlService { |
| 17 | + constructor(private readonly prisma: PrismaService) {} |
| 18 | + |
| 19 | + /** Staff (ADMIN, PRINCIPAL) are unscoped: they may act on any resource. */ |
| 20 | + isStaff(role: UserRole): boolean { |
| 21 | + return role === 'ADMIN' || role === 'PRINCIPAL'; |
| 22 | + } |
| 23 | + |
| 24 | + /** TeacherProfile-subject ids the teacher (identified by user id) teaches. */ |
| 25 | + async teacherSubjectIds(userId: string): Promise<string[]> { |
| 26 | + const rows = await this.prisma.teacherSubject.findMany({ |
| 27 | + where: { teacher: { userId } }, |
| 28 | + select: { subjectId: true }, |
| 29 | + }); |
| 30 | + return rows.map((row) => row.subjectId); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Whether the user may manage (grade, evaluate, weight) the subject. Staff |
| 35 | + * always may; a teacher only for subjects they are assigned to. |
| 36 | + */ |
| 37 | + async canManageSubject(user: JwtPayload, subjectId: string): Promise<boolean> { |
| 38 | + if (this.isStaff(user.role)) { |
| 39 | + return true; |
| 40 | + } |
| 41 | + if (user.role !== 'TEACHER') { |
| 42 | + return false; |
| 43 | + } |
| 44 | + const assignment = await this.prisma.teacherSubject.findFirst({ |
| 45 | + where: { subjectId, teacher: { userId: user.sub } }, |
| 46 | + }); |
| 47 | + return assignment !== null; |
| 48 | + } |
| 49 | + |
| 50 | + /** Throws {@link ForbiddenException} unless {@link canManageSubject} holds. */ |
| 51 | + async assertCanManageSubject(user: JwtPayload, subjectId: string): Promise<void> { |
| 52 | + if (!(await this.canManageSubject(user, subjectId))) { |
| 53 | + throw new ForbiddenException('You are not assigned to this subject'); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Whether the user may read the given student's academic data: |
| 59 | + * - staff: any student; |
| 60 | + * - teacher: students enrolled in a class group they teach a subject in; |
| 61 | + * - student: only themselves; |
| 62 | + * - guardian: only their linked students. |
| 63 | + */ |
| 64 | + async canViewStudent(user: JwtPayload, studentId: string): Promise<boolean> { |
| 65 | + if (this.isStaff(user.role)) { |
| 66 | + return true; |
| 67 | + } |
| 68 | + if (user.role === 'STUDENT') { |
| 69 | + const profile = await this.prisma.studentProfile.findUnique({ |
| 70 | + where: { id: studentId }, |
| 71 | + select: { userId: true }, |
| 72 | + }); |
| 73 | + return profile?.userId === user.sub; |
| 74 | + } |
| 75 | + if (user.role === 'GUARDIAN') { |
| 76 | + const link = await this.prisma.studentGuardian.findFirst({ |
| 77 | + where: { studentId, guardian: { userId: user.sub } }, |
| 78 | + }); |
| 79 | + return link !== null; |
| 80 | + } |
| 81 | + if (user.role === 'TEACHER') { |
| 82 | + const enrollment = await this.prisma.enrollment.findFirst({ |
| 83 | + where: { |
| 84 | + studentId, |
| 85 | + isActive: true, |
| 86 | + classGroup: { |
| 87 | + subjects: { |
| 88 | + some: { |
| 89 | + deletedAt: null, |
| 90 | + teacherSubjects: { some: { teacher: { userId: user.sub } } }, |
| 91 | + }, |
| 92 | + }, |
| 93 | + }, |
| 94 | + }, |
| 95 | + }); |
| 96 | + return enrollment !== null; |
| 97 | + } |
| 98 | + return false; |
| 99 | + } |
| 100 | + |
| 101 | + /** Throws {@link ForbiddenException} unless {@link canViewStudent} holds. */ |
| 102 | + async assertCanViewStudent(user: JwtPayload, studentId: string): Promise<void> { |
| 103 | + if (!(await this.canViewStudent(user, studentId))) { |
| 104 | + throw new ForbiddenException('You do not have permission to view this student'); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * StudentProfile ids a teacher may read: every active enrollment in a class |
| 110 | + * group where the teacher teaches at least one subject. |
| 111 | + */ |
| 112 | + async teacherStudentIds(userId: string): Promise<string[]> { |
| 113 | + const enrollments = await this.prisma.enrollment.findMany({ |
| 114 | + where: { |
| 115 | + isActive: true, |
| 116 | + classGroup: { |
| 117 | + subjects: { |
| 118 | + some: { |
| 119 | + deletedAt: null, |
| 120 | + teacherSubjects: { some: { teacher: { userId } } }, |
| 121 | + }, |
| 122 | + }, |
| 123 | + }, |
| 124 | + }, |
| 125 | + select: { studentId: true }, |
| 126 | + }); |
| 127 | + return [...new Set(enrollments.map((enrollment) => enrollment.studentId))]; |
| 128 | + } |
| 129 | +} |
0 commit comments