Files
Homarr/packages/auth/permissions/board-permissions.ts
Meier Lukas 7ab9dc2501 feat: implement board access control (#349)
* feat: implement board access control

* fix: deepsource issues

* wip: address pull request feedback

* chore: address pull request feedback

* fix: format issue

* test: improve tests

* fix: type and lint issue

* chore: address pull request feedback

* refactor: rename board procedures
2024-04-30 21:32:55 +02:00

36 lines
799 B
TypeScript

import type { Session } from "@auth/core/types";
export type BoardPermissionsProps = (
| {
creator: {
id: string;
} | null;
}
| {
creatorId: string | null;
}
) & {
permissions: {
permission: string;
}[];
isPublic: boolean;
};
export const constructBoardPermissions = (
board: BoardPermissionsProps,
session: Session | null,
) => {
const creatorId = "creator" in board ? board.creator?.id : board.creatorId;
return {
hasFullAccess: session?.user?.id === creatorId,
hasChangeAccess:
session?.user?.id === creatorId ||
board.permissions.some(({ permission }) => permission === "board-change"),
hasViewAccess:
session?.user?.id === creatorId ||
board.permissions.length >= 1 ||
board.isPublic,
};
};