style: fix linting issues met Prettier
This commit is contained in:
parent
7f670030a7
commit
0c47546814
30 changed files with 233 additions and 262 deletions
|
@ -1,8 +1,8 @@
|
|||
import {authorize} from "./auth-checks";
|
||||
import {AuthenticationInfo} from "../authentication-info";
|
||||
import {AuthenticatedRequest} from "../authenticated-request";
|
||||
import {fetchClass} from "../../../services/classes";
|
||||
import {mapToUsername} from "../../../interfaces/user";
|
||||
import { authorize } from './auth-checks';
|
||||
import { AuthenticationInfo } from '../authentication-info';
|
||||
import { AuthenticatedRequest } from '../authenticated-request';
|
||||
import { fetchClass } from '../../../services/classes';
|
||||
import { mapToUsername } from '../../../interfaces/user';
|
||||
|
||||
async function teaches(teacherUsername: string, classId: string): Promise<boolean> {
|
||||
const clazz = await fetchClass(classId);
|
||||
|
@ -14,53 +14,45 @@ async function teaches(teacherUsername: string, classId: string): Promise<boolea
|
|||
* Only allows requests whose username parameter is equal to the username of the user who is logged in and requests
|
||||
* whose classId parameter references a class the logged-in user is a teacher of.
|
||||
*/
|
||||
export const onlyAllowStudentHimselfAndTeachersOfClass = authorize(
|
||||
async (auth: AuthenticationInfo, req: AuthenticatedRequest) => {
|
||||
if (req.params.username === auth.username) {
|
||||
return true;
|
||||
} else if (auth.accountType === "teacher") {
|
||||
return teaches(auth.username, req.params.classId);
|
||||
}
|
||||
return false;
|
||||
|
||||
export const onlyAllowStudentHimselfAndTeachersOfClass = authorize(async (auth: AuthenticationInfo, req: AuthenticatedRequest) => {
|
||||
if (req.params.username === auth.username) {
|
||||
return true;
|
||||
} else if (auth.accountType === 'teacher') {
|
||||
return teaches(auth.username, req.params.classId);
|
||||
}
|
||||
);
|
||||
return false;
|
||||
});
|
||||
|
||||
/**
|
||||
* Only let the request pass through if its path parameter "username" is the username of the currently logged-in
|
||||
* teacher and the path parameter "classId" refers to a class the teacher teaches.
|
||||
*/
|
||||
export const onlyAllowTeacherOfClass = authorize(
|
||||
async (auth: AuthenticationInfo, req: AuthenticatedRequest) =>
|
||||
req.params.username === auth.username && teaches(auth.username, req.params.classId),
|
||||
async (auth: AuthenticationInfo, req: AuthenticatedRequest) => req.params.username === auth.username && teaches(auth.username, req.params.classId)
|
||||
);
|
||||
|
||||
/**
|
||||
* Only let the request pass through if the class id in it refers to a class the current user is in (as a student
|
||||
* or teacher)
|
||||
*/
|
||||
export const onlyAllowIfInClass = authorize(
|
||||
async (auth: AuthenticationInfo, req: AuthenticatedRequest) => {
|
||||
const classId = req.params.classId ?? req.params.classid ?? req.params.id;
|
||||
const clazz = await fetchClass(classId);
|
||||
if (auth.accountType === "teacher") {
|
||||
return clazz.teachers.map(mapToUsername).includes(auth.username);
|
||||
}
|
||||
return clazz.students.map(mapToUsername).includes(auth.username);
|
||||
export const onlyAllowIfInClass = authorize(async (auth: AuthenticationInfo, req: AuthenticatedRequest) => {
|
||||
const classId = req.params.classId ?? req.params.classid ?? req.params.id;
|
||||
const clazz = await fetchClass(classId);
|
||||
if (auth.accountType === 'teacher') {
|
||||
return clazz.teachers.map(mapToUsername).includes(auth.username);
|
||||
}
|
||||
);
|
||||
return clazz.students.map(mapToUsername).includes(auth.username);
|
||||
});
|
||||
|
||||
/**
|
||||
* Only allows the request to pass if the 'class' property in its body is a class the current user is a member of.
|
||||
*/
|
||||
export const onlyAllowOwnClassInBody = authorize(
|
||||
async (auth, req) => {
|
||||
const classId = (req.body as {class: string})?.class;
|
||||
const clazz = await fetchClass(classId);
|
||||
export const onlyAllowOwnClassInBody = authorize(async (auth, req) => {
|
||||
const classId = (req.body as { class: string })?.class;
|
||||
const clazz = await fetchClass(classId);
|
||||
|
||||
if (auth.accountType === "teacher") {
|
||||
return clazz.teachers.map(mapToUsername).includes(auth.username);
|
||||
}
|
||||
return clazz.students.map(mapToUsername).includes(auth.username);
|
||||
if (auth.accountType === 'teacher') {
|
||||
return clazz.teachers.map(mapToUsername).includes(auth.username);
|
||||
}
|
||||
);
|
||||
return clazz.students.map(mapToUsername).includes(auth.username);
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue