mirror of
https://github.com/fosrl/pangolin.git
synced 2026-09-03 17:59:29 +02:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d2936cd10b |
@@ -1424,6 +1424,15 @@
|
||||
"logoutError": "Error logging out",
|
||||
"signingAs": "Signed in as",
|
||||
"serverAdmin": "Server Admin",
|
||||
"promoteServerAdmin": "Promote to Server admin",
|
||||
"promoteServerAdminTitle": "Promote to Server Admin",
|
||||
"promoteServerAdminQuestion": "Are you sure you want to promote {selectedUser} to server admin?",
|
||||
"promoteServerAdminMessage": "Server admins have full access to every organization, user, and setting on this instance.",
|
||||
"promoteServerAdminWarning": "You cannot demote a server admin from this page.",
|
||||
"promoteServerAdminConfirm": "Promote to server admin",
|
||||
"promoteServerAdminSuccess": "User promoted",
|
||||
"promoteServerAdminSuccessDescription": "{selectedUser} is now a server admin.",
|
||||
"promoteServerAdminError": "Failed to promote user",
|
||||
"managedSelfhosted": "Managed Self-Hosted",
|
||||
"otpEnable": "Enable Two-factor",
|
||||
"otpDisable": "Disable Two-factor",
|
||||
|
||||
@@ -1378,6 +1378,12 @@ if (build !== "saas") {
|
||||
user.adminGeneratePasswordResetCode
|
||||
);
|
||||
|
||||
authenticated.post(
|
||||
"/user/:userId/promote-server-admin",
|
||||
verifyUserIsServerAdmin,
|
||||
user.adminPromoteServerAdmin
|
||||
);
|
||||
|
||||
authenticated.delete(
|
||||
"/user/:userId",
|
||||
verifyUserIsServerAdmin,
|
||||
|
||||
@@ -4,7 +4,7 @@ import { db, idp, users } from "@server/db";
|
||||
import response from "@server/lib/response";
|
||||
import HttpCode from "@server/types/HttpCode";
|
||||
import createHttpError from "http-errors";
|
||||
import { and, asc, desc, eq, like, or, sql } from "drizzle-orm";
|
||||
import { and, asc, desc, eq, like, or, sql, type SQL } from "drizzle-orm";
|
||||
import logger from "@server/logger";
|
||||
import { fromZodError } from "zod-validation-error";
|
||||
import { OpenAPITags, registry } from "@server/openApi";
|
||||
@@ -196,7 +196,7 @@ export async function adminListUsers(
|
||||
}
|
||||
}
|
||||
|
||||
const conditions = [eq(users.serverAdmin, false)];
|
||||
const conditions: Array<SQL<unknown> | undefined> = [];
|
||||
|
||||
if (query) {
|
||||
const q = "%" + query.toLowerCase() + "%";
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
import { Request, Response, NextFunction } from "express";
|
||||
import { z } from "zod";
|
||||
import { db, users } from "@server/db";
|
||||
import { eq } from "drizzle-orm";
|
||||
import response from "@server/lib/response";
|
||||
import HttpCode from "@server/types/HttpCode";
|
||||
import createHttpError from "http-errors";
|
||||
import logger from "@server/logger";
|
||||
import { fromError } from "zod-validation-error";
|
||||
import { OpenAPITags, registry } from "@server/openApi";
|
||||
import { createApiResponseSchema } from "@server/lib/openapi/createApiResponseSchema";
|
||||
|
||||
const promoteServerAdminParamsSchema = z.strictObject({
|
||||
userId: z.string()
|
||||
});
|
||||
|
||||
export type AdminPromoteServerAdminResponse = {
|
||||
userId: string;
|
||||
serverAdmin: boolean;
|
||||
};
|
||||
|
||||
const AdminPromoteServerAdminResponseDataSchema = z.object({
|
||||
userId: z.string(),
|
||||
serverAdmin: z.boolean()
|
||||
});
|
||||
|
||||
registry.registerPath({
|
||||
method: "post",
|
||||
path: "/user/{userId}/promote-server-admin",
|
||||
description: "Promote a user to server admin (server admin).",
|
||||
tags: [OpenAPITags.User],
|
||||
request: {
|
||||
params: promoteServerAdminParamsSchema
|
||||
},
|
||||
responses: {
|
||||
200: {
|
||||
description: "Successful response",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: createApiResponseSchema(
|
||||
AdminPromoteServerAdminResponseDataSchema
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
export async function adminPromoteServerAdmin(
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
): Promise<any> {
|
||||
try {
|
||||
const parsedParams = promoteServerAdminParamsSchema.safeParse(
|
||||
req.params
|
||||
);
|
||||
if (!parsedParams.success) {
|
||||
return next(
|
||||
createHttpError(
|
||||
HttpCode.BAD_REQUEST,
|
||||
fromError(parsedParams.error).toString()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
const { userId } = parsedParams.data;
|
||||
|
||||
const [existingUser] = await db
|
||||
.select({
|
||||
userId: users.userId,
|
||||
serverAdmin: users.serverAdmin
|
||||
})
|
||||
.from(users)
|
||||
.where(eq(users.userId, userId))
|
||||
.limit(1);
|
||||
|
||||
if (!existingUser) {
|
||||
return next(createHttpError(HttpCode.NOT_FOUND, "User not found"));
|
||||
}
|
||||
|
||||
if (existingUser.serverAdmin) {
|
||||
return next(
|
||||
createHttpError(
|
||||
HttpCode.BAD_REQUEST,
|
||||
"User is already a server admin"
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
logger.info(
|
||||
`Promoting user ${userId} to server admin (by ${req.user?.userId})`
|
||||
);
|
||||
|
||||
await db
|
||||
.update(users)
|
||||
.set({ serverAdmin: true })
|
||||
.where(eq(users.userId, userId));
|
||||
|
||||
return response<AdminPromoteServerAdminResponse>(res, {
|
||||
data: {
|
||||
userId: existingUser.userId,
|
||||
serverAdmin: true
|
||||
},
|
||||
success: true,
|
||||
error: false,
|
||||
message: "User promoted to server admin successfully",
|
||||
status: HttpCode.OK
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error(error);
|
||||
return next(
|
||||
createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred")
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ export * from "./adminListUsers";
|
||||
export * from "./adminRemoveUser";
|
||||
export * from "./adminGetUser";
|
||||
export * from "./adminGeneratePasswordResetCode";
|
||||
export * from "./adminPromoteServerAdmin";
|
||||
export * from "./listInvitations";
|
||||
export * from "./removeInvitation";
|
||||
export * from "./createOrgUser";
|
||||
|
||||
@@ -81,6 +81,9 @@ export default async function UsersPage(props: AdminUsersPageProps) {
|
||||
};
|
||||
});
|
||||
|
||||
console.log({
|
||||
userRows
|
||||
});
|
||||
return (
|
||||
<>
|
||||
<SettingsSectionTitle
|
||||
|
||||
@@ -19,7 +19,8 @@ import {
|
||||
ArrowRight,
|
||||
ArrowUp10Icon,
|
||||
ChevronsUpDownIcon,
|
||||
MoreHorizontal
|
||||
MoreHorizontal,
|
||||
ShieldUserIcon
|
||||
} from "lucide-react";
|
||||
import { useTranslations } from "next-intl";
|
||||
import { useRouter } from "next/navigation";
|
||||
@@ -43,6 +44,14 @@ import {
|
||||
CredenzaClose
|
||||
} from "@app/components/Credenza";
|
||||
import CopyToClipboard from "@app/components/CopyToClipboard";
|
||||
import { Badge } from "./ui/badge";
|
||||
import {
|
||||
Tooltip,
|
||||
TooltipContent,
|
||||
TooltipProvider,
|
||||
TooltipTrigger
|
||||
} from "./ui/tooltip";
|
||||
import { useUserContext } from "@app/hooks/useUserContext";
|
||||
|
||||
export type GlobalUserRow = {
|
||||
id: string;
|
||||
@@ -90,6 +99,9 @@ export default function UsersTable({
|
||||
const [passwordResetCodeData, setPasswordResetCodeData] =
|
||||
useState<AdminGeneratePasswordResetCodeResponse | null>(null);
|
||||
const [isGeneratingCode, setIsGeneratingCode] = useState(false);
|
||||
const [isPromoteModalOpen, setIsPromoteModalOpen] = useState(false);
|
||||
const [promoting, setPromoting] = useState<GlobalUserRow | null>(null);
|
||||
const user = useUserContext();
|
||||
|
||||
const [isRefreshing, startTransition] = useTransition();
|
||||
const {
|
||||
@@ -184,6 +196,37 @@ export default function UsersTable({
|
||||
}
|
||||
};
|
||||
|
||||
const promoteToServerAdmin = async (user: GlobalUserRow) => {
|
||||
try {
|
||||
await api.post(`/user/${user.id}/promote-server-admin`);
|
||||
|
||||
toast({
|
||||
title: t("promoteServerAdminSuccess"),
|
||||
description: t("promoteServerAdminSuccessDescription", {
|
||||
selectedUser: getUserDisplayName({
|
||||
email: user.email,
|
||||
name: user.name,
|
||||
username: user.username
|
||||
})
|
||||
})
|
||||
});
|
||||
|
||||
startTransition(() => {
|
||||
router.refresh();
|
||||
});
|
||||
} catch (e) {
|
||||
console.error(t("promoteServerAdminError"), e);
|
||||
toast({
|
||||
variant: "destructive",
|
||||
title: t("promoteServerAdminError"),
|
||||
description: formatAxiosError(e, t("promoteServerAdminError"))
|
||||
});
|
||||
} finally {
|
||||
setIsPromoteModalOpen(false);
|
||||
setPromoting(null);
|
||||
}
|
||||
};
|
||||
|
||||
function toggleSort(column: string) {
|
||||
const newSearch = getNextSortOrder(column, searchParams);
|
||||
filter({
|
||||
@@ -235,7 +278,35 @@ export default function UsersTable({
|
||||
<Icon className="ml-2 h-4 w-4" />
|
||||
</Button>
|
||||
);
|
||||
}
|
||||
},
|
||||
cell: ({ row }) => (
|
||||
<span className="inline-flex gap-1 items-center">
|
||||
{row.original.username}{" "}
|
||||
{row.original.id === user.user.userId && (
|
||||
<>
|
||||
<span className="text-muted-foreground">
|
||||
·
|
||||
</span>{" "}
|
||||
<span className="text-primary">you</span>
|
||||
</>
|
||||
)}
|
||||
{row.original.serverAdmin && (
|
||||
<>
|
||||
<TooltipProvider>
|
||||
<Tooltip>
|
||||
<TooltipTrigger asChild>
|
||||
<ShieldUserIcon className="text-primary size-5 flex-none" />
|
||||
</TooltipTrigger>
|
||||
<TooltipContent>
|
||||
{t("serverAdmin")}
|
||||
</TooltipContent>
|
||||
</Tooltip>
|
||||
</TooltipProvider>
|
||||
{/* <Badge>{t("serverAdmin")}</Badge> */}
|
||||
</>
|
||||
)}
|
||||
</span>
|
||||
)
|
||||
},
|
||||
{
|
||||
accessorKey: "email",
|
||||
@@ -369,11 +440,22 @@ export default function UsersTable({
|
||||
{t("generatePasswordResetCode")}
|
||||
</DropdownMenuItem>
|
||||
)}
|
||||
{!r.serverAdmin && (
|
||||
<DropdownMenuItem
|
||||
onClick={() => {
|
||||
setPromoting(r);
|
||||
setIsPromoteModalOpen(true);
|
||||
}}
|
||||
>
|
||||
{t("promoteServerAdmin")}
|
||||
</DropdownMenuItem>
|
||||
)}
|
||||
<DropdownMenuItem
|
||||
onClick={() => {
|
||||
setSelected(r);
|
||||
setIsDeleteModalOpen(true);
|
||||
}}
|
||||
className="text-red-400"
|
||||
>
|
||||
{t("delete")}
|
||||
</DropdownMenuItem>
|
||||
@@ -435,6 +517,42 @@ export default function UsersTable({
|
||||
/>
|
||||
)}
|
||||
|
||||
{promoting && (
|
||||
<ConfirmDeleteDialog
|
||||
open={isPromoteModalOpen}
|
||||
setOpen={(val) => {
|
||||
setIsPromoteModalOpen(val);
|
||||
if (!val) {
|
||||
setPromoting(null);
|
||||
}
|
||||
}}
|
||||
dialog={
|
||||
<div className="space-y-2">
|
||||
<p>
|
||||
{t("promoteServerAdminQuestion", {
|
||||
selectedUser: getUserDisplayName({
|
||||
email: promoting.email,
|
||||
name: promoting.name,
|
||||
username: promoting.username
|
||||
})
|
||||
})}
|
||||
</p>
|
||||
|
||||
<p>{t("promoteServerAdminMessage")}</p>
|
||||
</div>
|
||||
}
|
||||
buttonText={t("promoteServerAdminConfirm")}
|
||||
onConfirm={async () => promoteToServerAdmin(promoting)}
|
||||
string={getUserDisplayName({
|
||||
email: promoting.email,
|
||||
name: promoting.name,
|
||||
username: promoting.username
|
||||
})}
|
||||
warningText={t("promoteServerAdminWarning")}
|
||||
title={t("promoteServerAdminTitle")}
|
||||
/>
|
||||
)}
|
||||
|
||||
<ControlledDataTable
|
||||
columns={columns}
|
||||
rows={users}
|
||||
|
||||
@@ -155,13 +155,13 @@ export function CommandPalette({ orgId, orgs, navItems }: CommandPaletteProps) {
|
||||
}
|
||||
/>
|
||||
<CommandList className="max-h-118 min-h-0 h-(--cmdk-list-height) scroll-pb-4 scroll-pt-2 transition-[height] duration-250 ease-in-out">
|
||||
<CommandEmpty>{t("commandPaletteNoResults")}</CommandEmpty>
|
||||
|
||||
<CommandGroup
|
||||
heading={t("commandActionModeInfo")}
|
||||
className="[&_[cmdk-group-heading]]:text-sm"
|
||||
/>
|
||||
|
||||
<CommandEmpty>{t("commandPaletteNoResults")}</CommandEmpty>
|
||||
|
||||
{!isActionMode &&
|
||||
navigationGroups.map((group, groupIndex) => (
|
||||
<React.Fragment key={group.heading}>
|
||||
|
||||
Reference in New Issue
Block a user