97 lines
4.1 KiB
TypeScript
97 lines
4.1 KiB
TypeScript
import { redirect, useLoaderData, useLocation, useNavigate } from "react-router";
|
|
import type { LoaderFunctionArgs, MetaFunction } from "react-router";
|
|
import { useTranslation } from "react-i18next";
|
|
import ButtonTertiary from "~/components/ui/buttons/ButtonTertiary";
|
|
import { ErrorPage } from "~/components/ui/errors/ErrorPage";
|
|
import RefreshIcon from "~/components/ui/icons/RefreshIcon";
|
|
import { permissionDisplayDescription, permissionDisplayName } from "~/components/core/roles/roleDisplay";
|
|
import { getTranslations } from "~/locale/i18next.server";
|
|
import { safeRedirectPathFromRequest } from "~/utils/app/RedirectSafety.server";
|
|
import { getPermissionName } from "~/utils/db/permissions/permissions.db.server";
|
|
import { getUserPermission } from "~/utils/helpers/.server/PermissionsService";
|
|
import { getUserInfo } from "~/utils/session.server";
|
|
import { getTenantIdFromUrl } from "~/utils/services/.server/urlService";
|
|
|
|
type LoaderData = {
|
|
title: string;
|
|
heading: string;
|
|
permission: { name: string; description: string };
|
|
};
|
|
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
|
const { t } = await getTranslations(request);
|
|
const permission = await getPermissionName(params.permission ?? "");
|
|
const searchParams = new URLSearchParams(new URL(request.url).search);
|
|
const redirectTo = safeRedirectPathFromRequest(request, searchParams.get("redirect")?.toString());
|
|
if (searchParams.has("redirect")) {
|
|
if (!permission) {
|
|
throw redirect(redirectTo);
|
|
}
|
|
const userInfo = await getUserInfo(request);
|
|
const tenantId = await getTenantIdFromUrl(params);
|
|
const { userPermission } = await getUserPermission({ userId: userInfo.userId, permissionName: permission.name, tenantId });
|
|
if (userPermission) {
|
|
// Auto-redirecting back to the denied page caused an infinite redirect loop
|
|
// whenever the route gate (cached getPermissionsByUser) and this re-check
|
|
// (fresh findPermissionByUser) disagreed for the same user/permission/tenant.
|
|
// Mirror the global /unauthorized route and render the 401 page instead; the
|
|
// user can retry via the "Re-check permission" control.
|
|
// throw redirect(redirectTo);
|
|
}
|
|
} else if (!permission) {
|
|
throw redirect("/404?error=permission-not-found");
|
|
}
|
|
|
|
const data: LoaderData = {
|
|
title: `${t("shared.unauthorized")} | ${process.env.APP_NAME}`,
|
|
heading: t("shared.unauthorized"),
|
|
permission,
|
|
};
|
|
return data;
|
|
};
|
|
|
|
export const meta: MetaFunction<typeof loader> = ({ data }) => [{ title: data?.title }];
|
|
|
|
function PermissionDetails({ permission }: { permission: LoaderData["permission"] }) {
|
|
const { t } = useTranslation();
|
|
return (
|
|
<div className="text-muted-foreground mx-auto w-full max-w-md text-left text-base">
|
|
<div className="border-border bg-secondary flex justify-start space-y-2 border border-dashed p-4 dark:border-gray-700 dark:bg-gray-900">
|
|
<div className="space-y-2">
|
|
<div className="font-bold">{permissionDisplayDescription(permission, t) ?? permissionDisplayName(permission, t)}</div>
|
|
<div>
|
|
<span>{t("pages.unauthorized.permission")} →</span> <span className="font-light italic">{permission.name}</span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export default function Page401() {
|
|
const location = useLocation();
|
|
const navigate = useNavigate();
|
|
const { permission, heading } = useLoaderData<LoaderData>();
|
|
const { t } = useTranslation();
|
|
|
|
return (
|
|
<ErrorPage
|
|
status={401}
|
|
title={heading}
|
|
withGoBack={false}
|
|
diagnosticChildren={<PermissionDetails permission={permission} />}
|
|
actionSlot={
|
|
<div className="mx-auto mt-4 flex max-w-md items-center justify-between space-x-2">
|
|
<ButtonTertiary type="button" onClick={() => navigate(-1)}>
|
|
<div> ←</div>
|
|
<div>{t("shared.goBack")}</div>
|
|
</ButtonTertiary>
|
|
<ButtonTertiary type="button" onClick={() => navigate(location)}>
|
|
<div>{t("pages.unauthorized.recheckPermission")}</div>
|
|
<RefreshIcon className="h-4 w-4" />
|
|
</ButtonTertiary>
|
|
</div>
|
|
}
|
|
/>
|
|
);
|
|
}
|