Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions apps/webapp/app/routes/@.runs.$runParam.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { redirect, type LoaderFunctionArgs } from "@remix-run/server-runtime";
import { z } from "zod";
import { prisma } from "~/db.server";
import { redirectWithErrorMessage } from "~/models/message.server";
import { requireUser } from "~/services/session.server";
import { impersonate, rootPath, v3RunPath } from "~/utils/pathBuilder";

const ParamsSchema = z.object({
runParam: z.string(),
});

export async function loader({ params, request }: LoaderFunctionArgs) {
const user = await requireUser(request);

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

const isAdmin = user.admin || user.isImpersonating;

if (!isAdmin) {
return redirectWithErrorMessage(
rootPath(),
request,
"You're not an admin and cannot impersonate",
{
ephemeral: false,
}
);
}

const run = await prisma.taskRun.findFirst({
where: {
friendlyId: runParam,
},
select: {
runtimeEnvironment: {
select: {
slug: true,
},
},
project: {
select: {
slug: true,
organization: {
select: {
slug: true,
},
},
},
},
},
});

if (!run) {
return redirectWithErrorMessage(rootPath(), request, "Run doesn't exist", {
ephemeral: false,
});
}

const path = v3RunPath(
{ slug: run.project.organization.slug },
{ slug: run.project.slug },
{ slug: run.runtimeEnvironment.slug },
{ friendlyId: runParam }
);

return redirect(impersonate(path));
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ import { requireUser } from "~/services/session.server";

export async function loader({ request, params }: LoaderFunctionArgs) {
const user = await requireUser(request);

// If already impersonating, we need to clear the impersonation
if (user.isImpersonating) {
const url = new URL(request.url);
return clearImpersonation(request, url.pathname);
}

// Only admins can impersonate
if (!user.admin) {
return redirect("/");
}
Expand Down
14 changes: 7 additions & 7 deletions apps/webapp/app/routes/runs.$runParam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ export async function loader({ params, request }: LoaderFunctionArgs) {
);
}

return redirect(
v3RunPath(
{ slug: run.project.organization.slug },
{ slug: run.project.slug },
{ slug: run.runtimeEnvironment.slug },
{ friendlyId: runParam }
)
const path = v3RunPath(
{ slug: run.project.organization.slug },
{ slug: run.project.slug },
{ slug: run.runtimeEnvironment.slug },
{ friendlyId: runParam }
);

return redirect(path);
}
5 changes: 5 additions & 0 deletions apps/webapp/app/utils/pathBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ export function rootPath() {
return `/`;
}

/** Given a path, it makes it an impersonation path */
export function impersonate(path: string) {
return `/@${path}`;
}

export function accountPath() {
return `/account`;
}
Expand Down