Tighten API access checks by website section and share permissions
This commit is contained in:
1 parent
8bc49a29e3
commit
ade0819be1
69 files changed
+303
-188
No files matched your search
@@ -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';
|
||||
@@ -0,0 +1,74 @@
|
||||
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';
|
||||
|
||||
type ShareSectionInput = ShareSection | ShareSection[];
|
||||
|
||||
function shareTokenIncludesWebsite(auth: Auth | null | undefined, websiteId: string) {
|
||||
const { shareToken } = auth || {};
|
||||
|
||||
return shareToken?.websiteId === websiteId || shareToken?.websiteIds?.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 ||
|
||||
shareToken.shareType !== ENTITY_TYPE.website ||
|
||||
!shareTokenIncludesWebsite(auth, websiteId)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const sections = Array.isArray(section) ? section : [section];
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
const { shareToken } = auth || {};
|
||||
|
||||
return (
|
||||
shareToken?.shareType === ENTITY_TYPE.website && shareTokenIncludesWebsite(auth, websiteId)
|
||||
);
|
||||
}
|
||||
|
||||
export async function canViewAuthenticatedWebsite(
|
||||
auth: Auth | null | undefined,
|
||||
websiteId: string,
|
||||
) {
|
||||
if (!auth?.user) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return canViewWebsite(auth, websiteId);
|
||||
}
|
||||
Reference in new issue
Block a user