Skip to content

Commit 07bf520

Browse files
committed
feat(organization): refine organization deletion logic with enhanced membership checks
- Added verification to ensure the user is a member of the organization before allowing deletion. - Implemented checks to confirm the user is either the organization owner or has the owner role. - Improved error handling to return a FORBIDDEN response if the user is not authorized to delete the organization.
1 parent c42e859 commit 07bf520

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

apps/dokploy/server/api/routers/organization.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,7 @@ export const organizationRouter = createTRPCRouter({
165165
}),
166166
)
167167
.mutation(async ({ ctx, input }) => {
168-
if (ctx.user.role !== "owner" && ctx.user.role !== "admin" && !IS_CLOUD) {
169-
throw new TRPCError({
170-
code: "FORBIDDEN",
171-
message: "Only the organization owner can delete it",
172-
});
173-
}
168+
// First, verify the organization exists
174169
const org = await db.query.organization.findFirst({
175170
where: eq(organization.id, input.organizationId),
176171
});
@@ -182,7 +177,27 @@ export const organizationRouter = createTRPCRouter({
182177
});
183178
}
184179

185-
if (org.ownerId !== ctx.user.id) {
180+
// Verify user is a member of this organization
181+
const userMember = await db.query.member.findFirst({
182+
where: and(
183+
eq(member.organizationId, input.organizationId),
184+
eq(member.userId, ctx.user.id),
185+
),
186+
});
187+
188+
if (!userMember) {
189+
throw new TRPCError({
190+
code: "FORBIDDEN",
191+
message: "You are not a member of this organization",
192+
});
193+
}
194+
195+
// Only owners can delete the organization
196+
// Verify the user is either the organization owner or has the owner role
197+
const isOwner =
198+
org.ownerId === ctx.user.id || userMember.role === "owner";
199+
200+
if (!isOwner) {
186201
throw new TRPCError({
187202
code: "FORBIDDEN",
188203
message: "Only the organization owner can delete it",

0 commit comments

Comments
 (0)