From bab182960f301d327c6b21a1a049cf4470e847cc Mon Sep 17 00:00:00 2001 From: Hariom Balhara Date: Tue, 6 Feb 2024 18:22:40 +0530 Subject: [PATCH] Fix admin being able to update owner details (#13534) --- .../routers/viewer/organizations/updateUser.handler.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/trpc/server/routers/viewer/organizations/updateUser.handler.ts b/packages/trpc/server/routers/viewer/organizations/updateUser.handler.ts index b83a10c60f..7c39576e1a 100644 --- a/packages/trpc/server/routers/viewer/organizations/updateUser.handler.ts +++ b/packages/trpc/server/routers/viewer/organizations/updateUser.handler.ts @@ -37,8 +37,16 @@ export const updateUserHandler = async ({ ctx, input }: UpdateUserOptions) => { if (!(await isOrganisationAdmin(userId, organizationId))) throw new TRPCError({ code: "UNAUTHORIZED" }); + const isUpdaterAnOwner = await isOrganisationOwner(userId, organizationId); // only OWNER can update the role to OWNER - if (input.role === MembershipRole.OWNER && !(await isOrganisationOwner(userId, organizationId))) { + if (input.role === MembershipRole.OWNER && !isUpdaterAnOwner) { + throw new TRPCError({ code: "UNAUTHORIZED" }); + } + + const isUserBeingUpdatedOwner = await isOrganisationOwner(input.userId, organizationId); + + // only owner can update the role of another owner + if (isUserBeingUpdatedOwner && !isUpdaterAnOwner) { throw new TRPCError({ code: "UNAUTHORIZED" }); }