Files
formbricks-formbricks/packages/lib/auth.ts
Anshuman Pandey 892776c493 refactor: moves team settings to server components (#693)
* feat: moves edit team name to server components

* feat: server components for membership roles

* feat: adds server actions and services

* fix: fixes invite server action

* feat: adds packages for jwt and email

* feat: server actions

* feat: moves edit memberships logic to server components

* feat: moves delete team logic to server components

* fix: fixes team loading states

* rename getAllMembershipsByUserId -> getMembershipsByUserId

* remove cache from mutating functions

* remove cache from updateInvite

* refactoring

* fix build error

---------

Co-authored-by: Matthias Nannt <mail@matthiasnannt.com>
2023-09-18 17:32:42 +09:00

82 lines
1.8 KiB
TypeScript

import { prisma } from "@formbricks/database";
import { AuthenticationError } from "@formbricks/types/v1/errors";
export const hasTeamAccess = async (userId: string, teamId: string) => {
const membership = await prisma.membership.findUnique({
where: {
userId_teamId: {
userId,
teamId,
},
},
});
if (membership) {
return true;
}
return false;
};
export const isAdminOrOwner = async (userId: string, teamId: string) => {
const membership = await prisma.membership.findUnique({
where: {
userId_teamId: {
userId,
teamId,
},
},
});
if (membership && (membership.role === "admin" || membership.role === "owner")) {
return true;
}
return false;
};
export const isOwner = async (userId: string, teamId: string) => {
const membership = await prisma.membership.findUnique({
where: {
userId_teamId: {
userId,
teamId,
},
},
});
if (membership && membership.role === "owner") {
return true;
}
return false;
};
export const hasTeamAuthority = async (userId: string, teamId: string) => {
const hasAccess = await hasTeamAccess(userId, teamId);
if (!hasAccess) {
throw new AuthenticationError("Not authorized");
}
const isAdminOrOwnerAccess = await isAdminOrOwner(userId, teamId);
if (!isAdminOrOwnerAccess) {
throw new AuthenticationError("You are not the admin or owner of this team");
}
return true;
};
export const hasTeamOwnership = async (userId: string, teamId: string) => {
const hasAccess = await hasTeamAccess(userId, teamId);
if (!hasAccess) {
throw new AuthenticationError("Not authorized");
}
const isOwnerAccess = await isOwner(userId, teamId);
if (!isOwnerAccess) {
throw new AuthenticationError("You are not the owner of this team");
}
return true;
};