Skip to content
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
9 changes: 8 additions & 1 deletion backend/controller/advanceController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import ReportUsage from '../models/reportUsage.js'
import Travel from '../models/travel.js'
import { createBookingExportPackage, getBookingExportPreview } from './bookingExport.js'
import { Controller, checkOwner, GetterQuery } from './controller.js'
import { AuthorizationError, NotFoundError } from './error.js'
import { AuthorizationError, NotAllowedError, NotFoundError } from './error.js'
import { bulkSaveImport, resolveImportReferences, validateImportValues } from './reportImport.js'
import { AdvanceBulkImportPost, AuthenticatedExpressRequest, MoneyPost } from './types.js'

Expand Down Expand Up @@ -194,6 +194,9 @@ export class AdvanceApproveController extends Controller {
@Post('bulk')
public async postManyApproved(@Body() requestBody: AdvanceBulkImportPost[], @Request() request: AuthenticatedExpressRequest) {
const resolvedReferences = await resolveImportReferences(requestBody, false)
if (resolvedReferences.some(({ project }) => !checkIfUserIsProjectSupervisor(request.user, project))) {
throw new NotAllowedError('Not allowed to approve an advance for this project')
}
const currencies = await Currency.find({ _id: { $in: requestBody.map(({ budget }) => budget?.currency) } }, { _id: 1 }).lean()
validateImportValues(
requestBody.map(({ budget }) => budget?.currency),
Expand Down Expand Up @@ -262,6 +265,10 @@ export class AdvanceApproveController extends Controller {
| { _id: string; comment?: string; bookingRemark?: string | null; exchangeRateDate?: Date | string | null },
@Request() request: AuthenticatedExpressRequest
) {
const project = 'project' in requestBody ? requestBody.project : undefined
if (project && !checkIfUserIsProjectSupervisor(request.user, idDocumentToId(project))) {
throw new NotAllowedError('Not allowed to approve an advance for this project')
}
const extendedBody = Object.assign(requestBody, { state: AdvanceState.APPROVED, editor: request.user._id })
if (!extendedBody._id) {
if (!(extendedBody as AdvanceApplication).name) {
Expand Down
9 changes: 9 additions & 0 deletions backend/controller/expenseReportController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,9 @@ export class ExpenseReportExamineController extends Controller {
true
)
const resolvedReferences = await resolveImportReferences(normalizedRows)
if (resolvedReferences.some(({ project }) => !checkIfUserIsProjectSupervisor(request.user, project))) {
throw new NotAllowedError('Not allowed to create an expense report for this project')
}
const documents = normalizedRows.map((row, index) => {
let name = row.name
if (!name) {
Expand Down Expand Up @@ -424,6 +427,9 @@ export class ExpenseReportExamineController extends Controller {

@Post()
public async postAny(@Body() requestBody: ExpenseReportDetailsBody & { _id: string }, @Request() request: AuthenticatedExpressRequest) {
if (requestBody.project && !checkIfUserIsProjectSupervisor(request.user, idDocumentToId(requestBody.project))) {
throw new NotAllowedError('Not allowed to modify an expense report for this project')
}
const extendedBody = Object.assign(requestBody, { editor: request.user._id })

return await this.setter(ExpenseReport, {
Expand All @@ -449,6 +455,9 @@ export class ExpenseReportExamineController extends Controller {
requestBody: ExpenseReportDetailsBody & { owner?: IdDocument<Types.ObjectId>; comment?: string },
@Request() request: AuthenticatedExpressRequest
) {
if (requestBody.project && !checkIfUserIsProjectSupervisor(request.user, idDocumentToId(requestBody.project))) {
throw new NotAllowedError('Not allowed to save an expense report for this project')
}
const extendedBody = Object.assign(requestBody, { state: ExpenseReportState.IN_WORK, editor: request.user._id })
if (!extendedBody._id) {
if (!extendedBody.name) {
Expand Down
8 changes: 7 additions & 1 deletion backend/controller/healthCareCostController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import Organisation from '../models/organisation.js'
import User from '../models/user.js'
import { createBookingExportPackage, getBookingExportPreview } from './bookingExport.js'
import { assertAdvanceOwnership, Controller, checkOwner, GetterQuery, SetterBody } from './controller.js'
import { AuthorizationError, NotFoundError, ValidationClientError } from './error.js'
import { AuthorizationError, NotAllowedError, NotFoundError, ValidationClientError } from './error.js'
import { AuthenticatedExpressRequest } from './types.js'

const healthCareCostValidator = new Validator({ requireReceipts: true })
Expand Down Expand Up @@ -302,6 +302,9 @@ export class HealthCareCostExamineController extends Controller {
},
@Request() request: AuthenticatedExpressRequest
) {
if (requestBody.project && !checkIfUserIsProjectSupervisor(request.user, idDocumentToId(requestBody.project))) {
throw new NotAllowedError('Not allowed to modify health care costs for this project')
}
const extendedBody = Object.assign(requestBody, { editor: request.user._id })

return await this.setter(HealthCareCost, {
Expand Down Expand Up @@ -336,6 +339,9 @@ export class HealthCareCostExamineController extends Controller {
},
@Request() request: AuthenticatedExpressRequest
) {
if (requestBody.project && !checkIfUserIsProjectSupervisor(request.user, idDocumentToId(requestBody.project))) {
throw new NotAllowedError('Not allowed to save health care costs for this project')
}
const extendedBody = Object.assign(requestBody, { state: HealthCareCostState.IN_WORK, editor: request.user._id })
if (!extendedBody._id) {
if (!extendedBody.name) {
Expand Down
10 changes: 10 additions & 0 deletions backend/controller/travelController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,9 @@ export class TravelApproveController extends Controller {
@Post('bulk')
public async postManyApproved(@Body() requestBody: TravelBulkImportPost[], @Request() request: AuthenticatedExpressRequest) {
const resolvedReferences = await resolveImportReferences(requestBody)
if (resolvedReferences.some(({ project }) => !checkIfUserIsProjectSupervisor(request.user, project))) {
throw new NotAllowedError('Not allowed to approve a travel for this project')
}
const countries = await Country.find(
{ _id: { $in: requestBody.map(({ destinationPlace }) => destinationPlace?.country) } },
{ _id: 1, needsA1Certificate: 1 }
Expand Down Expand Up @@ -504,6 +507,10 @@ export class TravelApproveController extends Controller {
@Body() requestBody: ((TravelApplication & { owner: IdDocument }) | { _id: string }) & { comment?: string },
@Request() request: AuthenticatedExpressRequest
) {
const project = 'project' in requestBody ? requestBody.project : undefined
if (project && !checkIfUserIsProjectSupervisor(request.user, idDocumentToId(project))) {
throw new NotAllowedError('Not allowed to approve a travel for this project')
}
const extendedBody = Object.assign(requestBody, { state: TravelState.APPROVED, editor: request.user._id })
if (!extendedBody._id) {
const travelApplication = extendedBody as TravelApplication & { owner: IdDocument }
Expand Down Expand Up @@ -591,6 +598,9 @@ export class TravelExamineController extends Controller {

@Post()
public async postAny(@Body() requestBody: SetterBody<TravelPost>, @Request() request: AuthenticatedExpressRequest) {
if (requestBody.project && !checkIfUserIsProjectSupervisor(request.user, idDocumentToId(requestBody.project))) {
throw new NotAllowedError('Not allowed to modify a travel for this project')
}
const extendedBody = Object.assign(requestBody, { editor: request.user._id })

return await this.setter(Travel, {
Expand Down
216 changes: 216 additions & 0 deletions backend/tests/api/projectAuthorization.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
import { randomUUID } from 'node:crypto'
import { AdvanceState, ProjectSimple, State, TravelState, User } from 'abrechnung-common/types.js'
import { Base64 } from 'abrechnung-common/utils/encoding.js'
import test, { type ExecutionContext } from 'ava'
import mongoose, { Types } from 'mongoose'
import type { Response } from 'supertest'
import { objectToFormFields } from '../../helper.js'
import createAgent, { loginUser, shutdown } from '../_agent.js'

const agent = await createAgent()
const reader = await createAgent()
const ownerReader = await createAgent()
const reportTypes = ['advance', 'travel', 'expenseReport', 'healthCareCost'] as const
type ReportType = (typeof reportTypes)[number]
const collections = { advance: 'advances', travel: 'travels', expenseReport: 'expensereports', healthCareCost: 'healthcarecosts' }
let owner: User<string>
let supervisedProject: ProjectSimple<string>
let otherProject: ProjectSimple<string>
let insuranceId: string
let categoryId: string

function creationPath(type: ReportType) {
return type === 'advance' || type === 'travel' ? `/approve/${type}/approved` : `/examine/${type}/inWork`
}

function reportBody(type: ReportType, project: string) {
const body = { name: `Project authorization ${randomUUID()}`, owner: owner._id, project, advances: [] }
if (type === 'advance') return { ...body, reason: 'Conference costs', budget: { amount: 250, currency: 'EUR' } }
if (type === 'travel') {
return {
...body,
reason: 'Conference',
destinationPlace: { country: 'DE', place: 'Berlin' },
startDate: '2026-07-01',
endDate: '2026-07-01'
}
}
if (type === 'healthCareCost') return { ...body, patientName: 'Fry', insurance: insuranceId }
return body
}

async function restrictReviewer(type: ReportType) {
await loginUser(agent, type)
const reviewer = (await agent.get('/user').expect(200)).body.data as User<string>
await reader
.post('/admin/user')
.send({ _id: reviewer._id, projects: { ...reviewer.projects, supervised: [supervisedProject._id] } })
.expect(200)
await loginUser(agent, type)
}

async function readReport(type: ReportType, id: string) {
return (await reader.get(`/admin/${type}`).query({ _id: id }).expect(200)).body.data
}

async function readProjects() {
return await Promise.all(
[supervisedProject, otherProject].map(async ({ _id }) => (await reader.get('/admin/project').query({ _id }).expect(200)).body.data)
)
}

async function savedState(type: ReportType, id: string) {
// History is persisted but hidden by the read APIs.
const stored = await mongoose.connection
.collection(collections[type])
.findOne({ _id: new Types.ObjectId(id) }, { projection: { history: 1 } })
if (!stored) throw new Error('Missing report fixture')
return { report: await readReport(type, id), history: stored.history, projects: await readProjects() }
}

function assertForbidden(t: ExecutionContext, response: Response) {
t.is(response.status, 403)
t.is(response.body.name, 'alerts.notAllowed')
}

async function assertAbsent(t: ExecutionContext, type: ReportType, names: string[]) {
const stored = await ownerReader
.get(`/${type}`)
.query({ filterJSON: Base64.encode(JSON.stringify({ name: { $in: names } })) })
.expect(200)
t.deepEqual(stored.body.data, [])
}

async function createReview(type: 'expenseReport' | 'healthCareCost') {
const created = await agent.post(creationPath(type)).send(reportBody(type, supervisedProject._id)).expect(200)
const id = created.body.result._id as string
const expense = {
description: 'Train ticket',
cost: {
date: '2026-07-01',
currency: 'EUR',
receipts: [{ name: 'Receipt.pdf', type: 'application/pdf', data: 'tests/files/dummy.pdf' }],
positions: [
{ kind: 'manual', description: 'Train ticket', grossAmount: 100, vatRate: 0, project: supervisedProject._id, category: categoryId }
]
}
}
let request = ownerReader.post(`/${type}/expense`).query({ parentId: id })
for (const entry of objectToFormFields(expense)) {
request = entry.field.endsWith('[data]') ? request.attach(entry.field, entry.val) : request.field(entry.field, entry.val)
}
await request.expect(200)
await ownerReader.post(`/${type}/underExamination`).send({ _id: id }).expect(200)
return id
}

test.before(async () => {
await loginUser(reader, 'admin')
await loginUser(ownerReader, 'user')
owner = (await ownerReader.get('/user').expect(200)).body.data
insuranceId = (await ownerReader.get('/healthInsurance').expect(200)).body.data[0]._id
const categories = (await ownerReader.get('/category').expect(200)).body.data
categoryId = categories.find(({ for: value }: { for: string }) => value === 'ExpenseReport' || value === 'both')._id
const organisation = (await reader.get('/admin/organisation').expect(200)).body.data[0]
const projects: ProjectSimple<string>[] = []
for (const name of ['Supervised project', 'Other project']) {
const response = await reader
.post('/admin/project')
.send({ identifier: `AUTH-${randomUUID()}`, name, organisation: organisation._id, balance: { amount: 0 } })
.expect(200)
projects.push(response.body.result)
}
;[supervisedProject, otherProject] = projects
})

for (const type of reportTypes) {
test.serial(`${type}: reviewer creation requires authority over the target project`, async (t) => {
await restrictReviewer(type)
const body = reportBody(type, otherProject._id)
const before = await readProjects()
assertForbidden(t, await agent.post(creationPath(type)).send(body))
await assertAbsent(t, type, [body.name])
t.deepEqual(await readProjects(), before)
const accepted = await agent
.post(creationPath(type))
.send({ ...body, project: supervisedProject._id })
.expect(200)
const saved = await readReport(type, accepted.body.result._id)
t.is(saved.project._id, supervisedProject._id)
t.is(saved.owner._id, owner._id)
t.is(saved.state, type === 'advance' ? AdvanceState.APPROVED : State.EDITABLE_BY_OWNER)
})
}

for (const type of ['advance', 'travel'] as const) {
test.serial(`${type}: approval cannot reassign an application to an unauthorized project`, async (t) => {
await restrictReviewer(type)
const body = reportBody(type, supervisedProject._id)
const created = await ownerReader.post(`/${type}/appliedFor`).send(body).expect(200)
const id = created.body.result._id as string
const before = await savedState(type, id)
assertForbidden(t, await agent.post(creationPath(type)).send({ ...body, _id: id, project: { _id: otherProject._id } }))
t.deepEqual(await savedState(type, id), before)
await agent
.post(creationPath(type))
.send({ ...body, _id: id, project: { _id: supervisedProject._id } })
.expect(200)
const saved = await readReport(type, id)
t.is(saved.project._id, supervisedProject._id)
t.is(saved.state, type === 'advance' ? AdvanceState.APPROVED : TravelState.APPROVED)
})
}

for (const type of ['travel', 'expenseReport', 'healthCareCost'] as const) {
for (const action of type === 'travel' ? ['edit'] : ['edit', 'return']) {
test.serial(`${type}: examiner ${action} checks the target project before changing data or history`, async (t) => {
await restrictReviewer(type)
const id =
type === 'travel'
? (await agent.post(creationPath(type)).send(reportBody(type, supervisedProject._id)).expect(200)).body.result._id
: await createReview(type)
const endpoint = `/examine/${type}${action === 'return' ? '/inWork' : ''}`
const before = await savedState(type, id)
assertForbidden(t, await agent.post(endpoint).send({ _id: id, project: { _id: otherProject._id } }))
t.deepEqual(await savedState(type, id), before)
await agent
.post(endpoint)
.send({ _id: id, project: { _id: supervisedProject._id }, name: 'Permitted change' })
.expect(200)
const saved = await readReport(type, id)
t.is(saved.project._id, supervisedProject._id)
t.is(saved.name, 'Permitted change')
t.is(saved.state, action === 'return' || type === 'travel' ? State.EDITABLE_BY_OWNER : State.IN_REVIEW)
})
}
}

for (const type of ['advance', 'travel', 'expenseReport'] as const) {
test.serial(`${type}: bulk import rejects an unauthorized mixed batch without saving any rows`, async (t) => {
await restrictReviewer(type)
const endpoint = `/${type === 'expenseReport' ? 'examine' : 'approve'}/${type}/bulk`
const rows = [supervisedProject, otherProject].map((project) => ({
...reportBody(type, project._id),
project: project.identifier,
owner: owner.email
}))
const before = await readProjects()
assertForbidden(t, await agent.post(endpoint).send(rows))
await assertAbsent(
t,
type,
rows.map(({ name }) => name)
)
t.deepEqual(await readProjects(), before)
const accepted = await agent.post(endpoint).send([rows[0]]).expect(200)
t.is(accepted.body.result.length, 1)
const saved = await readReport(type, accepted.body.result[0]._id)
t.is(saved.name, rows[0].name)
t.is(saved.project._id, supervisedProject._id)
t.is(saved.owner._id, owner._id)
})
}

test.after.always(async () => {
await shutdown()
})