mirror of
https://github.com/formbricks/formbricks.git
synced 2026-05-03 12:21:05 -05:00
a5fa876aa3
Co-authored-by: Piyush Gupta <piyushguptaa2z123@gmail.com> Co-authored-by: Piyush Gupta <56182734+gupta-piyush19@users.noreply.github.com> Co-authored-by: Victor Hugo dos Santos <115753265+victorvhs017@users.noreply.github.com> Co-authored-by: pandeymangg <anshuman.pandey9999@gmail.com> Co-authored-by: Matti Nannt <matti@formbricks.com> Co-authored-by: Matti Nannt <mail@matthiasnannt.com> Co-authored-by: Johannes <johannes@formbricks.com> Co-authored-by: Johannes <72809645+jobenjada@users.noreply.github.com>
52 lines
1.7 KiB
TypeScript
52 lines
1.7 KiB
TypeScript
import { getServerSession } from "next-auth";
|
|
import { redirect } from "next/navigation";
|
|
import { AuthorizationError } from "@formbricks/types/errors";
|
|
import { hasUserEnvironmentAccess } from "@/lib/environment/auth";
|
|
import { getMembershipByUserIdOrganizationId } from "@/lib/membership/service";
|
|
import { getAccessFlags } from "@/lib/membership/utils";
|
|
import { getOrganizationByEnvironmentId } from "@/lib/organization/service";
|
|
import { getProjectByEnvironmentId } from "@/lib/project/service";
|
|
import { getTranslate } from "@/lingodotdev/server";
|
|
import { authOptions } from "@/modules/auth/lib/authOptions";
|
|
|
|
const ConfigLayout = async (props) => {
|
|
const params = await props.params;
|
|
|
|
const { children } = props;
|
|
|
|
const t = await getTranslate();
|
|
const [organization, session] = await Promise.all([
|
|
getOrganizationByEnvironmentId(params.environmentId),
|
|
getServerSession(authOptions),
|
|
]);
|
|
|
|
if (!organization) {
|
|
throw new Error(t("common.organization_not_found"));
|
|
}
|
|
|
|
if (!session) {
|
|
throw new Error(t("common.session_not_found"));
|
|
}
|
|
|
|
const hasAccess = await hasUserEnvironmentAccess(session.user.id, params.environmentId);
|
|
if (!hasAccess) {
|
|
throw new AuthorizationError(t("common.not_authorized"));
|
|
}
|
|
|
|
const currentUserMembership = await getMembershipByUserIdOrganizationId(session.user.id, organization.id);
|
|
const { isBilling } = getAccessFlags(currentUserMembership?.role);
|
|
|
|
if (isBilling) {
|
|
return redirect(`/environments/${params.environmentId}/settings/billing`);
|
|
}
|
|
|
|
const project = await getProjectByEnvironmentId(params.environmentId);
|
|
if (!project) {
|
|
throw new Error(t("common.project_not_found"));
|
|
}
|
|
|
|
return children;
|
|
};
|
|
|
|
export default ConfigLayout;
|