## What does this PR do? This PR adds user UUID plumbing from API v2 controllers through to packages/features functions. This is preparatory work extracted from PR #25125 to support future audit logging functionality. **Key changes:** - API v2 controllers (2024-04-15, 2024-08-13) now extract and pass `userUuid` to downstream functions - `handleMarkNoShow` and `CancelBookingInput` types now accept optional `userUuid` parameter - `UserRepository.findUnlockedUserForSession` now selects `uuid` field - Session middleware now includes `uuid` in the returned user object - Fixed lint warning: changed `PromiseSettledResult<any>` to `PromiseSettledResult<unknown>` **Refactoring (optimization):** - Renamed `getOwnerId` → `getOwner` and `getOwnerIdRescheduledBooking` → `getOwnerRescheduledBooking` - These methods now return `{ id: number; uuid: string } | null` instead of just `number | undefined` - This eliminates redundant database calls by fetching user id and uuid in a single query **Important:** This is plumbing-only - packages/features receives the `userUuid` but does not use it directly (note the `_userUuid` prefix). The actual audit logging usage will come in a follow-up PR. Requested by: @hariombalhara (hariom@cal.com) Link to Devin run: https://app.devin.ai/sessions/545209189f6347cd807bf1b336f9ac40 ## Mandatory Tasks (DO NOT REMOVE) - [x] I have self-reviewed the code (A decent size PR without self-review might be rejected). - [x] I have updated the developer docs in /docs if this PR makes changes that would require a [documentation change](https://cal.com/docs). N/A - no documentation changes needed. - [x] I confirm automated tests are in place that prove my fix is effective or that my feature works. N/A - plumbing only, existing tests cover the functionality. ## How should this be tested? 1. Run type checks: `yarn type-check:ci --force` 2. Verify the changes compile without new type errors related to uuid 3. The `userUuid` parameters are optional, so existing functionality should work unchanged ## Checklist - [x] My code follows the style guidelines of this project - [x] I have checked if my changes generate no new warnings ## Human Review Checklist - [ ] Verify the `userUuid` parameter is intentionally unused (prefixed with `_userUuid`) - this is plumbing for future audit logging - [ ] Verify all callers of `getOwner` and `getOwnerRescheduledBooking` correctly handle the new `{ id, uuid } | null` return type - [ ] Confirm the change from `undefined` to `null` as the "not found" return value is handled consistently - [ ] Verify the `uuid` field exists in the User model schema
175 lines
5.5 KiB
TypeScript
175 lines
5.5 KiB
TypeScript
import { setUser as SentrySetUser } from "@sentry/nextjs";
|
|
import type { Session } from "next-auth";
|
|
|
|
import { ProfileRepository } from "@calcom/features/profile/repositories/ProfileRepository";
|
|
import { UserRepository } from "@calcom/features/users/repositories/UserRepository";
|
|
import { WEBAPP_URL } from "@calcom/lib/constants";
|
|
import logger from "@calcom/lib/logger";
|
|
import { safeStringify } from "@calcom/lib/safeStringify";
|
|
import prisma from "@calcom/prisma";
|
|
import { teamMetadataSchema, userMetadata } from "@calcom/prisma/zod-utils";
|
|
|
|
import { TRPCError } from "@trpc/server";
|
|
|
|
import type { TRPCContextInner } from "../createContext";
|
|
import { middleware } from "../trpc";
|
|
|
|
type Maybe<T> = T | null | undefined;
|
|
|
|
export async function getUserFromSession(ctx: TRPCContextInner, session: Maybe<Session>) {
|
|
if (!session) {
|
|
return null;
|
|
}
|
|
|
|
if (!session.user?.id) {
|
|
return null;
|
|
}
|
|
|
|
const userRepo = new UserRepository(prisma);
|
|
const userFromDb = await userRepo.findUnlockedUserForSession({ userId: session.user.id });
|
|
|
|
// some hacks to make sure `username` and `email` are never inferred as `null`
|
|
if (!userFromDb) {
|
|
return null;
|
|
}
|
|
|
|
const upId = session.upId;
|
|
|
|
const user = await userRepo.enrichUserWithTheProfile({
|
|
user: userFromDb,
|
|
upId,
|
|
});
|
|
|
|
logger.debug(
|
|
`getUserFromSession: enriched user with profile - ${ctx.req?.url}`,
|
|
safeStringify({ user, userFromDb, upId })
|
|
);
|
|
|
|
const { email, username, id, uuid } = user;
|
|
if (!email || !id) {
|
|
return null; // should we return null here?
|
|
}
|
|
|
|
const userMetaData = userMetadata.parse(user.metadata || {});
|
|
const orgMetadata = teamMetadataSchema.parse(user.profile?.organization?.metadata || {});
|
|
// This helps to prevent reaching the 4MB payload limit by avoiding base64 and instead passing the avatar url
|
|
|
|
const locale = user?.locale ?? ctx.locale;
|
|
const { members = [], ..._organization } = user.profile?.organization || {};
|
|
const isOrgAdmin = members.some((member: { role: string }) => ["OWNER", "ADMIN"].includes(member.role));
|
|
|
|
if (isOrgAdmin) {
|
|
logger.debug("User is an org admin", safeStringify({ userId: user.id }));
|
|
} else {
|
|
logger.debug("User is not an org admin", safeStringify({ userId: user.id }));
|
|
}
|
|
const organization = {
|
|
..._organization,
|
|
id: user.profile?.organization?.id ?? null,
|
|
isOrgAdmin,
|
|
metadata: orgMetadata,
|
|
requestedSlug: orgMetadata?.requestedSlug ?? null,
|
|
};
|
|
|
|
return {
|
|
...user,
|
|
avatar: `${WEBAPP_URL}/${user.username}/avatar.png${organization.id ? `?orgId=${organization.id}` : ""}`,
|
|
// TODO: OrgNewSchema - later - We could consolidate the props in user.profile?.organization as organization is a profile thing now.
|
|
organization,
|
|
organizationId: organization.id,
|
|
id,
|
|
uuid,
|
|
email,
|
|
username,
|
|
locale,
|
|
defaultBookerLayouts: userMetaData?.defaultBookerLayouts || null,
|
|
requiresBookerEmailVerification: user.requiresBookerEmailVerification,
|
|
};
|
|
}
|
|
|
|
export type UserFromSession = Awaited<ReturnType<typeof getUserFromSession>>;
|
|
|
|
export const getSession = async (ctx: TRPCContextInner) => {
|
|
const { req } = ctx;
|
|
const { getServerSession } = await import("@calcom/features/auth/lib/getServerSession");
|
|
return req ? await getServerSession({ req }) : null;
|
|
};
|
|
|
|
export const getUserSession = async (ctx: TRPCContextInner) => {
|
|
/**
|
|
* It is possible that the session and user have already been added to the context by a previous middleware
|
|
* or when creating the context
|
|
*/
|
|
const session = ctx.session || (await getSession(ctx));
|
|
const user = session ? await getUserFromSession(ctx, session) : null;
|
|
let foundProfile = null;
|
|
// Check authorization for profile
|
|
if (session?.profileId && user?.id) {
|
|
foundProfile = await ProfileRepository.findByUserIdAndProfileId({
|
|
userId: user.id,
|
|
profileId: session.profileId,
|
|
});
|
|
if (!foundProfile) {
|
|
logger.error(
|
|
"Profile not found or not authorized",
|
|
safeStringify({ profileId: session.profileId, userId: user?.id })
|
|
);
|
|
// TODO: Test that logout should happen automatically
|
|
throw new TRPCError({ code: "UNAUTHORIZED", message: "Profile not found or not authorized" });
|
|
}
|
|
}
|
|
|
|
let sessionWithUpId = null;
|
|
if (session) {
|
|
let upId = session.upId;
|
|
if (!upId) {
|
|
upId = foundProfile?.upId ?? `usr-${user?.id}`;
|
|
}
|
|
|
|
if (!upId) {
|
|
throw new TRPCError({ code: "INTERNAL_SERVER_ERROR", message: "No upId found for session" });
|
|
}
|
|
sessionWithUpId = {
|
|
...session,
|
|
upId,
|
|
};
|
|
}
|
|
return { user, session: sessionWithUpId };
|
|
};
|
|
|
|
export const isAuthed = middleware(async ({ ctx, next }) => {
|
|
const middlewareStart = performance.now();
|
|
|
|
const { user, session } = await getUserSession(ctx);
|
|
|
|
const middlewareEnd = performance.now();
|
|
logger.debug("Perf:t.isAuthed", middlewareEnd - middlewareStart);
|
|
|
|
if (!user || !session) {
|
|
throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
}
|
|
|
|
SentrySetUser({ id: user.id });
|
|
|
|
return next({
|
|
ctx: { user, session },
|
|
});
|
|
});
|
|
|
|
export const isAdminMiddleware = isAuthed.unstable_pipe(({ ctx, next }) => {
|
|
const { user } = ctx;
|
|
if (user?.role !== "ADMIN") {
|
|
throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
}
|
|
return next({ ctx: { user: user } });
|
|
});
|
|
|
|
// Org admins can be admins or owners
|
|
export const isOrgAdminMiddleware = isAuthed.unstable_pipe(({ ctx, next }) => {
|
|
const { user } = ctx;
|
|
if (!user?.organization?.isOrgAdmin) {
|
|
throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
}
|
|
return next({ ctx: { user: user } });
|
|
});
|