* fix: cache is not exportet from react * fix: format issue * wip: add usage of group permissions * feat: show inherited groups and add manage group * refactor: improve board access management * chore: address pull request feedback * fix: type issues * fix: migrations * test: add unit tests for board permissions, permissions and board router * test: add unit tests for board router and get current user permissions method * fix: format issues * fix: deepsource issue
49 lines
1.2 KiB
TypeScript
49 lines
1.2 KiB
TypeScript
import type { Session } from "@auth/core/types";
|
|
|
|
export type BoardPermissionsProps = (
|
|
| {
|
|
creator: {
|
|
id: string;
|
|
} | null;
|
|
}
|
|
| {
|
|
creatorId: string | null;
|
|
}
|
|
) & {
|
|
userPermissions: {
|
|
permission: string;
|
|
}[];
|
|
groupPermissions: {
|
|
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 ||
|
|
session?.user.permissions.includes("board-full-access"),
|
|
hasChangeAccess:
|
|
session?.user?.id === creatorId ||
|
|
board.userPermissions.some(
|
|
({ permission }) => permission === "board-change",
|
|
) ||
|
|
board.groupPermissions.some(
|
|
({ permission }) => permission === "board-change",
|
|
) ||
|
|
session?.user.permissions.includes("board-modify-all"),
|
|
hasViewAccess:
|
|
session?.user?.id === creatorId ||
|
|
board.userPermissions.length >= 1 ||
|
|
board.groupPermissions.length >= 1 ||
|
|
board.isPublic ||
|
|
session?.user.permissions.includes("board-view-all"),
|
|
};
|
|
};
|