feat: Add user management invite links without SMTP set up (#5084)

* feat: update n8n-users-list to no longer use preset list of actions

* feat: prepared users settings for invite links feature

* refactor: Return invite link URLs when inviting users (#5079)

* refactor: Return invite link URLs when inviting users

* test: Refactor and add tests to mailer

* feat: Add FE inviteAcceptUrl integration (#5085)

* feat: update n8n-users-list to no longer use preset list of actions

* feat: prepared users settings for invite links feature

* feat: add integration with new inviteAcceptUrl changes

* feat: Add inviteAcceptUrl to user list for pending users

Co-authored-by: Alex Grozav <alex@grozav.com>

* fix conflicts

* fix lint issue

* test: Make sure inviteAcceptUrl is defined

* feat: update smtp setup suggestion

* feat: add invite link summary when inviting multiple users

* refactor: Add telemetry flag for when email is sent

* fix: add email_sent correctly to telemetry event

* feat: move SMTP info-tip to invite modal

Co-authored-by: Omar Ajoue <krynble@gmail.com>
This commit is contained in:
Alex Grozav 2023-01-05 17:10:08 +02:00 committed by GitHub
parent 11a46a4cbc
commit 2327563c44
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
22 changed files with 419 additions and 247 deletions

View file

@ -436,6 +436,7 @@ export class InternalHooksClass implements IInternalHooksClass {
user: User;
target_user_id: string[];
public_api: boolean;
email_sent: boolean;
}): Promise<void> {
void Promise.all([
eventBus.sendAuditEvent({
@ -449,6 +450,7 @@ export class InternalHooksClass implements IInternalHooksClass {
user_id: userInviteData.user.id,
target_user_id: userInviteData.target_user_id,
public_api: userInviteData.public_api,
email_sent: userInviteData.email_sent,
}),
]);
}

View file

@ -23,6 +23,7 @@ export interface PublicUser {
passwordResetToken?: string;
createdAt: Date;
isPending: boolean;
inviteAcceptUrl?: string;
}
export interface N8nApp {

View file

@ -99,6 +99,10 @@ export function getInstanceBaseUrl(): string {
return n8nBaseUrl.endsWith('/') ? n8nBaseUrl.slice(0, n8nBaseUrl.length - 1) : n8nBaseUrl;
}
export function generateUserInviteUrl(inviterId: string, inviteeId: string): string {
return `${getInstanceBaseUrl()}/signup?inviterId=${inviterId}&inviteeId=${inviteeId}`;
}
// TODO: Enforce at model level
export function validatePassword(password?: string): string {
if (!password) {
@ -156,6 +160,13 @@ export function sanitizeUser(user: User, withoutKeys?: string[]): PublicUser {
return sanitizedUser;
}
export function addInviteLinktoUser(user: PublicUser, inviterId: string): PublicUser {
if (user.isPending) {
user.inviteAcceptUrl = generateUserInviteUrl(inviterId, user.id);
}
return user;
}
export async function getUserById(userId: string): Promise<User> {
const user = await Db.collections.User.findOneOrFail(userId, {
relations: ['globalRole'],

View file

@ -1,4 +1,5 @@
export interface UserManagementMailerImplementation {
init: () => Promise<void>;
sendMail: (mailData: MailData) => Promise<SendEmailResult>;
verifyConnection: () => Promise<void>;
}
@ -20,8 +21,7 @@ export type PasswordResetData = {
};
export type SendEmailResult = {
success: boolean;
error?: Error;
emailSent: boolean;
};
export type MailData = {

View file

@ -5,9 +5,9 @@ import config from '@/config';
import { MailData, SendEmailResult, UserManagementMailerImplementation } from './Interfaces';
export class NodeMailer implements UserManagementMailerImplementation {
private transport: Transporter;
private transport?: Transporter;
constructor() {
async init(): Promise<void> {
this.transport = createTransport({
host: config.getEnv('userManagement.emails.smtp.host'),
port: config.getEnv('userManagement.emails.smtp.port'),
@ -20,12 +20,15 @@ export class NodeMailer implements UserManagementMailerImplementation {
}
async verifyConnection(): Promise<void> {
if (!this.transport) {
await this.init();
}
const host = config.getEnv('userManagement.emails.smtp.host');
const user = config.getEnv('userManagement.emails.smtp.auth.user');
const pass = config.getEnv('userManagement.emails.smtp.auth.pass');
try {
await this.transport.verify();
await this.transport?.verify();
} catch (error) {
const message: string[] = [];
if (!host) message.push('SMTP host not defined (N8N_SMTP_HOST).');
@ -36,6 +39,9 @@ export class NodeMailer implements UserManagementMailerImplementation {
}
async sendMail(mailData: MailData): Promise<SendEmailResult> {
if (!this.transport) {
await this.init();
}
let sender = config.getEnv('userManagement.emails.smtp.sender');
const user = config.getEnv('userManagement.emails.smtp.auth.user');
@ -44,7 +50,7 @@ export class NodeMailer implements UserManagementMailerImplementation {
}
try {
await this.transport.sendMail({
await this.transport?.sendMail({
from: sender,
to: mailData.emailRecipients,
subject: mailData.subject,
@ -57,12 +63,9 @@ export class NodeMailer implements UserManagementMailerImplementation {
} catch (error) {
ErrorReporter.error(error);
Logger.error('Failed to send email', { recipients: mailData.emailRecipients, error });
return {
success: false,
error,
};
throw error;
}
return { success: true };
return { emailSent: true };
}
}

View file

@ -44,57 +44,52 @@ export class UserManagementMailer {
constructor() {
// Other implementations can be used in the future.
if (config.getEnv('userManagement.emails.mode') === 'smtp') {
if (
config.getEnv('userManagement.emails.mode') === 'smtp' &&
config.getEnv('userManagement.emails.smtp.host') !== ''
) {
this.mailer = new NodeMailer();
}
}
async verifyConnection(): Promise<void> {
if (!this.mailer) return Promise.reject();
if (!this.mailer) throw new Error('No mailer configured.');
return this.mailer.verifyConnection();
}
async invite(inviteEmailData: InviteEmailData): Promise<SendEmailResult> {
if (!this.mailer) return Promise.reject();
const template = await getTemplate('invite');
const result = await this.mailer.sendMail({
const result = await this.mailer?.sendMail({
emailRecipients: inviteEmailData.email,
subject: 'You have been invited to n8n',
body: template(inviteEmailData),
});
// If mailer does not exist it means mail has been disabled.
return result ?? { success: true };
// No error, just say no email was sent.
return result ?? { emailSent: false };
}
async passwordReset(passwordResetData: PasswordResetData): Promise<SendEmailResult> {
if (!this.mailer) return Promise.reject();
const template = await getTemplate('passwordReset');
const result = await this.mailer.sendMail({
const result = await this.mailer?.sendMail({
emailRecipients: passwordResetData.email,
subject: 'n8n password reset',
body: template(passwordResetData),
});
// If mailer does not exist it means mail has been disabled.
return result ?? { success: true };
// No error, just say no email was sent.
return result ?? { emailSent: false };
}
}
let mailerInstance: UserManagementMailer | undefined;
export async function getInstance(): Promise<UserManagementMailer> {
export function getInstance(): UserManagementMailer {
if (mailerInstance === undefined) {
mailerInstance = new UserManagementMailer();
try {
await mailerInstance.verifyConnection();
} catch (error) {
mailerInstance = undefined;
throw error;
}
}
return mailerInstance;
}

View file

@ -76,7 +76,7 @@ export function passwordResetNamespace(this: N8nApp): void {
url.searchParams.append('token', resetPasswordToken);
try {
const mailer = await UserManagementMailer.getInstance();
const mailer = UserManagementMailer.getInstance();
await mailer.passwordReset({
email,
firstName,

View file

@ -14,6 +14,8 @@ import { UserRequest } from '@/requests';
import * as UserManagementMailer from '../email/UserManagementMailer';
import { N8nApp, PublicUser } from '../Interfaces';
import {
addInviteLinktoUser,
generateUserInviteUrl,
getInstanceBaseUrl,
hashPassword,
isEmailSetUp,
@ -34,25 +36,7 @@ export function usersNamespace(this: N8nApp): void {
this.app.post(
`/${this.restEndpoint}/users`,
ResponseHelper.send(async (req: UserRequest.Invite) => {
if (config.getEnv('userManagement.emails.mode') === '') {
Logger.debug(
'Request to send email invite(s) to user(s) failed because emailing was not set up',
);
throw new ResponseHelper.InternalServerError(
'Email sending must be set up in order to request a password reset email',
);
}
let mailer: UserManagementMailer.UserManagementMailer | undefined;
try {
mailer = await UserManagementMailer.getInstance();
} catch (error) {
if (error instanceof Error) {
throw new ResponseHelper.InternalServerError(
`There is a problem with your SMTP setup! ${error.message}`,
);
}
}
const mailer = UserManagementMailer.getInstance();
// TODO: this should be checked in the middleware rather than here
if (isUserManagementDisabled()) {
@ -143,19 +127,13 @@ export function usersNamespace(this: N8nApp): void {
}),
);
});
void InternalHooksManager.getInstance().onUserInvite({
user: req.user,
target_user_id: Object.values(createUsers) as string[],
public_api: false,
});
} catch (error) {
ErrorReporter.error(error);
Logger.error('Failed to create user shells', { userShells: createUsers });
throw new ResponseHelper.InternalServerError('An error occurred during user creation');
}
Logger.info('Created user shell(s) successfully', { userId: req.user.id });
Logger.debug('Created user shell(s) successfully', { userId: req.user.id });
Logger.verbose(total > 1 ? `${total} user shells created` : '1 user shell created', {
userShells: createUsers,
});
@ -168,39 +146,61 @@ export function usersNamespace(this: N8nApp): void {
const emailingResults = await Promise.all(
usersPendingSetup.map(async ([email, id]) => {
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
const inviteAcceptUrl = `${baseUrl}/signup?inviterId=${req.user.id}&inviteeId=${id}`;
const result = await mailer?.invite({
email,
inviteAcceptUrl,
domain: baseUrl,
});
const resp: { user: { id: string | null; email: string }; error?: string } = {
if (!id) {
// This should never happen since those are removed from the list before reaching this point
throw new ResponseHelper.InternalServerError(
'User ID is missing for user with email address',
);
}
const inviteAcceptUrl = generateUserInviteUrl(req.user.id, id);
const resp: {
user: { id: string | null; email: string; inviteAcceptUrl: string; emailSent: boolean };
error?: string;
} = {
user: {
id,
email,
inviteAcceptUrl,
emailSent: false,
},
};
if (result?.success) {
void InternalHooksManager.getInstance().onUserTransactionalEmail({
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
user_id: id!,
message_type: 'New user invite',
public_api: false,
});
} else {
void InternalHooksManager.getInstance().onEmailFailed({
user: req.user,
message_type: 'New user invite',
public_api: false,
});
Logger.error('Failed to send email', {
userId: req.user.id,
try {
const result = await mailer.invite({
email,
inviteAcceptUrl,
domain: baseUrl,
email,
});
resp.error = 'Email could not be sent';
if (result.emailSent) {
resp.user.emailSent = true;
void InternalHooksManager.getInstance().onUserTransactionalEmail({
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
user_id: id,
message_type: 'New user invite',
public_api: false,
});
}
void InternalHooksManager.getInstance().onUserInvite({
user: req.user,
target_user_id: Object.values(createUsers) as string[],
public_api: false,
email_sent: result.emailSent,
});
} catch (error) {
if (error instanceof Error) {
void InternalHooksManager.getInstance().onEmailFailed({
user: req.user,
message_type: 'New user invite',
public_api: false,
});
Logger.error('Failed to send email', {
userId: req.user.id,
inviteAcceptUrl,
domain: baseUrl,
email,
});
resp.error = error.message;
}
}
return resp;
}),
@ -361,10 +361,13 @@ export function usersNamespace(this: N8nApp): void {
this.app.get(
`/${this.restEndpoint}/users`,
ResponseHelper.send(async () => {
ResponseHelper.send(async (req: UserRequest.List) => {
const users = await Db.collections.User.find({ relations: ['globalRole'] });
return users.map((user): PublicUser => sanitizeUser(user, ['personalizationAnswers']));
return users.map(
(user): PublicUser =>
addInviteLinktoUser(sanitizeUser(user, ['personalizationAnswers']), req.user.id),
);
}),
);
@ -563,22 +566,27 @@ export function usersNamespace(this: N8nApp): void {
const baseUrl = getInstanceBaseUrl();
const inviteAcceptUrl = `${baseUrl}/signup?inviterId=${req.user.id}&inviteeId=${reinvitee.id}`;
let mailer: UserManagementMailer.UserManagementMailer | undefined;
const mailer = UserManagementMailer.getInstance();
try {
mailer = await UserManagementMailer.getInstance();
} catch (error) {
if (error instanceof Error) {
throw new ResponseHelper.InternalServerError(error.message);
const result = await mailer.invite({
email: reinvitee.email,
inviteAcceptUrl,
domain: baseUrl,
});
if (result.emailSent) {
void InternalHooksManager.getInstance().onUserReinvite({
user: req.user,
target_user_id: reinvitee.id,
public_api: false,
});
void InternalHooksManager.getInstance().onUserTransactionalEmail({
user_id: reinvitee.id,
message_type: 'Resend invite',
public_api: false,
});
}
}
const result = await mailer?.invite({
email: reinvitee.email,
inviteAcceptUrl,
domain: baseUrl,
});
if (!result?.success) {
} catch (error) {
void InternalHooksManager.getInstance().onEmailFailed({
user: reinvitee,
message_type: 'Resend invite',
@ -591,19 +599,6 @@ export function usersNamespace(this: N8nApp): void {
});
throw new ResponseHelper.InternalServerError(`Failed to send email to ${reinvitee.email}`);
}
void InternalHooksManager.getInstance().onUserReinvite({
user: reinvitee,
target_user_id: reinvitee.id,
public_api: false,
});
void InternalHooksManager.getInstance().onUserTransactionalEmail({
user_id: reinvitee.id,
message_type: 'Resend invite',
public_api: false,
});
return { success: true };
}),
);

View file

@ -197,6 +197,8 @@ export declare namespace PasswordResetRequest {
// ----------------------------------
export declare namespace UserRequest {
export type List = AuthenticatedRequest;
export type Invite = AuthenticatedRequest<{}, {}, Array<{ email: string }>>;
export type ResolveSignUp = AuthlessRequest<

View file

@ -65,7 +65,8 @@ beforeEach(async () => {
config.set('userManagement.disabled', false);
config.set('userManagement.isInstanceOwnerSetUp', true);
config.set('userManagement.emails.mode', '');
config.set('userManagement.emails.mode', 'smtp');
config.set('userManagement.emails.smtp.host', '');
});
afterAll(async () => {
@ -432,26 +433,28 @@ test('POST /users/:id should fail with already accepted invite', async () => {
expect(storedMember.password).not.toBe(newMemberData.password);
});
test('POST /users should fail if emailing is not set up', async () => {
test('POST /users should succeed if emailing is not set up', async () => {
const owner = await testDb.createUser({ globalRole: globalOwnerRole });
const response = await authAgent(owner)
.post('/users')
.send([{ email: randomEmail() }]);
expect(response.statusCode).toBe(500);
expect(response.statusCode).toBe(200);
expect(response.body.data[0].user.inviteAcceptUrl).toBeDefined();
});
test('POST /users should fail if user management is disabled', async () => {
const owner = await testDb.createUser({ globalRole: globalOwnerRole });
config.set('userManagement.disabled', true);
config.set('userManagement.isInstanceOwnerSetUp', false);
const response = await authAgent(owner)
.post('/users')
.send([{ email: randomEmail() }]);
expect(response.statusCode).toBe(500);
expect(response.statusCode).toBe(400);
});
test('POST /users should email invites and create user shells but ignore existing', async () => {
@ -567,16 +570,34 @@ test('POST /users/:id/reinvite should send reinvite, but fail if user already ac
expect(reinviteMemberResponse.statusCode).toBe(400);
});
test('UserManagementMailer expect NodeMailer.verifyConnection have been called', async () => {
jest.spyOn(NodeMailer.prototype, 'verifyConnection').mockImplementation(async () => {});
test('UserManagementMailer expect NodeMailer.verifyConnection not be called when SMTP not set up', async () => {
const mockVerifyConnection = jest.spyOn(NodeMailer.prototype, 'verifyConnection');
mockVerifyConnection.mockImplementation(async () => {});
// NodeMailer.verifyConnection called 1 time
const userManagementMailer = UserManagementMailer.getInstance();
// NodeMailer.verifyConnection called 2 time
(await userManagementMailer).verifyConnection();
// NodeMailer.verifyConnection gets called only explicitly
expect(async () => await userManagementMailer.verifyConnection()).rejects.toThrow();
expect(NodeMailer.prototype.verifyConnection).toHaveBeenCalledTimes(2);
expect(NodeMailer.prototype.verifyConnection).toHaveBeenCalledTimes(0);
// @ts-ignore
NodeMailer.prototype.verifyConnection.mockRestore();
mockVerifyConnection.mockRestore();
});
test('UserManagementMailer expect NodeMailer.verifyConnection to be called when SMTP set up', async () => {
const mockVerifyConnection = jest.spyOn(NodeMailer.prototype, 'verifyConnection');
mockVerifyConnection.mockImplementation(async () => {});
const mockInit = jest.spyOn(NodeMailer.prototype, 'init');
mockInit.mockImplementation(async () => {});
// host needs to be set, otherwise smtp is skipped
config.set('userManagement.emails.smtp.host', 'host');
config.set('userManagement.emails.mode', 'smtp');
const userManagementMailer = new UserManagementMailer.UserManagementMailer();
// NodeMailer.verifyConnection gets called only explicitly
expect(async () => await userManagementMailer.verifyConnection()).not.toThrow();
// expect(NodeMailer.prototype.verifyConnection).toHaveBeenCalledTimes(1);
mockVerifyConnection.mockRestore();
mockInit.mockRestore();
});

View file

@ -1,6 +1,7 @@
import N8nUsersList from './UsersList.vue';
import { action } from '@storybook/addon-actions';
import type { StoryFn } from '@storybook/vue';
import { IUser } from '@/types';
export default {
title: 'Modules/UsersList',
@ -21,12 +22,24 @@ const Template: StoryFn = (args, { argTypes }) => ({
components: {
N8nUsersList,
},
template: '<n8n-users-list v-bind="$props" @reinvite="onReinvite" @delete="onDelete" />',
template:
'<n8n-users-list v-bind="$props" :actions="actions" @reinvite="onReinvite" @delete="onDelete" />',
methods,
});
export const UsersList = Template.bind({});
UsersList.args = {
actions: [
{
label: 'Resend Invite',
value: 'reinvite',
guard: (user: IUser) => !user.firstName,
},
{
label: 'Delete User',
value: 'delete',
},
],
users: [
{
id: '1',

View file

@ -25,20 +25,14 @@
</template>
<script lang="ts">
import { IUser } from '../../types';
import { IUser, IUserListAction } from '../../types';
import N8nActionToggle from '../N8nActionToggle';
import N8nBadge from '../N8nBadge';
import N8nUserInfo from '../N8nUserInfo';
import Locale from '../../mixins/locale';
import mixins from 'vue-typed-mixins';
import { t } from '../../locale';
import { PropType } from 'vue';
export interface IUserListAction {
label: string;
value: string;
}
export default mixins(Locale).extend({
name: 'n8n-users-list',
components: {
@ -61,17 +55,9 @@ export default mixins(Locale).extend({
currentUserId: {
type: String,
},
deleteLabel: {
type: String,
default: () => t('nds.usersList.deleteUser'),
},
reinviteLabel: {
type: String,
default: () => t('nds.usersList.reinviteUser'),
},
actions: {
type: Array as PropType<string[]>,
default: () => ['delete', 'reinvite'],
type: Array as PropType<IUserListAction[]>,
default: () => [],
},
},
computed: {
@ -115,37 +101,16 @@ export default mixins(Locale).extend({
},
methods: {
getActions(user: IUser): IUserListAction[] {
const actions = [];
const DELETE: IUserListAction = {
label: this.deleteLabel,
value: 'delete',
};
const REINVITE: IUserListAction = {
label: this.reinviteLabel,
value: 'reinvite',
};
if (user.isOwner) {
return [];
}
if (!user.firstName) {
if (this.actions.includes('reinvite')) {
actions.push(REINVITE);
}
}
const defaultGuard = () => true;
if (this.actions.includes('delete')) {
actions.push(DELETE);
}
return actions;
return this.actions.filter((action) => (action.guard || defaultGuard)(user));
},
onUserAction(user: IUser, action: string): void {
if (action === 'delete' || action === 'reinvite') {
this.$emit(action, user.id);
}
this.$emit(action, user.id);
},
},
});

View file

@ -3,8 +3,6 @@ export default {
'nds.userInfo.you': '(you)',
'nds.userSelect.selectUser': 'Select User',
'nds.userSelect.noMatchingUsers': 'No matching users',
'nds.usersList.deleteUser': 'Delete User',
'nds.usersList.reinviteUser': 'Resend invite',
'notice.showMore': 'Show more',
'notice.showLess': 'Show less',
'formInput.validator.fieldRequired': 'This field is required',

View file

@ -6,4 +6,17 @@ export interface IUser {
email?: string;
isOwner: boolean;
isPendingUser: boolean;
inviteAcceptUrl?: string;
}
export interface IUserListAction {
label: string;
value: string;
guard?: (user: IUser) => boolean;
}
export interface IUserListAction {
label: string;
value: string;
guard?: (user: IUser) => boolean;
}

View file

@ -641,6 +641,7 @@ export interface IUser extends IUserResponse {
isDefaultUser: boolean;
isPendingUser: boolean;
isOwner: boolean;
inviteAcceptUrl?: string;
fullName?: string;
createdAt?: Date;
}
@ -1297,6 +1298,8 @@ export interface IInviteResponse {
user: {
id: string;
email: string;
emailSent: boolean;
inviteAcceptUrl: string;
};
error?: string;
}

View file

@ -120,6 +120,13 @@ export async function reinvite(context: IRestApiContext, { id }: { id: string })
await makeRestApiRequest(context, 'POST', `/users/${id}/reinvite`);
}
export async function getInviteLink(
context: IRestApiContext,
{ id }: { id: string },
): Promise<{ link: string }> {
return await makeRestApiRequest(context, 'GET', `/users/${id}/invite-link`);
}
export async function submitPersonalizationSurvey(
context: IRestApiContext,
params: IPersonalizationLatestVersion,

View file

@ -3,13 +3,19 @@
<div v-if="!isSharingEnabled">
<n8n-action-box
:heading="
$locale.baseText(contextBasedTranslationKeys.credentials.sharing.unavailable.title)
$locale.baseText(
uiStore.contextBasedTranslationKeys.credentials.sharing.unavailable.title,
)
"
:description="
$locale.baseText(contextBasedTranslationKeys.credentials.sharing.unavailable.description)
$locale.baseText(
uiStore.contextBasedTranslationKeys.credentials.sharing.unavailable.description,
)
"
:buttonText="
$locale.baseText(contextBasedTranslationKeys.credentials.sharing.unavailable.button)
$locale.baseText(
uiStore.contextBasedTranslationKeys.credentials.sharing.unavailable.button,
)
"
@click="goToUpgrade"
/>
@ -62,9 +68,9 @@
</template>
</n8n-user-select>
<n8n-users-list
:actions="usersListActions"
:users="sharedWithList"
:currentUserId="usersStore.currentUser.id"
:delete-label="$locale.baseText('credentialEdit.credentialSharing.list.delete')"
:readonly="!credentialPermissions.updateSharing"
@delete="onRemoveSharee"
/>
@ -73,7 +79,7 @@
</template>
<script lang="ts">
import { IUser, UIState } from '@/Interface';
import { IUser, IUserListAction, UIState } from '@/Interface';
import mixins from 'vue-typed-mixins';
import { showMessage } from '@/mixins/showMessage';
import { mapStores } from 'pinia';
@ -96,12 +102,17 @@ export default mixins(showMessage).extend({
],
computed: {
...mapStores(useCredentialsStore, useUsersStore, useUsageStore, useUIStore, useSettingsStore),
usersListActions(): IUserListAction[] {
return [
{
label: this.$locale.baseText('credentialEdit.credentialSharing.list.delete'),
value: 'delete',
},
];
},
isDefaultUser(): boolean {
return this.usersStore.isDefaultUser;
},
contextBasedTranslationKeys(): UIState['contextBasedTranslationKeys'] {
return this.uiStore.contextBasedTranslationKeys;
},
isSharingEnabled(): boolean {
return this.settingsStore.isEnterpriseFeatureEnabled(EnterpriseEditionFeature.Sharing);
},
@ -168,7 +179,7 @@ export default mixins(showMessage).extend({
this.modalBus.$emit('close');
},
goToUpgrade() {
let linkUrl = this.$locale.baseText(this.contextBasedTranslationKeys.upgradeLinkUrl);
let linkUrl = this.$locale.baseText(this.uiStore.contextBasedTranslationKeys.upgradeLinkUrl);
if (linkUrl.includes('subscription')) {
linkUrl = this.usageStore.viewPlansUrl;
}

View file

@ -2,21 +2,54 @@
<Modal
:name="INVITE_USER_MODAL_KEY"
@enter="onSubmit"
:title="$locale.baseText('settings.users.inviteNewUsers')"
:title="
$locale.baseText(
showInviteUrls ? 'settings.users.copyInviteUrls' : 'settings.users.inviteNewUsers',
)
"
:center="true"
width="460px"
:eventBus="modalBus"
>
<template #content>
<div v-if="showInviteUrls">
<n8n-users-list :users="invitedUsers">
<template #actions="{ user }">
<n8n-tooltip>
<template #content>
{{ $locale.baseText('settings.users.inviteLink.copy') }}
</template>
<n8n-icon-button
icon="link"
type="tertiary"
@click="onCopyInviteLink(user)"
></n8n-icon-button>
</n8n-tooltip>
</template>
</n8n-users-list>
</div>
<n8n-form-inputs
v-else
:inputs="config"
:eventBus="formBus"
:columnView="true"
@input="onInput"
@submit="onSubmit"
/>
<n8n-info-tip v-if="!settingsStore.isSmtpSetup" class="mt-s">
<i18n path="settings.users.setupSMTPInfo">
<template #link>
<a
href="https://docs.n8n.io/reference/user-management.html#step-one-smtp"
target="_blank"
>
{{ $locale.baseText('settings.users.setupSMTPInfo.link') }}
</a>
</template>
</i18n>
</n8n-info-tip>
</template>
<template #footer>
<template v-if="!showInviteUrls" #footer>
<n8n-button
:loading="loading"
:disabled="!enabledButton"
@ -32,13 +65,15 @@
import mixins from 'vue-typed-mixins';
import { showMessage } from '@/mixins/showMessage';
import { copyPaste } from '@/mixins/copyPaste';
import Modal from './Modal.vue';
import Vue from 'vue';
import { IFormInputs, IInviteResponse } from '@/Interface';
import { IFormInputs, IInviteResponse, IUser } from '@/Interface';
import { VALID_EMAIL_REGEX, INVITE_USER_MODAL_KEY } from '@/constants';
import { ROLE } from '@/utils';
import { mapStores } from 'pinia';
import { useUsersStore } from '@/stores/users';
import { useSettingsStore } from '@/stores/settings';
const NAME_EMAIL_FORMAT_REGEX = /^.* <(.*)>$/;
@ -53,7 +88,7 @@ function getEmail(email: string): string {
return parsed;
}
export default mixins(showMessage).extend({
export default mixins(showMessage, copyPaste).extend({
components: { Modal },
name: 'InviteUsersModal',
props: {
@ -67,6 +102,7 @@ export default mixins(showMessage).extend({
formBus: new Vue(),
modalBus: new Vue(),
emails: '',
showInviteUrls: null as IInviteResponse[] | null,
loading: false,
INVITE_USER_MODAL_KEY,
};
@ -108,22 +144,35 @@ export default mixins(showMessage).extend({
];
},
computed: {
...mapStores(useUsersStore),
...mapStores(useUsersStore, useSettingsStore),
emailsCount(): number {
return this.emails.split(',').filter((email: string) => !!email.trim()).length;
},
buttonLabel(): string {
if (this.emailsCount > 1) {
return this.$locale.baseText('settings.users.inviteXUser', {
interpolate: { count: this.emailsCount.toString() },
});
return this.$locale.baseText(
`settings.users.inviteXUser${this.settingsStore.isSmtpSetup ? '' : '.inviteUrl'}`,
{
interpolate: { count: this.emailsCount.toString() },
},
);
}
return this.$locale.baseText('settings.users.inviteUser');
return this.$locale.baseText(
`settings.users.inviteUser${this.settingsStore.isSmtpSetup ? '' : '.inviteUrl'}`,
);
},
enabledButton(): boolean {
return this.emailsCount >= 1;
},
invitedUsers(): IUser[] {
console.log(this.usersStore.allUsers, this.showInviteUrls);
return this.showInviteUrls
? this.usersStore.allUsers.filter((user) =>
this.showInviteUrls!.find((invite) => invite.user.id === user.id),
)
: [];
},
},
methods: {
validateEmails(value: string | number | boolean | null | undefined) {
@ -165,56 +214,106 @@ export default mixins(showMessage).extend({
}
const invited: IInviteResponse[] = await this.usersStore.inviteUsers(emails);
const invitedEmails = invited.reduce(
(accu, { user, error }) => {
if (error) {
accu.error.push(user.email);
} else {
accu.success.push(user.email);
}
return accu;
},
{
success: [] as string[],
error: [] as string[],
},
const erroredInvites = invited.filter((invite) => invite.error);
const successfulEmailInvites = invited.filter(
(invite) => !invite.error && invite.user.emailSent,
);
const successfulUrlInvites = invited.filter(
(invite) => !invite.error && !invite.user.emailSent,
);
if (invitedEmails.success.length) {
if (successfulEmailInvites.length) {
this.$showMessage({
type: 'success',
title: this.$locale.baseText(
invitedEmails.success.length > 1
successfulEmailInvites.length > 1
? 'settings.users.usersInvited'
: 'settings.users.userInvited',
),
message: this.$locale.baseText('settings.users.emailInvitesSent', {
interpolate: { emails: invitedEmails.success.join(', ') },
interpolate: {
emails: successfulEmailInvites.map(({ user }) => user.email).join(', '),
},
}),
});
}
if (invitedEmails.error.length) {
if (successfulUrlInvites.length) {
if (successfulUrlInvites.length === 1) {
this.copyToClipboard(successfulUrlInvites[0].user.inviteAcceptUrl);
}
this.$showMessage({
type: 'success',
title: this.$locale.baseText(
successfulUrlInvites.length > 1
? 'settings.users.multipleInviteUrlsCreated'
: 'settings.users.inviteUrlCreated',
),
message: this.$locale.baseText(
successfulUrlInvites.length > 1
? 'settings.users.multipleInviteUrlsCreated.message'
: 'settings.users.inviteUrlCreated.message',
{
interpolate: {
emails: successfulUrlInvites.map(({ user }) => user.email).join(', '),
},
},
),
});
}
if (erroredInvites.length) {
setTimeout(() => {
this.$showMessage({
type: 'error',
title: this.$locale.baseText('settings.users.usersEmailedError'),
message: this.$locale.baseText('settings.users.emailInvitesSentError', {
interpolate: { emails: invitedEmails.error.join(', ') },
interpolate: { emails: erroredInvites.map(({ error }) => error).join(', ') },
}),
});
}, 0); // notifications stack on top of each other otherwise
}
this.modalBus.$emit('close');
if (successfulUrlInvites.length > 1) {
this.showInviteUrls = successfulUrlInvites;
} else {
this.modalBus.$emit('close');
}
} catch (error) {
this.$showError(error, this.$locale.baseText('settings.users.usersInvitedError'));
}
this.loading = false;
},
showCopyInviteLinkToast(successfulUrlInvites: IInviteResponse[]) {
this.$showMessage({
type: 'success',
title: this.$locale.baseText(
successfulUrlInvites.length > 1
? 'settings.users.multipleInviteUrlsCreated'
: 'settings.users.inviteUrlCreated',
),
message: this.$locale.baseText(
successfulUrlInvites.length > 1
? 'settings.users.multipleInviteUrlsCreated.message'
: 'settings.users.inviteUrlCreated.message',
{
interpolate: {
emails: successfulUrlInvites.map(({ user }) => user.email).join(', '),
},
},
),
});
},
onSubmitClick() {
this.formBus.$emit('submit');
},
onCopyInviteLink(user: IUser) {
if (user.inviteAcceptUrl && this.showInviteUrls) {
this.copyToClipboard(user.inviteAcceptUrl);
this.showCopyInviteLinkToast([]);
}
},
},
});
</script>

View file

@ -12,7 +12,7 @@
<n8n-text>
{{
$locale.baseText(
contextBasedTranslationKeys.workflows.sharing.unavailable.description.modal,
uiStore.contextBasedTranslationKeys.workflows.sharing.unavailable.description.modal,
)
}}
</n8n-text>
@ -50,7 +50,6 @@
:currentUserId="currentUser.id"
:delete-label="$locale.baseText('workflows.shareModal.list.delete')"
:readonly="!workflowPermissions.updateSharing"
@delete="onRemoveSharee"
>
<template #actions="{ user }">
<n8n-select
@ -71,7 +70,9 @@
<template #fallback>
<n8n-text>
<i18n
:path="contextBasedTranslationKeys.workflows.sharing.unavailable.description"
:path="
uiStore.contextBasedTranslationKeys.workflows.sharing.unavailable.description
"
tag="span"
>
<template #action />
@ -85,7 +86,11 @@
<template #footer>
<div v-if="!isSharingEnabled" :class="$style.actionButtons">
<n8n-button @click="goToUpgrade">
{{ $locale.baseText(contextBasedTranslationKeys.workflows.sharing.unavailable.button) }}
{{
$locale.baseText(
uiStore.contextBasedTranslationKeys.workflows.sharing.unavailable.button,
)
}}
</n8n-button>
</div>
<div v-else-if="isDefaultUser" :class="$style.actionButtons">
@ -112,10 +117,12 @@
</n8n-button>
<template #fallback>
<n8n-link :to="contextBasedTranslationKeys.workflows.sharing.unavailable.linkUrl">
<n8n-link :to="uiStore.contextBasedTranslationKeys.workflows.sharing.unavailable.linkUrl">
<n8n-button :loading="loading" size="medium">
{{
$locale.baseText(contextBasedTranslationKeys.workflows.sharing.unavailable.button)
$locale.baseText(
uiStore.contextBasedTranslationKeys.workflows.sharing.unavailable.button,
)
}}
</n8n-button>
</n8n-link>
@ -192,8 +199,8 @@ export default mixins(showMessage).extend({
modalTitle(): string {
return this.$locale.baseText(
this.isSharingEnabled
? this.contextBasedTranslationKeys.workflows.sharing.title
: this.contextBasedTranslationKeys.workflows.sharing.unavailable.title,
? this.uiStore.contextBasedTranslationKeys.workflows.sharing.title
: this.uiStore.contextBasedTranslationKeys.workflows.sharing.unavailable.title,
{
interpolate: { name: this.workflow.name },
},
@ -235,9 +242,6 @@ export default mixins(showMessage).extend({
workflowOwnerName(): string {
return this.workflowsEEStore.getWorkflowOwnerName(`${this.workflow.id}`);
},
contextBasedTranslationKeys(): UIState['contextBasedTranslationKeys'] {
return this.uiStore.contextBasedTranslationKeys;
},
isDirty(): boolean {
const previousSharedWith = this.workflow.sharedWith || [];
@ -433,7 +437,7 @@ export default mixins(showMessage).extend({
});
},
goToUpgrade() {
let linkUrl = this.$locale.baseText(this.contextBasedTranslationKeys.upgradeLinkUrl);
let linkUrl = this.$locale.baseText(this.uiStore.contextBasedTranslationKeys.upgradeLinkUrl);
if (linkUrl.includes('subscription')) {
linkUrl = this.usageStore.viewPlansUrl;
}

View file

@ -1078,24 +1078,35 @@
"settings.users.deleteConfirmationMessage": "Type “delete all data” to confirm",
"settings.users.deleteConfirmationText": "delete all data",
"settings.users.deleteUser": "Delete {user}",
"settings.users.actions.delete": "Delete User",
"settings.users.actions.reinvite": "Resend Invite",
"settings.users.actions.copyInviteLink": "Copy Invite Link",
"settings.users.deleteWorkflowsAndCredentials": "Delete their workflows and credentials",
"settings.users.emailInvitesSent": "An invite email was sent to {emails}",
"settings.users.emailInvitesSentError": "Could not invite {emails}",
"settings.users.emailSentTo": "Email sent to {email}",
"settings.users.invalidEmailError": "{email} is not a valid email",
"settings.users.inviteLink.copy": "Copy Invite Link",
"settings.users.inviteLink.error": "Could not retrieve invite link",
"settings.users.invite": "Invite",
"settings.users.inviteNewUsers": "Invite new users",
"settings.users.copyInviteUrls": "You can now send the invitation links directly to your users",
"settings.users.inviteResent": "Invite resent",
"settings.users.inviteUser": "Invite user",
"settings.users.inviteUser.inviteUrl": "Create invite link",
"settings.users.inviteXUser": "Invite {count} users",
"settings.users.inviteXUser.inviteUrl": "Create {count} invite links",
"settings.users.inviteUrlCreated": "Invite link copied to clipboard",
"settings.users.inviteUrlCreated.message": "Send the invite link to your invitee for activation",
"settings.users.multipleInviteUrlsCreated": "Invite links created",
"settings.users.multipleInviteUrlsCreated.message": "Send the invite links to your invitees for activation",
"settings.users.newEmailsToInvite": "New User Email Addresses",
"settings.users.noUsersToInvite": "No users to invite",
"settings.users.setupMyAccount": "Set up my owner account",
"settings.users.setupSMTPToInviteUsers": "Set up SMTP to invite users. {action}",
"settings.users.setupSMTPToInviteUsers.instructions": "Instructions",
"settings.users.setupToInviteUsers": "To invite users, set up your own account",
"settings.users.setupToInviteUsersInfo": "Invited users wont be able to see workflows and credentials of other users unless you upgrade. <a href=\"https://docs.n8n.io/reference/user-management.html\" target=\"_blank\">More info</a> <br /> <br />",
"settings.users.setupSMTPInfo": "You will need details of an <a href=\"https://docs.n8n.io/reference/user-management.html#step-one-smtp\" target=\"_blank\">SMTP server</a> to complete the setup.",
"settings.users.setupToInviteUsersInfo": "Invited users wont be able to see workflows and credentials of other users unless you upgrade. <a href=\"https://docs.n8n.io/reference/user-management.html\" target=\"_blank\">More info</a>",
"settings.users.setupSMTPInfo": "You can also send invite emails if you provide a valid {link} setup.",
"settings.users.setupSMTPInfo.link": "SMTP server",
"settings.users.smtpToAddUsersWarning": "Set up SMTP before adding users (so that n8n can send them invitation emails). <a target=\"_blank\" href=\"https://docs.n8n.io/reference/user-management.html#step-one-smtp\">Instructions</a>",
"settings.users.transferWorkflowsAndCredentials": "Transfer their workflows and credentials to another user",
"settings.users.transferredToUser": "Data transferred to {user}",

View file

@ -2,6 +2,7 @@ import {
changePassword,
deleteUser,
getCurrentUser,
getInviteLink,
getUsers,
inviteUsers,
login,
@ -255,6 +256,10 @@ export const useUsersStore = defineStore(STORES.USERS, {
const rootStore = useRootStore();
await reinvite(rootStore.getRestApiContext, params);
},
async getUserInviteLink(params: { id: string }): Promise<{ link: string }> {
const rootStore = useRootStore();
return await getInviteLink(rootStore.getRestApiContext, params);
},
async submitPersonalizationSurvey(results: IPersonalizationLatestVersion): Promise<void> {
const rootStore = useRootStore();
await submitPersonalizationSurvey(rootStore.getRestApiContext, results);

View file

@ -3,27 +3,11 @@
<div>
<n8n-heading size="2xlarge">{{ $locale.baseText('settings.users') }}</n8n-heading>
<div :class="$style.buttonContainer" v-if="!usersStore.showUMSetupWarning">
<n8n-tooltip :disabled="settingsStore.isSmtpSetup" placement="bottom">
<template #content>
<i18n path="settings.users.setupSMTPToInviteUsers" tag="span">
<template #action>
<a
href="https://docs.n8n.io/reference/user-management.html#step-one-smtp"
target="_blank"
v-text="$locale.baseText('settings.users.setupSMTPToInviteUsers.instructions')"
/>
</template>
</i18n>
</template>
<div>
<n8n-button
:label="$locale.baseText('settings.users.invite')"
@click="onInvite"
size="large"
:disabled="!settingsStore.isSmtpSetup"
/>
</div>
</n8n-tooltip>
<n8n-button
:label="$locale.baseText('settings.users.invite')"
@click="onInvite"
size="large"
/>
</div>
</div>
<div v-if="usersStore.showUMSetupWarning" :class="$style.setupInfoContainer">
@ -32,27 +16,25 @@
:buttonText="$locale.baseText('settings.users.setupMyAccount')"
:description="`${
isSharingEnabled ? '' : $locale.baseText('settings.users.setupToInviteUsersInfo')
}${$locale.baseText('settings.users.setupSMTPInfo')}`"
}`"
@click="redirectToSetup"
/>
</div>
<div :class="$style.usersContainer" v-else>
<PageAlert
v-if="!settingsStore.isSmtpSetup"
:message="$locale.baseText('settings.users.smtpToAddUsersWarning')"
:popupClass="$style.alert"
/>
<n8n-users-list
:actions="usersListActions"
:users="usersStore.allUsers"
:currentUserId="usersStore.currentUserId"
@delete="onDelete"
@reinvite="onReinvite"
@copyInviteLink="onCopyInviteLink"
/>
</div>
<feature-coming-soon
v-for="fakeDoorFeature in fakeDoorFeatures"
:key="fakeDoorFeature.id"
:featureId="fakeDoorFeature.id"
class="pb-3xl"
showTitle
/>
</div>
@ -63,15 +45,16 @@ import { EnterpriseEditionFeature, INVITE_USER_MODAL_KEY, VIEWS } from '@/consta
import PageAlert from '../components/PageAlert.vue';
import FeatureComingSoon from '@/components/FeatureComingSoon.vue';
import { IFakeDoor, IUser } from '@/Interface';
import { IFakeDoor, IUser, IUserListAction } from '@/Interface';
import mixins from 'vue-typed-mixins';
import { showMessage } from '@/mixins/showMessage';
import { copyPaste } from '@/mixins/copyPaste';
import { mapStores } from 'pinia';
import { useUIStore } from '@/stores/ui';
import { useSettingsStore } from '@/stores/settings';
import { useUsersStore } from '@/stores/users';
export default mixins(showMessage).extend({
export default mixins(showMessage, copyPaste).extend({
name: 'SettingsUsersView',
components: {
PageAlert,
@ -87,6 +70,24 @@ export default mixins(showMessage).extend({
isSharingEnabled() {
return this.settingsStore.isEnterpriseFeatureEnabled(EnterpriseEditionFeature.Sharing);
},
usersListActions(): IUserListAction[] {
return [
{
label: this.$locale.baseText('settings.users.actions.copyInviteLink'),
value: 'copyInviteLink',
guard: (user) => !user.firstName && !!user.inviteAcceptUrl,
},
{
label: this.$locale.baseText('settings.users.actions.reinvite'),
value: 'reinvite',
guard: (user) => !user.firstName && this.settingsStore.isSmtpSetup,
},
{
label: this.$locale.baseText('settings.users.actions.delete'),
value: 'delete',
},
];
},
fakeDoorFeatures(): IFakeDoor[] {
return this.uiStore.getFakeDoorByLocation('settings/users');
},
@ -122,6 +123,18 @@ export default mixins(showMessage).extend({
}
}
},
async onCopyInviteLink(userId: string) {
const user = this.usersStore.getUserById(userId) as IUser | null;
if (user?.inviteAcceptUrl) {
this.copyToClipboard(user.inviteAcceptUrl);
this.$showToast({
type: 'success',
title: this.$locale.baseText('settings.users.inviteUrlCreated'),
message: this.$locale.baseText('settings.users.inviteUrlCreated.message'),
});
}
},
},
});
</script>