Lock down access control and add image previews, file locations and cropping to the CMS.

Security:
- only admins can assign user roles; profile updates are whitelisted
- SBP payment confirmation requires an HMAC-signed webhook or an admin, and is idempotent
- patient receipts and documents are private media owned by the patient, downloaded via the site
- patients cannot change booking price or payment status, forge invoices or write for other accounts

CMS images:
- every upload field shows a large preview, focal point, server file path and public URL
- crop/resize keeps the original in media/originals with a "restore original" action
- legacy image URL fields show where the file really lives and can be moved into the media library
- specialties get a visual icon picker (Untitled UI) and an optional custom icon upload

Fixes: saving the cabinet profile, receipt PDF generation, uploaded doctor photos on the site.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alexey Novikov
2026-11-11 16:23:05 +03:00
co-authored by Claude Opus 5.5
parent 959e693dcc
commit 0b447e67bf
30 changed files with 1290 additions and 34 deletions
+22 -7
View File
@@ -24,15 +24,30 @@ export async function PATCH(request: Request) {
const token = store.get(AUTH_COOKIE)?.value;
if (!token || token === "demo-token") return NextResponse.json({ message: "Unauthorized" }, { status: 401 });
const body = await request.json();
const payload = {
...body,
fullName: body.fullName || body.name,
};
delete payload.name;
const body = ((await request.json().catch(() => ({}))) ?? {}) as Record<string, unknown>;
// Whitelist profile fields: never forward role, email, password or other account internals.
const payload: Record<string, unknown> = {};
const fullName = body.fullName ?? body.name;
if (typeof fullName === "string") payload.fullName = fullName;
if (typeof body.phone === "string") payload.phone = body.phone;
if (typeof body.birthDate === "string" || body.birthDate === null) payload.birthDate = body.birthDate;
if (typeof body.locale === "string") payload.locale = body.locale;
if (typeof body.smsOptIn === "boolean") payload.smsOptIn = body.smsOptIn;
if (typeof body.emailOptIn === "boolean") payload.emailOptIn = body.emailOptIn;
try {
const res = await fetch(`${CMS_URL}/api/users/me`, {
// Payload has no PATCH /users/me: resolve the current user id, then update by id.
const meRes = await fetch(`${CMS_URL}/api/users/me`, {
headers: { Authorization: `Bearer ${token}` },
cache: "no-store",
});
const me = (await meRes.json().catch(() => ({}))) as { user?: { id?: string | number } | null };
const userId = me.user?.id;
if (!meRes.ok || userId == null) {
return NextResponse.json({ message: "Unauthorized" }, { status: 401 });
}
const res = await fetch(`${CMS_URL}/api/users/${encodeURIComponent(String(userId))}`, {
method: "PATCH",
headers: {
Authorization: `Bearer ${token}`,
@@ -0,0 +1,37 @@
import { CMS_URL, getAuthToken } from "@/lib/api";
import { NextResponse } from "next/server";
type Params = { params: Promise<{ filename: string }> };
/**
* Streams a private patient file from the CMS using the patient's session token.
* The CMS media access rules decide whether this patient may read the file.
*/
export async function GET(_request: Request, { params }: Params) {
const { filename } = await params;
if (!filename || filename.includes("/") || filename.includes("..")) {
return NextResponse.json({ error: "invalid" }, { status: 400 });
}
const token = await getAuthToken();
if (!token || token === "demo-token") {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
const res = await fetch(`${CMS_URL}/api/media/file/${encodeURIComponent(filename)}`, {
headers: { Authorization: `Bearer ${token}` },
cache: "no-store",
}).catch(() => null);
if (!res || !res.ok || !res.body) {
return NextResponse.json({ error: "Not found" }, { status: res?.status === 403 ? 403 : 404 });
}
const headers = new Headers();
headers.set("Content-Type", res.headers.get("Content-Type") || "application/octet-stream");
const length = res.headers.get("Content-Length");
if (length) headers.set("Content-Length", length);
headers.set("Content-Disposition", `inline; filename*=UTF-8''${encodeURIComponent(filename)}`);
headers.set("Cache-Control", "private, no-store");
return new NextResponse(res.body, { status: 200, headers });
}
+6 -8
View File
@@ -266,13 +266,9 @@ function mapDoctor(raw: Record<string, unknown>): Doctor {
.filter(Boolean)
.join(" ")
: "";
const mediaUrl =
typeof raw.photo === "string"
? raw.photo
: raw.photo && typeof raw.photo === "object"
? asText((raw.photo as Record<string, unknown>).url)
: "";
const photoUrl = asText(raw.photoUrl) || mediaUrl;
// An uploaded photo (media library) wins over the legacy photoUrl link.
const mediaUrl = raw.photo && typeof raw.photo === "object" ? mediaSrc(raw.photo) : "";
const photoUrl = mediaUrl || resolveAssetUrl(asText(raw.photoUrl));
const photoPending = Boolean(raw.photoPending) || !photoUrl;
const name = asText(raw.name) || asText(raw.fullName);
@@ -1016,7 +1012,9 @@ export async function getMyDocuments(): Promise<CabinetDocument[]> {
return unwrapList(data)
.map((raw) => {
const file = raw.file && typeof raw.file === "object" ? (raw.file as Record<string, unknown>) : null;
const href = mediaSrc(file);
// Patient files are private in the CMS: download them through the site with the session token.
const filename = asText(file?.filename);
const href = filename ? `/api/cabinet/files/${encodeURIComponent(filename)}` : mediaSrc(file);
const title = asText(raw.title);
if (!title || !href) return null;
return { id: String(raw.id ?? ""), title, href, issuedAt: asText(raw.issuedAt) || undefined };