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
Show file tree
Hide file tree
Changes from all commits
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
49 changes: 49 additions & 0 deletions app/api/uploads/[...path]/route.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,54 @@
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 check for CVs
if (suffix.split("/")[0] !== "cvs") {
return "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 +72,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
4 changes: 2 additions & 2 deletions app/auth/login/page.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { auth, signIn } from "@/auth"
import Link from "@/components/Link"
import ThemedLogo from "@/components/ThemedLogo"

import styles from "./page.module.scss"

import { Button, Flex, Separator, Text } from "@radix-ui/themes"
import { Heading } from "@react-email/components"
import { AuthError } from "next-auth"
import Image from "next/image"
import { redirect } from "next/navigation"
import React from "react"

Expand Down Expand Up @@ -36,7 +36,7 @@ const LoginPage = async () => {
return (
<Flex gap="6" direction="column">
<Flex className={styles.logosContainer}>
<Image src="/images/imperial-logo-blue.svg" alt="imperial logo in blue" width={0} height={0} />
<ThemedLogo />
</Flex>
<Flex pl="9" pr="9" direction="column" gap="5">
<Flex direction="column" justify="center" align="center">
Expand Down
2 changes: 1 addition & 1 deletion app/auth/login/partner/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const LoginPage = () => {
</TextField.Root>

<Button size="3" style={{ width: "100%" }}>
{isPending ? <Spinner /> : "Sign In With Magic Link"}
{isPending ? <Spinner /> : "Sign in with magic link"}
</Button>
</form>
</Flex>
Expand Down
20 changes: 14 additions & 6 deletions app/students/[shortcode]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,20 @@ const StudentProfilePage = async ({ params }: { params: { shortcode: string } })
)}

{studentProfile.cv && (
<Flex align="center" gap="2" asChild>
<Link href={`/api/uploads/${studentProfile.cv}`} target="_blank" underline="none">
<BsFileEarmarkText title="download cv" color="black" />
<Text>{studentProfile.user.name?.split(",").reverse()[0].trim()}&apos;s CV</Text>
</Link>
</Flex>
<RestrictedArea
showMessage={false}
allowedRoles={["COMPANY", "STUDENT"]}
additionalCheck={async session =>
session.user.role === "COMPANY" || session.user.id === studentProfile.userId
}
>
<Flex align="center" gap="2" asChild>
<Link href={`/api/uploads/${studentProfile.cv}`} target="_blank" underline="none">
<BsFileEarmarkText title="download cv" color="var(--gray-12)" />
<Text>{studentProfile.user.name?.split(",").reverse()[0].trim()}&apos;s CV</Text>
</Link>
</Flex>
</RestrictedArea>
)}
<Flex align="center" gap="2">
<BsEnvelope />
Expand Down
19 changes: 19 additions & 0 deletions components/ThemedLogo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"use client"

import { useTheme } from "next-themes"
import Image from "next/image"

const ThemedLogo = () => {
const { resolvedTheme } = useTheme()
let src

if (resolvedTheme === "dark") {
src = "/images/imperial-logo.svg"
} else {
src = "/images/imperial-logo-blue.svg"
}

return <Image src={src} alt="imperial logo" width={0} height={0} />
}

export default ThemedLogo
Loading