Track HiveGrid Cloud planning reference
This commit is contained in:
1 parent
b3d5ea6764
commit
e341c5938a
5784 files changed
+1104929
-3
No files matched your search
@@ -0,0 +1,105 @@
|
||||
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";
|
||||
|
||||
type LoaderData = {
|
||||
title: string;
|
||||
heading: string;
|
||||
permission: { name: string; description: string };
|
||||
};
|
||||
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
||||
const [
|
||||
{ getTranslations },
|
||||
{ getPermissionName },
|
||||
{ getUserPermission },
|
||||
{ getUserInfo },
|
||||
{ safeRedirectPathFromRequest },
|
||||
{ getTenantIdFromUrl },
|
||||
] = await Promise.all([
|
||||
import("~/locale/i18next.server"),
|
||||
import("~/utils/db/permissions/permissions.db.server"),
|
||||
import("~/utils/helpers/.server/PermissionsService"),
|
||||
import("~/utils/session.server"),
|
||||
import("~/utils/app/RedirectSafety.server"),
|
||||
import("~/utils/services/.server/urlService"),
|
||||
]);
|
||||
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>
|
||||
}
|
||||
/>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user