Files
formbricks/packages/lib/product/auth.ts
Neil Chauhan cac02c77a1 feat: FOR-683 Role Switch (#1450)
Co-authored-by: Matthias Nannt <mail@matthiasnannt.com>
2023-11-05 14:26:03 +00:00

57 lines
1.8 KiB
TypeScript

import { ZId } from "@formbricks/types/environment";
import { validateInputs } from "../utils/validate";
import { getProduct } from "./service";
import { unstable_cache } from "next/cache";
import { getTeamsByUserId } from "../team/service";
import { SERVICES_REVALIDATION_INTERVAL } from "../constants";
import { productCache } from "./cache";
import { getMembershipByUserIdTeamId } from "../../lib/membership/service";
import { getAccessFlags } from "../../lib/membership/utils";
export const canUserAccessProduct = async (userId: string, productId: string): Promise<boolean> =>
await unstable_cache(
async () => {
validateInputs([userId, ZId], [productId, ZId]);
if (!userId || !productId) return false;
const product = await getProduct(productId);
if (!product) return false;
const teamIds = (await getTeamsByUserId(userId)).map((team) => team.id);
return teamIds.includes(product.teamId);
},
[`canUserAccessProduct-${userId}-${productId}`],
{
revalidate: SERVICES_REVALIDATION_INTERVAL,
tags: [productCache.tag.byId(productId), productCache.tag.byUserId(userId)],
}
)();
export const verifyUserRoleAccess = async (
teamId: string,
userId: string
): Promise<{
hasCreateOrUpdateAccess: boolean;
hasDeleteAccess: boolean;
}> => {
const accessObject = {
hasCreateOrUpdateAccess: true,
hasDeleteAccess: true,
};
if (!teamId) {
throw new Error("Team not found");
}
const currentUserMembership = await getMembershipByUserIdTeamId(userId, teamId);
const { isDeveloper, isViewer } = getAccessFlags(currentUserMembership?.role);
if (isDeveloper || isViewer) {
accessObject.hasCreateOrUpdateAccess = false;
accessObject.hasDeleteAccess = false;
}
return accessObject;
};