Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
@@ -4,6 +4,8 @@ import { getOnboardingSession, OnboardingSessionWithDetails } from "~/modules/on
|
||||
import { OnboardingSessionActionDto } from "~/modules/onboarding/dtos/OnboardingSessionActionDto";
|
||||
import OnboardingSessionService from "~/modules/onboarding/services/OnboardingSessionService";
|
||||
import { getUserInfo, validateCSRFToken } from "~/utils/session.server";
|
||||
import { getTenantSessionIdentityForUser } from "~/utils/db/tenants.db.server";
|
||||
import { runWithRlsContext } from "@hivegrid/db/rls-context.server";
|
||||
|
||||
type LoaderData = {
|
||||
item: OnboardingSessionWithDetails | null;
|
||||
@@ -13,13 +15,12 @@ type ServerT = Awaited<ReturnType<typeof getTranslations>>["t"];
|
||||
|
||||
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
||||
const { t } = await getTranslations(request);
|
||||
const item = await getOnboardingSession(params.id!);
|
||||
const unauthorized = await requireOnboardingSessionOwner({ request, session: item, t });
|
||||
if (unauthorized) {
|
||||
throw unauthorized;
|
||||
const access = await getAuthorizedOnboardingSession({ request, sessionId: params.id ?? "", t });
|
||||
if (access instanceof Response) {
|
||||
throw access;
|
||||
}
|
||||
const data: LoaderData = {
|
||||
item,
|
||||
item: access.session,
|
||||
};
|
||||
return data;
|
||||
};
|
||||
@@ -27,20 +28,15 @@ export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
||||
export const action: ActionFunction = async ({ request, params }) => {
|
||||
const { t } = await getTranslations(request);
|
||||
const sessionId = params.id!;
|
||||
const session = await getOnboardingSession(sessionId);
|
||||
if (!session) {
|
||||
return Response.json({ error: t("onboarding.session.errors.notFound") }, { status: 404 });
|
||||
}
|
||||
|
||||
const userInfo = await getUserInfo(request);
|
||||
if (!userInfo.userId || session.userId !== userInfo.userId) {
|
||||
return Response.json({ error: t("onboarding.session.errors.unauthorized") }, { status: 401 });
|
||||
const access = await getAuthorizedOnboardingSession({ request, sessionId, t });
|
||||
if (access instanceof Response) {
|
||||
return access;
|
||||
}
|
||||
|
||||
try {
|
||||
await validateCSRFToken(request);
|
||||
} catch (error) {
|
||||
return Response.json({ error: error instanceof Error ? error.message : t("errors:invalidCsrfToken") }, { status: 400 });
|
||||
} catch {
|
||||
return Response.json({ error: t("errors:invalidCsrfToken") }, { status: 400 });
|
||||
}
|
||||
|
||||
const formData = await request.formData();
|
||||
@@ -48,57 +44,77 @@ export const action: ActionFunction = async ({ request, params }) => {
|
||||
const actions = formData.getAll("actions[]").map((action: FormDataEntryValue) => {
|
||||
return JSON.parse(action.toString()) as OnboardingSessionActionDto;
|
||||
});
|
||||
switch (actionName) {
|
||||
case "started": {
|
||||
await OnboardingSessionService.started({ session, request });
|
||||
break;
|
||||
return runWithRlsContext(
|
||||
{
|
||||
userId: access.userId,
|
||||
...(access.session.tenantId ? { tenantId: access.session.tenantId } : {}),
|
||||
},
|
||||
async () => {
|
||||
switch (actionName) {
|
||||
case "started": {
|
||||
await OnboardingSessionService.started({ session: access.session, request });
|
||||
break;
|
||||
}
|
||||
case "dismissed": {
|
||||
await OnboardingSessionService.dismissed({ session: access.session, request });
|
||||
break;
|
||||
}
|
||||
case "add-actions": {
|
||||
await OnboardingSessionService.addActions(access.session, { actions });
|
||||
break;
|
||||
}
|
||||
case "set-step": {
|
||||
await OnboardingSessionService.setStep(access.session, {
|
||||
fromIdx: Number(formData.get("fromIdx")),
|
||||
toIdx: Number(formData.get("toIdx")),
|
||||
actions,
|
||||
});
|
||||
break;
|
||||
}
|
||||
case "complete": {
|
||||
await OnboardingSessionService.complete({
|
||||
session: access.session,
|
||||
data: { fromIdx: Number(formData.get("fromIdx")), actions },
|
||||
request,
|
||||
});
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
return Response.json({ error: t("shared.invalidForm") }, { status: 400 });
|
||||
}
|
||||
}
|
||||
return Response.json({});
|
||||
}
|
||||
case "dismissed": {
|
||||
await OnboardingSessionService.dismissed({ session, request });
|
||||
break;
|
||||
}
|
||||
case "add-actions": {
|
||||
await OnboardingSessionService.addActions(session, { actions });
|
||||
break;
|
||||
}
|
||||
case "set-step": {
|
||||
await OnboardingSessionService.setStep(session, {
|
||||
fromIdx: Number(formData.get("fromIdx")),
|
||||
toIdx: Number(formData.get("toIdx")),
|
||||
actions,
|
||||
});
|
||||
break;
|
||||
}
|
||||
case "complete": {
|
||||
await OnboardingSessionService.complete({
|
||||
session,
|
||||
data: { fromIdx: Number(formData.get("fromIdx")), actions },
|
||||
request,
|
||||
});
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
return Response.json({ error: t("shared.invalidForm") }, { status: 400 });
|
||||
}
|
||||
}
|
||||
return Response.json({});
|
||||
);
|
||||
};
|
||||
|
||||
async function requireOnboardingSessionOwner({
|
||||
async function getAuthorizedOnboardingSession({
|
||||
request,
|
||||
session,
|
||||
sessionId,
|
||||
t,
|
||||
}: {
|
||||
request: Request;
|
||||
session: OnboardingSessionWithDetails | null;
|
||||
sessionId: string;
|
||||
t: ServerT;
|
||||
}): Promise<Response | undefined> {
|
||||
if (!session) {
|
||||
}): Promise<{ session: OnboardingSessionWithDetails; userId: string } | Response> {
|
||||
const userInfo = await getUserInfo(request);
|
||||
if (!userInfo.userId) {
|
||||
return Response.json({ error: t("onboarding.session.errors.unauthorized") }, { status: 401 });
|
||||
}
|
||||
|
||||
const session = await runWithRlsContext({ userId: userInfo.userId }, () => getOnboardingSession(sessionId));
|
||||
// FORCE RLS normally hides another user's resource. Keep the explicit owner
|
||||
// check so mocked clients and future policy regressions still fail closed.
|
||||
if (!session || session.userId !== userInfo.userId) {
|
||||
return Response.json({ error: t("onboarding.session.errors.notFound") }, { status: 404 });
|
||||
}
|
||||
|
||||
const userInfo = await getUserInfo(request);
|
||||
if (!userInfo.userId || session.userId !== userInfo.userId) {
|
||||
return Response.json({ error: t("onboarding.session.errors.unauthorized") }, { status: 401 });
|
||||
if (session.tenantId) {
|
||||
const tenant = await getTenantSessionIdentityForUser({ userId: userInfo.userId, tenant: session.tenantId });
|
||||
if (!tenant || tenant.id !== session.tenantId) {
|
||||
return Response.json({ error: t("onboarding.session.errors.notFound") }, { status: 404 });
|
||||
}
|
||||
}
|
||||
|
||||
return { session, userId: userInfo.userId };
|
||||
}
|
||||
Reference in new issue
Block a user