mirror of
https://github.com/documenso/documenso.git
synced 2026-07-25 09:25:08 +10:00
fix: allow users to download templates (#2746)
This commit is contained in:
@@ -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',
|
||||
|
||||
Reference in New Issue
Block a user