Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.
TypeScript i18nextnext-authnextjspostgresqlprismareact-emailt3-stacktailwindcsstrpcturborepotypescriptzod
🐛 Honor edit token in participants.list when participants are hidden (#2399)
Luke Vella2f3b5c0bbf89
diff --git a/apps/web/src/app/[locale]/invite/[urlId]/page.tsx b/apps/web/src/app/[locale]/invite/[urlId]/page.tsxindex 21f817217..206354fbc 100644--- a/apps/web/src/app/[locale]/invite/[urlId]/page.tsx+++ b/apps/web/src/app/[locale]/invite/[urlId]/page.tsx@@ -43,15 +43,15 @@ export default async function Page(props: {const trpc = await createPublicSSRHelper();+ const token = (await props.searchParams).token;+await Promise.all([trpc.user.getMe.prefetch(),trpc.polls.get.prefetch({ urlId: params.urlId }),- trpc.polls.participants.list.prefetch({ pollId: params.urlId }),+ trpc.polls.participants.list.prefetch({ pollId: params.urlId, token }),trpc.polls.comments.list.prefetch({ pollId: params.urlId }),]);- const token = (await props.searchParams).token;-let impersonatedUserId: string | null = null;if (token) {const res = await decryptToken<{ userId: string }>(token);diff --git a/apps/web/src/components/participants-provider.tsx b/apps/web/src/components/participants-provider.tsxindex d91fbd373..db950e095 100644--- a/apps/web/src/components/participants-provider.tsx+++ b/apps/web/src/components/participants-provider.tsx@@ -1,5 +1,5 @@import type { VoteType } from "@rallly/database";-import { useParams } from "next/navigation";+import { useParams, useSearchParams } from "next/navigation";import * as React from "react";import { useVisibility } from "@/components/visibility";import { usePermissions } from "@/contexts/permissions";@@ -17,8 +17,10 @@ export function filterParticipantsByVote<export const useParticipants = () => {const urlId = useParams<{ urlId: string }>().urlId;+ const token = useSearchParams().get("token") ?? undefined;const [participants] = trpc.polls.participants.list.useSuspenseQuery({pollId: urlId,+ token,});return { participants };diff --git a/apps/web/src/components/poll/mutations.ts b/apps/web/src/components/poll/mutations.tsindex 378a462fa..d3c5018f4 100644--- a/apps/web/src/components/poll/mutations.ts+++ b/apps/web/src/components/poll/mutations.ts@@ -26,10 +26,11 @@ export const useAddParticipantMutation = () => {export const useUpdateParticipantMutation = () => {const queryClient = trpc.useUtils();+ const token = useEditToken();return trpc.polls.participants.update.useMutation({onSuccess: (participant) => {queryClient.polls.participants.list.setData(- { pollId: participant.pollId },+ { pollId: participant.pollId, token },(existingParticipants = []) => {const newParticipants = [...existingParticipants];@@ -51,10 +52,11 @@ export const useUpdateParticipantMutation = () => {export const useDeleteParticipantMutation = () => {const queryClient = trpc.useUtils();const { poll } = usePoll();+ const token = useEditToken();return trpc.polls.participants.delete.useMutation({onMutate: ({ participantId }) => {queryClient.polls.participants.list.setData(- { pollId: poll.id },+ { pollId: poll.id, token },(existingParticipants = []) => {return existingParticipants.filter(({ id }) => id !== participantId);},diff --git a/apps/web/src/trpc/routers/polls/participants.ts b/apps/web/src/trpc/routers/polls/participants.tsindex b020fec46..b145b4e10 100644--- a/apps/web/src/trpc/routers/polls/participants.ts+++ b/apps/web/src/trpc/routers/polls/participants.ts@@ -16,7 +16,11 @@ import {requireUserMiddleware,router,} from "../../trpc";-import { createParticipantEditToken, resolveUserId } from "./utils";+import {+ createParticipantEditToken,+ resolveUserId,+ tryResolveUserId,+} from "./utils";const logger = createLogger("participants");@@ -107,9 +111,10 @@ export const participants = router({.input(z.object({pollId: z.string(),+ token: z.string().optional(),}),)- .query(async ({ ctx, input: { pollId } }) => {+ .query(async ({ ctx, input: { pollId, token } }) => {const poll = await prisma.poll.findUnique({where: { id: pollId },select: {@@ -152,12 +157,16 @@ export const participants = router({// Hide participants if the poll has hideParticipants enabled// and the current user is not an adminif (poll.hideParticipants) {+ // Admin check is intentionally bound to ctx.user only — an edit+ // token must never unlock the admin view of other participants.const isAdmin =ctx.user && (await hasPollAdminAccess(pollId, ctx.user.id));if (!isAdmin) {+ // Fall back to the edit token so a guest can still see their own+ // response when opening the email link in a fresh browser.+ const viewerId = await tryResolveUserId(token, ctx.user);return participants.map((participant) => {- // If the current user is the participant, return the participant- if (ctx.user && participant.userId === ctx.user.id) {+ if (viewerId && participant.userId === viewerId) {return participant;}diff --git a/apps/web/src/trpc/routers/polls/utils.ts b/apps/web/src/trpc/routers/polls/utils.tsindex 808dcaf53..f86aa4359 100644--- a/apps/web/src/trpc/routers/polls/utils.ts+++ b/apps/web/src/trpc/routers/polls/utils.ts@@ -27,8 +27,7 @@ export async function resolveUserId(token: string | undefined,ctxUser: { id: string } | undefined,): Promise<string> {- const userIdFromToken = await getUserIdFromToken(token);- const userId = userIdFromToken ?? ctxUser?.id;+ const userId = await tryResolveUserId(token, ctxUser);if (!userId) {throw new TRPCError({code: "UNAUTHORIZED",@@ -37,3 +36,11 @@ export async function resolveUserId(}return userId;}++export async function tryResolveUserId(+ token: string | undefined,+ ctxUser: { id: string } | undefined,+): Promise<string | null> {+ const userIdFromToken = await getUserIdFromToken(token);+ return userIdFromToken ?? ctxUser?.id ?? null;+}