feat: authorization in deletePerson action (#1063)

This commit is contained in:
Shubham Palriwala
2023-10-10 14:32:56 +05:30
committed by GitHub
parent 32449e6f69
commit 395ff50ac6
2 changed files with 37 additions and 0 deletions
@@ -1,7 +1,17 @@
"use server";
import { authOptions } from "@formbricks/lib/authOptions";
import { getServerSession } from "next-auth";
import { AuthorizationError } from "@formbricks/types/v1/errors";
import { deletePerson } from "@formbricks/lib/person/service";
import { canUserAccessPerson } from "@formbricks/lib/person/auth";
export const deletePersonAction = async (personId: string) => {
const session = await getServerSession(authOptions);
if (!session) throw new AuthorizationError("Not authorized");
const isAuthorized = await canUserAccessPerson(session.user.id, personId);
if (!isAuthorized) throw new AuthorizationError("Not authorized");
await deletePerson(personId);
};
+27
View File
@@ -0,0 +1,27 @@
import "server-only";
import { ZId } from "@formbricks/types/v1/environment";
import { validateInputs } from "../utils/validate";
import { hasUserEnvironmentAccess } from "../environment/auth";
import { getPersonCached } from "./service";
import { unstable_cache } from "next/cache";
import { SERVICES_REVALIDATION_INTERVAL } from "../constants";
export const canUserAccessPerson = async (userId: string, personId: string): Promise<boolean> =>
await unstable_cache(
async () => {
validateInputs([userId, ZId], [personId, ZId]);
if (!userId) return false;
const person = await getPersonCached(personId);
if (!person) return false;
const hasAccessToEnvironment = await hasUserEnvironmentAccess(userId, person.environmentId);
if (!hasAccessToEnvironment) return false;
return true;
},
[`users-${userId}-persons-${personId}`],
{ revalidate: SERVICES_REVALIDATION_INTERVAL, tags: [`persons-${personId}`] }
)();