From 2ae2c15083e4f92a66fdc91deb24a8adf166e45a Mon Sep 17 00:00:00 2001 From: Hariom Balhara Date: Mon, 22 Jan 2024 16:31:55 +0530 Subject: [PATCH] Validate that a user isnt a member of the organization before marking the username unavailable (#13354) --- packages/lib/server/username.ts | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/packages/lib/server/username.ts b/packages/lib/server/username.ts index 01d1ffd952..03487afca0 100644 --- a/packages/lib/server/username.ts +++ b/packages/lib/server/username.ts @@ -118,12 +118,28 @@ const usernameCheck = async (usernameRaw: string) => { const user = await prisma.user.findFirst({ where: { username, organizationId: null }, select: { + id: true, username: true, }, }); if (user) { - response.available = false; + const userIsAMemberOfAnOrg = await prisma.membership.findFirst({ + where: { + userId: user.id, + team: { + metadata: { + path: ["isOrganization"], + equals: true, + }, + }, + }, + }); + // When we invite an email, that doesn't match the orgAutoAcceptEmail, we create a user with organizationId=null. + // The only way to differentiate b/w 'a new email that was invited to an Org' and 'a user that was created using regular signup' is to check if the user is a member of an org. + if (!userIsAMemberOfAnOrg) { + response.available = false; + } } if (await isPremiumUserName(username)) {