mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-08 08:27:53 +00:00
Improvements
This commit is contained in:
@@ -94,6 +94,14 @@ export const userDALFactory = (db: TDbClient) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const findMergeableUsers = async (email: string, tx?: Knex) => {
|
||||||
|
const users = await (tx || db)(TableName.Users).where((builder) => {
|
||||||
|
void builder.where({ email, isEmailVerified: true }).orWhere({ email, isAccepted: false });
|
||||||
|
});
|
||||||
|
|
||||||
|
return users;
|
||||||
|
};
|
||||||
|
|
||||||
const updateUserEncryptionByUserId = async (userId: string, data: TUserEncryptionKeysUpdate, tx?: Knex) => {
|
const updateUserEncryptionByUserId = async (userId: string, data: TUserEncryptionKeysUpdate, tx?: Knex) => {
|
||||||
try {
|
try {
|
||||||
const [userEnc] = await (tx || db)(TableName.UserEncryptionKey)
|
const [userEnc] = await (tx || db)(TableName.UserEncryptionKey)
|
||||||
@@ -154,6 +162,7 @@ export const userDALFactory = (db: TDbClient) => {
|
|||||||
findUsersByProjectMembershipIds,
|
findUsersByProjectMembershipIds,
|
||||||
upsertUserEncryptionKey,
|
upsertUserEncryptionKey,
|
||||||
createUserEncryption,
|
createUserEncryption,
|
||||||
|
findMergeableUsers,
|
||||||
findOneUserAction,
|
findOneUserAction,
|
||||||
createUserAction
|
createUserAction
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ type TUserServiceFactoryDep = {
|
|||||||
userDAL: Pick<
|
userDAL: Pick<
|
||||||
TUserDALFactory,
|
TUserDALFactory,
|
||||||
| "find"
|
| "find"
|
||||||
|
| "findMergeableUsers"
|
||||||
| "findOne"
|
| "findOne"
|
||||||
| "findById"
|
| "findById"
|
||||||
| "transaction"
|
| "transaction"
|
||||||
@@ -86,13 +87,7 @@ export const userServiceFactory = ({
|
|||||||
);
|
);
|
||||||
|
|
||||||
// check if there are users with the same email.
|
// check if there are users with the same email.
|
||||||
const users = await userDAL.find(
|
const users = await userDAL.findMergeableUsers(email, tx);
|
||||||
{
|
|
||||||
email,
|
|
||||||
isEmailVerified: true
|
|
||||||
},
|
|
||||||
{ tx }
|
|
||||||
);
|
|
||||||
|
|
||||||
if (users.length > 1) {
|
if (users.length > 1) {
|
||||||
// merge users
|
// merge users
|
||||||
@@ -134,59 +129,14 @@ export const userServiceFactory = ({
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
const existingUserWithUsername = await userDAL.findOne({
|
// update current user's username to [email]
|
||||||
username: email
|
await userDAL.updateById(
|
||||||
});
|
user.id,
|
||||||
|
{
|
||||||
if (existingUserWithUsername && existingUserWithUsername.id !== user.id) {
|
username: email
|
||||||
// merge users
|
},
|
||||||
const mergeUserOrgMembershipSet = new Set(
|
tx
|
||||||
(await orgMembershipDAL.find({ userId: existingUserWithUsername.id }, { tx })).map((m) => m.orgId)
|
);
|
||||||
);
|
|
||||||
|
|
||||||
const myOrgMemberships = (await orgMembershipDAL.find({ userId: user.id }, { tx })).filter(
|
|
||||||
(m) => !mergeUserOrgMembershipSet.has(m.orgId)
|
|
||||||
);
|
|
||||||
|
|
||||||
const userAliases = await userAliasDAL.find(
|
|
||||||
{
|
|
||||||
userId: user.id
|
|
||||||
},
|
|
||||||
{ tx }
|
|
||||||
);
|
|
||||||
|
|
||||||
// delete current user
|
|
||||||
await userDAL.deleteById(user.id, tx);
|
|
||||||
|
|
||||||
if (myOrgMemberships.length) {
|
|
||||||
await orgMembershipDAL.insertMany(
|
|
||||||
myOrgMemberships.map((orgMembership) => ({
|
|
||||||
...orgMembership,
|
|
||||||
userId: existingUserWithUsername.id
|
|
||||||
})),
|
|
||||||
tx
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (userAliases.length) {
|
|
||||||
await userAliasDAL.insertMany(
|
|
||||||
userAliases.map((userAlias) => ({
|
|
||||||
...userAlias,
|
|
||||||
userId: existingUserWithUsername.id
|
|
||||||
})),
|
|
||||||
tx
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// update current user's username to [email]
|
|
||||||
await userDAL.updateById(
|
|
||||||
user.id,
|
|
||||||
{
|
|
||||||
username: email
|
|
||||||
},
|
|
||||||
tx
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user