feat(core): Allow admin creation (#7837)

https://linear.app/n8n/issue/PAY-1038
This commit is contained in:
Iván Ovejero
2023-11-29 13:55:41 +01:00
committed by GitHub
parent 5ba5ed8e3c
commit 476806ebb0
9 changed files with 318 additions and 136 deletions

View File

@@ -238,18 +238,19 @@ export class UserService {
);
}
public async inviteMembers(owner: User, emails: string[]) {
async inviteUsers(owner: User, attributes: Array<{ email: string; role: 'member' | 'admin' }>) {
const memberRole = await this.roleService.findGlobalMemberRole();
const adminRole = await this.roleService.findGlobalAdminRole();
const existingUsers = await this.findMany({
where: { email: In(emails) },
where: { email: In(attributes.map(({ email }) => email)) },
relations: ['globalRole'],
select: ['email', 'password', 'id'],
});
const existUsersEmails = existingUsers.map((user) => user.email);
const toCreateUsers = emails.filter((email) => !existUsersEmails.includes(email));
const toCreateUsers = attributes.filter(({ email }) => !existUsersEmails.includes(email));
const pendingUsersToInvite = existingUsers.filter((email) => email.isPending);
@@ -264,10 +265,10 @@ export class UserService {
try {
await this.getManager().transaction(async (transactionManager) =>
Promise.all(
toCreateUsers.map(async (email) => {
toCreateUsers.map(async ({ email, role }) => {
const newUser = Object.assign(new User(), {
email,
globalRole: memberRole,
globalRole: role === 'member' ? memberRole : adminRole,
});
const savedUser = await transactionManager.save<User>(newUser);
createdUsers.set(email, savedUser.id);
@@ -285,6 +286,6 @@ export class UserService {
const usersInvited = await this.sendEmails(owner, Object.fromEntries(createdUsers));
return { usersInvited, usersCreated: toCreateUsers };
return { usersInvited, usersCreated: toCreateUsers.map(({ email }) => email) };
}
}