Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

Fix for inviteUserToOrganization for usernames with no email formats

carlosmonastyrski635f0277527e
diff --git a/backend/src/services/org/org-service.ts b/backend/src/services/org/org-service.ts
index bfd24e639e..4a72f12e89 100644
--- a/backend/src/services/org/org-service.ts
+++ b/backend/src/services/org/org-service.ts
@@ -835,16 +835,22 @@ export const orgServiceFactory = ({
// if the user doesn't exist we create the user with the email
if (!inviteeUser) {
- inviteeUser = await userDAL.create(
- {
- isAccepted: false,
- email: inviteeEmail,
- username: inviteeEmail,
- authMethods: [AuthMethod.EMAIL],
- isGhost: false
- },
- tx
- );
+ // TODO(carlos): will be removed once the function receives usernames instead of emails
+ const usersByEmail = await userDAL.findUserByEmail(inviteeEmail, tx);
+ if (usersByEmail?.length === 1) {
+ [inviteeUser] = usersByEmail;
+ } else {
+ inviteeUser = await userDAL.create(
+ {
+ isAccepted: false,
+ email: inviteeEmail,
+ username: inviteeEmail,
+ authMethods: [AuthMethod.EMAIL],
+ isGhost: false
+ },
+ tx
+ );
+ }
}
const inviteeUserId = inviteeUser?.id;
diff --git a/backend/src/services/user/user-dal.ts b/backend/src/services/user/user-dal.ts
index b5a29fc8cc..add1b4532c 100644
--- a/backend/src/services/user/user-dal.ts
+++ b/backend/src/services/user/user-dal.ts
@@ -21,6 +21,9 @@ export const userDALFactory = (db: TDbClient) => {
const findUserByUsername = async (username: string, tx?: Knex) =>
(tx || db)(TableName.Users).whereRaw('lower("username") = :username', { username: username.toLowerCase() });
+ const findUserByEmail = async (email: string, tx?: Knex) =>
+ (tx || db)(TableName.Users).whereRaw('lower("email") = :email', { email: email.toLowerCase() });
+
const getUsersByFilter = async ({
limit,
offset,
@@ -234,6 +237,7 @@ export const userDALFactory = (db: TDbClient) => {
findOneUserAction,
createUserAction,
getUsersByFilter,
- findAllMyAccounts
+ findAllMyAccounts,
+ findUserByEmail
};
};