e7795a06ad
* remove pgadmin * remove V3_ENABLED * v3 is always enabled * enfore docker machine presets by default * rename autoremove env var * prefix more k8s-specific env vars * same prefix for all docker settings * improve profile switcher copy * supervisor can load token from file * optional webapp worker group bootstrap * fix error message * fix app origin fallback for otlp endpoint * use pnpm cache for webapp docker builds * increase default org and env concurrency limit to 100 * optional machine preset overrides * improve s3 pre-signing errors * fix DOCKER_ENFORCE_MACHINE_PRESETS bool coercion * shard unit tests * fix for s3-compatible services * optional object store region * Update apps/supervisor/src/workerToken.ts Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * fix DEPLOY_REGISTRY_HOST example * fix platform mock * remove remaining v3Enabled refs * fix error type.. bad bot --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
231 lines
4.9 KiB
TypeScript
231 lines
4.9 KiB
TypeScript
import { redirect } from "@remix-run/server-runtime";
|
|
import { prisma } from "~/db.server";
|
|
import { SearchParams } from "~/routes/admin._index";
|
|
import {
|
|
clearImpersonationId,
|
|
commitImpersonationSession,
|
|
setImpersonationId,
|
|
} from "~/services/impersonation.server";
|
|
import { requireUser } from "~/services/session.server";
|
|
|
|
const pageSize = 20;
|
|
|
|
export async function adminGetUsers(userId: string, { page, search }: SearchParams) {
|
|
page = page || 1;
|
|
|
|
search = search ? decodeURIComponent(search) : undefined;
|
|
|
|
const user = await prisma.user.findUnique({
|
|
where: {
|
|
id: userId,
|
|
},
|
|
});
|
|
|
|
if (user?.admin !== true) {
|
|
throw new Error("Unauthorized");
|
|
}
|
|
|
|
const users = await prisma.user.findMany({
|
|
select: {
|
|
id: true,
|
|
name: true,
|
|
email: true,
|
|
admin: true,
|
|
createdAt: true,
|
|
displayName: true,
|
|
orgMemberships: {
|
|
select: {
|
|
organization: {
|
|
select: {
|
|
title: true,
|
|
slug: true,
|
|
deletedAt: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
where: search
|
|
? {
|
|
OR: [
|
|
{
|
|
name: {
|
|
contains: search,
|
|
mode: "insensitive",
|
|
},
|
|
},
|
|
{
|
|
email: {
|
|
contains: search,
|
|
mode: "insensitive",
|
|
},
|
|
},
|
|
{
|
|
orgMemberships: {
|
|
some: {
|
|
organization: {
|
|
title: {
|
|
contains: search,
|
|
mode: "insensitive",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
orgMemberships: {
|
|
some: {
|
|
organization: {
|
|
slug: {
|
|
contains: search,
|
|
mode: "insensitive",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: undefined,
|
|
orderBy: {
|
|
createdAt: "desc",
|
|
},
|
|
take: pageSize,
|
|
skip: (page - 1) * pageSize,
|
|
});
|
|
|
|
const totalUsers = await prisma.user.count();
|
|
|
|
return {
|
|
users,
|
|
page,
|
|
pageCount: Math.ceil(totalUsers / pageSize),
|
|
filters: {
|
|
search,
|
|
},
|
|
};
|
|
}
|
|
|
|
export async function adminGetOrganizations(userId: string, { page, search }: SearchParams) {
|
|
page = page || 1;
|
|
|
|
search = search ? decodeURIComponent(search) : undefined;
|
|
|
|
const user = await prisma.user.findUnique({
|
|
where: {
|
|
id: userId,
|
|
},
|
|
});
|
|
|
|
if (user?.admin !== true) {
|
|
throw new Error("Unauthorized");
|
|
}
|
|
|
|
const organizations = await prisma.organization.findMany({
|
|
select: {
|
|
id: true,
|
|
slug: true,
|
|
title: true,
|
|
v2Enabled: true,
|
|
v3Enabled: true,
|
|
deletedAt: true,
|
|
members: {
|
|
select: {
|
|
user: {
|
|
select: {
|
|
email: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
where: search
|
|
? {
|
|
OR: [
|
|
{
|
|
members: {
|
|
some: {
|
|
user: {
|
|
name: {
|
|
contains: search,
|
|
mode: "insensitive",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
members: {
|
|
some: {
|
|
user: {
|
|
email: {
|
|
contains: search,
|
|
mode: "insensitive",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
slug: {
|
|
contains: search,
|
|
mode: "insensitive",
|
|
},
|
|
},
|
|
{
|
|
title: {
|
|
contains: search,
|
|
mode: "insensitive",
|
|
},
|
|
},
|
|
{
|
|
id: {
|
|
contains: search,
|
|
mode: "insensitive",
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: undefined,
|
|
orderBy: {
|
|
createdAt: "desc",
|
|
},
|
|
take: pageSize,
|
|
skip: (page - 1) * pageSize,
|
|
});
|
|
|
|
const totalOrgs = await prisma.organization.count();
|
|
|
|
return {
|
|
organizations,
|
|
page,
|
|
pageCount: Math.ceil(totalOrgs / pageSize),
|
|
filters: {
|
|
search,
|
|
},
|
|
};
|
|
}
|
|
|
|
export async function redirectWithImpersonation(request: Request, userId: string, path: string) {
|
|
const user = await requireUser(request);
|
|
if (!user.admin) {
|
|
throw new Error("Unauthorized");
|
|
}
|
|
|
|
const session = await setImpersonationId(userId, request);
|
|
|
|
return redirect(path, {
|
|
headers: { "Set-Cookie": await commitImpersonationSession(session) },
|
|
});
|
|
}
|
|
|
|
export async function clearImpersonation(request: Request, path: string) {
|
|
const session = await clearImpersonationId(request);
|
|
|
|
return redirect(path, {
|
|
headers: {
|
|
"Set-Cookie": await commitImpersonationSession(session),
|
|
},
|
|
});
|
|
}
|