feat: authenticatie voor submissions en groups toegevoegd

This commit is contained in:
Adriaan Jacquet 2025-04-22 16:47:31 +02:00
parent 7c41c8e615
commit a4ccae6c0d
5 changed files with 45 additions and 17 deletions

View file

@ -18,8 +18,8 @@ export const onlyAllowIfHasAccessToGroup = authorize(
const clazz = await fetchClass(classId);
return clazz.teachers.map(mapToUsername).includes(auth.username);
} else { // user is student
const group = await fetchGroup(classId, assignmentId, groupId, false);
return clazz.students.map(mapToUsername).includes(auth.username);
const group = await fetchGroup(classId, assignmentId, groupId);
return group.members.map(mapToUsername).includes(auth.username);
}
}
);

View file

@ -9,11 +9,11 @@ import {fetchAnswer} from "../../../services/answers";
import {mapToUsername} from "../../../interfaces/user";
export const onlyAllowAuthor = authorize(
(auth: AuthenticationInfo, req: AuthenticatedRequest) => req.body.author === auth.username
(auth: AuthenticationInfo, req: AuthenticatedRequest) => (req.body as { author: string }).author === auth.username
);
export const onlyAllowAuthorRequest = authorize(
(auth: AuthenticationInfo, req: AuthenticatedRequest) => {
async (auth: AuthenticationInfo, req: AuthenticatedRequest) => {
const hruid = req.params.hruid;
const version = req.params.version;
const language = req.query.lang as string;
@ -30,7 +30,7 @@ export const onlyAllowAuthorRequest = authorize(
);
export const onlyAllowAuthorRequestAnswer = authorize(
(auth: AuthenticationInfo, req: AuthenticatedRequest) => {
async (auth: AuthenticationInfo, req: AuthenticatedRequest) => {
const hruid = req.params.hruid;
const version = req.params.version;
const language = req.query.lang as string;
@ -49,7 +49,7 @@ export const onlyAllowAuthorRequestAnswer = authorize(
);
export const onlyAllowIfHasAccessToQuestion = authorize(
async (auth, req) => {
async (auth: AuthenticationInfo, req: AuthenticatedRequest) => {
const hruid = req.params.hruid;
const version = req.params.version;
const language = req.query.lang as string;

View file

@ -0,0 +1,29 @@
import { languageMap } from "dwengo-1-common/util/language";
import { LearningObjectIdentifier } from "../../../entities/content/learning-object-identifier";
import { fetchSubmission } from "../../../services/submissions";
import { AuthenticatedRequest } from "../authenticated-request";
import { AuthenticationInfo } from "../authentication-info";
import { authorize } from "./auth-checks";
import { FALLBACK_LANG } from "../../../config";
import { mapToUsername } from "../../../interfaces/user";
export const onlyAllowSubmitter = authorize(
(auth: AuthenticationInfo, req: AuthenticatedRequest) => (req.body as { submitter: string }).submitter === auth.username
);
export const onlyAllowIfHasAccessToSubmission = authorize(
async (auth: AuthenticationInfo, req: AuthenticatedRequest) => {
const { hruid: lohruid, id: submissionNumber } = req.params;
const { language: lang, version: version } = req.query;
const loId = new LearningObjectIdentifier(lohruid, languageMap[lang as string] ?? FALLBACK_LANG, Number(version))
const submission = await fetchSubmission(loId, Number(submissionNumber));
if (auth.accountType === "teacher") {
// Dit kan niet werken om dat al deze objecten niet gepopulate zijn.
return submission.onBehalfOf.assignment.within.teachers.map(mapToUsername).includes(auth.username);
}
return submission.onBehalfOf.members.map(mapToUsername).includes(auth.username);
}
)