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
+108
@@ -0,0 +1,108 @@
|
||||
import { CHAT_PERMISSIONS, type ChatPermission } from "@hivegrid/chat";
|
||||
import { CatalogError, foundationErrors, requirePermission } from "@hivegrid/foundation";
|
||||
|
||||
import { TenantUserStatus } from "~/application/enums/tenants/TenantUserStatus";
|
||||
import { tenantHasModule } from "~/utils/connector/tenantConnector.server";
|
||||
import { withTenantRLS } from "~/utils/db/.server/tenantRls.server";
|
||||
import { getPermissionsByUser } from "~/utils/db/permissions/userRoles.db.server";
|
||||
import { requireAuth } from "~/utils/loaders.middleware";
|
||||
import { getUserInfo } from "~/utils/session.server";
|
||||
import { getTenantIdFromUrl } from "~/utils/services/.server/urlService";
|
||||
|
||||
import { CHAT_MODULE_KEY } from "./chatRoutePaths";
|
||||
|
||||
export type ChatRouteContext = {
|
||||
tenantId: string;
|
||||
userId: string;
|
||||
tenantUserId: string;
|
||||
permissions: readonly string[];
|
||||
hasModule: true;
|
||||
};
|
||||
|
||||
export type ChatChannelAccessSnapshot = {
|
||||
isActiveMember: boolean;
|
||||
isChannelAdmin: boolean;
|
||||
isArchived: boolean;
|
||||
};
|
||||
|
||||
export type ChatChannelReadMode = "member";
|
||||
|
||||
export async function loadChatContext({ request, params }: { request: Request; params: Record<string, string | undefined> }): Promise<ChatRouteContext> {
|
||||
await requireAuth({ request, params });
|
||||
const [tenantId, userInfo] = await Promise.all([getTenantIdFromUrl(params), getUserInfo(request)]);
|
||||
if (!tenantId || !userInfo.userId) {
|
||||
throw new CatalogError(foundationErrors.notFound);
|
||||
}
|
||||
|
||||
const [hasModule, permissions, membership] = await Promise.all([
|
||||
tenantHasModule({ tenantId, userId: userInfo.userId, moduleKey: CHAT_MODULE_KEY }),
|
||||
getPermissionsByUser(userInfo.userId, tenantId),
|
||||
withTenantRLS(tenantId, (tx) =>
|
||||
tx.tenantUser.findFirst({
|
||||
where: {
|
||||
tenantId,
|
||||
userId: userInfo.userId,
|
||||
status: TenantUserStatus.ACTIVE,
|
||||
user: { active: true },
|
||||
tenant: { active: true },
|
||||
},
|
||||
select: { id: true },
|
||||
})
|
||||
),
|
||||
]);
|
||||
|
||||
if (!membership || !hasModule) {
|
||||
throw new CatalogError(foundationErrors.notFound, !hasModule ? { module: CHAT_MODULE_KEY } : undefined);
|
||||
}
|
||||
|
||||
return {
|
||||
tenantId,
|
||||
userId: userInfo.userId,
|
||||
tenantUserId: membership.id,
|
||||
permissions,
|
||||
hasModule: true,
|
||||
};
|
||||
}
|
||||
|
||||
export function chatHasPermission(ctx: ChatRouteContext, permission: ChatPermission): boolean {
|
||||
return ctx.permissions.includes(permission);
|
||||
}
|
||||
|
||||
export function requireChatPermission(ctx: ChatRouteContext, permission: ChatPermission): ChatRouteContext {
|
||||
if (!chatHasPermission(ctx, permission)) {
|
||||
throw new CatalogError(foundationErrors.notFound, { permission });
|
||||
}
|
||||
|
||||
requirePermission({ tenantId: ctx.tenantId, userId: ctx.userId, permissions: ctx.permissions }, permission);
|
||||
return ctx;
|
||||
}
|
||||
|
||||
export function requireChatChannelReadAccess(ctx: ChatRouteContext, channel: ChatChannelAccessSnapshot): ChatChannelReadMode {
|
||||
if (channel.isActiveMember && chatHasPermission(ctx, CHAT_PERMISSIONS.readOwnChannels)) {
|
||||
return "member";
|
||||
}
|
||||
|
||||
throw new CatalogError(foundationErrors.notFound, { chat: "not-visible" });
|
||||
}
|
||||
|
||||
export function requireChatChannelWriteAccess(ctx: ChatRouteContext, channel: ChatChannelAccessSnapshot): ChatRouteContext {
|
||||
if (channel.isActiveMember && !channel.isArchived && chatHasPermission(ctx, CHAT_PERMISSIONS.write)) {
|
||||
requirePermission({ tenantId: ctx.tenantId, userId: ctx.userId, permissions: ctx.permissions }, CHAT_PERMISSIONS.write);
|
||||
return ctx;
|
||||
}
|
||||
|
||||
throw new CatalogError(foundationErrors.notFound, { chat: "not-writable" });
|
||||
}
|
||||
|
||||
export function requireChatChannelAdminAccess(ctx: ChatRouteContext, channel: ChatChannelAccessSnapshot): ChatRouteContext {
|
||||
if (chatHasPermission(ctx, CHAT_PERMISSIONS.admin)) {
|
||||
requirePermission({ tenantId: ctx.tenantId, userId: ctx.userId, permissions: ctx.permissions }, CHAT_PERMISSIONS.admin);
|
||||
return ctx;
|
||||
}
|
||||
|
||||
if (channel.isActiveMember && channel.isChannelAdmin && chatHasPermission(ctx, CHAT_PERMISSIONS.readOwnChannels)) {
|
||||
return ctx;
|
||||
}
|
||||
|
||||
throw new CatalogError(foundationErrors.notFound, { chat: "not-administerable" });
|
||||
}
|
||||
Reference in new issue
Block a user