mirror of
https://github.com/andwati/dokploy.git
synced 2026-11-03 10:42:16 +00:00
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.
This commit is contained in:
@@ -165,12 +165,7 @@ export const organizationRouter = createTRPCRouter({
|
|||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
.mutation(async ({ ctx, input }) => {
|
.mutation(async ({ ctx, input }) => {
|
||||||
if (ctx.user.role !== "owner" && ctx.user.role !== "admin" && !IS_CLOUD) {
|
// First, verify the organization exists
|
||||||
throw new TRPCError({
|
|
||||||
code: "FORBIDDEN",
|
|
||||||
message: "Only the organization owner can delete it",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
const org = await db.query.organization.findFirst({
|
const org = await db.query.organization.findFirst({
|
||||||
where: eq(organization.id, input.organizationId),
|
where: eq(organization.id, input.organizationId),
|
||||||
});
|
});
|
||||||
@@ -182,7 +177,27 @@ export const organizationRouter = createTRPCRouter({
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (org.ownerId !== ctx.user.id) {
|
// Verify user is a member of this organization
|
||||||
|
const userMember = await db.query.member.findFirst({
|
||||||
|
where: and(
|
||||||
|
eq(member.organizationId, input.organizationId),
|
||||||
|
eq(member.userId, ctx.user.id),
|
||||||
|
),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!userMember) {
|
||||||
|
throw new TRPCError({
|
||||||
|
code: "FORBIDDEN",
|
||||||
|
message: "You are not a member of this organization",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only owners can delete the organization
|
||||||
|
// Verify the user is either the organization owner or has the owner role
|
||||||
|
const isOwner =
|
||||||
|
org.ownerId === ctx.user.id || userMember.role === "owner";
|
||||||
|
|
||||||
|
if (!isOwner) {
|
||||||
throw new TRPCError({
|
throw new TRPCError({
|
||||||
code: "FORBIDDEN",
|
code: "FORBIDDEN",
|
||||||
message: "Only the organization owner can delete it",
|
message: "Only the organization owner can delete it",
|
||||||
|
|||||||
Reference in New Issue
Block a user