Skip to content

armgit / umami

Public Imported from GitHub
0

Umami is a privacy-first web and product analytics platform. Traffic, campaigns, behavior, conversions, and revenue in one place. No cookies, no surveillance, self-hosted or in the cloud.

fix sharetoken permissions on mixed boards

Francis Caoa87d899e171a
diff --git a/src/app/api/share/[slug]/route.ts b/src/app/api/share/[slug]/route.ts
index d702669b5..4bb79fcf5 100644
--- a/src/app/api/share/[slug]/route.ts
+++ b/src/app/api/share/[slug]/route.ts
@@ -1,4 +1,4 @@
-import { getBoardWebsiteIds } from '@/lib/boards';
+import { getBoardEntityIds } from '@/lib/boards';
import { ENTITY_TYPE, ROLES } from '@/lib/constants';
import { secret } from '@/lib/crypto';
import { createToken } from '@/lib/jwt';
@@ -66,10 +66,13 @@ export async function GET(_request: Request, { params }: { params: Promise<{ slu
if (!board) return notFound();
entity = board;
data.boardId = share.entityId;
- data.websiteIds = getBoardWebsiteIds({
+ const boardEntityIds = getBoardEntityIds({
type: board.type,
parameters: board.parameters as BoardParameters,
});
+ data.websiteIds = boardEntityIds.websiteIds;
+ data.pixelIds = boardEntityIds.pixelIds;
+ data.linkIds = boardEntityIds.linkIds;
} else if (share.shareType === ENTITY_TYPE.website) {
entity = await getWebsite(share.entityId);
if (!entity) return notFound();
diff --git a/src/lib/boards.ts b/src/lib/boards.ts
index e0087c16c..77199fb88 100644
--- a/src/lib/boards.ts
+++ b/src/lib/boards.ts
@@ -164,18 +164,34 @@ export function getFirstBoardComponentEntity(
return {};
}
-export function getBoardWebsiteIds(
- board?: Pick<Board, 'type' | 'parameters'> | Partial<Board>,
-): string[] {
- const ids = new Set<string>();
+export function getBoardEntityIds(board?: Pick<Board, 'type' | 'parameters'> | Partial<Board>): {
+ websiteIds: string[];
+ pixelIds: string[];
+ linkIds: string[];
+} {
+ const websiteIds = new Set<string>();
+ const pixelIds = new Set<string>();
+ const linkIds = new Set<string>();
const boardEntity = getBoardEntity(board);
if (boardEntity.entityType === BOARD_ENTITY_TYPES.website && boardEntity.entityId) {
- ids.add(boardEntity.entityId);
+ websiteIds.add(boardEntity.entityId);
+ } else if (boardEntity.entityType === BOARD_ENTITY_TYPES.pixel && boardEntity.entityId) {
+ pixelIds.add(boardEntity.entityId);
+ } else if (boardEntity.entityType === BOARD_ENTITY_TYPES.link && boardEntity.entityId) {
+ linkIds.add(boardEntity.entityId);
}
if (board?.parameters?.websiteId) {
- ids.add(board.parameters.websiteId);
+ websiteIds.add(board.parameters.websiteId);
+ }
+
+ if (board?.parameters?.pixelId) {
+ pixelIds.add(board.parameters.pixelId);
+ }
+
+ if (board?.parameters?.linkId) {
+ linkIds.add(board.parameters.linkId);
}
for (const row of board?.parameters?.rows ?? []) {
@@ -183,12 +199,20 @@ export function getBoardWebsiteIds(
const entity = getComponentEntity(column.component);
if (entity.entityType === BOARD_ENTITY_TYPES.website && entity.entityId) {
- ids.add(entity.entityId);
+ websiteIds.add(entity.entityId);
+ } else if (entity.entityType === BOARD_ENTITY_TYPES.pixel && entity.entityId) {
+ pixelIds.add(entity.entityId);
+ } else if (entity.entityType === BOARD_ENTITY_TYPES.link && entity.entityId) {
+ linkIds.add(entity.entityId);
}
}
}
- return [...ids];
+ return {
+ websiteIds: [...websiteIds],
+ pixelIds: [...pixelIds],
+ linkIds: [...linkIds],
+ };
}
export function clearBoardEntity(parameters: BoardParameters = {}): BoardParameters {
diff --git a/src/lib/types.ts b/src/lib/types.ts
index f2e49dab6..72104352e 100644
--- a/src/lib/types.ts
+++ b/src/lib/types.ts
@@ -24,7 +24,9 @@ export interface Auth {
websiteIds?: string[];
boardId?: string;
pixelId?: string;
+ pixelIds?: string[];
linkId?: string;
+ linkIds?: string[];
};
}
diff --git a/src/permissions/link.ts b/src/permissions/link.ts
index e77676998..f274c7b25 100644
--- a/src/permissions/link.ts
+++ b/src/permissions/link.ts
@@ -8,7 +8,7 @@ export async function canViewLink({ user, shareToken }: Auth, linkId: string) {
return true;
}
- if (shareToken?.linkId === linkId || shareToken?.websiteId === linkId) {
+ if (shareToken?.linkId === linkId || shareToken?.websiteId === linkId || shareToken?.linkIds?.includes(linkId)) {
return true;
}
diff --git a/src/permissions/pixel.ts b/src/permissions/pixel.ts
index bf7c7aed0..79b1b5976 100644
--- a/src/permissions/pixel.ts
+++ b/src/permissions/pixel.ts
@@ -8,7 +8,7 @@ export async function canViewPixel({ user, shareToken }: Auth, pixelId: string)
return true;
}
- if (shareToken?.pixelId === pixelId || shareToken?.websiteId === pixelId) {
+ if (shareToken?.pixelId === pixelId || shareToken?.websiteId === pixelId || shareToken?.pixelIds?.includes(pixelId)) {
return true;
}
diff --git a/src/permissions/website.ts b/src/permissions/website.ts
index 7d4efe1d3..cf8e8d82d 100644
--- a/src/permissions/website.ts
+++ b/src/permissions/website.ts
@@ -13,7 +13,9 @@ export async function canViewWebsite({ user, shareToken }: Auth, websiteId: stri
shareToken?.websiteId === websiteId ||
shareToken?.pixelId === websiteId ||
shareToken?.linkId === websiteId ||
- shareToken?.websiteIds?.includes(websiteId)
+ shareToken?.websiteIds?.includes(websiteId) ||
+ shareToken?.pixelIds?.includes(websiteId) ||
+ shareToken?.linkIds?.includes(websiteId)
) {
return true;
}