diff --git a/src/astro-typesafe-api-caller.ts b/src/astro-typesafe-api-caller.ts index 8534c72d..85eb31a1 100644 --- a/src/astro-typesafe-api-caller.ts +++ b/src/astro-typesafe-api-caller.ts @@ -34,9 +34,9 @@ export const createCaller = createCallerFactory({ "user/self": await import("../src/pages/api/user/self.ts"), "verbrauchsausweis-wohnen/[id]": await import("../src/pages/api/verbrauchsausweis-wohnen/[id].ts"), "verbrauchsausweis-wohnen": await import("../src/pages/api/verbrauchsausweis-wohnen/index.ts"), + "webhooks/mollie": await import("../src/pages/api/webhooks/mollie.ts"), "aufnahme/[id]/bilder": await import("../src/pages/api/aufnahme/[id]/bilder.ts"), "aufnahme/[id]": await import("../src/pages/api/aufnahme/[id]/index.ts"), "aufnahme/[id]/unterlagen": await import("../src/pages/api/aufnahme/[id]/unterlagen.ts"), - "webhooks/mollie": await import("../src/pages/api/webhooks/mollie.ts"), "objekt/[id]": await import("../src/pages/api/objekt/[id]/index.ts"), }) \ No newline at end of file diff --git a/src/client/lib/lesen.ts b/src/client/lib/lesen.ts new file mode 100644 index 00000000..c7ded8a2 --- /dev/null +++ b/src/client/lib/lesen.ts @@ -0,0 +1,15 @@ +import { API_ACCESS_TOKEN_COOKIE_NAME } from "#lib/constants.js"; +import { Benutzer } from "#lib/client/prisma.js"; +import { api } from "astro-typesafe-api/client"; +import Cookies from "js-cookie"; + +export async function benutzerLesen(benutzerId: string): Promise { + const benutzer = await api.user.GET.fetch({ id: benutzerId } + , { + headers: { + Authorization: `Bearer ${Cookies.get(API_ACCESS_TOKEN_COOKIE_NAME)}` + } + }); + + return benutzer[0]; +} \ No newline at end of file diff --git a/src/modules/KundendatenModule.svelte b/src/modules/KundendatenModule.svelte index 10995576..335338b9 100644 --- a/src/modules/KundendatenModule.svelte +++ b/src/modules/KundendatenModule.svelte @@ -33,6 +33,7 @@ import { endEnergieVerbrauchVerbrauchsausweis_2016_Client } from "#lib/Berechnungen/VerbrauchsausweisWohnen/VerbrauchsausweisWohnen_2016_Client.js"; import { endEnergieVerbrauchVerbrauchsausweisGewerbe_2016_Client } from "#lib/Berechnungen/VerbrauchsausweisGewerbe/VerbrauchsausweisGewerbe_2016_Client.js"; import { benutzerSpeichern } from "#client/lib/speichern.js"; + import { benutzerLesen } from "#client/lib/lesen.js"; import { exclude } from "#lib/exclude.js"; export let user: Partial; @@ -240,7 +241,7 @@ } } - async function speichern() { + async function speichern(authuser = null) { loginAction = speichern; if (!await validateAccessTokenClient()) { loginOverlayHidden = false; @@ -256,7 +257,11 @@ } else { result = await ausweisSpeichern(ausweis, objekt, aufnahme, bilder, unterlagen, ausweisart) } - + + if (authuser) { + user = await benutzerLesen(authuser.id); + } + let resultUser: Awaited> | Awaited> | null = null; const { passwort, ...baseUser } = impersonatedUser ?? user; @@ -289,7 +294,7 @@ } } - async function bestellen() { + async function bestellen(authuser = null) { if (!form.checkValidity()) { addNotification({ dismissable: true, @@ -332,6 +337,10 @@ } } + if (authuser) { + user = await benutzerLesen(authuser.id); + } + let resultUser: Awaited> | Awaited> | null = null; const { passwort, ...baseUser } = impersonatedUser ?? user; @@ -1104,7 +1113,7 @@ sm:grid-cols-[min-content_min-content_min-content] sm:justify-self-end sm:mt-8" - + {#if rechnung && rechnung.status === "paid"} @@ -1112,7 +1121,7 @@ sm:grid-cols-[min-content_min-content_min-content] sm:justify-self-end sm:mt-8" class="order-1 sm:order-2 button cursor-pointer" data-cy="bestellen" type="button" - on:click={bestellen}>Absenden bestellen()}>Absenden {:else} {#if gegAnfrage} @@ -1127,7 +1136,7 @@ sm:grid-cols-[min-content_min-content_min-content] sm:justify-self-end sm:mt-8" class="order-1 sm:order-2 button cursor-pointer" data-cy="bestellen" type="button" - on:click={bestellen}>Kostenpflichtig bestellen bestellen()}>Kostenpflichtig bestellen {/if} {/if} diff --git a/src/pages/api/user/index.ts b/src/pages/api/user/index.ts index 43050ab0..ba9f3cbc 100644 --- a/src/pages/api/user/index.ts +++ b/src/pages/api/user/index.ts @@ -38,7 +38,7 @@ export const POST = defineApiRoute({ if (input.telefon) updateData.telefon = input.telefon; if (input.verified) updateData.telefon = input.verified; - //Admin may update other users + //Only Admin can update other users if (user.rolle == Enums.BenutzerRolle.ADMIN && input.id != user.id) { updateData.id = input.id; } else if(user.rolle != Enums.BenutzerRolle.ADMIN && input.id != user.id){ @@ -64,9 +64,14 @@ export const GET = defineApiRoute({ email: z.string() })), output: z.array(BenutzerSchema), - middleware: adminMiddleware, + middleware: authorizationMiddleware, async fetch(input, context, admin) { if ("id" in input) { + //Only Admin can read other users + if (admin.rolle != Enums.BenutzerRolle.ADMIN && input.id != admin.id) { + return; + } + const user = await prisma.benutzer.findUnique({ where: { id: input.id @@ -79,6 +84,11 @@ export const GET = defineApiRoute({ return [user]; } else { + //Only admin can read many users + if (admin.rolle != Enums.BenutzerRolle.ADMIN ) { + return; + } + const users = await prisma.benutzer.findMany({ where: { email: {