169 lines
7.0 KiB
TypeScript
169 lines
7.0 KiB
TypeScript
import { Role } from "@toolctrl/db/prisma-browser";
|
|
import { ActionFunctionArgs, LoaderFunctionArgs, redirect, useActionData, useLoaderData } from "react-router";
|
|
import { Form, useNavigate, useNavigation } from "react-router";
|
|
import { useState } from "react";
|
|
import { useTranslation } from "react-i18next";
|
|
import { DefaultAdminRoles } from "~/application/dtos/shared/DefaultAdminRoles";
|
|
import { roleDisplayDescription, roleDisplayName } from "~/components/core/roles/roleDisplay";
|
|
import ButtonSecondary from "~/components/ui/buttons/ButtonSecondary";
|
|
import LoadingButton from "~/components/ui/buttons/LoadingButton";
|
|
import InputCheckboxWithDescription from "~/components/ui/input/InputCheckboxWithDescription";
|
|
import ActionResultModal from "~/components/ui/modals/ActionResultModal";
|
|
import { db } from "~/utils/db.server";
|
|
import { RoleWithPermissions, getAllRoles, getRoles } from "~/utils/db/permissions/roles.db.server";
|
|
import { UserWithDetails, getUserByEmail, getUserByEmailWithDetails } from "~/utils/db/users.db.server";
|
|
import { verifyUserHasPermission } from "~/utils/helpers/.server/PermissionsService";
|
|
import { setUserRoles } from "~/utils/services/userService";
|
|
import { getUserInfo, validateCSRFToken } from "~/utils/session.server";
|
|
import { adminPath } from "~/utils/app/CanonicalUrlUtils";
|
|
import { getTranslations } from "~/locale/i18next.server";
|
|
|
|
type LoaderData = {
|
|
user: UserWithDetails;
|
|
adminRoles: RoleWithPermissions[];
|
|
};
|
|
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
|
await verifyUserHasPermission(request, "admin.roles.set");
|
|
const user = await getUserByEmailWithDetails(params.user!);
|
|
if (!user) {
|
|
return redirect(adminPath("accounts/users", { mode: "host-aware" }));
|
|
}
|
|
const data: LoaderData = {
|
|
user,
|
|
adminRoles: await getAllRoles("admin"),
|
|
};
|
|
return data;
|
|
};
|
|
|
|
type ActionData = {
|
|
error?: string;
|
|
success?: string;
|
|
};
|
|
export const action = async ({ request, params }: ActionFunctionArgs) => {
|
|
await verifyUserHasPermission(request, "admin.roles.set");
|
|
const userInfo = await getUserInfo(request);
|
|
const { t } = await getTranslations(request);
|
|
|
|
await validateCSRFToken(request);
|
|
const form = await request.formData();
|
|
const action = form.get("action")?.toString();
|
|
const user = await getUserByEmail(params.user);
|
|
|
|
if (!user) {
|
|
return redirect(adminPath("accounts/users", { mode: "host-aware" }));
|
|
}
|
|
|
|
if (action === "set-user-roles") {
|
|
const arrRoles: { id: string; tenantId: string | undefined }[] = form.getAll("roles[]").map((f: FormDataEntryValue) => {
|
|
return JSON.parse(f.toString());
|
|
});
|
|
const allRoles = await getRoles(arrRoles.map((r) => r.id));
|
|
let setRoles: { role: Role; tenantId: string | null }[] = [];
|
|
for (const { id, tenantId } of arrRoles) {
|
|
const role = allRoles.find((r) => r.id === id);
|
|
if (!role) {
|
|
return Response.json({ error: t("admin.accounts.rolesAndPermissions.roles.errors.roleNotFoundWithId", { id }) }, { status: 400 });
|
|
}
|
|
setRoles.push({ role, tenantId: tenantId ?? null });
|
|
}
|
|
setRoles = setRoles.filter((v, i, a) => a.findIndex((t) => t.role.id === v.role.id) === i);
|
|
|
|
const isAdmin = setRoles.some((r) => r.role.type === "admin");
|
|
|
|
const countAdmins = await db.adminUser.count();
|
|
if (!isAdmin && countAdmins === 1) {
|
|
return Response.json({ error: t("admin.users.errors.cannotRemoveLastAdmin") }, { status: 400 });
|
|
}
|
|
const hasSuperAdmin = setRoles.some((r) => r.role.name === DefaultAdminRoles.SuperAdmin);
|
|
if (user.id === userInfo.userId && !hasSuperAdmin) {
|
|
return Response.json({ error: t("admin.users.errors.cannotRemoveOwnSuperAdmin") }, { status: 400 });
|
|
}
|
|
|
|
await setUserRoles({ user, roles: setRoles, isAdmin, type: "admin" });
|
|
|
|
return redirect(adminPath("accounts/users", { mode: "host-aware" }));
|
|
// return Response.json({ success: t("shared.updated") });
|
|
} else {
|
|
return Response.json({ error: t("shared.invalidForm") }, { status: 400 });
|
|
}
|
|
};
|
|
|
|
export default function SetUserRolesRoute() {
|
|
const data = useLoaderData<LoaderData>();
|
|
const actionData = useActionData<ActionData>();
|
|
|
|
const { t } = useTranslation();
|
|
const navigation = useNavigation();
|
|
const navigate = useNavigate();
|
|
|
|
const [selectedRoles, setSelectedRoles] = useState<string[]>(data.user.roles?.filter((f) => f.role.type === "admin").map((r) => r.role.id));
|
|
|
|
function hasSomeAdminRoles() {
|
|
return data.adminRoles.some((ar) => selectedRoles?.includes(ar.id));
|
|
}
|
|
function hasAllAdminRoles() {
|
|
return data.adminRoles.every((ar) => selectedRoles?.includes(ar.id));
|
|
}
|
|
|
|
return (
|
|
<div>
|
|
<Form method="post" className="inline-block w-full overflow-hidden p-1 text-left align-bottom sm:align-middle">
|
|
<input type="hidden" name="action" value="set-user-roles" hidden readOnly />
|
|
{selectedRoles?.map((role, idx) => {
|
|
return (
|
|
<input
|
|
key={idx}
|
|
type="hidden"
|
|
name="roles[]"
|
|
value={JSON.stringify({
|
|
id: role,
|
|
tenantId: undefined,
|
|
})}
|
|
/>
|
|
);
|
|
})}
|
|
|
|
<div className="border-border flex items-center justify-between space-x-2 border-b pb-2">
|
|
<div className="text-foreground/80 text-lg font-medium">{t("models.role.plural")}</div>
|
|
<div className="flex items-center space-x-2">
|
|
<ButtonSecondary disabled={!hasSomeAdminRoles()} onClick={() => setSelectedRoles([])}>
|
|
{t("shared.clear")}
|
|
</ButtonSecondary>
|
|
|
|
<ButtonSecondary disabled={hasAllAdminRoles()} onClick={() => setSelectedRoles(data.adminRoles.map((r) => r.id))}>
|
|
{t("shared.selectAll")}
|
|
</ButtonSecondary>
|
|
</div>
|
|
</div>
|
|
<div className="relative mt-1 rounded-md shadow-2xs">
|
|
{data.adminRoles?.map((role, idx) => (
|
|
<InputCheckboxWithDescription
|
|
key={idx}
|
|
name={role.name}
|
|
title={roleDisplayName(role, t)}
|
|
description={roleDisplayDescription(role, t)}
|
|
value={selectedRoles.includes(role.id)}
|
|
setValue={(e) => {
|
|
if (e) {
|
|
setSelectedRoles((f) => [...f, role.id]);
|
|
} else {
|
|
setSelectedRoles((f) => f.filter((f) => f !== role.id));
|
|
}
|
|
}}
|
|
/>
|
|
))}
|
|
</div>
|
|
<div className="mt-5 sm:mt-6 sm:grid sm:grid-flow-row-dense sm:grid-cols-2 sm:gap-3">
|
|
<ButtonSecondary type="button" className="w-full" onClick={() => navigate(adminPath("accounts/users", { mode: "host-aware" }))}>
|
|
<div className="w-full text-center">{t("shared.back")}</div>
|
|
</ButtonSecondary>
|
|
<LoadingButton type="submit" disabled={navigation.state === "submitting"} className="w-full">
|
|
{t("shared.save")}
|
|
</LoadingButton>
|
|
</div>
|
|
</Form>
|
|
<ActionResultModal actionData={actionData ?? undefined} />
|
|
</div>
|
|
);
|
|
}
|