mirror of
https://github.com/formbricks/formbricks.git
synced 2026-04-21 03:03:25 -05:00
ca67c4d5a8
Co-authored-by: Johannes <johannes@formbricks.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.workspace_not_found"));
|
|
}
|
|
|
|
return children;
|
|
};
|
|
|
|
export default ConfigLayout;
|