mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-18 10:31:15 +00:00
refactor(core): Port 3 more controllers to use DTOs (no-changelog) (#12375)
This commit is contained in:
committed by
GitHub
parent
1d5e891a0d
commit
371a09de96
@@ -1,17 +1,17 @@
|
||||
import { DismissBannerRequestDto, OwnerSetupRequestDto } from '@n8n/api-types';
|
||||
import { Response } from 'express';
|
||||
import { Logger } from 'n8n-core';
|
||||
import validator from 'validator';
|
||||
|
||||
import { AuthService } from '@/auth/auth.service';
|
||||
import config from '@/config';
|
||||
import { SettingsRepository } from '@/databases/repositories/settings.repository';
|
||||
import { UserRepository } from '@/databases/repositories/user.repository';
|
||||
import { GlobalScope, Post, RestController } from '@/decorators';
|
||||
import { Body, GlobalScope, Post, RestController } from '@/decorators';
|
||||
import { BadRequestError } from '@/errors/response-errors/bad-request.error';
|
||||
import { EventService } from '@/events/event.service';
|
||||
import { validateEntity } from '@/generic-helpers';
|
||||
import { PostHogClient } from '@/posthog';
|
||||
import { OwnerRequest } from '@/requests';
|
||||
import { AuthenticatedRequest } from '@/requests';
|
||||
import { PasswordUtility } from '@/services/password.utility';
|
||||
import { UserService } from '@/services/user.service';
|
||||
|
||||
@@ -33,8 +33,8 @@ export class OwnerController {
|
||||
* and enable `isInstanceOwnerSetUp` setting.
|
||||
*/
|
||||
@Post('/setup', { skipAuth: true })
|
||||
async setupOwner(req: OwnerRequest.Post, res: Response) {
|
||||
const { email, firstName, lastName, password } = req.body;
|
||||
async setupOwner(req: AuthenticatedRequest, res: Response, @Body payload: OwnerSetupRequestDto) {
|
||||
const { email, firstName, lastName, password } = payload;
|
||||
|
||||
if (config.getEnv('userManagement.isInstanceOwnerSetUp')) {
|
||||
this.logger.debug(
|
||||
@@ -43,31 +43,15 @@ export class OwnerController {
|
||||
throw new BadRequestError('Instance owner already setup');
|
||||
}
|
||||
|
||||
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');
|
||||
}
|
||||
|
||||
const validPassword = this.passwordUtility.validate(password);
|
||||
|
||||
if (!firstName || !lastName) {
|
||||
this.logger.debug(
|
||||
'Request to claim instance ownership failed because of missing first name or last name in payload',
|
||||
{ payload: req.body },
|
||||
);
|
||||
throw new BadRequestError('First and last names are mandatory');
|
||||
}
|
||||
|
||||
let owner = await this.userRepository.findOneOrFail({
|
||||
where: { role: 'global:owner' },
|
||||
});
|
||||
owner.email = email;
|
||||
owner.firstName = firstName;
|
||||
owner.lastName = lastName;
|
||||
owner.password = await this.passwordUtility.hash(validPassword);
|
||||
owner.password = await this.passwordUtility.hash(password);
|
||||
|
||||
// TODO: move XSS validation out into the DTO class
|
||||
await validateEntity(owner);
|
||||
|
||||
owner = await this.userRepository.save(owner, { transaction: false });
|
||||
@@ -92,8 +76,13 @@ export class OwnerController {
|
||||
|
||||
@Post('/dismiss-banner')
|
||||
@GlobalScope('banner:dismiss')
|
||||
async dismissBanner(req: OwnerRequest.DismissBanner) {
|
||||
const bannerName = 'banner' in req.body ? (req.body.banner as string) : '';
|
||||
async dismissBanner(
|
||||
_req: AuthenticatedRequest,
|
||||
_res: Response,
|
||||
@Body payload: DismissBannerRequestDto,
|
||||
) {
|
||||
const bannerName = payload.banner;
|
||||
if (!bannerName) return;
|
||||
return await this.settingsRepository.dismissBanner({ bannerName });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user