fix: allow users to download templates (#2746)

This commit is contained in:
David Nguyen
2026-04-30 16:50:07 +10:00
committed by GitHub
parent 84fc866cfb
commit c428170b5c
4 changed files with 265 additions and 98 deletions
@@ -2,6 +2,7 @@ import { EnvelopeType } from '@prisma/client';
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
import { getEnvelopeWhereInput } from '@documenso/lib/server-only/envelope/get-envelope-by-id';
import { getOrganisationTemplateWhereInput } from '@documenso/lib/server-only/template/get-organisation-template-by-id';
import { prisma } from '@documenso/prisma';
import { maybeAuthenticatedProcedure } from '../trpc';
@@ -101,7 +102,7 @@ const handleGetEnvelopeItemsByUser = async ({
userId: number;
teamId: number;
}) => {
const { envelopeWhereInput } = await getEnvelopeWhereInput({
const { envelopeWhereInput, team: callerTeam } = await getEnvelopeWhereInput({
id: {
type: 'envelopeId',
id: envelopeId,
@@ -111,7 +112,8 @@ const handleGetEnvelopeItemsByUser = async ({
teamId,
});
const envelope = await prisma.envelope.findUnique({
// Try the standard team-scoped access path first (owner / current team / team email).
let envelope = await prisma.envelope.findUnique({
where: envelopeWhereInput,
include: {
envelopeItems: {
@@ -122,6 +124,28 @@ const handleGetEnvelopeItemsByUser = async ({
},
});
// Fallback: if the envelope is an ORGANISATION template owned by a sibling team
// in the caller's organisation, allow read access to the items metadata.
// Mirrors the access logic used by `createDocumentFromTemplate` and the
// file-download endpoint's `checkEnvelopeFileAccess` so this route stays in
// sync with where actual file access is granted.
if (!envelope) {
envelope = await prisma.envelope.findFirst({
where: getOrganisationTemplateWhereInput({
id: { type: 'envelopeId', id: envelopeId },
organisationId: callerTeam.organisationId,
teamRole: callerTeam.currentTeamRole,
}),
include: {
envelopeItems: {
include: {
documentData: true,
},
},
},
});
}
if (!envelope) {
throw new AppError(AppErrorCode.NOT_FOUND, {
message: 'Envelope could not be found',