2023-01-27 02:19:47 -08:00
|
|
|
import validator from 'validator';
|
|
|
|
import { validateEntity } from '@/GenericHelpers';
|
2023-02-17 01:59:09 -08:00
|
|
|
import { Get, 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';
|
|
|
|
import type {
|
|
|
|
CredentialsRepository,
|
|
|
|
SettingsRepository,
|
|
|
|
UserRepository,
|
|
|
|
WorkflowRepository,
|
|
|
|
} from '@db/repositories';
|
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
|
|
|
|
2023-04-12 01:59:14 -07:00
|
|
|
private readonly credentialsRepository: CredentialsRepository;
|
2023-02-17 01:59:09 -08:00
|
|
|
|
2023-04-12 01:59:14 -07:00
|
|
|
private readonly workflowsRepository: WorkflowRepository;
|
2023-02-17 01:59:09 -08:00
|
|
|
|
2023-01-27 02:19:47 -08:00
|
|
|
constructor({
|
|
|
|
config,
|
|
|
|
logger,
|
|
|
|
internalHooks,
|
|
|
|
repositories,
|
|
|
|
}: {
|
|
|
|
config: Config;
|
|
|
|
logger: ILogger;
|
|
|
|
internalHooks: IInternalHooksClass;
|
2023-02-17 01:59:09 -08:00
|
|
|
repositories: Pick<IDatabaseCollections, 'User' | 'Settings' | 'Credentials' | 'Workflow'>;
|
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;
|
2023-02-17 01:59:09 -08:00
|
|
|
this.credentialsRepository = repositories.Credentials;
|
|
|
|
this.workflowsRepository = repositories.Workflow;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Get('/pre-setup')
|
|
|
|
async preSetup(): Promise<{ credentials: number; workflows: number }> {
|
|
|
|
if (this.config.getEnv('userManagement.isInstanceOwnerSetUp')) {
|
|
|
|
throw new BadRequestError('Instance owner already setup');
|
|
|
|
}
|
|
|
|
|
|
|
|
const [credentials, workflows] = await Promise.all([
|
|
|
|
this.credentialsRepository.countBy({}),
|
|
|
|
this.workflowsRepository.countBy({}),
|
|
|
|
]);
|
|
|
|
return { credentials, workflows };
|
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;
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Persist that the instance owner setup has been skipped
|
|
|
|
*/
|
|
|
|
@Post('/skip-setup')
|
|
|
|
async skipSetup() {
|
|
|
|
await this.settingsRepository.update(
|
|
|
|
{ key: 'userManagement.skipInstanceOwnerSetup' },
|
|
|
|
{ value: JSON.stringify(true) },
|
|
|
|
);
|
|
|
|
|
|
|
|
this.config.set('userManagement.skipInstanceOwnerSetup', true);
|
|
|
|
|
|
|
|
return { success: true };
|
|
|
|
}
|
|
|
|
}
|