fixed review suggestions

This commit is contained in:
Laurin
2022-06-09 16:23:54 +02:00
committed by La_Felx
parent 474b3f8185
commit 0fea7edb25
4 changed files with 26 additions and 50 deletions
+17 -37
View File
@@ -11,6 +11,7 @@ import { requireConfiguredAuthentication } from "../Middleware/auth/auth";
import { isTeamleaderJWTPayload, TeamleaderJWTPayload } from "../Middleware/auth/teamleaderAuth"; import { isTeamleaderJWTPayload, TeamleaderJWTPayload } from "../Middleware/auth/teamleaderAuth";
import { AuthJWTPayload } from "./admin_auth.controller"; import { AuthJWTPayload } from "./admin_auth.controller";
import AuthError from "../Middleware/error/AuthError"; import AuthError from "../Middleware/error/AuthError";
import { getGroupsByTeamPid } from "./team.controller";
require("express-async-errors"); require("express-async-errors");
@@ -45,14 +46,16 @@ const returnedParticipant = {
const _getAllParticipants = async ( const _getAllParticipants = async (
res: Response, res: Response,
authentication: TeamleaderJWTPayload | AuthJWTPayload, req: Request,
teamPid?: string teamPid?: string
) => { ) => {
if (isTeamleaderJWTPayload(authentication)) { if (req.teamleader?.isAuthenticated) {
teamPid = authentication.team; await requireLeaderOfTeam(req.teamleader, teamPid);
} else { } else if (req.auth?.permission_level == "STANDARD") {
if (authentication.permission_level !== "ELEVATED") { if (!teamPid) {
throw new AuthError(); throw new AuthError("A STANDARD Admin is not allowed to fetch all Participants!");
} else {
requireResponsibleForGroups(req.auth, await getGroupsByTeamPid(teamPid));
} }
} }
@@ -70,48 +73,19 @@ const _getAllParticipants = async (
}; };
export const getAllParticipants = async (req: Request<{}, {}, {}, { teamPid?: string }>, res: Response) => { export const getAllParticipants = async (req: Request<{}, {}, {}, { teamPid?: string }>, res: Response) => {
const auth = req.auth || req.teamleader; return _getAllParticipants(res, req, req.query.teamPid);
if (!auth) {
throw new AuthError("No authentication provided");
}
return _getAllParticipants(res, auth, req.query.teamPid);
}; };
export const getAllParticipantsParams = async (req: Request<{ teamPid: string }>, res: Response) => { export const getAllParticipantsParams = async (req: Request<{ teamPid: string }>, res: Response) => {
const auth = req.auth || req.teamleader; return _getAllParticipants(res, req, req.params.teamPid);
if (!auth) {
throw new AuthError("Not authentication provided");
}
return _getAllParticipants(res, auth, req.params.teamPid);
}; };
export const getParticipantForRole = async (req: Request<{ rolePid: string }>, res: Response) => { export const getParticipantForRole = async (req: Request<{ rolePid: string }>, res: Response) => {
let authenticated = false;
if (req.auth && req.auth.permission_level !== "ELEVATED") {
return res.status(403).json(createInsufficientPermissionsError());
} else if (req.auth) {
authenticated = true;
}
const participant = await prisma.participant.findFirst({ const participant = await prisma.participant.findFirst({
where: { roles: { some: { pid: req.params.rolePid } } }, where: { roles: { some: { pid: req.params.rolePid } } },
select: returnedParticipant, select: returnedParticipant,
}); });
if (!authenticated) {
requireLeaderOfTeam(req.teamleader, participant?.team.pid);
authenticated = true;
}
if (!authenticated) {
throw new AuthError(); // REVIEW: Is this check neccesary?
}
if (!participant) { if (!participant) {
return res.status(404).json({ return res.status(404).json({
type: "error", type: "error",
@@ -121,6 +95,12 @@ export const getParticipantForRole = async (req: Request<{ rolePid: string }>, r
}); });
} }
if (req.teamleader?.isAuthenticated) {
await requireLeaderOfTeam(req.teamleader, participant?.team.pid);
} else if (req.auth?.permission_level == "STANDARD") {
requireResponsibleForGroups(req.auth, await getGroupsByTeamPid(participant?.team.pid));
}
return res.status(200).json({ return res.status(200).json({
type: "success", type: "success",
payload: { participant }, payload: { participant },
+8 -2
View File
@@ -68,7 +68,7 @@ export async function getRolesForTeam(req: Request<{ pid: string }>, res: Respon
if (req.teamleader?.isAuthenticated) { if (req.teamleader?.isAuthenticated) {
await requireLeaderOfTeam(req.teamleader, pid); await requireLeaderOfTeam(req.teamleader, pid);
} else { } else if (req.auth?.permission_level == "STANDARD") {
requireResponsibleForGroups(req.auth, await getGroupsByTeamPid(pid)); requireResponsibleForGroups(req.auth, await getGroupsByTeamPid(pid));
} }
@@ -88,12 +88,18 @@ export async function getRolesForTeam(req: Request<{ pid: string }>, res: Respon
export async function getRole(req: Request<{ rolePid: string }>, res: Response) { export async function getRole(req: Request<{ rolePid: string }>, res: Response) {
const rolePid = req.params.rolePid; const rolePid = req.params.rolePid;
const role = await prisma.role.findUnique({ where: { pid: rolePid }, select: basicRole }); const role = await prisma.role.findUnique({ where: { pid: rolePid }, select: detailedRole });
if (!role) { if (!role) {
throw new NotFoundError("role", rolePid); throw new NotFoundError("role", rolePid);
} }
if (req.teamleader?.isAuthenticated) {
await requireLeaderOfTeam(req.teamleader, role.team.pid);
} else if (req.auth?.permission_level == "STANDARD" && role.participant?.pid !== undefined) {
requireResponsibleForGroups(req.auth, await getGroupByParticipantPid(role.participant?.pid));
}
return res.status(200).json({ return res.status(200).json({
type: "success", type: "success",
payload: { payload: {
-7
View File
@@ -88,13 +88,6 @@ export const updateTeam = async (req: Request, res: Response) => {
const body = result.data; const body = result.data;
try {
requireLeaderOfTeam(req.teamleader, pid);
} catch {
// TODO: DO NOT CATCH THESE ERRORS
return res.status(401).json(createInsufficientPermissionsError("STANDARD"));
}
try { try {
const team = await prisma.team.update({ const team = await prisma.team.update({
where: { where: {
+1 -4
View File
@@ -16,10 +16,7 @@ require("express-async-errors");
const router = express.Router(); const router = express.Router();
router.get( router.get(
"/", "/", requireConfiguredAuthentication({ optional: false, type: { admin: true, teamleader: true } }), getAllParticipants);
requireConfiguredAuthentication({ type: { admin: true, teamleader: true }, optional: false }),
getAllParticipants
);
teamRouter.get( teamRouter.get(
"/:teamPid/particpants", "/:teamPid/particpants",