|
|
@ -7,6 +7,8 @@ import express, { Request, Response, Router } from 'express'
|
|
|
|
|
|
|
|
|
|
|
|
import { createStatusRouter } from './middlewares'
|
|
|
|
import { createStatusRouter } from './middlewares'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import { StatusCodes } from 'http-status-codes'
|
|
|
|
|
|
|
|
|
|
|
|
import {
|
|
|
|
import {
|
|
|
|
createDatabase,
|
|
|
|
createDatabase,
|
|
|
|
createProblem,
|
|
|
|
createProblem,
|
|
|
@ -17,24 +19,37 @@ import {
|
|
|
|
getSolutions,
|
|
|
|
getSolutions,
|
|
|
|
getUser,
|
|
|
|
getUser,
|
|
|
|
getUsers,
|
|
|
|
getUsers,
|
|
|
|
|
|
|
|
getVisibleSolutions,
|
|
|
|
updateSolution,
|
|
|
|
updateSolution,
|
|
|
|
} from './db/database'
|
|
|
|
} from './db/database'
|
|
|
|
|
|
|
|
|
|
|
|
import { isAdministrator, isStudent, Problem, ProblemId, UserId } from '../shared/model'
|
|
|
|
import {
|
|
|
|
|
|
|
|
Id,
|
|
|
|
|
|
|
|
isAdministrator,
|
|
|
|
|
|
|
|
isStudent,
|
|
|
|
|
|
|
|
Opaque,
|
|
|
|
|
|
|
|
Problem,
|
|
|
|
|
|
|
|
ProblemId,
|
|
|
|
|
|
|
|
Solution as SolutionModel,
|
|
|
|
|
|
|
|
SolutionId,
|
|
|
|
|
|
|
|
UserId,
|
|
|
|
|
|
|
|
} from '../shared/model'
|
|
|
|
import { initialDatabaseValue } from './db/example-data'
|
|
|
|
import { initialDatabaseValue } from './db/example-data'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import { validateObjectKeys } from '../shared/utils'
|
|
|
|
|
|
|
|
|
|
|
|
export async function createApiRouter() {
|
|
|
|
export async function createApiRouter() {
|
|
|
|
type SessionId = string
|
|
|
|
type SessionId = Opaque<string, string, 'session'>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const sessionStore: Record<SessionId, UserId> = {}
|
|
|
|
const sessions = {
|
|
|
|
const sessions = {
|
|
|
|
store: {},
|
|
|
|
|
|
|
|
createSession(userId: UserId) {
|
|
|
|
createSession(userId: UserId) {
|
|
|
|
const sid = crypto.randomBytes(10).toString('hex')
|
|
|
|
const sid = crypto.randomBytes(10).toString('hex') as SessionId
|
|
|
|
this.store[sid] = userId
|
|
|
|
sessionStore[sid] = userId
|
|
|
|
return sid
|
|
|
|
return sid
|
|
|
|
},
|
|
|
|
},
|
|
|
|
getUserForSession(sid: SessionId) {
|
|
|
|
getUserForSession(sid: SessionId) {
|
|
|
|
return this.store[sid] ?? null
|
|
|
|
return sessionStore[sid] ?? null
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -68,7 +83,7 @@ export async function createApiRouter() {
|
|
|
|
|
|
|
|
|
|
|
|
const user = await getUser(db, id)
|
|
|
|
const user = await getUser(db, id)
|
|
|
|
if (!user) {
|
|
|
|
if (!user) {
|
|
|
|
res.sendStatus(403)
|
|
|
|
res.sendStatus(StatusCodes.FORBIDDEN)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -83,8 +98,8 @@ export async function createApiRouter() {
|
|
|
|
|
|
|
|
|
|
|
|
r.get('/api/users', async (req, res) => {
|
|
|
|
r.get('/api/users', async (req, res) => {
|
|
|
|
const requestUser = await getRequestUser(req)
|
|
|
|
const requestUser = await getRequestUser(req)
|
|
|
|
if (requestUser.role !== 'admin' && requestUser.role !== 'moderator') {
|
|
|
|
if (!requestUser || !isAdministrator(requestUser.role)) {
|
|
|
|
res.sendStatus(401)
|
|
|
|
res.sendStatus(StatusCodes.UNAUTHORIZED)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -119,11 +134,11 @@ export async function createApiRouter() {
|
|
|
|
r.post('/api/problem', async (req, res) => {
|
|
|
|
r.post('/api/problem', async (req, res) => {
|
|
|
|
const user = await getRequestUser(req)
|
|
|
|
const user = await getRequestUser(req)
|
|
|
|
if (!user) {
|
|
|
|
if (!user) {
|
|
|
|
res.sendStatus(401)
|
|
|
|
res.sendStatus(StatusCodes.UNAUTHORIZED)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (user.role !== 'admin' && user.role !== 'moderator') {
|
|
|
|
if (user.role !== 'admin' && user.role !== 'moderator') {
|
|
|
|
res.sendStatus(401)
|
|
|
|
res.sendStatus(StatusCodes.UNAUTHORIZED)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -135,86 +150,107 @@ export async function createApiRouter() {
|
|
|
|
res.json(id)
|
|
|
|
res.json(id)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
r.get('/api/solution/:id', async (req, res) => {
|
|
|
|
r.get('/api/solutions', async (req, res) => {
|
|
|
|
const user = await getRequestUser(req)
|
|
|
|
let queryUser = (req.query.user ?? null) as UserId | null
|
|
|
|
|
|
|
|
let queryProblem = (req.query.problem ?? null) as ProblemId | null
|
|
|
|
// l'utente deve essere loggato
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
|
|
|
|
res.sendStatus(401)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const solution = await getSolution(db, req.params.id)
|
|
|
|
const requestUser = await getRequestUser(req)
|
|
|
|
|
|
|
|
|
|
|
|
// uno studente che prova a ottenere la soluzione di un altro utente
|
|
|
|
let solutions = await getSolutions(db)
|
|
|
|
if (!isAdministrator(user.role) && solution.sentBy !== user.id) {
|
|
|
|
// se l'utente non è loggato o se non è un amministratore allora mostra solo le soluzioni "visibili"
|
|
|
|
res.sendStatus(401)
|
|
|
|
if (!requestUser || !isAdministrator(requestUser.role)) {
|
|
|
|
return
|
|
|
|
solutions = solutions.filter(
|
|
|
|
|
|
|
|
s => s.visible || (requestUser && s.sentBy === requestUser.id)
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// filtra rispetto agli utenti
|
|
|
|
|
|
|
|
if (queryUser !== null) {
|
|
|
|
|
|
|
|
solutions = solutions.filter(s => s.sentBy === queryUser)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// filtra rispetto ai problemi
|
|
|
|
|
|
|
|
if (queryProblem !== null) {
|
|
|
|
|
|
|
|
solutions = solutions.filter(s => s.forProblem === queryProblem)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
res.json(solution)
|
|
|
|
res.json(solutions)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
r.get('/api/solutions', async (req, res) => {
|
|
|
|
r.get('/api/solution/:id', async (req, res) => {
|
|
|
|
let queryUserId = req.query.user as string
|
|
|
|
const user = await getRequestUser(req)
|
|
|
|
let queryProblemId = req.query.problem as string
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const requestUser = await getRequestUser(req)
|
|
|
|
const solution = await getSolution(db, req.params.id as SolutionId)
|
|
|
|
if (!requestUser) {
|
|
|
|
// la soluzione deve esistere
|
|
|
|
res.sendStatus(401)
|
|
|
|
if (solution === null) {
|
|
|
|
|
|
|
|
res.sendStatus(StatusCodes.NOT_FOUND)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// uno studente può vedere solo soluzioni visibili o proprie soluzione
|
|
|
|
// if current user is not an administrator then force the user query to current user
|
|
|
|
if (!solution.visible && user && isStudent(user.role) && solution.sentBy !== user.id) {
|
|
|
|
if (!isAdministrator(requestUser.role)) {
|
|
|
|
res.sendStatus(StatusCodes.UNAUTHORIZED)
|
|
|
|
queryUserId = requestUser.id
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
res.json(
|
|
|
|
res.json(solution)
|
|
|
|
await getSolutions(db, {
|
|
|
|
|
|
|
|
sentBy: queryUserId as UserId,
|
|
|
|
|
|
|
|
forProblem: queryProblemId as ProblemId,
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
r.post('/api/solution', async (req, res) => {
|
|
|
|
r.post('/api/solution', async (req, res) => {
|
|
|
|
const user = await getRequestUser(req)
|
|
|
|
const user = await getRequestUser(req)
|
|
|
|
if (!user) {
|
|
|
|
if (!user) {
|
|
|
|
res.sendStatus(401)
|
|
|
|
res.sendStatus(StatusCodes.UNAUTHORIZED)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await createSolution(db, {
|
|
|
|
await createSolution(db, {
|
|
|
|
sentBy: user.id,
|
|
|
|
sentBy: user.id,
|
|
|
|
forProblem: req.body.problemId,
|
|
|
|
forProblem: req.body.forProblem,
|
|
|
|
content: req.body.content,
|
|
|
|
content: req.body.content,
|
|
|
|
status: 'pending',
|
|
|
|
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
res.send({ status: 'ok' })
|
|
|
|
res.send({ status: 'ok' })
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
r.post('/api/solution/:id', async (req, res) => {
|
|
|
|
r.patch('/api/solution/:id', async (req, res) => {
|
|
|
|
const user = await getRequestUser(req)
|
|
|
|
const id = req.params.id as SolutionId
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const user = await getRequestUser(req)
|
|
|
|
// l'utente deve essere loggato
|
|
|
|
// l'utente deve essere loggato
|
|
|
|
if (!user) {
|
|
|
|
if (!user) {
|
|
|
|
res.sendStatus(401)
|
|
|
|
res.sendStatus(StatusCodes.UNAUTHORIZED)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const solutionId = req.params.id
|
|
|
|
const solution = await getSolution(db, id)
|
|
|
|
const solution = await getSolution(db, solutionId)
|
|
|
|
// la soluzione deve esistere
|
|
|
|
|
|
|
|
if (solution === null) {
|
|
|
|
|
|
|
|
res.sendStatus(404)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
// uno studente non può modificare una soluzione di un altro utente
|
|
|
|
// uno studente non può modificare una soluzione di un altro utente
|
|
|
|
if (isStudent(user.role) && solution.sentBy !== user.id) {
|
|
|
|
if (user.role === 'student' && solution.sentBy !== user.id) {
|
|
|
|
res.sendStatus(401)
|
|
|
|
res.status(StatusCodes.UNAUTHORIZED)
|
|
|
|
|
|
|
|
res.send(`a student can only modify its own solution`)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// uno studente può modificare solo il campo "content"
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
|
|
|
user.role === 'student' &&
|
|
|
|
|
|
|
|
!validateObjectKeys<keyof SolutionModel>(req.body, ['content'])
|
|
|
|
|
|
|
|
) {
|
|
|
|
|
|
|
|
res.status(StatusCodes.UNAUTHORIZED)
|
|
|
|
|
|
|
|
res.send(`a student can only modify the field "content"`)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// un moderatore può modificare solo i campi "content", "visible", "status"
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
|
|
|
user.role === 'moderator' &&
|
|
|
|
|
|
|
|
!validateObjectKeys<keyof SolutionModel>(req.body, ['content', 'status', 'visible'])
|
|
|
|
|
|
|
|
) {
|
|
|
|
|
|
|
|
res.status(StatusCodes.UNAUTHORIZED)
|
|
|
|
|
|
|
|
res.send(`a moderator can only modify the fields "content", "visible", "status"`)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// modifico la soluzione con il json mandato dal client nel body della richiesta
|
|
|
|
await updateSolution(db, id, req.body)
|
|
|
|
await updateSolution(db, solutionId, req.body)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
res.json({ status: 'ok' })
|
|
|
|
res.json({ status: 'ok' })
|
|
|
|
})
|
|
|
|
})
|
|
|
@ -224,13 +260,13 @@ export async function createApiRouter() {
|
|
|
|
|
|
|
|
|
|
|
|
// intanto l'utente deve essere loggato
|
|
|
|
// intanto l'utente deve essere loggato
|
|
|
|
if (!user) {
|
|
|
|
if (!user) {
|
|
|
|
res.sendStatus(401)
|
|
|
|
res.sendStatus(StatusCodes.UNAUTHORIZED)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// solo gli amministratori possono usare questa route
|
|
|
|
// solo gli amministratori possono usare questa route
|
|
|
|
if (!isAdministrator(user.role)) {
|
|
|
|
if (!isAdministrator(user.role)) {
|
|
|
|
res.sendStatus(401)
|
|
|
|
res.sendStatus(StatusCodes.UNAUTHORIZED)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|