Skip to content

Commit e666cfb

Browse files
committed
feat(organization): enhance organization update logic with member verification
- Added checks to ensure the organization exists before allowing updates. - Implemented user membership verification to restrict updates to organization members only. - Ensured that only the organization owner or users with the owner role can perform updates.
1 parent 1d9b9ff commit e666cfb

1 file changed

Lines changed: 34 additions & 3 deletions

File tree

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

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ export const organizationRouter = createTRPCRouter({
3232
.returning()
3333
.then((res) => res[0]);
3434

35-
console.log("result", result);
36-
3735
if (!result) {
3836
throw new TRPCError({
3937
code: "INTERNAL_SERVER_ERROR",
@@ -96,12 +94,45 @@ export const organizationRouter = createTRPCRouter({
9694
}),
9795
)
9896
.mutation(async ({ ctx, input }) => {
99-
if (ctx.user.role !== "owner" && ctx.user.role !== "admin" && !IS_CLOUD) {
97+
// First, verify the organization exists
98+
const org = await db.query.organization.findFirst({
99+
where: eq(organization.id, input.organizationId),
100+
});
101+
102+
if (!org) {
103+
throw new TRPCError({
104+
code: "NOT_FOUND",
105+
message: "Organization not found",
106+
});
107+
}
108+
109+
// Verify user is a member of this organization
110+
const userMember = await db.query.member.findFirst({
111+
where: and(
112+
eq(member.organizationId, input.organizationId),
113+
eq(member.userId, ctx.user.id),
114+
),
115+
});
116+
117+
if (!userMember) {
118+
throw new TRPCError({
119+
code: "FORBIDDEN",
120+
message: "You are not a member of this organization",
121+
});
122+
}
123+
124+
// Only owners can update the organization
125+
// Verify the user is either the organization owner or has the owner role
126+
const isOwner =
127+
org.ownerId === ctx.user.id || userMember.role === "owner";
128+
129+
if (!isOwner) {
100130
throw new TRPCError({
101131
code: "FORBIDDEN",
102132
message: "Only the organization owner can update it",
103133
});
104134
}
135+
105136
const result = await db
106137
.update(organization)
107138
.set({

0 commit comments

Comments
 (0)