mirror of
https://github.com/umami-software/umami.git
synced 2026-02-06 21:57:16 +01:00
share api, queries, permissions, migration, entity lib
Some checks are pending
Node.js CI / build (push) Waiting to run
Some checks are pending
Node.js CI / build (push) Waiting to run
This commit is contained in:
parent
a270b0afea
commit
29f2c7b7d4
11 changed files with 256 additions and 23 deletions
65
src/permissions/entity.ts
Normal file
65
src/permissions/entity.ts
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
import { hasPermission } from '@/lib/auth';
|
||||
import { PERMISSIONS } from '@/lib/constants';
|
||||
import { getEntity } from '@/lib/entity';
|
||||
import type { Auth } from '@/lib/types';
|
||||
import { getTeamUser } from '@/queries/prisma';
|
||||
|
||||
export async function canViewEntity({ user }: Auth, entityId: string) {
|
||||
if (user?.isAdmin) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const entity = await getEntity(entityId);
|
||||
|
||||
if (entity.userId) {
|
||||
return user.id === entity.userId;
|
||||
}
|
||||
|
||||
if (entity.teamId) {
|
||||
const teamUser = await getTeamUser(entity.teamId, user.id);
|
||||
|
||||
return !!teamUser;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
export async function canUpdateEntity({ user }: Auth, entityId: string) {
|
||||
if (user.isAdmin) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const entity = await getEntity(entityId);
|
||||
|
||||
if (entity.userId) {
|
||||
return user.id === entity.userId;
|
||||
}
|
||||
|
||||
if (entity.teamId) {
|
||||
const teamUser = await getTeamUser(entity.teamId, user.id);
|
||||
|
||||
return teamUser && hasPermission(teamUser.role, PERMISSIONS.websiteUpdate);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
export async function canDeleteEntity({ user }: Auth, entityId: string) {
|
||||
if (user.isAdmin) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const entity = await getEntity(entityId);
|
||||
|
||||
if (entity.userId) {
|
||||
return user.id === entity.userId;
|
||||
}
|
||||
|
||||
if (entity.teamId) {
|
||||
const teamUser = await getTeamUser(entity.teamId, user.id);
|
||||
|
||||
return teamUser && hasPermission(teamUser.role, PERMISSIONS.websiteDelete);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
|
@ -1,3 +1,4 @@
|
|||
export * from './entity';
|
||||
export * from './link';
|
||||
export * from './pixel';
|
||||
export * from './report';
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue