Files
formbricks-formbricks/packages/lib/actionClass/auth.ts
Anshuman Pandey 53ef8771f3 feat: Make formbricks-js ready for public websites (#1470)
Co-authored-by: Matthias Nannt <mail@matthiasnannt.com>
Co-authored-by: Johannes <johannes@formbricks.com>
2023-11-12 09:12:58 +00:00

63 lines
1.9 KiB
TypeScript

import "server-only";
import { ZId } from "@formbricks/types/environment";
import { validateInputs } from "../utils/validate";
import { hasUserEnvironmentAccess } from "../environment/auth";
import { getActionClass } from "./service";
import { unstable_cache } from "next/cache";
import { SERVICES_REVALIDATION_INTERVAL } from "../constants";
import { actionClassCache } from "./cache";
import { getMembershipByUserIdTeamId } from "../../lib/membership/service";
import { getAccessFlags } from "../../lib/membership/utils";
import { getTeamByEnvironmentId } from "../../lib/team/service";
export const canUserUpdateActionClass = async (userId: string, actionClassId: string): Promise<boolean> =>
await unstable_cache(
async () => {
validateInputs([userId, ZId], [actionClassId, ZId]);
if (!userId) return false;
const actionClass = await getActionClass(actionClassId);
if (!actionClass) return false;
const hasAccessToEnvironment = await hasUserEnvironmentAccess(userId, actionClass.environmentId);
if (!hasAccessToEnvironment) return false;
return true;
},
[`users-${userId}-actionClasses-${actionClassId}`],
{
revalidate: SERVICES_REVALIDATION_INTERVAL,
tags: [actionClassCache.tag.byId(actionClassId)],
}
)();
export const verifyUserRoleAccess = async (
environmentId: string,
userId: string
): Promise<{
hasCreateOrUpdateAccess: boolean;
hasDeleteAccess: boolean;
}> => {
const accessObject = {
hasCreateOrUpdateAccess: true,
hasDeleteAccess: true,
};
const team = await getTeamByEnvironmentId(environmentId);
if (!team) {
throw new Error("Team not found");
}
const currentUserMembership = await getMembershipByUserIdTeamId(userId, team.id);
const { isViewer } = getAccessFlags(currentUserMembership?.role);
if (isViewer) {
accessObject.hasCreateOrUpdateAccess = false;
accessObject.hasDeleteAccess = false;
}
return accessObject;
};