2023-01-27 02:19:47 -08:00
|
|
|
import validator from 'validator';
|
|
|
|
import { validateEntity } from '@/GenericHelpers';
|
2023-06-21 04:21:34 -07:00
|
|
|
import { Authorized, Post, RestController } from '@/decorators';
|
2023-01-27 02:19:47 -08:00
|
|
|
import { BadRequestError } from '@/ResponseHelper';
|
|
|
|
import {
|
|
|
|
hashPassword,
|
|
|
|
sanitizeUser,
|
|
|
|
validatePassword,
|
|
|
|
} from '@/UserManagement/UserManagementHelper';
|
|
|
|
import { issueCookie } from '@/auth/jwt';
|
2023-01-27 05:56:56 -08:00
|
|
|
import { Response } from 'express';
|
2023-01-27 02:19:47 -08:00
|
|
|
import type { ILogger } from 'n8n-workflow';
|
|
|
|
import type { Config } from '@/config';
|
2023-01-27 05:56:56 -08:00
|
|
|
import { OwnerRequest } from '@/requests';
|
2023-04-12 01:59:14 -07:00
|
|
|
import type { IDatabaseCollections, IInternalHooksClass } from '@/Interfaces';
|
2023-06-21 04:21:34 -07:00
|
|
|
import type { SettingsRepository, UserRepository } from '@db/repositories';
|
2023-01-27 02:19:47 -08:00
|
|
|
|
2023-04-24 02:45:31 -07:00
|
|
|
@Authorized(['global', 'owner'])
|
2023-01-27 02:19:47 -08:00
|
|
|
@RestController('/owner')
|
|
|
|
export class OwnerController {
|
|
|
|
private readonly config: Config;
|
|
|
|
|
|
|
|
private readonly logger: ILogger;
|
|
|
|
|
|
|
|
private readonly internalHooks: IInternalHooksClass;
|
|
|
|
|
2023-04-12 01:59:14 -07:00
|
|
|
private readonly userRepository: UserRepository;
|
2023-01-27 02:19:47 -08:00
|
|
|
|
2023-04-12 01:59:14 -07:00
|
|
|
private readonly settingsRepository: SettingsRepository;
|
2023-01-27 02:19:47 -08:00
|
|
|
|
|
|
|
constructor({
|
|
|
|
config,
|
|
|
|
logger,
|
|
|
|
internalHooks,
|
|
|
|
repositories,
|
|
|
|
}: {
|
|
|
|
config: Config;
|
|
|
|
logger: ILogger;
|
|
|
|
internalHooks: IInternalHooksClass;
|
2023-06-21 04:21:34 -07:00
|
|
|
repositories: Pick<IDatabaseCollections, 'User' | 'Settings'>;
|
2023-01-27 02:19:47 -08:00
|
|
|
}) {
|
|
|
|
this.config = config;
|
|
|
|
this.logger = logger;
|
|
|
|
this.internalHooks = internalHooks;
|
|
|
|
this.userRepository = repositories.User;
|
|
|
|
this.settingsRepository = repositories.Settings;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
2023-02-17 01:59:09 -08:00
|
|
|
const { id: userId, globalRole } = req.user;
|
2023-01-27 02:19:47 -08:00
|
|
|
|
|
|
|
if (this.config.getEnv('userManagement.isInstanceOwnerSetUp')) {
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
|
|
|
const validPassword = validatePassword(password);
|
|
|
|
|
|
|
|
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
|
|
|
// TODO: This check should be in a middleware outside this class
|
|
|
|
if (globalRole.scope === 'global' && globalRole.name !== 'owner') {
|
2023-01-27 02:19:47 -08:00
|
|
|
this.logger.debug(
|
|
|
|
'Request to claim instance ownership failed because user shell does not exist or has wrong role!',
|
|
|
|
{
|
|
|
|
userId,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
throw new BadRequestError('Invalid request');
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
password: await hashPassword(validPassword),
|
|
|
|
});
|
|
|
|
|
|
|
|
await validateEntity(owner);
|
|
|
|
|
|
|
|
owner = await this.userRepository.save(owner);
|
|
|
|
|
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) },
|
|
|
|
);
|
|
|
|
|
|
|
|
this.config.set('userManagement.isInstanceOwnerSetUp', true);
|
|
|
|
|
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 });
|
|
|
|
|
|
|
|
return sanitizeUser(owner);
|
|
|
|
}
|
2023-06-19 07:23:57 -07:00
|
|
|
|
|
|
|
@Post('/dismiss-v1')
|
|
|
|
async dismissBanner() {
|
|
|
|
await this.settingsRepository.saveSetting('ui.banners.v1.dismissed', JSON.stringify(true));
|
|
|
|
|
|
|
|
return { success: true };
|
|
|
|
}
|
2023-01-27 02:19:47 -08:00
|
|
|
}
|