Compare commits

..
Author SHA1 Message Date
PaddyandClaude Opus 5.5 43c7dccc9f Deploy with Gitea Actions instead of Jenkins
Test and deploy / test (pull_request) Successful in 32s
Test and deploy / deploy (pull_request) Skipped
Tests every push and pull request (tsc, unit tests); pushes to master build,
prune dev dependencies and rsync dist/, src/, public/, node_modules/ and
package.json over SSH as the Plesk subscription user, then restart Passenger
via tmp/restart.txt. Only the app's own folders are mirrored with --delete;
.env, logs/ and tmp/ in the app root are never touched. Optional smoke test
via DEPLOY_SMOKE_URL. Same pattern as the calendar frontend; setup and what
the deploy may touch are in docs/deployment.md.

Also removes leftovers: bcrypt (unused since better-auth, and the only
production dependency with a native build) and the SonarQube setup
(sonar-project.properties, vitest-sonar-reporter).

Checked locally: actionlint; in a clean node:26 container npm ci, tsc,
233 unit tests, build and prune; the pruned bundle started against a
throwaway MariaDB (public feed, iCal, /docs, /admin/me); the upload step
against a throwaway SSH server with .env, logs/ and tmp/ in place.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 22:35:57 +01:00
Paddy c4661d1dc0 Merge pull request 'Escape and fold the iCal export, serve it as text/calendar, add subscription links' (#20) from feature/calendar-ical-escaping-subscriptions into master
Jenkins Production Deployment
Reviewed-on: #20
2026-11-08 20:13:16 +00:00
Paddy 81397ba61f Merge branch 'master' into feature/calendar-ical-escaping-subscriptions 2026-11-08 19:47:29 +00:00
Paddy 314bdc6296 Merge pull request 'Add a read-only role per app for calendar, feedback and tickets' (#21) from feature/readonly-role into master
Jenkins Production Deployment
Reviewed-on: #21
2026-10-30 20:24:34 +00:00
PaddyandClaude Opus 5.5 bfb8f2ff9c Add a read-only role per app for calendar, feedback and tickets
A grant can now be `read` instead of `access`: requireAppAccess lets it
through for GET/HEAD/OPTIONS and answers 403 to every other method, so
new write routes are covered without being marked. `read` and `access`
on the same app are rejected together; user management has no read role.
/tickets/admin/me and /feedback/admin/me report `readOnly`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-29 23:24:28 +01:00
Paddy bc865813cb Merge pull request 'Fix issue where email would still be required for personalized vouchers in the backend' (#19) from feature/optional-personalized-voucher-email into master
Jenkins Production Deployment
Reviewed-on: #19
2026-09-21 21:01:40 +00:00
Paddy e8f971b54e Fix issue where email would still be required for personalized vouchers in the backend 2026-09-21 23:01:22 +02:00
17 changed files with 603 additions and 787 deletions

No files matched your search

+104
View File
@@ -0,0 +1,104 @@
# Tests every push and pull request; deploys pushes to master to the Plesk server.
# Setup and what the deploy may touch: docs/deployment.md.
#
# Same pattern as the calendar frontend (Calendar_Frontend/.gitea/workflows/deploy.yml): build here,
# deliver over SSH as the Plesk subscription user, restart Passenger. Unlike there, the app folder
# also holds things the deploy must never touch (.env with the secrets, logs/, tmp/), so only the
# app's own folders are synced with --delete and nothing is deleted at the top level.
name: Test and deploy
on:
push:
branches: [master]
pull_request:
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version-file: .nvmrc
- run: npm ci
- run: npx tsc --noEmit
# Unit tests only. The integration tests (npm run test:integration) start their own MariaDB
# container and aren't run here.
# FEEDBACK_IP_SALT (which the Jenkins job exported) isn't needed: vitest.config.ts sets one.
- run: npm test
deploy:
needs: test
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
runs-on: ubuntu-latest
env:
DEPLOY_HOST: ${{ vars.DEPLOY_HOST }}
DEPLOY_PORT: ${{ vars.DEPLOY_PORT }}
DEPLOY_USER: ${{ vars.DEPLOY_USER }}
DEPLOY_PATH: ${{ vars.DEPLOY_PATH }}
# Absolute on purpose: rsync passes its -e string to ssh without a shell, and ssh expands "~"
# from the passwd entry, not $HOME (see the calendar frontend's docs/gotchas.md).
SSH_DIR: /tmp/deploy-ssh
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version-file: .nvmrc
- run: npm ci
- run: npm run build
# Only runtime dependencies go to the server (no typescript, vitest, ...).
- run: npm prune --omit=dev
- name: SSH setup
env:
DEPLOY_SSH_KEY: ${{ secrets.DEPLOY_SSH_KEY }}
DEPLOY_KNOWN_HOSTS: ${{ secrets.DEPLOY_KNOWN_HOSTS }}
run: |
test -n "$DEPLOY_HOST" && test -n "$DEPLOY_USER" && test -n "$DEPLOY_PATH"
if ! command -v rsync >/dev/null 2>&1 || ! command -v ssh >/dev/null 2>&1; then
apt-get update && apt-get install -y --no-install-recommends rsync openssh-client
fi
install -m 700 -d "$SSH_DIR"
printf '%s\n' "$DEPLOY_SSH_KEY" > "$SSH_DIR/key"
chmod 600 "$SSH_DIR/key"
# Pinned host key, never trust-on-first-use.
printf '%s\n' "$DEPLOY_KNOWN_HOSTS" > "$SSH_DIR/known_hosts"
- name: Upload
run: |
rsh="ssh -p ${DEPLOY_PORT:-22} -i $SSH_DIR/key -o UserKnownHostsFile=$SSH_DIR/known_hosts -o IdentitiesOnly=yes -o BatchMode=yes"
target="$DEPLOY_USER@$DEPLOY_HOST:$DEPLOY_PATH"
# The app's own folders, each mirrored exactly (--delete within the folder only).
# src/ is needed at runtime: swagger-jsdoc reads the route docs from src/models/**/*.ts.
for dir in dist src public node_modules; do
rsync -az --delete -e "$rsh" "$dir/" "$target/$dir/"
done
# Single files at the top level, without --delete: .env, logs/ and tmp/ live there too.
# package.json is what makes Node treat dist/ as ES modules ("type": "module").
rsync -az -e "$rsh" package.json package-lock.json "$target/"
- name: Restart
run: |
# Passenger restarts the app on its next request when tmp/restart.txt is touched.
ssh -p "${DEPLOY_PORT:-22}" -i "$SSH_DIR/key" -o "UserKnownHostsFile=$SSH_DIR/known_hosts" \
-o IdentitiesOnly=yes -o BatchMode=yes "$DEPLOY_USER@$DEPLOY_HOST" \
"mkdir -p '$DEPLOY_PATH/tmp' && touch '$DEPLOY_PATH/tmp/restart.txt'"
# Set DEPLOY_SMOKE_URL to an endpoint that must answer 200 without a session, e.g.
# https://api.nachklang.art/calendar/events/public/json (what nachklang.art itself reads).
- name: Smoke test
if: vars.DEPLOY_SMOKE_URL != ''
env:
DEPLOY_SMOKE_URL: ${{ vars.DEPLOY_SMOKE_URL }}
run: |
# Passenger starts the app on the first request after the restart; give it a moment.
for _ in 1 2 3 4 5 6; do
if curl -fsS -o /dev/null "$DEPLOY_SMOKE_URL"; then echo "OK: $DEPLOY_SMOKE_URL"; exit 0; fi
sleep 5
done
echo "Smoke test failed: $DEPLOY_SMOKE_URL"
exit 1
- name: Remove key
if: always()
run: rm -f "$SSH_DIR/key"
+10 -3
View File
@@ -8,7 +8,7 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co
npm run build # Compile TypeScript → dist/
npm run start # Build and start (tsc && node ./dist/app.js)
npm run debug # Start with DEBUG=* environment variable
npm run test # Run the vitest suite once with coverage (lcov + testResults/sonar-report.xml)
npm run test # Run the vitest suite once with coverage (text summary + lcov in coverage/)
npm run test:watch # vitest in watch mode
npm run test:integration # Admin-module tests against a throwaway MariaDB (needs docker or podman)
```
@@ -18,6 +18,12 @@ Run a single test file:
npx vitest run test/some.test.ts
```
**Deploys** run in Gitea Actions (`.gitea/workflows/deploy.yml`, since 2026-11-08; Jenkins is
retired): every push/PR runs `tsc` and the unit tests, pushes to master build, prune dev
dependencies and rsync `dist/`, `src/`, `public/`, `node_modules/` and `package.json` to Plesk over
SSH. `.env`, `logs/` and `tmp/` on the server are never touched; SQL migrations stay manual.
Details: `docs/deployment.md`.
## Architecture
Express.js REST API in TypeScript with a service-oriented layering. Domains: `Calendar` (events, users), `Feedback` (concert feedback forms, mounted at `/feedback`, backed by its own `FEEDBACK_DB` — see `src/models/feedback/`: public submission flow, admin CRUD, reporting, and a Salesforce newsletter-sync integration), `Tickets` (mounted at `/tickets`), and `Admin` (identity and permissions, mounted at `/admin`, backed by `ADMIN_DB` — see below).
@@ -47,8 +53,9 @@ and `/admin` for the JSON routes. Sessions are httpOnly cookies scoped to
`.nachklang.art`, so one sign-in covers every app. Accounts are **invite-only** — public
sign-up is disabled, and `invitations.plugin.ts` is the only code that creates users.
A permission is **(app, role)** in `user_app_permissions`, keyed on
`(user_id, app, role)` so one user can hold several roles per app. `access` is the only role
today and means "may use this app at all"; `APP_ROLES` in `admin.schema.ts` is the contract,
`(user_id, app, role)` so one user can hold several roles per app. Two roles exist: `access`
(full access) and `read` (read-only, not for `admin`) - see `docs/admin-roles.md`, including
how `requireAppAccess` enforces `read` by HTTP method; `APP_ROLES` in `admin.schema.ts` is the contract,
and a role not listed there is rejected rather than written. `requireAppAccess(app)` in
`admin.middleware.ts` is the single authenticator - it takes an optional second argument to
narrow to one role, and queries the database on every request (no cookie cache) so disabling
+73
View File
@@ -0,0 +1,73 @@
# Admin roles: full access and read-only
Added 2026-10-29. A user's permission is a list of **(app, role)** grants in
`user_app_permissions` (see `src/models/admin/admin.schema.ts`). Each app can now be granted at
one of two levels:
| Role | UI label (admin app) | Apps | Meaning |
|----------|----------------------|-------------------------------|--------------------------------------------|
| `access` | Vollzugriff | calendar, feedback, tickets, admin | Everything in that app, as before. |
| `read` | Nur lesen | calendar, feedback, tickets | See everything, change nothing. |
Rules:
- **`access` and `read` on the same app are mutually exclusive.** `toPermissions` returns `null`
for a list holding both, so `PUT /admin/users/:id/permissions` and `POST /admin/invitations`
answer 400. There is no "higher one wins".
- **`admin` (user management) has no `read` role.** Viewing it means viewing other people's
email addresses, IPs and sessions; that is a different trust level from viewing concerts.
- Existing grants were not migrated: every pre-existing row is `access` and still means full
access.
## Enforcement
`requireAppAccess(app)` (no role argument - which is how every app router uses it) does, after
the existing session/disabled/has-any-grant checks:
- `GET`, `HEAD`, `OPTIONS` - allowed for any grant on the app.
- anything else - allowed only with `access` (`canWrite`). A `read`-only user gets
`403 {status: 'FORBIDDEN', message: 'Du hast für diesen Bereich nur Lesezugriff.'}`.
This is by method on purpose: a write route added later is covered without being marked. It
relies on one convention that **must stay true**: admin routes read with GET and change with
POST/PUT/PATCH/DELETE. When this was built, every GET handler in tickets, feedback and calendar
was checked to call only read functions (no SQL writes, no mail). A GET with a side effect, or a
POST that only reads, breaks the read-only role in one direction or the other.
Consequences worth knowing:
- "Resend confirmation email" (tickets) is a POST and therefore not available read-only - by
decision: it has an effect outside the system.
- CSV exports and reports are GETs and therefore available read-only - also by decision.
Read-only means all data, including personal data. A restricted variant would be a new role.
- `requireAppAccess(app, role)` with an explicit role does **not** apply the method rule: the
caller has already named exactly the grant it needs.
## Telling the frontend
- `GET /tickets/admin/me` and `GET /feedback/admin/me` return `readOnly: boolean` alongside
`email` and `fullName`. Frontends hide write controls when it is `true`; the API refuses the
writes regardless.
- `GET /admin/me` returns the full `permissions` list; `isReadOnly(permissions, app)` in
`admin.schema.ts` is the same rule.
## Calendar
The API side is done: calendar writes sit behind `requireAppAccess('calendar')` and so refuse a
`read`-only user, and the optional-cookie reads (`/:calendar/json`, `/ical`) check
`access.apps.includes('calendar')`, which a `read` grant satisfies - read-only users see the
restricted calendars.
The current Angular frontend (`Calendar_Frontend`) was deliberately **not** changed, because it
is being rewritten in Next.js. Until then a read-only calendar user sees the edit buttons and
gets the 403 above when using them. Contract for the rewrite: there is no `/calendar/.../me`
with `readOnly`; read `GET /admin/me` and compute `isReadOnly(permissions, 'calendar')` - or add
a calendar `/me` mirroring tickets/feedback.
## Adding a finer role later
`canWrite` checks for `access` specifically, so a new role (e.g. `tickets:redeem`) grants no
writes through the method rule. Watch out: tickets and feedback apply the role-less
`requireAdminAuth` once at the top of their admin router, so that gate would already refuse the
new role's POST before any route-level `requireAppAccess(app, 'redeem')` runs. A finer role
therefore needs `canWrite` (or the top-level gate) taught about it, not just a route-level check.
+102
View File
@@ -0,0 +1,102 @@
# Deployment
The API runs on the Plesk server as a Node.js app (Phusion Passenger) and is deployed by **Gitea
Actions** (`.gitea/workflows/deploy.yml`), which replaced the Jenkins job on 2026-11-08. Same
pattern as the calendar frontend; the reusable how-to (server facts, one-time setup, gotchas) is in
`Calendar_Frontend/docs/deployment.md` and in Patrick's PKM ("Jenkins to Gitea Actions for a Plesk
Node.js App").
```
push to master ──▶ Gitea ──▶ runner (container on the server)
├─ test job: npm ci, tsc --noEmit, npm test (unit tests)
└─ deploy job: npm ci, build, prune dev deps ─ rsync over SSH ─▶
plesk-nachklang@nachklang.art:<app root> + tmp/restart.txt
```
Pull requests only run the test job.
## Plesk (as configured, 2026-11-08)
*Websites & Domains → api.nachklang.art → Node.js*:
| Setting | Value |
|---|---|
| Node.js version | 26.13.0 |
| Application root | `/api.nachklang.art` (= `/var/www/vhosts/nachklang.art/api.nachklang.art`) |
| Document root | `/api.nachklang.art/public` |
| Application startup file | `dist/app.js` |
| Application mode | production |
| Environment variable | `PORT=443` (Passenger takes over the port; `app.ts` exits without `PORT`) |
## What the deploy touches - and what it must not
The app root holds more than the build, so the workflow syncs **only the app's own parts**:
| Path | How | Why it's needed at runtime |
|---|---|---|
| `dist/` | mirrored (`--delete` inside) | the compiled app |
| `src/` | mirrored | swagger-jsdoc reads the route docs from `src/models/**/*.router.ts` / `*.interface.ts` for `/docs` |
| `public/` | mirrored | Passenger's document root |
| `node_modules/` | mirrored, production dependencies only (`npm prune --omit=dev`) | runtime dependencies |
| `package.json`, `package-lock.json` | copied, no delete | `"type": "module"` makes Node load `dist/` as ES modules |
**Never touched** (nothing is deleted at the top level): `.env` (the secrets; `dotenv` reads it from
the app root), `logs/` (Winston writes there, relative to the app root), `tmp/` (Passenger's
`restart.txt`), and anything else in that folder.
The Jenkins job copied `dist/`, `src/` and `node_modules/` with `cp` (never deleting, so stale files
piled up) and never `package.json` or `public/`; the first rsync deploy cleans that up.
**SQL migrations are not part of the deploy.** Files under `sql/` are still applied by hand, before
the deploy that needs them.
## Gitea settings
Shared by all Nachklang repos, on the organisation (*Nachklang → Settings → Actions*): secrets
`DEPLOY_SSH_KEY`, `DEPLOY_KNOWN_HOSTS`; variables `DEPLOY_HOST=nachklang.art`, `DEPLOY_PORT=42069`,
`DEPLOY_USER=plesk-nachklang`.
On this repository (*API → Settings → Actions → Variables*):
| Variable | Value |
|---|---|
| `DEPLOY_PATH` | `/var/www/vhosts/nachklang.art/api.nachklang.art` |
| `DEPLOY_SMOKE_URL` | `https://api.nachklang.art/calendar/events/public/json` |
The smoke test only needs an endpoint that answers 200 without a session; the public calendar feed
is the one nachklang.art itself depends on.
## Switchover (done once)
1. Pause the Jenkins API job (done 2026-11-08).
2. Set `DEPLOY_PATH` and `DEPLOY_SMOKE_URL`.
3. Open a PR → green test job → merge → watch the deploy job. Unlike the calendar there is no gap:
the folder keeps working while files are replaced, and Passenger restarts on the next request.
4. Check `https://api.nachklang.art/docs` and the calendar/feedback/tickets apps.
Rollback: revert the commit on master; the workflow deploys the previous state.
## Checked locally (2026-11-08)
- `actionlint`: no findings.
- In a clean `node:26` container (26.13.0, no `.env`): `npm ci`, `tsc --noEmit`, all 233 unit tests,
build, `npm prune --omit=dev`.
- The pruned bundle, laid out like the server folder with a test `.env`, started with
`node dist/app.js` against a throwaway MariaDB: public JSON 200, iCal `text/calendar`, `/docs` 200
including the subscriptions route (so `src/` is read), `/admin/me` 401, `logs/` created in the app
root.
- The workflow's upload step against a throwaway SSH server with a folder like the real one: `.env`,
`logs/`, `tmp/` and an unrelated top-level file kept; a stale package in `node_modules/` and a stale
file in `dist/` removed; `package.json` updated.
Not checked: the workflow on the real runner.
## Notes
- `bcrypt` was removed on 2026-11-08: nothing imported it any more (passwords moved to better-auth,
which hashes with its own scrypt). It was the only production dependency with a native build step.
- `FEEDBACK_IP_SALT`, which the Jenkins test step exported, isn't needed in CI: `vitest.config.ts`
sets one for the test run.
- SonarQube isn't used any more; `sonar-project.properties`, the `vitest-sonar-reporter` and its
`testResults/sonar-report.xml` were removed on 2026-11-08 (the Jenkins job ran the tests only to
feed Sonar's coverage).
+6 -757
View File
File diff suppressed because it is too large. Load diff
+1 -4
View File
@@ -23,7 +23,6 @@
"@better-auth/passkey": "^1.7.2",
"app-root-path": "^3.0.0",
"axios": "^1.20.0",
"bcrypt": "^5.0.1",
"better-auth": "^1.7.2",
"cors": "^2.8.5",
"debug": "^4.3.1",
@@ -41,7 +40,6 @@
},
"devDependencies": {
"@types/app-root-path": "^1.2.4",
"@types/bcrypt": "^3.0.1",
"@types/cors": "^2.8.19",
"@types/debug": "^4.1.5",
"@types/express": "^4.17.15",
@@ -56,8 +54,7 @@
"source-map-support": "^0.5.19",
"supertest": "^7.2.2",
"typescript": "^5.9.3",
"vitest": "^5.0.0",
"vitest-sonar-reporter": "^3.0.0"
"vitest": "^5.0.0"
},
"overrides": {
"better-auth": {
-6
View File
@@ -1,6 +0,0 @@
sonar.projectKey=nk-api
sonar.sources=src
sonar.tests=test
sonar.language=ts
sonar.javascript.lcov.reportPaths=coverage/lcov.info
sonar.testExecutionReportPaths=testResults/sonar-report.xml
+15 -1
View File
@@ -2,9 +2,14 @@ import express from 'express';
import {fromNodeHeaders} from 'better-auth/node';
import {auth} from './admin.auth.js';
import * as UsersService from './users/users.admin.service.js';
import {AppName, AppPermission, AppRole} from './admin.schema.js';
import {AppName, AppPermission, AppRole, canWrite} from './admin.schema.js';
import {sendServerError} from './admin.errors.js';
// What a read-only grant may do. Every admin route reads with GET and changes
// with POST/PUT/PATCH/DELETE - keep it that way, or a read-only user either
// loses a view or gains a write.
const READ_METHODS = new Set(['GET', 'HEAD', 'OPTIONS']);
/**
* The one authenticator for every admin area in this API. It replaces
* feedback.auth.ts and tickets.auth.ts, which each re-implemented the same
@@ -131,6 +136,15 @@ export const requireAppAccess = (app: AppName, role?: AppRole): express.RequestH
return;
}
// The read-only level, enforced by method rather than per route so a
// write route added later is covered without being marked. Only the
// role-less form applies it: a caller asking for a specific role has
// already said exactly what is required.
if (role === undefined && !READ_METHODS.has(req.method) && !canWrite(access.permissions, app)) {
forbidden(res, 'Du hast für diesen Bereich nur Lesezugriff.');
return;
}
res.locals.admin = access;
next();
} catch (e: any) {
+36 -4
View File
@@ -35,13 +35,26 @@ export const isAppName = (value: unknown): value is AppName => {
*/
export const ACCESS_ROLE = 'access';
/**
* Read-only access to an app: everything `access` may see, nothing it may
* change. requireAppAccess enforces it by HTTP method - a user holding only
* this role gets GET/HEAD/OPTIONS and a 403 for anything else - so a new write
* route is covered without anyone remembering to mark it.
*
* It and `access` are mutually exclusive on one app (toPermissions rejects a
* list holding both), because "full access and also read-only" has no sensible
* reading. The `admin` app deliberately has no read role: viewing user
* management means viewing other people's addresses, IPs and sessions.
*/
export const READ_ROLE = 'read';
export type AppRole = string;
/** Every role that exists, per app, in display order. Extend to add one. */
export const APP_ROLES: Record<AppName, readonly AppRole[]> = {
calendar: [ACCESS_ROLE],
feedback: [ACCESS_ROLE],
tickets: [ACCESS_ROLE],
calendar: [ACCESS_ROLE, READ_ROLE],
feedback: [ACCESS_ROLE, READ_ROLE],
tickets: [ACCESS_ROLE, READ_ROLE],
admin: [ACCESS_ROLE]
};
@@ -85,7 +98,7 @@ export const toPermissions = (value: unknown): AppPermission[] | null => {
}
const seen = new Set<string>();
return permissions.filter(permission => {
const unique = permissions.filter(permission => {
const key = `${permission.app}:${permission.role}`;
if (seen.has(key)) {
return false;
@@ -93,6 +106,11 @@ export const toPermissions = (value: unknown): AppPermission[] | null => {
seen.add(key);
return true;
});
// Rejected rather than resolved to either level: whichever one we picked,
// the caller asked for something else.
const contradictory = APP_NAMES.some(app => seen.has(`${app}:${ACCESS_ROLE}`) && seen.has(`${app}:${READ_ROLE}`));
return contradictory ? null : unique;
};
/** The distinct apps a permission list grants any access to. */
@@ -100,6 +118,20 @@ export const appsOf = (permissions: AppPermission[]): AppName[] => {
return APP_NAMES.filter(app => permissions.some(permission => permission.app === app));
};
/**
* Whether these grants allow changing anything in `app`. Only `access` does:
* `read` is the read-only level, and a finer role added later grants exactly
* what its own check says, not writes in general.
*/
export const canWrite = (permissions: AppPermission[], app: AppName): boolean => {
return permissions.some(permission => permission.app === app && permission.role === ACCESS_ROLE);
};
/** Holds some grant on `app`, but none that allows changes. */
export const isReadOnly = (permissions: AppPermission[], app: AppName): boolean => {
return appsOf(permissions).includes(app) && !canWrite(permissions, app);
};
export interface UserTable {
id: string;
name: string;
+10 -1
View File
@@ -3,6 +3,7 @@
*/
import express, {Request, Response} from 'express';
import {requireAdminAuth} from '../feedback.auth.js';
import {isReadOnly} from '../../admin/admin.schema.js';
import {sendServerError} from '../feedback.errors.js';
import {eventsAdminRouter} from './events.admin.router.js';
import {songsAdminRouter} from './songs.admin.router.js';
@@ -40,13 +41,21 @@ adminRouter.use(requireAdminAuth);
* type: string
* fullName:
* type: string
* readOnly:
* type: boolean
* description: Holds only the read role for feedback - every write answers 403.
* 401:
* description: Unauthorized
* 403:
* description: Signed in without the permission for this app, or account disabled
*/
adminRouter.get('/me', (req: Request, res: Response) => {
res.status(200).send({email: res.locals.admin.email, fullName: res.locals.admin.displayName});
res.status(200).send({
email: res.locals.admin.email,
fullName: res.locals.admin.displayName,
// Lets the frontend hide what the API would refuse anyway.
readOnly: isReadOnly(res.locals.admin.permissions, 'feedback')
});
});
/**
+19 -1
View File
@@ -1,5 +1,6 @@
import express, {Request, Response} from 'express';
import {requireAdminAuth} from '../tickets.auth.js';
import {isReadOnly} from '../../admin/admin.schema.js';
import {vouchersAdminRouter} from './vouchers.admin.router.js';
import {redemptionsAdminRouter, voucherHistoryRouter} from './redemptions.admin.router.js';
import {eventsAdminRouter} from './events.admin.router.js';
@@ -21,13 +22,30 @@ adminRouter.use(requireAdminAuth);
* responses:
* 200:
* description: Success
* content:
* application/json:
* schema:
* type: object
* properties:
* email:
* type: string
* fullName:
* type: string
* readOnly:
* type: boolean
* description: Holds only the read role for tickets - every write answers 403.
* 401:
* description: Unauthorized
* 403:
* description: Signed in without the permission for this app, or account disabled
*/
adminRouter.get('/me', (req: Request, res: Response) => {
res.status(200).send({email: res.locals.admin.email, fullName: res.locals.admin.displayName});
res.status(200).send({
email: res.locals.admin.email,
fullName: res.locals.admin.displayName,
// Lets the frontend hide what the API would refuse anyway.
readOnly: isReadOnly(res.locals.admin.permissions, 'tickets')
});
});
adminRouter.use('/vouchers', vouchersAdminRouter);
@@ -129,12 +129,13 @@ vouchersAdminRouter.post('/wildcard', async (req: Request, res: Response) => {
* type: array
* items:
* type: object
* required: [name, email, eventIds]
* required: [name, eventIds]
* properties:
* name:
* type: string
* email:
* type: string
* description: Optional - a guest can supply their own email when redeeming.
* eventIds:
* type: array
* items:
@@ -123,10 +123,10 @@ export const generatePersonalizedBatch = async (rows: PersonalizedRowInput[], cr
throw new Error('at least one row is required');
}
for (const row of rows) {
if (!row.name || !row.email || row.eventIds.length === 0) {
throw new Error('each row requires a name, email, and at least one eligible event');
if (!row.name || row.eventIds.length === 0) {
throw new Error('each row requires a name and at least one eligible event');
}
if (!isValidEmail(row.email)) {
if (row.email && !isValidEmail(row.email)) {
throw new Error(`"${row.email}" does not look like a valid email address`);
}
}
@@ -147,7 +147,7 @@ export const generatePersonalizedBatch = async (rows: PersonalizedRowInput[], cr
codes.push(code);
await conn.query(
'INSERT INTO voucher_codes (code, status, max_guests, prefill_name, prefill_email, batch_id, created_by_email) VALUES (?,?,?,?,?,?,?)',
[code, 'UNUSED', row.maxGuests, row.name, row.email, batchId, createdByEmail]
[code, 'UNUSED', row.maxGuests, row.name, row.email || null, batchId, createdByEmail]
);
for (const eventId of row.eventIds) {
await conn.query('INSERT INTO voucher_code_events (code, event_id) VALUES (?,?)', [code, eventId]);
+59 -1
View File
@@ -15,7 +15,7 @@ import {requireAppAccess, requireSignedIn, resolveAccess} from '../../src/models
const mockGetSession = auth.api.getSession as unknown as Mock;
const mockLoadAccess = UsersService.loadAccess as Mock;
const makeReq = (): Request => ({headers: {cookie: 'nachklang.session_token=abc'}} as unknown as Request);
const makeReq = (method = 'GET'): Request => ({method, headers: {cookie: 'nachklang.session_token=abc'}} as unknown as Request);
const makeRes = (): Response => {
const res: any = {};
@@ -219,4 +219,62 @@ describe('requireAppAccess', () => {
expect(next).toHaveBeenCalled();
});
describe('read-only grant', () => {
const readOnlyUser = {
...activeUser,
permissions: [{app: 'tickets', role: 'read'}],
apps: ['tickets']
};
it.each(['GET', 'HEAD', 'OPTIONS'])('lets %s through', async method => {
mockGetSession.mockResolvedValue({user: {id: 'u1'}});
mockLoadAccess.mockResolvedValue(readOnlyUser);
const res = makeRes();
const next = vi.fn();
await requireAppAccess('tickets')(makeReq(method), res, next);
expect(next).toHaveBeenCalled();
});
it.each(['POST', 'PUT', 'PATCH', 'DELETE'])('403s %s', async method => {
mockGetSession.mockResolvedValue({user: {id: 'u1'}});
mockLoadAccess.mockResolvedValue(readOnlyUser);
const res = makeRes();
const next = vi.fn();
await requireAppAccess('tickets')(makeReq(method), res, next);
expect(res.status).toHaveBeenCalledWith(403);
expect(res.send).toHaveBeenCalledWith(expect.objectContaining({message: 'Du hast für diesen Bereich nur Lesezugriff.'}));
expect(next).not.toHaveBeenCalled();
});
it('does not let full access to another app write here', async () => {
mockGetSession.mockResolvedValue({user: {id: 'u1'}});
mockLoadAccess.mockResolvedValue({
...activeUser,
permissions: [{app: 'tickets', role: 'read'}, {app: 'feedback', role: 'access'}],
apps: ['feedback', 'tickets']
});
const res = makeRes();
const next = vi.fn();
await requireAppAccess('tickets')(makeReq('POST'), res, next);
expect(res.status).toHaveBeenCalledWith(403);
});
it('lets full access write', async () => {
mockGetSession.mockResolvedValue({user: {id: 'u1'}});
mockLoadAccess.mockResolvedValue(activeUser);
const res = makeRes();
const next = vi.fn();
await requireAppAccess('feedback')(makeReq('DELETE'), res, next);
expect(next).toHaveBeenCalled();
});
});
});
+41
View File
@@ -1,9 +1,12 @@
import {describe, expect, it} from 'vitest';
import {
ACCESS_ROLE,
READ_ROLE,
appsOf,
canWrite,
isAppPermission,
isAppRole,
isReadOnly,
toPermissions
} from '../../src/models/admin/admin.schema.js';
@@ -59,6 +62,44 @@ describe('toPermissions', () => {
it('reads an empty list as "no permissions", not as invalid', () => {
expect(toPermissions([])).toEqual([]);
});
it('reads the read-only role', () => {
expect(toPermissions([{app: 'tickets', role: READ_ROLE}])).toEqual([
{app: 'tickets', role: READ_ROLE}
]);
});
it('rejects full and read-only access to the same app together', () => {
expect(toPermissions(['tickets', {app: 'tickets', role: READ_ROLE}])).toBeNull();
});
it('allows full access to one app next to read-only access to another', () => {
expect(toPermissions(['tickets', {app: 'feedback', role: READ_ROLE}])).toEqual([
{app: 'tickets', role: ACCESS_ROLE},
{app: 'feedback', role: READ_ROLE}
]);
});
it('rejects read-only access to user management', () => {
expect(toPermissions([{app: 'admin', role: READ_ROLE}])).toBeNull();
});
});
describe('canWrite / isReadOnly', () => {
it('lets only the access role write', () => {
const permissions = [
{app: 'tickets' as const, role: ACCESS_ROLE},
{app: 'feedback' as const, role: READ_ROLE}
];
expect(canWrite(permissions, 'tickets')).toBe(true);
expect(canWrite(permissions, 'feedback')).toBe(false);
expect(isReadOnly(permissions, 'tickets')).toBe(false);
expect(isReadOnly(permissions, 'feedback')).toBe(true);
});
it('is neither for an app without any grant', () => {
expect(canWrite([], 'calendar')).toBe(false);
expect(isReadOnly([], 'calendar')).toBe(false);
});
});
describe('isAppRole', () => {
+121
View File
@@ -0,0 +1,121 @@
import {describe, it, expect, beforeAll, beforeEach, afterAll} from 'vitest';
import type {Application} from 'express';
import {createApp} from '../../src/app.factory.js';
import {READ_ROLE} from '../../src/models/admin/admin.schema.js';
import {closeDatabase, createAndAcceptInvitation, resetDatabase} from './helpers.js';
/**
* The read-only role end to end: a real session cookie through the real
* middleware into each app's routers. The writes are rejected before any
* handler runs, which is why this suite needs no tickets/feedback/calendar
* database - a 403 here proves the gate held, not that a query failed.
*/
let app: Application;
beforeAll(() => {
app = createApp();
});
beforeEach(async () => {
await resetDatabase();
});
afterAll(async () => {
await closeDatabase();
});
const readOnlyEverywhere = async () => {
return createAndAcceptInvitation(app, 'reader@nachklang.art', 'Reader', [
{app: 'tickets', role: READ_ROLE},
{app: 'feedback', role: READ_ROLE},
{app: 'calendar', role: READ_ROLE}
]);
};
describe('read-only access', () => {
it('reports readOnly on each app\'s /me', async () => {
const {agent} = await readOnlyEverywhere();
const tickets = await agent.get('/tickets/admin/me');
const feedback = await agent.get('/feedback/admin/me');
expect(tickets.status).toBe(200);
expect(tickets.body.readOnly).toBe(true);
expect(feedback.status).toBe(200);
expect(feedback.body.readOnly).toBe(true);
});
it('reports readOnly false for full access', async () => {
const {agent} = await createAndAcceptInvitation(app, 'full@nachklang.art', 'Full', ['tickets']);
const res = await agent.get('/tickets/admin/me');
expect(res.status).toBe(200);
expect(res.body.readOnly).toBe(false);
});
it.each([
['post', '/tickets/admin/vouchers/wildcard'],
['post', '/tickets/admin/redemptions/1/resend-confirmation'],
['patch', '/tickets/admin/redemptions/1'],
['put', '/tickets/admin/events/1/settings'],
['delete', '/feedback/admin/submissions/1'],
['post', '/feedback/admin/events'],
['post', '/calendar/events'],
['delete', '/calendar/events/1']
] as const)('refuses %s %s', async (method, path) => {
const {agent} = await readOnlyEverywhere();
const res = await agent[method](path).send({});
expect(res.status).toBe(403);
expect(res.body.message).toBe('Du hast für diesen Bereich nur Lesezugriff.');
});
it('does not open user management', async () => {
const {agent} = await readOnlyEverywhere();
expect((await agent.get('/admin/users')).status).toBe(403);
});
});
describe('granting read-only access', () => {
const signedInAdmin = () => createAndAcceptInvitation(app, 'admin@nachklang.art', 'Admin', ['admin']);
it('stores the read role per app', async () => {
const {agent} = await signedInAdmin();
const other = await createAndAcceptInvitation(app, 'user@nachklang.art', 'User', ['feedback']);
const res = await agent
.put(`/admin/users/${other.userId}/permissions`)
.send({permissions: [{app: 'tickets', role: READ_ROLE}, {app: 'feedback', role: 'access'}]});
expect(res.status).toBe(200);
expect(res.body.permissions).toEqual(expect.arrayContaining([
{app: 'tickets', role: READ_ROLE},
{app: 'feedback', role: 'access'}
]));
});
it('rejects full and read-only access to the same app', async () => {
const {agent} = await signedInAdmin();
const other = await createAndAcceptInvitation(app, 'user@nachklang.art', 'User', ['feedback']);
const res = await agent
.put(`/admin/users/${other.userId}/permissions`)
.send({permissions: [{app: 'tickets', role: READ_ROLE}, {app: 'tickets', role: 'access'}]});
expect(res.status).toBe(400);
});
it('rejects inviting someone with read-only user management', async () => {
const {agent} = await signedInAdmin();
const res = await agent
.post('/admin/invitations')
.send({email: 'new@nachklang.art', name: 'New', permissions: [{app: 'admin', role: READ_ROLE}]});
expect(res.status).toBe(400);
});
});
-4
View File
@@ -14,10 +14,6 @@ export default defineConfig({
env: {
FEEDBACK_IP_SALT: 'vitest-salt'
},
reporters: [
'default',
['vitest-sonar-reporter', {outputFile: 'testResults/sonar-report.xml'}]
],
coverage: {
provider: 'v8',
reporter: ['text-summary', 'lcov'],