2023-01-27 02:19:47 -08:00
|
|
|
import validator from 'validator';
|
2023-12-27 02:50:43 -08:00
|
|
|
import { Response } from 'express';
|
|
|
|
|
|
|
|
import config from '@/config';
|
2023-01-27 02:19:47 -08:00
|
|
|
import { validateEntity } from '@/GenericHelpers';
|
2023-06-21 04:21:34 -07:00
|
|
|
import { Authorized, Post, RestController } from '@/decorators';
|
2023-12-11 09:23:42 -08:00
|
|
|
import { PasswordUtility } from '@/services/password.utility';
|
2023-01-27 02:19:47 -08:00
|
|
|
import { issueCookie } from '@/auth/jwt';
|
2023-01-27 05:56:56 -08:00
|
|
|
import { OwnerRequest } from '@/requests';
|
2023-11-10 06:04:26 -08:00
|
|
|
import { SettingsRepository } from '@db/repositories/settings.repository';
|
2023-08-25 04:23:22 -07:00
|
|
|
import { PostHogClient } from '@/posthog';
|
2023-08-22 06:58:05 -07:00
|
|
|
import { UserService } from '@/services/user.service';
|
2023-10-25 07:35:22 -07:00
|
|
|
import { Logger } from '@/Logger';
|
2023-11-28 01:19:27 -08:00
|
|
|
import { BadRequestError } from '@/errors/response-errors/bad-request.error';
|
2023-12-27 02:50:43 -08:00
|
|
|
import { InternalHooks } from '@/InternalHooks';
|
2024-01-02 08:53:24 -08:00
|
|
|
import { UserRepository } from '@/databases/repositories/user.repository';
|
2023-01-27 02:19:47 -08:00
|
|
|
|
2024-01-24 04:38:57 -08:00
|
|
|
@Authorized('global:owner')
|
2023-01-27 02:19:47 -08:00
|
|
|
@RestController('/owner')
|
|
|
|
export class OwnerController {
|
2023-08-25 04:23:22 -07:00
|
|
|
constructor(
|
2023-10-25 07:35:22 -07:00
|
|
|
private readonly logger: Logger,
|
2023-12-27 02:50:43 -08:00
|
|
|
private readonly internalHooks: InternalHooks,
|
2023-08-25 04:23:22 -07:00
|
|
|
private readonly settingsRepository: SettingsRepository,
|
|
|
|
private readonly userService: UserService,
|
2023-12-11 09:23:42 -08:00
|
|
|
private readonly passwordUtility: PasswordUtility,
|
2023-12-27 02:50:43 -08:00
|
|
|
private readonly postHog: PostHogClient,
|
2024-01-02 08:53:24 -08:00
|
|
|
private readonly userRepository: UserRepository,
|
2023-08-25 04:23:22 -07:00
|
|
|
) {}
|
2023-01-27 02:19:47 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Promote a shell into the owner of the n8n instance,
|
|
|
|
* and enable `isInstanceOwnerSetUp` setting.
|
|
|
|
*/
|
2023-02-17 01:59:09 -08:00
|
|
|
@Post('/setup')
|
|
|
|
async setupOwner(req: OwnerRequest.Post, res: Response) {
|
2023-01-27 02:19:47 -08:00
|
|
|
const { email, firstName, lastName, password } = req.body;
|
2024-01-24 04:38:57 -08:00
|
|
|
const { id: userId } = req.user;
|
2023-01-27 02:19:47 -08:00
|
|
|
|
2023-12-27 02:50:43 -08:00
|
|
|
if (config.getEnv('userManagement.isInstanceOwnerSetUp')) {
|
2023-01-27 02:19:47 -08:00
|
|
|
this.logger.debug(
|
|
|
|
'Request to claim instance ownership failed because instance owner already exists',
|
|
|
|
{
|
|
|
|
userId,
|
|
|
|
},
|
|
|
|
);
|
2023-02-17 01:59:09 -08:00
|
|
|
throw new BadRequestError('Instance owner already setup');
|
2023-01-27 02:19:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!email || !validator.isEmail(email)) {
|
|
|
|
this.logger.debug('Request to claim instance ownership failed because of invalid email', {
|
|
|
|
userId,
|
|
|
|
invalidEmail: email,
|
|
|
|
});
|
|
|
|
throw new BadRequestError('Invalid email address');
|
|
|
|
}
|
|
|
|
|
2023-12-11 09:23:42 -08:00
|
|
|
const validPassword = this.passwordUtility.validate(password);
|
2023-01-27 02:19:47 -08:00
|
|
|
|
|
|
|
if (!firstName || !lastName) {
|
|
|
|
this.logger.debug(
|
|
|
|
'Request to claim instance ownership failed because of missing first name or last name in payload',
|
|
|
|
{ userId, payload: req.body },
|
|
|
|
);
|
|
|
|
throw new BadRequestError('First and last names are mandatory');
|
|
|
|
}
|
|
|
|
|
2023-02-17 01:59:09 -08:00
|
|
|
let owner = req.user;
|
|
|
|
|
2023-01-27 02:19:47 -08:00
|
|
|
Object.assign(owner, {
|
|
|
|
email,
|
|
|
|
firstName,
|
|
|
|
lastName,
|
2023-12-11 09:23:42 -08:00
|
|
|
password: await this.passwordUtility.hash(validPassword),
|
2023-01-27 02:19:47 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
await validateEntity(owner);
|
|
|
|
|
2024-01-02 08:53:24 -08:00
|
|
|
owner = await this.userRepository.save(owner);
|
2023-01-27 02:19:47 -08:00
|
|
|
|
2023-02-17 01:59:09 -08:00
|
|
|
this.logger.info('Owner was set up successfully', { userId });
|
2023-01-27 02:19:47 -08:00
|
|
|
|
|
|
|
await this.settingsRepository.update(
|
|
|
|
{ key: 'userManagement.isInstanceOwnerSetUp' },
|
|
|
|
{ value: JSON.stringify(true) },
|
|
|
|
);
|
|
|
|
|
2023-12-27 02:50:43 -08:00
|
|
|
config.set('userManagement.isInstanceOwnerSetUp', true);
|
2023-01-27 02:19:47 -08:00
|
|
|
|
2023-02-17 01:59:09 -08:00
|
|
|
this.logger.debug('Setting isInstanceOwnerSetUp updated successfully', { userId });
|
2023-01-27 02:19:47 -08:00
|
|
|
|
|
|
|
await issueCookie(res, owner);
|
|
|
|
|
|
|
|
void this.internalHooks.onInstanceOwnerSetup({ user_id: userId });
|
|
|
|
|
2024-01-17 07:08:50 -08:00
|
|
|
return await this.userService.toPublic(owner, { posthog: this.postHog, withScopes: true });
|
2023-01-27 02:19:47 -08:00
|
|
|
}
|
2023-06-19 07:23:57 -07:00
|
|
|
|
2023-07-14 06:36:17 -07:00
|
|
|
@Post('/dismiss-banner')
|
|
|
|
async dismissBanner(req: OwnerRequest.DismissBanner) {
|
|
|
|
const bannerName = 'banner' in req.body ? (req.body.banner as string) : '';
|
|
|
|
const response = await this.settingsRepository.dismissBanner({ bannerName });
|
|
|
|
return response;
|
2023-06-19 07:23:57 -07:00
|
|
|
}
|
2023-01-27 02:19:47 -08:00
|
|
|
}
|