Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
+58
-62
@@ -1,81 +1,77 @@
|
||||
import { DefaultAdminRoles } from "~/application/dtos/shared/DefaultAdminRoles";
|
||||
import { DefaultPermission } from "~/application/dtos/shared/DefaultPermissions";
|
||||
import { TimeFunction } from "~/modules/metrics/services/.server/MetricTracker";
|
||||
import OnboardingService from "~/modules/onboarding/services/OnboardingService";
|
||||
import UrlUtils from "~/utils/app/UrlUtils";
|
||||
import { getMyGroups } from "~/utils/db/permissions/groups.db.server";
|
||||
import { getAllRolesWithoutPermissions } from "~/utils/db/permissions/roles.db.server";
|
||||
import { getPermissionsByUser, getUserRoleInAdmin } from "~/utils/db/permissions/userRoles.db.server";
|
||||
import { getTenantsForUser as getMyTenants } from "@hivegrid/db/auth-boundary.server";
|
||||
import { getAllTenantTypes } from "~/utils/db/tenants/tenantTypes.db.server";
|
||||
import { getUserTotpStatus } from "~/utils/db/users.db.server";
|
||||
import { enforceAdminMfa } from "~/utils/auth/mfa/enforcement.server";
|
||||
import { checkMfaStepUpState } from "~/utils/auth/mfa/step-up.server";
|
||||
import { promiseHash } from "~/utils/promises/promiseHash";
|
||||
import { getUserInfo, getUserSession } from "~/utils/session.server";
|
||||
import { AdminLoaderData } from "../useAdminData";
|
||||
import { getAuthUserWithoutPasswordById as getUser } from "@hivegrid/db/auth-boundary.server";
|
||||
import { TFunction } from "i18next";
|
||||
import type { TFunction } from "i18next";
|
||||
import { redirect } from "react-router";
|
||||
import { NOT_FOUND_ENVELOPE } from "~/utils/errors.server";
|
||||
|
||||
export async function loadAdminData({ request, t }: { request: Request; t: TFunction }, time: TimeFunction) {
|
||||
import { adminGetAllActiveTenants } from "@hivegrid/db/auth-boundary.server";
|
||||
import { listAdminPermissionNames } from "@hivegrid/db/admin-rbac.server";
|
||||
import { runWithRlsContext } from "@hivegrid/db/rls-context.server";
|
||||
|
||||
import type { DefaultPermission } from "~/application/dtos/shared/DefaultPermissions";
|
||||
import type { TimeFunction } from "~/modules/metrics/services/.server/MetricTracker";
|
||||
import UrlUtils from "~/utils/app/UrlUtils";
|
||||
import { db } from "~/utils/db.server";
|
||||
import { NOT_FOUND_ENVELOPE } from "~/utils/errors.server";
|
||||
import { getUserInfo, getValidatedServerSession } from "~/utils/session.server";
|
||||
import type { AdminLoaderData } from "../useAdminData";
|
||||
|
||||
export async function loadAdminData({ request, t: _t }: { request: Request; t: TFunction }, time: TimeFunction) {
|
||||
const userInfo = await time(getUserInfo(request), "getUserInfo");
|
||||
const url = new URL(request.url);
|
||||
if (UrlUtils.stripTrailingSlash(url.pathname) === `/admin`) {
|
||||
if (UrlUtils.stripTrailingSlash(url.pathname) === "/admin") {
|
||||
throw Response.json(NOT_FOUND_ENVELOPE, { status: 404 });
|
||||
}
|
||||
const user = await time(getUser(userInfo?.userId), "getUser");
|
||||
const redirectTo = url.pathname + url.search;
|
||||
if (!userInfo || !user) {
|
||||
let searchParams = new URLSearchParams([["redirect", redirectTo]]);
|
||||
if (!userInfo.userId) {
|
||||
const searchParams = new URLSearchParams([["redirect", url.pathname + url.search]]);
|
||||
throw redirect(`/login?${searchParams}`);
|
||||
}
|
||||
|
||||
if (!user.admin) {
|
||||
throw Response.json({ error: t("admin.errors.adminOnly") }, { status: 401 });
|
||||
const validatedSession = await time(getValidatedServerSession(request, userInfo), "getValidatedAdminSession");
|
||||
if (!validatedSession || !("adminUser" in validatedSession)) {
|
||||
throw Response.json(NOT_FOUND_ENVELOPE, { status: 404 });
|
||||
}
|
||||
const admin = validatedSession.adminUser;
|
||||
|
||||
const session = await time(getUserSession(request), "getUserSession");
|
||||
const totpStatus = await time(getUserTotpStatus(user.id), "getUserTotpStatus");
|
||||
const stepUp = checkMfaStepUpState(session, user.id);
|
||||
const mfaEnforcement = enforceAdminMfa({
|
||||
requestUrl: url,
|
||||
userId: user.id,
|
||||
isAdmin: Boolean(user.admin),
|
||||
totpStatus,
|
||||
stepUp,
|
||||
redirectMode: "host-aware",
|
||||
});
|
||||
if (!mfaEnforcement.allowed) {
|
||||
throw redirect(mfaEnforcement.redirectTo);
|
||||
}
|
||||
|
||||
const myTenants = await time(getMyTenants(user.id), "getMyTenants");
|
||||
|
||||
const { allPermissions, superAdminRole, allRoles, myGroups, onboardingSession } = await time(
|
||||
promiseHash({
|
||||
allPermissions: getPermissionsByUser(userInfo.userId, null),
|
||||
superAdminRole: getUserRoleInAdmin(userInfo.userId, DefaultAdminRoles.SuperAdmin),
|
||||
allRoles: getAllRolesWithoutPermissions("admin"),
|
||||
myGroups: getMyGroups(user.id, null),
|
||||
onboardingSession: OnboardingService.getUserActiveOnboarding({ userId: user.id, tenantId: null, request }),
|
||||
}),
|
||||
const { permissions, roles, myTenants } = await time(
|
||||
runWithRlsContext({ userId: admin.id }, async () => ({
|
||||
permissions: await listAdminPermissionNames(admin.id),
|
||||
roles: await db.adminRole.findMany({
|
||||
where: { active: true },
|
||||
select: { id: true, name: true, description: true },
|
||||
orderBy: { name: "asc" },
|
||||
}),
|
||||
myTenants: await adminGetAllActiveTenants(),
|
||||
})),
|
||||
"loadAdminData.getDetails"
|
||||
);
|
||||
const isSuperAdmin = await runWithRlsContext({ userId: admin.id }, () =>
|
||||
db.adminUserRole.findFirst({
|
||||
where: { adminUserId: admin.id, role: { systemKey: "super_admin", active: true } },
|
||||
select: { adminUserId: true },
|
||||
})
|
||||
);
|
||||
|
||||
const data: AdminLoaderData = {
|
||||
user,
|
||||
user: {
|
||||
id: admin.id,
|
||||
email: admin.email,
|
||||
firstName: admin.firstName,
|
||||
lastName: admin.lastName,
|
||||
avatar: admin.avatar,
|
||||
phone: null,
|
||||
admin: { userId: admin.id },
|
||||
defaultTenantId: null,
|
||||
createdAt: validatedSession.createdAt,
|
||||
locale: admin.locale,
|
||||
lightOrDarkMode: null,
|
||||
},
|
||||
myTenants,
|
||||
currentTenant: null,
|
||||
// roles,
|
||||
allRoles,
|
||||
permissions: allPermissions.map((f) => f as DefaultPermission),
|
||||
isSuperUser: !!superAdminRole,
|
||||
isSuperAdmin: !!superAdminRole,
|
||||
myGroups,
|
||||
lng: user?.locale ?? userInfo.lng,
|
||||
onboardingSession,
|
||||
tenantTypes: await getAllTenantTypes(),
|
||||
allRoles: roles,
|
||||
permissions: permissions as DefaultPermission[],
|
||||
isSuperUser: isSuperAdmin !== null,
|
||||
isSuperAdmin: isSuperAdmin !== null,
|
||||
myGroups: [],
|
||||
lng: admin.locale ?? userInfo.lng,
|
||||
onboardingSession: null,
|
||||
};
|
||||
return data;
|
||||
}
|
||||
Reference in new issue
Block a user