Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/routes/avatars.$userId.tsx
T

82 lines
3.3 KiB
TypeScript

// Avatar auth-proxy (AD-23). The media bucket is private, so this route is the
// ONLY delivery path for a stored profile picture: an authorized request is
// redirected (302) to a short-lived signed URL. Mirrors media.$fileId.$variant,
// but keyed by user instead of by File record — an avatar is a user-level asset
// that also renders on the admin host, so it cannot hang off a tenant-scoped
// File. Anything not deliverable answers 404, never 403 (Bauprinzip A.5.3).
import type { LoaderFunctionArgs } from "react-router";
import { redirect } from "react-router";
import { runWithRlsContext } from "@hivegrid/db/rls-context.server";
import { db } from "~/utils/db.server";
import { getUserInfo } from "~/utils/session.server";
import { createMediaDownloadUrl } from "~/utils/integrations/mediaSigning.server";
import { avatarStorageKey, isAvatarProxyPath } from "~/utils/app/avatarPaths";
type AvatarRow = { avatar: string | null };
function notFound(): Response {
return new Response("Not Found", { status: 404 });
}
/**
* Reads the target's stored picture under the requester's RLS identity, so the
* `hg_User_select` policy is what authorizes the delivery — a picture is visible
* to its owner, to an operator, and to anyone sharing a workspace with the owner
* (the only place avatars render). A row we may not see is indistinguishable
* from "does not exist", which is exactly the 404 we want (Bauprinzip A.5.3).
*
* This route is host-level, not tenant-scoped, so no tenant GUC is bound by the
* caller. Owner and operator resolve with the user GUC alone; a co-worker's row
* only becomes visible once a tenant is bound, so membership is re-proven tenant
* by tenant instead of switching a GUC inside an open transaction.
*/
async function readDeliverableAvatar(requesterId: string, targetUserId: string): Promise<AvatarRow | null> {
const asOwnerOrOperator = await runWithRlsContext({ userId: requesterId }, () =>
db.user.findUnique({ where: { id: targetUserId }, select: { avatar: true } })
);
if (asOwnerOrOperator) {
return asOwnerOrOperator;
}
// Own memberships are the one TenantUser projection visible without a tenant
// GUC (`hg_TenantUser_select`: userId = app.user_id).
const memberships = await runWithRlsContext({ userId: requesterId }, () =>
db.tenantUser.findMany({ where: { userId: requesterId }, select: { tenantId: true } })
);
for (const { tenantId } of memberships) {
const asCoWorker = await runWithRlsContext({ userId: requesterId, tenantId }, () =>
db.user.findUnique({ where: { id: targetUserId }, select: { avatar: true } })
);
if (asCoWorker) {
return asCoWorker;
}
}
return null;
}
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
const targetUserId = params.userId;
if (!targetUserId) {
return notFound();
}
const { userId: requesterId } = await getUserInfo(request);
if (!requesterId) {
return notFound();
}
const target = await readDeliverableAvatar(requesterId, targetUserId);
// Only a proxy-backed value has an object behind it. Legacy data: URIs and
// absolute URLs are rendered straight from the DB and never reach this route.
if (!target || !isAvatarProxyPath(target.avatar)) {
return notFound();
}
const url = await createMediaDownloadUrl({ key: avatarStorageKey(targetUserId) });
return redirect(url, 302);
};