diff --git a/src/app/api/share/[slug]/route.ts b/src/app/api/share/[slug]/route.ts index d371e01e3..609e489b2 100644 --- a/src/app/api/share/[slug]/route.ts +++ b/src/app/api/share/[slug]/route.ts @@ -10,6 +10,7 @@ import { canViewLink, canViewPixel, canViewWebsite } from '@/permissions'; import { getBoard, getLink, getPixel, getShareByCode, getUser, getWebsite } from '@/queries/prisma'; type BoardEntityIds = ReturnType; +type OwnedEntity = { userId?: string | null; teamId?: string | null } | null; async function getAccountId(entity: { userId?: string; teamId?: string }): Promise { if (entity.userId) { @@ -64,23 +65,38 @@ async function filterEntityIds( return results.filter((id): id is string => !!id); } +async function getTeamUserIds(teamId: string) { + const teamUsers = await prisma.client.teamUser.findMany({ + where: { teamId }, + select: { userId: true }, + }); + + return new Set(teamUsers.map(({ userId }) => userId)); +} + +function isOwnedByTeam(entity: OwnedEntity, teamId: string, teamUserIds: Set) { + return entity?.teamId === teamId || !!(entity?.userId && teamUserIds.has(entity.userId)); +} + async function filterBoardEntityIdsForShare( entity: { userId?: string | null; teamId?: string | null }, ids: BoardEntityIds, ): Promise { if (entity.teamId) { + const teamUserIds = await getTeamUserIds(entity.teamId); + return { websiteIds: await filterEntityIds( ids.websiteIds, - async id => (await getWebsite(id))?.teamId === entity.teamId, + async id => isOwnedByTeam(await getWebsite(id), entity.teamId, teamUserIds), ), pixelIds: await filterEntityIds( ids.pixelIds, - async id => (await getPixel(id))?.teamId === entity.teamId, + async id => isOwnedByTeam(await getPixel(id), entity.teamId, teamUserIds), ), linkIds: await filterEntityIds( ids.linkIds, - async id => (await getLink(id))?.teamId === entity.teamId, + async id => isOwnedByTeam(await getLink(id), entity.teamId, teamUserIds), ), }; } diff --git a/src/permissions/board.ts b/src/permissions/board.ts index b78da0eb9..d63b4a495 100644 --- a/src/permissions/board.ts +++ b/src/permissions/board.ts @@ -21,7 +21,7 @@ export async function canViewBoardEntities( parameters: BoardParameters = {}, ) { const { websiteIds, pixelIds, linkIds } = getBoardEntityIds({ type, parameters }); - const userOnlyAuth: Auth = auth.user ? { user: auth.user } : {}; + const userOnlyAuth: Auth = { user: auth.user }; const checks = [ ...websiteIds.map(id => checkBoardEntityAccess(canViewWebsite(userOnlyAuth, id))), ...pixelIds.map(id => checkBoardEntityAccess(canViewPixel(userOnlyAuth, id))),