Skip to content

Commit a220314

Browse files
committed
Separate run impersonation into a separate @/runs/runid route
1 parent 9e7cabf commit a220314

File tree

2 files changed

+75
-12
lines changed

2 files changed

+75
-12
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { redirect, type LoaderFunctionArgs } from "@remix-run/server-runtime";
2+
import { z } from "zod";
3+
import { prisma } from "~/db.server";
4+
import { redirectWithErrorMessage } from "~/models/message.server";
5+
import { requireUser } from "~/services/session.server";
6+
import { impersonate, rootPath, v3RunPath } from "~/utils/pathBuilder";
7+
8+
const ParamsSchema = z.object({
9+
runParam: z.string(),
10+
});
11+
12+
export async function loader({ params, request }: LoaderFunctionArgs) {
13+
const user = await requireUser(request);
14+
15+
const { runParam } = ParamsSchema.parse(params);
16+
17+
const isAdmin = user.admin || user.isImpersonating;
18+
19+
if (!isAdmin) {
20+
return redirectWithErrorMessage(
21+
rootPath(),
22+
request,
23+
"You're not an admin and cannot impersonate",
24+
{
25+
ephemeral: false,
26+
}
27+
);
28+
}
29+
30+
const run = await prisma.taskRun.findFirst({
31+
where: {
32+
friendlyId: runParam,
33+
},
34+
select: {
35+
runtimeEnvironment: {
36+
select: {
37+
slug: true,
38+
},
39+
},
40+
project: {
41+
select: {
42+
slug: true,
43+
organization: {
44+
select: {
45+
slug: true,
46+
},
47+
},
48+
},
49+
},
50+
},
51+
});
52+
53+
if (!run) {
54+
return redirectWithErrorMessage(rootPath(), request, "Run doesn't exist", {
55+
ephemeral: false,
56+
});
57+
}
58+
59+
const path = v3RunPath(
60+
{ slug: run.project.organization.slug },
61+
{ slug: run.project.slug },
62+
{ slug: run.runtimeEnvironment.slug },
63+
{ friendlyId: runParam }
64+
);
65+
66+
return redirect(impersonate(path));
67+
}

apps/webapp/app/routes/runs.$runParam.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { z } from "zod";
33
import { prisma } from "~/db.server";
44
import { redirectWithErrorMessage } from "~/models/message.server";
55
import { requireUser } from "~/services/session.server";
6-
import { impersonate, rootPath, v3RunPath } from "~/utils/pathBuilder";
6+
import { rootPath, v3RunPath } from "~/utils/pathBuilder";
77

88
const ParamsSchema = z.object({
99
runParam: z.string(),
@@ -14,22 +14,18 @@ export async function loader({ params, request }: LoaderFunctionArgs) {
1414

1515
const { runParam } = ParamsSchema.parse(params);
1616

17-
const isAdmin = user.admin || user.isImpersonating;
18-
1917
const run = await prisma.taskRun.findFirst({
2018
where: {
2119
friendlyId: runParam,
22-
...(!isAdmin && {
23-
project: {
24-
organization: {
25-
members: {
26-
some: {
27-
userId: user.id,
28-
},
20+
project: {
21+
organization: {
22+
members: {
23+
some: {
24+
userId: user.id,
2925
},
3026
},
3127
},
32-
}),
28+
},
3329
},
3430
select: {
3531
runtimeEnvironment: {
@@ -68,5 +64,5 @@ export async function loader({ params, request }: LoaderFunctionArgs) {
6864
{ friendlyId: runParam }
6965
);
7066

71-
return redirect(isAdmin ? impersonate(path) : path);
67+
return redirect(path);
7268
}

0 commit comments

Comments
 (0)