Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: restrict cv #64

Merged
merged 4 commits into from
Aug 21, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions app/api/uploads/[...path]/route.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,49 @@
import { auth } from "@/auth"
import prisma from "@/lib/db"
import { validateFilePath } from "@/lib/files/saveFile"

import fs from "fs/promises"
import mime from "mime-types"
import { Session } from "next-auth"
import { NextRequest } from "next/server"
import path from "path"

/**
* @param session The user's session
* @param suffix The suffix of the file path
* @returns A response if the user is unauthorised to view the requested CV, or "authorised" if they are authorised
*/
const checkAuthorisedForCV = async (session: Session, suffix: string): Promise<Response | "authorised"> => {
// Only companies and admins can view any CV
if (session.user.role === "COMPANY" || session.user.role === "ADMIN") {
return "authorised"
}

// Students can only view their own CV
if (session.user.role === "STUDENT") {
const studentProfile = await prisma.studentProfile.findUnique({
select: {
cv: true,
},
where: {
userId: session.user.id,
},
})

if (!studentProfile) {
return new Response("Student profile not found", { status: 404 })
}

if (suffix !== "/" + studentProfile.cv) {
return new Response("Unauthorised to view this CV", { status: 403 })
}

return "authorised"
}

return new Response("Unexpected error", { status: 500 })
}

/**
* Upon request: return the file provided at the path in the volume
* @example GET /api/uploads/banner/myCompanyBanner.png
Expand All @@ -29,6 +67,12 @@ export const GET = async (req: NextRequest) => {
return new Response("Invalid path", { status: 400 })
}

const res = await checkAuthorisedForCV(session, suffix)
matalex412 marked this conversation as resolved.
Show resolved Hide resolved

if (res !== "authorised") {
return res
}

const filePath = path.join(process.env.UPLOAD_DIR, suffix)

try {
Expand Down
Loading