Merge branch 'dev' into feat-2FA
This commit is contained in:
559 files changed
+21955
-9347
No files matched your search
@@ -0,0 +1,258 @@
|
||||
import { beforeEach, expect, test, vi } from 'vitest';
|
||||
import { BOARD_TYPES } from '@/lib/boards';
|
||||
import { getReport } from '@/queries/prisma';
|
||||
import {
|
||||
canViewBoardEntities,
|
||||
hasValidBoardReports,
|
||||
stripInvalidBoardReports,
|
||||
} from './board';
|
||||
import { canViewLink } from './link';
|
||||
import { canViewPixel } from './pixel';
|
||||
import { canViewWebsite } from './website';
|
||||
|
||||
vi.mock('@/queries/prisma', () => ({
|
||||
getBoard: vi.fn(),
|
||||
getReport: vi.fn(),
|
||||
getTeamUser: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/prisma', () => ({
|
||||
default: {},
|
||||
}));
|
||||
|
||||
vi.mock('./website', () => ({
|
||||
canViewWebsite: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('./pixel', () => ({
|
||||
canViewPixel: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('./link', () => ({
|
||||
canViewLink: vi.fn(),
|
||||
}));
|
||||
|
||||
const auth = {
|
||||
user: {
|
||||
id: 'user-1',
|
||||
username: 'user',
|
||||
role: 'user',
|
||||
isAdmin: false,
|
||||
},
|
||||
shareToken: {
|
||||
websiteIds: ['victim-website-id'],
|
||||
},
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
vi.mocked(canViewWebsite).mockReset();
|
||||
vi.mocked(canViewPixel).mockReset();
|
||||
vi.mocked(canViewLink).mockReset();
|
||||
vi.mocked(getReport).mockReset();
|
||||
});
|
||||
|
||||
test('canViewBoardEntities validates board IDs with user auth only', async () => {
|
||||
vi.mocked(canViewWebsite).mockResolvedValue(true);
|
||||
|
||||
await expect(
|
||||
canViewBoardEntities(auth, BOARD_TYPES.website, { websiteId: 'owned-website-id' }),
|
||||
).resolves.toBe(true);
|
||||
|
||||
expect(canViewWebsite).toHaveBeenCalledWith(
|
||||
{
|
||||
user: auth.user,
|
||||
},
|
||||
'owned-website-id',
|
||||
);
|
||||
});
|
||||
|
||||
test('canViewBoardEntities rejects IDs not accessible to the user', async () => {
|
||||
vi.mocked(canViewWebsite).mockResolvedValue(false);
|
||||
|
||||
await expect(
|
||||
canViewBoardEntities(auth, BOARD_TYPES.mixed, {
|
||||
rows: [
|
||||
{
|
||||
id: 'row-1',
|
||||
columns: [
|
||||
{
|
||||
id: 'column-1',
|
||||
component: {
|
||||
type: 'WebsiteChart',
|
||||
entityType: 'website',
|
||||
entityId: 'victim-website-id',
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
}),
|
||||
).resolves.toBe(false);
|
||||
});
|
||||
|
||||
test('hasValidBoardReports allows saved reports for the board website', async () => {
|
||||
vi.mocked(getReport).mockResolvedValue({
|
||||
id: 'report-1',
|
||||
websiteId: 'website-1',
|
||||
type: 'goal',
|
||||
} as any);
|
||||
|
||||
await expect(
|
||||
hasValidBoardReports(BOARD_TYPES.website, {
|
||||
websiteId: 'website-1',
|
||||
rows: [
|
||||
{
|
||||
id: 'row-1',
|
||||
columns: [
|
||||
{
|
||||
id: 'column-1',
|
||||
component: {
|
||||
type: 'Goal',
|
||||
props: {
|
||||
reportId: 'report-1',
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
}),
|
||||
).resolves.toBe(true);
|
||||
});
|
||||
|
||||
test('hasValidBoardReports rejects saved reports from a different website', async () => {
|
||||
vi.mocked(getReport).mockResolvedValue({
|
||||
id: 'report-1',
|
||||
websiteId: 'website-2',
|
||||
type: 'goal',
|
||||
} as any);
|
||||
|
||||
await expect(
|
||||
hasValidBoardReports(BOARD_TYPES.website, {
|
||||
websiteId: 'website-1',
|
||||
rows: [
|
||||
{
|
||||
id: 'row-1',
|
||||
columns: [
|
||||
{
|
||||
id: 'column-1',
|
||||
component: {
|
||||
type: 'Goal',
|
||||
props: {
|
||||
reportId: 'report-1',
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
}),
|
||||
).resolves.toBe(false);
|
||||
});
|
||||
|
||||
test('hasValidBoardReports rejects saved reports with the wrong type', async () => {
|
||||
vi.mocked(getReport).mockResolvedValue({
|
||||
id: 'report-1',
|
||||
websiteId: 'website-1',
|
||||
type: 'funnel',
|
||||
} as any);
|
||||
|
||||
await expect(
|
||||
hasValidBoardReports(BOARD_TYPES.website, {
|
||||
websiteId: 'website-1',
|
||||
rows: [
|
||||
{
|
||||
id: 'row-1',
|
||||
columns: [
|
||||
{
|
||||
id: 'column-1',
|
||||
component: {
|
||||
type: 'Goal',
|
||||
props: {
|
||||
reportId: 'report-1',
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
}),
|
||||
).resolves.toBe(false);
|
||||
});
|
||||
|
||||
test('hasValidBoardReports rejects report widgets without a website context', async () => {
|
||||
await expect(
|
||||
hasValidBoardReports(BOARD_TYPES.pixel, {
|
||||
pixelId: 'pixel-1',
|
||||
rows: [
|
||||
{
|
||||
id: 'row-1',
|
||||
columns: [
|
||||
{
|
||||
id: 'column-1',
|
||||
component: {
|
||||
type: 'Goal',
|
||||
props: {
|
||||
reportId: 'report-1',
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
}),
|
||||
).resolves.toBe(false);
|
||||
});
|
||||
|
||||
test('stripInvalidBoardReports removes only invalid report IDs', async () => {
|
||||
vi.mocked(getReport).mockResolvedValue({
|
||||
id: 'report-1',
|
||||
websiteId: 'website-2',
|
||||
type: 'goal',
|
||||
} as any);
|
||||
|
||||
await expect(
|
||||
stripInvalidBoardReports(BOARD_TYPES.website, {
|
||||
websiteId: 'website-1',
|
||||
rows: [
|
||||
{
|
||||
id: 'row-1',
|
||||
columns: [
|
||||
{
|
||||
id: 'column-1',
|
||||
component: {
|
||||
type: 'Goal',
|
||||
title: 'Saved signup goal',
|
||||
description: 'Should stay on the board',
|
||||
props: {
|
||||
reportId: 'report-1',
|
||||
customValue: 'keep-me',
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
}),
|
||||
).resolves.toEqual({
|
||||
websiteId: 'website-1',
|
||||
rows: [
|
||||
{
|
||||
id: 'row-1',
|
||||
columns: [
|
||||
{
|
||||
id: 'column-1',
|
||||
component: {
|
||||
type: 'Goal',
|
||||
title: 'Saved signup goal',
|
||||
description: 'Should stay on the board',
|
||||
props: {
|
||||
customValue: 'keep-me',
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
+150
-2
@@ -1,7 +1,155 @@
|
||||
import { hasPermission } from '@/lib/auth';
|
||||
import { BOARD_ENTITY_TYPES, getBoardEntityIds, getResolvedComponentEntity } from '@/lib/boards';
|
||||
import { PERMISSIONS } from '@/lib/constants';
|
||||
import type { Auth } from '@/lib/types';
|
||||
import { getBoard, getTeamUser } from '@/queries/prisma';
|
||||
import type { Auth, BoardComponentConfig, BoardParameters } from '@/lib/types';
|
||||
import { getBoard, getReport, getTeamUser } from '@/queries/prisma';
|
||||
import { canViewLink } from './link';
|
||||
import { canViewPixel } from './pixel';
|
||||
import { canViewWebsite } from './website';
|
||||
|
||||
const BOARD_COMPONENT_REPORT_TYPES = {
|
||||
Funnel: 'funnel',
|
||||
Goal: 'goal',
|
||||
} as const;
|
||||
|
||||
function getExpectedBoardReportType(componentType?: string) {
|
||||
return componentType && componentType in BOARD_COMPONENT_REPORT_TYPES
|
||||
? BOARD_COMPONENT_REPORT_TYPES[componentType as keyof typeof BOARD_COMPONENT_REPORT_TYPES]
|
||||
: null;
|
||||
}
|
||||
|
||||
async function isValidBoardReportReference(
|
||||
board: { type?: string; parameters?: BoardParameters },
|
||||
component: BoardComponentConfig,
|
||||
reportCache: Map<string, ReturnType<typeof getReport>>,
|
||||
) {
|
||||
const expectedReportType = getExpectedBoardReportType(component.type);
|
||||
|
||||
if (!expectedReportType) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const reportId = component.props?.reportId;
|
||||
|
||||
if (reportId == null || reportId === '') {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (typeof reportId !== 'string') {
|
||||
return false;
|
||||
}
|
||||
|
||||
const entity = getResolvedComponentEntity(board, component);
|
||||
|
||||
if (entity.entityType !== BOARD_ENTITY_TYPES.website || !entity.entityId) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const reportPromise = reportCache.get(reportId) ?? getReport(reportId);
|
||||
reportCache.set(reportId, reportPromise);
|
||||
|
||||
const report = await reportPromise;
|
||||
|
||||
return !!report && report.type === expectedReportType && report.websiteId === entity.entityId;
|
||||
}
|
||||
|
||||
async function checkBoardEntityAccess(check: Promise<boolean>) {
|
||||
try {
|
||||
return await check;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export async function canViewBoardEntities(
|
||||
auth: Auth,
|
||||
type: string | undefined,
|
||||
parameters: BoardParameters = {},
|
||||
) {
|
||||
const { websiteIds, pixelIds, linkIds } = getBoardEntityIds({ type, parameters });
|
||||
const userOnlyAuth: Auth = { user: auth.user };
|
||||
const checks = [
|
||||
...websiteIds.map(id => checkBoardEntityAccess(canViewWebsite(userOnlyAuth, id))),
|
||||
...pixelIds.map(id => checkBoardEntityAccess(canViewPixel(userOnlyAuth, id))),
|
||||
...linkIds.map(id => checkBoardEntityAccess(canViewLink(userOnlyAuth, id))),
|
||||
];
|
||||
|
||||
const results = await Promise.all(checks);
|
||||
|
||||
return results.every(Boolean);
|
||||
}
|
||||
|
||||
export async function hasValidBoardReports(
|
||||
type: string | undefined,
|
||||
parameters: BoardParameters = {},
|
||||
) {
|
||||
const board = { type, parameters };
|
||||
const reportCache = new Map<string, ReturnType<typeof getReport>>();
|
||||
|
||||
for (const row of parameters.rows ?? []) {
|
||||
for (const column of row.columns ?? []) {
|
||||
const component = column.component;
|
||||
if (!component) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!(await isValidBoardReportReference(board, component, reportCache))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
export async function stripInvalidBoardReports(
|
||||
type: string | undefined,
|
||||
parameters: BoardParameters = {},
|
||||
) {
|
||||
const board = { type, parameters };
|
||||
const reportCache = new Map<string, ReturnType<typeof getReport>>();
|
||||
let hasChanges = false;
|
||||
|
||||
const rows = await Promise.all(
|
||||
(parameters.rows ?? []).map(async row => {
|
||||
const columns = await Promise.all(
|
||||
row.columns.map(async column => {
|
||||
const component = column.component;
|
||||
|
||||
if (!component || (await isValidBoardReportReference(board, component, reportCache))) {
|
||||
return column;
|
||||
}
|
||||
|
||||
hasChanges = true;
|
||||
|
||||
const { reportId: _reportId, ...nextProps } = component.props ?? {};
|
||||
|
||||
return {
|
||||
...column,
|
||||
component: {
|
||||
...component,
|
||||
props: Object.keys(nextProps).length > 0 ? nextProps : undefined,
|
||||
},
|
||||
};
|
||||
}),
|
||||
);
|
||||
|
||||
return {
|
||||
...row,
|
||||
columns,
|
||||
};
|
||||
}),
|
||||
);
|
||||
|
||||
if (!hasChanges) {
|
||||
return parameters;
|
||||
}
|
||||
|
||||
return {
|
||||
...parameters,
|
||||
rows,
|
||||
};
|
||||
}
|
||||
|
||||
export async function canViewBoard({ user, shareToken }: Auth, boardId: string) {
|
||||
if (user?.isAdmin) {
|
||||
|
||||
@@ -3,6 +3,7 @@ export * from './entity';
|
||||
export * from './link';
|
||||
export * from './pixel';
|
||||
export * from './report';
|
||||
export * from './share';
|
||||
export * from './team';
|
||||
export * from './user';
|
||||
export * from './website';
|
||||
+17
-1
@@ -8,7 +8,11 @@ export async function canViewLink({ user, shareToken }: Auth, linkId: string) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (shareToken?.linkId === linkId || shareToken?.websiteId === linkId || shareToken?.linkIds?.includes(linkId)) {
|
||||
if (
|
||||
shareToken?.linkId === linkId ||
|
||||
shareToken?.websiteId === linkId ||
|
||||
shareToken?.linkIds?.includes(linkId)
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -18,6 +22,10 @@ export async function canViewLink({ user, shareToken }: Auth, linkId: string) {
|
||||
|
||||
const link = await getLink(linkId);
|
||||
|
||||
if (!link) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (link.userId) {
|
||||
return user.id === link.userId;
|
||||
}
|
||||
@@ -42,6 +50,10 @@ export async function canUpdateLink({ user }: Auth, linkId: string) {
|
||||
|
||||
const link = await getLink(linkId);
|
||||
|
||||
if (!link) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (link.userId) {
|
||||
return user.id === link.userId;
|
||||
}
|
||||
@@ -66,6 +78,10 @@ export async function canDeleteLink({ user }: Auth, linkId: string) {
|
||||
|
||||
const link = await getLink(linkId);
|
||||
|
||||
if (!link) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (link.userId) {
|
||||
return user.id === link.userId;
|
||||
}
|
||||
|
||||
@@ -8,7 +8,11 @@ export async function canViewPixel({ user, shareToken }: Auth, pixelId: string)
|
||||
return true;
|
||||
}
|
||||
|
||||
if (shareToken?.pixelId === pixelId || shareToken?.websiteId === pixelId || shareToken?.pixelIds?.includes(pixelId)) {
|
||||
if (
|
||||
shareToken?.pixelId === pixelId ||
|
||||
shareToken?.websiteId === pixelId ||
|
||||
shareToken?.pixelIds?.includes(pixelId)
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -18,6 +22,10 @@ export async function canViewPixel({ user, shareToken }: Auth, pixelId: string)
|
||||
|
||||
const pixel = await getPixel(pixelId);
|
||||
|
||||
if (!pixel) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (pixel.userId) {
|
||||
return user.id === pixel.userId;
|
||||
}
|
||||
@@ -42,6 +50,10 @@ export async function canUpdatePixel({ user }: Auth, pixelId: string) {
|
||||
|
||||
const pixel = await getPixel(pixelId);
|
||||
|
||||
if (!pixel) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (pixel.userId) {
|
||||
return user.id === pixel.userId;
|
||||
}
|
||||
@@ -66,6 +78,10 @@ export async function canDeletePixel({ user }: Auth, pixelId: string) {
|
||||
|
||||
const pixel = await getPixel(pixelId);
|
||||
|
||||
if (!pixel) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (pixel.userId) {
|
||||
return user.id === pixel.userId;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
import { expect, test, vi } from 'vitest';
|
||||
import { ENTITY_TYPE } from '@/lib/constants';
|
||||
import { canViewReport, getReportSection } from './report';
|
||||
import { canViewWebsite } from './website';
|
||||
|
||||
vi.mock('./website', () => ({
|
||||
canViewWebsite: vi.fn(),
|
||||
}));
|
||||
|
||||
test('getReportSection maps saved report types to share sections', () => {
|
||||
expect(getReportSection('goal')).toBe('goals');
|
||||
expect(getReportSection('funnel')).toBe('funnels');
|
||||
expect(getReportSection('journey')).toBe('journeys');
|
||||
expect(getReportSection('revenue')).toBe('revenue');
|
||||
expect(getReportSection('heatmap')).toBe(null);
|
||||
});
|
||||
|
||||
test('canViewReport allows board shares to fetch included saved goal reports', async () => {
|
||||
await expect(
|
||||
canViewReport(
|
||||
{
|
||||
shareToken: {
|
||||
shareType: ENTITY_TYPE.board,
|
||||
websiteIds: ['website-1'],
|
||||
parameters: {},
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 'report-1',
|
||||
userId: 'owner-1',
|
||||
websiteId: 'website-1',
|
||||
type: 'goal',
|
||||
} as any,
|
||||
),
|
||||
).resolves.toBe(true);
|
||||
});
|
||||
|
||||
test('canViewReport respects share section flags for saved funnel and goal reports', async () => {
|
||||
await expect(
|
||||
canViewReport(
|
||||
{
|
||||
shareToken: {
|
||||
shareType: ENTITY_TYPE.website,
|
||||
websiteId: 'website-1',
|
||||
parameters: {
|
||||
overview: true,
|
||||
funnels: false,
|
||||
goals: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 'report-1',
|
||||
userId: 'owner-1',
|
||||
websiteId: 'website-1',
|
||||
type: 'funnel',
|
||||
} as any,
|
||||
),
|
||||
).resolves.toBe(false);
|
||||
|
||||
await expect(
|
||||
canViewReport(
|
||||
{
|
||||
shareToken: {
|
||||
shareType: ENTITY_TYPE.website,
|
||||
websiteId: 'website-1',
|
||||
parameters: {
|
||||
overview: true,
|
||||
funnels: false,
|
||||
goals: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 'report-2',
|
||||
userId: 'owner-1',
|
||||
websiteId: 'website-1',
|
||||
type: 'goal',
|
||||
} as any,
|
||||
),
|
||||
).resolves.toBe(true);
|
||||
});
|
||||
|
||||
test('canViewReport denies share-token access to report types without a share section', async () => {
|
||||
await expect(
|
||||
canViewReport(
|
||||
{
|
||||
shareToken: {
|
||||
shareType: ENTITY_TYPE.website,
|
||||
websiteId: 'website-1',
|
||||
parameters: {},
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 'report-1',
|
||||
userId: 'owner-1',
|
||||
websiteId: 'website-1',
|
||||
type: 'heatmap',
|
||||
} as any,
|
||||
),
|
||||
).resolves.toBe(false);
|
||||
});
|
||||
|
||||
test('canViewReport falls back to website access for authenticated users', async () => {
|
||||
vi.mocked(canViewWebsite).mockResolvedValue(true);
|
||||
|
||||
await expect(
|
||||
canViewReport(
|
||||
{
|
||||
user: {
|
||||
id: 'user-1',
|
||||
username: 'user',
|
||||
role: 'user',
|
||||
isAdmin: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 'report-1',
|
||||
userId: 'owner-1',
|
||||
websiteId: 'website-1',
|
||||
type: 'heatmap',
|
||||
} as any,
|
||||
),
|
||||
).resolves.toBe(true);
|
||||
});
|
||||
@@ -1,8 +1,34 @@
|
||||
import type { Report } from '@/generated/prisma/client';
|
||||
import type { Auth } from '@/lib/types';
|
||||
import type { ShareSection } from './share';
|
||||
import { canViewWebsiteSection } from './share';
|
||||
import { canDeleteWebsite, canUpdateWebsite, canViewWebsite } from './website';
|
||||
|
||||
export async function canViewReport(auth: Auth, report: Report) {
|
||||
export function getReportSection(type?: string): ShareSection | null {
|
||||
switch (type) {
|
||||
case 'attribution':
|
||||
case 'breakdown':
|
||||
case 'performance':
|
||||
case 'retention':
|
||||
case 'revenue':
|
||||
case 'utm':
|
||||
return type;
|
||||
case 'funnel':
|
||||
return 'funnels';
|
||||
case 'goal':
|
||||
return 'goals';
|
||||
case 'journey':
|
||||
return 'journeys';
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function canViewReport(auth: Auth, report: Report | null) {
|
||||
if (!report) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (auth.user?.isAdmin) {
|
||||
return true;
|
||||
}
|
||||
@@ -11,7 +37,13 @@ export async function canViewReport(auth: Auth, report: Report) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return !!(await canViewWebsite(auth, report.websiteId));
|
||||
const section = getReportSection(report.type);
|
||||
|
||||
if (section) {
|
||||
return !!(await canViewWebsiteSection(auth, report.websiteId, section));
|
||||
}
|
||||
|
||||
return !!auth.user && !!(await canViewWebsite(auth, report.websiteId));
|
||||
}
|
||||
|
||||
export async function canUpdateReport(auth: Auth, report: Report) {
|
||||
|
||||
@@ -0,0 +1,148 @@
|
||||
import { expect, test, vi } from 'vitest';
|
||||
import { ENTITY_TYPE } from '@/lib/constants';
|
||||
import {
|
||||
canViewSharedWebsite,
|
||||
canViewSharedWebsiteFilters,
|
||||
canViewWebsiteSection,
|
||||
} from './share';
|
||||
|
||||
vi.mock('./website', () => ({
|
||||
canViewWebsite: vi.fn(),
|
||||
}));
|
||||
|
||||
test('canViewWebsiteSection allows board shares for included websites', async () => {
|
||||
await expect(
|
||||
canViewWebsiteSection(
|
||||
{
|
||||
shareToken: {
|
||||
shareType: ENTITY_TYPE.board,
|
||||
websiteIds: ['website-1'],
|
||||
parameters: {},
|
||||
},
|
||||
},
|
||||
'website-1',
|
||||
'goals',
|
||||
),
|
||||
).resolves.toBe(true);
|
||||
});
|
||||
|
||||
test('canViewWebsiteSection respects section flags on website shares', async () => {
|
||||
await expect(
|
||||
canViewWebsiteSection(
|
||||
{
|
||||
shareToken: {
|
||||
shareType: ENTITY_TYPE.website,
|
||||
websiteId: 'website-1',
|
||||
parameters: {
|
||||
overview: true,
|
||||
goals: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
'website-1',
|
||||
'goals',
|
||||
),
|
||||
).resolves.toBe(false);
|
||||
});
|
||||
|
||||
test('canViewWebsiteSection allows any requested enabled section', async () => {
|
||||
await expect(
|
||||
canViewWebsiteSection(
|
||||
{
|
||||
shareToken: {
|
||||
shareType: ENTITY_TYPE.website,
|
||||
websiteId: 'website-1',
|
||||
parameters: {
|
||||
overview: true,
|
||||
compare: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
'website-1',
|
||||
['overview', 'compare'],
|
||||
),
|
||||
).resolves.toBe(true);
|
||||
});
|
||||
|
||||
test('canViewSharedWebsite allows board shares for included websites', async () => {
|
||||
await expect(
|
||||
canViewSharedWebsite(
|
||||
{
|
||||
shareToken: {
|
||||
shareType: ENTITY_TYPE.board,
|
||||
websiteIds: ['website-1'],
|
||||
parameters: {},
|
||||
},
|
||||
},
|
||||
'website-1',
|
||||
),
|
||||
).resolves.toBe(true);
|
||||
});
|
||||
|
||||
test('canViewSharedWebsiteFilters requires allowFilter for share tokens', async () => {
|
||||
await expect(
|
||||
canViewSharedWebsiteFilters(
|
||||
{
|
||||
shareToken: {
|
||||
shareType: ENTITY_TYPE.website,
|
||||
websiteId: 'website-1',
|
||||
parameters: {
|
||||
allowFilter: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
'website-1',
|
||||
),
|
||||
).resolves.toBe(false);
|
||||
|
||||
await expect(
|
||||
canViewSharedWebsiteFilters(
|
||||
{
|
||||
shareToken: {
|
||||
shareType: ENTITY_TYPE.website,
|
||||
websiteId: 'website-1',
|
||||
parameters: {
|
||||
allowFilter: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
'website-1',
|
||||
),
|
||||
).resolves.toBe(true);
|
||||
});
|
||||
|
||||
test('canViewWebsiteSection allows pixel shares for the shared entity id', async () => {
|
||||
await expect(
|
||||
canViewWebsiteSection(
|
||||
{
|
||||
shareToken: {
|
||||
shareType: ENTITY_TYPE.pixel,
|
||||
pixelId: 'pixel-1',
|
||||
parameters: {
|
||||
overview: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
'pixel-1',
|
||||
'overview',
|
||||
),
|
||||
).resolves.toBe(true);
|
||||
});
|
||||
|
||||
test('canViewWebsiteSection allows link shares for the shared entity id', async () => {
|
||||
await expect(
|
||||
canViewWebsiteSection(
|
||||
{
|
||||
shareToken: {
|
||||
shareType: ENTITY_TYPE.link,
|
||||
linkId: 'link-1',
|
||||
parameters: {
|
||||
overview: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
'link-1',
|
||||
'overview',
|
||||
),
|
||||
).resolves.toBe(true);
|
||||
});
|
||||
@@ -0,0 +1,110 @@
|
||||
import { ENTITY_TYPE } from '@/lib/constants';
|
||||
import type { Auth } from '@/lib/types';
|
||||
import { canViewWebsite } from './website';
|
||||
|
||||
export type ShareSection =
|
||||
| 'overview'
|
||||
| 'events'
|
||||
| 'sessions'
|
||||
| 'realtime'
|
||||
| 'performance'
|
||||
| 'compare'
|
||||
| 'breakdown'
|
||||
| 'goals'
|
||||
| 'funnels'
|
||||
| 'journeys'
|
||||
| 'retention'
|
||||
| 'utm'
|
||||
| 'revenue'
|
||||
| 'attribution';
|
||||
|
||||
const SHARE_SECTIONS: ShareSection[] = [
|
||||
'overview',
|
||||
'events',
|
||||
'sessions',
|
||||
'realtime',
|
||||
'performance',
|
||||
'compare',
|
||||
'breakdown',
|
||||
'goals',
|
||||
'funnels',
|
||||
'journeys',
|
||||
'retention',
|
||||
'utm',
|
||||
'revenue',
|
||||
'attribution',
|
||||
];
|
||||
|
||||
type ShareSectionInput = ShareSection | ShareSection[];
|
||||
|
||||
function shareTokenIncludesWebsite(auth: Auth | null | undefined, websiteId: string) {
|
||||
const { shareToken } = auth || {};
|
||||
|
||||
return (
|
||||
shareToken?.websiteId === websiteId ||
|
||||
shareToken?.pixelId === websiteId ||
|
||||
shareToken?.linkId === websiteId ||
|
||||
shareToken?.websiteIds?.includes(websiteId) ||
|
||||
shareToken?.pixelIds?.includes(websiteId) ||
|
||||
shareToken?.linkIds?.includes(websiteId)
|
||||
);
|
||||
}
|
||||
|
||||
export async function canViewWebsiteSection(
|
||||
auth: Auth | null | undefined,
|
||||
websiteId: string,
|
||||
section: ShareSectionInput,
|
||||
) {
|
||||
if (auth?.user) {
|
||||
return canViewWebsite(auth, websiteId);
|
||||
}
|
||||
|
||||
const { shareToken } = auth || {};
|
||||
|
||||
if (!shareToken || !shareTokenIncludesWebsite(auth, websiteId)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const sections = Array.isArray(section) ? section : [section];
|
||||
const hasSectionParameters = SHARE_SECTIONS.some(
|
||||
key => typeof shareToken.parameters?.[key] === 'boolean',
|
||||
);
|
||||
|
||||
if (!hasSectionParameters) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return sections.some(key => shareToken.parameters?.[key] === true);
|
||||
}
|
||||
|
||||
export async function canViewSharedWebsite(auth: Auth | null | undefined, websiteId: string) {
|
||||
if (auth?.user) {
|
||||
return canViewWebsite(auth, websiteId);
|
||||
}
|
||||
|
||||
return shareTokenIncludesWebsite(auth, websiteId);
|
||||
}
|
||||
|
||||
export async function canViewSharedWebsiteFilters(
|
||||
auth: Auth | null | undefined,
|
||||
websiteId: string,
|
||||
) {
|
||||
if (auth?.user) {
|
||||
return canViewWebsite(auth, websiteId);
|
||||
}
|
||||
|
||||
const { shareToken } = auth || {};
|
||||
|
||||
return shareTokenIncludesWebsite(auth, websiteId) && shareToken?.parameters?.allowFilter !== false;
|
||||
}
|
||||
|
||||
export async function canViewAuthenticatedWebsite(
|
||||
auth: Auth | null | undefined,
|
||||
websiteId: string,
|
||||
) {
|
||||
if (!auth?.user) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return canViewWebsite(auth, websiteId);
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { hasPermission } from '@/lib/auth';
|
||||
import { PERMISSIONS } from '@/lib/constants';
|
||||
import { getEntity } from '@/lib/entity';
|
||||
import prisma from '@/lib/prisma';
|
||||
import type { Auth } from '@/lib/types';
|
||||
import { getTeamUser, getWebsite } from '@/queries/prisma';
|
||||
|
||||
@@ -39,6 +40,76 @@ export async function canViewWebsite({ user, shareToken }: Auth, websiteId: stri
|
||||
return false;
|
||||
}
|
||||
|
||||
export async function canViewBatchWebsites({ user, shareToken }: Auth, websiteIds: string[]) {
|
||||
if (!websiteIds.length) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const requestedIds = Array.from(new Set(websiteIds));
|
||||
|
||||
if (user?.isAdmin) {
|
||||
return requestedIds;
|
||||
}
|
||||
|
||||
const shareAllowedIds = new Set(
|
||||
[
|
||||
shareToken?.websiteId,
|
||||
shareToken?.pixelId,
|
||||
shareToken?.linkId,
|
||||
...(shareToken?.websiteIds ?? []),
|
||||
...(shareToken?.pixelIds ?? []),
|
||||
...(shareToken?.linkIds ?? []),
|
||||
].filter((id): id is string => Boolean(id)),
|
||||
);
|
||||
|
||||
if (!user) {
|
||||
return requestedIds.filter(id => shareAllowedIds.has(id));
|
||||
}
|
||||
|
||||
const websites = await prisma.client.website.findMany({
|
||||
where: {
|
||||
id: {
|
||||
in: requestedIds,
|
||||
},
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
userId: true,
|
||||
teamId: true,
|
||||
},
|
||||
});
|
||||
|
||||
const ownedIds = new Set(
|
||||
websites.filter(website => website.userId === user.id).map(website => website.id),
|
||||
);
|
||||
const teamIds = Array.from(
|
||||
new Set(websites.map(website => website.teamId).filter((teamId): teamId is string => Boolean(teamId))),
|
||||
);
|
||||
const teamUsers = teamIds.length
|
||||
? await prisma.client.teamUser.findMany({
|
||||
where: {
|
||||
userId: user.id,
|
||||
teamId: {
|
||||
in: teamIds,
|
||||
},
|
||||
},
|
||||
select: {
|
||||
teamId: true,
|
||||
},
|
||||
})
|
||||
: [];
|
||||
const allowedTeamIds = new Set(teamUsers.map(teamUser => teamUser.teamId));
|
||||
const teamOwnedIds = new Set(
|
||||
websites
|
||||
.filter(website => website.teamId && allowedTeamIds.has(website.teamId))
|
||||
.map(website => website.id),
|
||||
);
|
||||
|
||||
return requestedIds.filter(
|
||||
id => shareAllowedIds.has(id) || ownedIds.has(id) || teamOwnedIds.has(id),
|
||||
);
|
||||
}
|
||||
|
||||
export async function canViewAllWebsites({ user }: Auth) {
|
||||
return user?.isAdmin ?? false;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user