2023-01-27 02:19:47 -08:00
|
|
|
import validator from 'validator';
|
2023-12-27 02:50:43 -08:00
|
|
|
import { Response } from 'express';
|
|
|
|
|
2024-02-28 04:12:28 -08:00
|
|
|
import { AuthService } from '@/auth/auth.service';
|
2023-12-27 02:50:43 -08:00
|
|
|
import config from '@/config';
|
2023-01-27 02:19:47 -08:00
|
|
|
import { validateEntity } from '@/GenericHelpers';
|
2024-02-28 08:02:18 -08:00
|
|
|
import { GlobalScope, Post, RestController } from '@/decorators';
|
2023-12-11 09:23:42 -08:00
|
|
|
import { PasswordUtility } from '@/services/password.utility';
|
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';
|
2024-02-28 04:12:28 -08:00
|
|
|
import { UserRepository } from '@db/repositories/user.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';
|
2024-08-07 07:09:42 -07:00
|
|
|
import { EventService } from '@/events/event.service';
|
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,
|
2024-08-07 07:09:42 -07:00
|
|
|
private readonly eventService: EventService,
|
2023-08-25 04:23:22 -07:00
|
|
|
private readonly settingsRepository: SettingsRepository,
|
2024-02-28 04:12:28 -08:00
|
|
|
private readonly authService: AuthService,
|
2023-08-25 04:23:22 -07:00
|
|
|
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.
|
|
|
|
*/
|
2024-02-28 08:02:18 -08:00
|
|
|
@Post('/setup', { skipAuth: true })
|
2023-02-17 01:59:09 -08:00
|
|
|
async setupOwner(req: OwnerRequest.Post, res: Response) {
|
2023-01-27 02:19:47 -08:00
|
|
|
const { email, firstName, lastName, password } = req.body;
|
|
|
|
|
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',
|
|
|
|
);
|
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', {
|
|
|
|
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',
|
2024-02-28 08:02:18 -08:00
|
|
|
{ payload: req.body },
|
2023-01-27 02:19:47 -08:00
|
|
|
);
|
|
|
|
throw new BadRequestError('First and last names are mandatory');
|
|
|
|
}
|
|
|
|
|
2024-02-28 08:02:18 -08:00
|
|
|
let owner = await this.userRepository.findOneOrFail({
|
|
|
|
where: { role: 'global:owner' },
|
2023-01-27 02:19:47 -08:00
|
|
|
});
|
2024-02-28 08:02:18 -08:00
|
|
|
owner.email = email;
|
|
|
|
owner.firstName = firstName;
|
|
|
|
owner.lastName = lastName;
|
|
|
|
owner.password = await this.passwordUtility.hash(validPassword);
|
2023-01-27 02:19:47 -08:00
|
|
|
|
|
|
|
await validateEntity(owner);
|
|
|
|
|
2024-02-20 07:34:54 -08:00
|
|
|
owner = await this.userRepository.save(owner, { transaction: false });
|
2023-01-27 02:19:47 -08:00
|
|
|
|
2024-02-28 08:02:18 -08:00
|
|
|
this.logger.info('Owner was set up successfully');
|
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
|
|
|
|
2024-02-28 08:02:18 -08:00
|
|
|
this.logger.debug('Setting isInstanceOwnerSetUp updated successfully');
|
2023-01-27 02:19:47 -08:00
|
|
|
|
2024-04-09 02:20:35 -07:00
|
|
|
this.authService.issueCookie(res, owner, req.browserId);
|
2023-01-27 02:19:47 -08:00
|
|
|
|
2024-08-07 07:09:42 -07:00
|
|
|
this.eventService.emit('instance-owner-setup', { userId: owner.id });
|
2023-01-27 02:19:47 -08:00
|
|
|
|
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')
|
2024-02-28 08:02:18 -08:00
|
|
|
@GlobalScope('banner:dismiss')
|
2023-07-14 06:36:17 -07:00
|
|
|
async dismissBanner(req: OwnerRequest.DismissBanner) {
|
|
|
|
const bannerName = 'banner' in req.body ? (req.body.banner as string) : '';
|
2024-02-28 08:02:18 -08:00
|
|
|
return await this.settingsRepository.dismissBanner({ bannerName });
|
2023-06-19 07:23:57 -07:00
|
|
|
}
|
2023-01-27 02:19:47 -08:00
|
|
|
}
|