diff --git a/src/Controllers/participant.controller.ts b/src/Controllers/participant.controller.ts index 2b7e75e..c0df55d 100644 --- a/src/Controllers/participant.controller.ts +++ b/src/Controllers/participant.controller.ts @@ -11,6 +11,7 @@ import { requireConfiguredAuthentication } from "../Middleware/auth/auth"; import { isTeamleaderJWTPayload, TeamleaderJWTPayload } from "../Middleware/auth/teamleaderAuth"; import { AuthJWTPayload } from "./admin_auth.controller"; import AuthError from "../Middleware/error/AuthError"; +import { getGroupsByTeamPid } from "./team.controller"; require("express-async-errors"); @@ -45,14 +46,16 @@ const returnedParticipant = { const _getAllParticipants = async ( res: Response, - authentication: TeamleaderJWTPayload | AuthJWTPayload, + req: Request, teamPid?: string ) => { - if (isTeamleaderJWTPayload(authentication)) { - teamPid = authentication.team; - } else { - if (authentication.permission_level !== "ELEVATED") { - throw new AuthError(); + if (req.teamleader?.isAuthenticated) { + await requireLeaderOfTeam(req.teamleader, teamPid); + } else if (req.auth?.permission_level == "STANDARD") { + if (!teamPid) { + 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) => { - const auth = req.auth || req.teamleader; - - if (!auth) { - throw new AuthError("No authentication provided"); - } - - return _getAllParticipants(res, auth, req.query.teamPid); + return _getAllParticipants(res, req, req.query.teamPid); }; export const getAllParticipantsParams = async (req: Request<{ teamPid: string }>, res: Response) => { - const auth = req.auth || req.teamleader; - - if (!auth) { - throw new AuthError("Not authentication provided"); - } - - return _getAllParticipants(res, auth, req.params.teamPid); + return _getAllParticipants(res, req, req.params.teamPid); }; 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({ where: { roles: { some: { pid: req.params.rolePid } } }, select: returnedParticipant, }); - if (!authenticated) { - requireLeaderOfTeam(req.teamleader, participant?.team.pid); - authenticated = true; - } - - if (!authenticated) { - throw new AuthError(); // REVIEW: Is this check neccesary? - } - if (!participant) { return res.status(404).json({ 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({ type: "success", payload: { participant }, diff --git a/src/Controllers/role.controller.ts b/src/Controllers/role.controller.ts index 7b4f671..7fe04fb 100644 --- a/src/Controllers/role.controller.ts +++ b/src/Controllers/role.controller.ts @@ -68,7 +68,7 @@ export async function getRolesForTeam(req: Request<{ pid: string }>, res: Respon if (req.teamleader?.isAuthenticated) { await requireLeaderOfTeam(req.teamleader, pid); - } else { + } else if (req.auth?.permission_level == "STANDARD") { 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) { 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) { 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({ type: "success", payload: { diff --git a/src/Controllers/team.controller.ts b/src/Controllers/team.controller.ts index 9d1f0f2..be0a66e 100644 --- a/src/Controllers/team.controller.ts +++ b/src/Controllers/team.controller.ts @@ -88,13 +88,6 @@ export const updateTeam = async (req: Request, res: Response) => { const body = result.data; - try { - requireLeaderOfTeam(req.teamleader, pid); - } catch { - // TODO: DO NOT CATCH THESE ERRORS - return res.status(401).json(createInsufficientPermissionsError("STANDARD")); - } - try { const team = await prisma.team.update({ where: { diff --git a/src/Routes/participant.routes.ts b/src/Routes/participant.routes.ts index 391e3e6..4ecab8e 100644 --- a/src/Routes/participant.routes.ts +++ b/src/Routes/participant.routes.ts @@ -16,10 +16,7 @@ require("express-async-errors"); const router = express.Router(); router.get( - "/", - requireConfiguredAuthentication({ type: { admin: true, teamleader: true }, optional: false }), - getAllParticipants -); + "/", requireConfiguredAuthentication({ optional: false, type: { admin: true, teamleader: true } }), getAllParticipants); teamRouter.get( "/:teamPid/particpants",